-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
config.rs
1276 lines (1121 loc) · 47.1 KB
/
config.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
//! The config module provides functionality for managing the configuration of the Relay compiler.
use std::env::current_dir;
use std::ffi::OsStr;
use std::fmt;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use std::vec;
use async_trait::async_trait;
use common::DiagnosticsResult;
use common::DirectiveName;
use common::FeatureFlags;
use common::Rollout;
use common::ScalarName;
use dunce::canonicalize;
use fnv::FnvBuildHasher;
use fnv::FnvHashSet;
use globset::Glob;
use globset::GlobSetBuilder;
use graphql_ir::OperationDefinition;
use graphql_ir::Program;
use indexmap::IndexMap;
use intern::string_key::StringKey;
use js_config_loader::LoaderSource;
use persist_query::PersistError;
use rayon::prelude::*;
use regex::Regex;
use relay_config::CustomType;
use relay_config::DiagnosticReportConfig;
pub use relay_config::ExtraArtifactsConfig;
use relay_config::JsModuleFormat;
pub use relay_config::LocalPersistConfig;
use relay_config::ModuleImportConfig;
pub use relay_config::PersistConfig;
pub use relay_config::ProjectConfig;
use relay_config::ProjectName;
pub use relay_config::RemotePersistConfig;
use relay_config::ResolversSchemaModuleConfig;
use relay_config::SchemaConfig;
pub use relay_config::SchemaLocation;
use relay_config::TypegenConfig;
pub use relay_config::TypegenLanguage;
use relay_docblock::DocblockIr;
use relay_saved_state_loader::SavedStateLoader;
use relay_transforms::CustomTransformsConfig;
use schemars::gen::SchemaSettings;
use schemars::gen::{self};
use schemars::JsonSchema;
use serde::de::Error as DeError;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
use serde_json::Value;
use sha1::Digest;
use sha1::Sha1;
use watchman_client::pdu::ScmAwareClockData;
use crate::build_project::artifact_writer::ArtifactFileWriter;
use crate::build_project::artifact_writer::ArtifactWriter;
use crate::build_project::generate_extra_artifacts::GenerateExtraArtifactsFn;
use crate::build_project::get_artifacts_file_hash_map::GetArtifactsFileHashMapFn;
use crate::build_project::AdditionalValidations;
use crate::compiler_state::CompilerState;
use crate::compiler_state::DeserializableProjectSet;
use crate::compiler_state::ProjectSet;
use crate::errors::ConfigValidationError;
use crate::errors::Error;
use crate::errors::Result;
use crate::source_control_for_root;
use crate::status_reporter::ConsoleStatusReporter;
use crate::status_reporter::StatusReporter;
use crate::GraphQLAsts;
pub type FnvIndexMap<K, V> = IndexMap<K, V, FnvBuildHasher>;
type PostArtifactsWriter = Box<
dyn Fn(&Config) -> std::result::Result<(), Box<dyn std::error::Error + Send + Sync>>
+ Send
+ Sync,
>;
type OperationPersisterCreator =
Box<dyn Fn(&ProjectConfig) -> Option<Box<dyn OperationPersister + Send + Sync>> + Send + Sync>;
type UpdateCompilerStateFromSavedState =
Option<Box<dyn Fn(&mut CompilerState, &Config) + Send + Sync>>;
type ShouldExtractFullSource = Box<dyn Fn(&str) -> bool + Send + Sync>;
type GenerateVirtualIdFieldName =
Box<dyn Fn(&ProjectConfig, &OperationDefinition, &Program) -> Option<StringKey> + Send + Sync>;
type CustomExtractRelayResolvers = Box<
dyn Fn(
ProjectName,
&FnvIndexMap<ScalarName, CustomType>,
&CompilerState,
Option<&GraphQLAsts>,
) -> DiagnosticsResult<(Vec<DocblockIr>, Vec<DocblockIr>)>
// (Types, Fields)
+ Send
+ Sync,
>;
type CustomOverrideSchemaDeterminator =
Box<dyn Fn(&ProjectConfig, &OperationDefinition) -> Option<String> + Send + Sync>;
/// The full compiler config. This is a combination of:
/// - the configuration file
/// - the absolute path to the root of the compiled projects
/// - command line options
/// - TODO: injected code to produce additional files
pub struct Config {
/// Optional name for this config. This might be used by compiler extension
/// code like logging or extra artifact generation.
pub name: Option<String>,
/// Root directory of all projects to compile. Any other paths in the
/// compiler should be relative to this root unless otherwise noted.
pub root_dir: PathBuf,
pub sources: FnvIndexMap<PathBuf, ProjectSet>,
pub excludes: Vec<String>,
/// Some projects may need to include extra source directories without being
/// affected by exclusion globs from the `excludes` config (e.g. generated
/// directories).
pub generated_sources: FnvIndexMap<PathBuf, ProjectSet>,
pub projects: FnvIndexMap<ProjectName, ProjectConfig>,
pub header: Vec<String>,
pub codegen_command: Option<String>,
/// If set, tries to initialize the compiler from the saved state file.
pub load_saved_state_file: Option<PathBuf>,
/// Function to generate extra
pub generate_extra_artifacts: Option<GenerateExtraArtifactsFn>,
pub generate_virtual_id_file_name: Option<GenerateVirtualIdFieldName>,
/// Path to which to write the output of the compilation
pub artifact_writer: Box<dyn ArtifactWriter + Send + Sync>,
// Function to get the file hash for an artifact file.
pub get_artifacts_file_hash_map: Option<GetArtifactsFileHashMapFn>,
/// Compile all files. Persist ids are still re-used unless
/// `Config::repersist_operations` is also set.
pub compile_everything: bool,
/// Do not reuse persist ids from artifacts even if the text hash matches.
pub repersist_operations: bool,
pub saved_state_config: Option<ScmAwareClockData>,
pub saved_state_loader: Option<Box<dyn SavedStateLoader + Send + Sync>>,
pub saved_state_version: String,
/// Function that creates a function that is
/// called to save operation text (e.g. to a database) and to generate an id.
pub create_operation_persister: Option<OperationPersisterCreator>,
pub post_artifacts_write: Option<PostArtifactsWriter>,
/// Validations that can be added to the config that will be called in addition to default
/// validation rules.
pub additional_validations: Option<AdditionalValidations>,
pub status_reporter: Box<dyn StatusReporter + Send + Sync>,
/// We may generate some content in the artifacts that's stripped in production if __DEV__ variable is set
/// This config option is here to define the name of that special variable
pub is_dev_variable_name: Option<String>,
/// Type of file source to use in the Compiler
pub file_source_config: FileSourceKind,
/// A set of custom transform functions, that can be applied before,
/// and after each major transformation step (common, operations, etc)
/// in the `apply_transforms(...)`.
pub custom_transforms: Option<CustomTransformsConfig>,
pub custom_override_schema_determinator: Option<CustomOverrideSchemaDeterminator>,
pub export_persisted_query_ids_to_file: Option<PathBuf>,
/// The async function is called before the compiler connects to the file
/// source.
pub initialize_resources: Option<Box<dyn Fn() + Send + Sync>>,
/// Runs in `try_saved_state` when the compiler state is initialized from saved state.
pub update_compiler_state_from_saved_state: UpdateCompilerStateFromSavedState,
// Allow incremental build for some schema changes
pub has_schema_change_incremental_build: bool,
/// A custom function to extract resolver Dockblock IRs from sources
pub custom_extract_relay_resolvers: Option<CustomExtractRelayResolvers>,
/// A function to determine if full file source should be extracted instead of docblock
pub should_extract_full_source: Option<ShouldExtractFullSource>,
/// Names of directives that will be automatically copied from the parent fragment to refetchable queries
pub transferrable_refetchable_query_directives: Vec<DirectiveName>,
}
pub enum FileSourceKind {
Watchman,
/// List with changed files in format "file_path,exists".
/// This can be used to replace watchman queries
External(PathBuf),
WalkDir,
}
fn normalize_path_from_config(
current_dir: PathBuf,
common_path: PathBuf,
path_from_config: PathBuf,
) -> PathBuf {
let mut src = current_dir.join(path_from_config.clone());
src = canonicalize(src.clone())
.unwrap_or_else(|err| panic!("Unable to canonicalize file {:?}. Error: {:?}", &src, err));
src.strip_prefix(common_path.clone())
.unwrap_or_else(|_| {
panic!(
"Expect to be able to strip common_path from {:?} {:?}",
&src,
&common_path.clone(),
);
})
.to_path_buf()
}
impl From<SingleProjectConfigFile> for Config {
fn from(config: SingleProjectConfigFile) -> Self {
Self::from_struct(
"/virtual/path".into(),
ConfigFile::SingleProject(config),
false,
)
.unwrap()
}
}
impl Config {
pub fn search(start_dir: &Path) -> Result<Self> {
Self::load_config(
start_dir,
&[
LoaderSource::PackageJson("relay".to_string()),
LoaderSource::Json("relay.config.json".to_string()),
LoaderSource::Js("relay.config.js".to_string()),
],
)
}
pub fn load(config_path: PathBuf) -> Result<Self> {
let loader = if config_path.extension() == Some(OsStr::new("js")) {
LoaderSource::Js(config_path.display().to_string())
} else if config_path.extension() == Some(OsStr::new("json")) {
LoaderSource::Json(config_path.display().to_string())
} else {
return Err(Error::ConfigError {
details: format!(
"Invalid file extension. Expected `.js` or `.json`. Provided file \"{}\".",
config_path.display()
),
});
};
Self::load_config(
¤t_dir().expect("Unable to get current working directory."),
&[loader],
)
}
fn load_config(start_dir: &Path, loaders_sources: &[LoaderSource]) -> Result<Self> {
match js_config_loader::load(start_dir, loaders_sources) {
Ok(Some(config)) => Self::from_struct(config.path, config.value, true),
Ok(None) => Err(Error::ConfigError {
details: format!(
r#"
Configuration for Relay compiler not found.
Please make sure that the configuration file is created in {}.
You can also pass the path to the configuration file as `relay-compiler ./path-to-config/relay.json`.
Example file:
{{
"src": "./src",
"schema": "./path-to/schema.graphql",
"language": "javascript"
}}
"#,
match loaders_sources.len() {
1 => loaders_sources[0].to_string(),
2 => format!("{} or {}", loaders_sources[0], loaders_sources[1]),
_ => {
let mut loaders_str = loaders_sources
.iter()
.map(|loader| loader.to_string())
.collect::<Vec<_>>();
let last_option = loaders_str.pop().unwrap();
format!("{}, or {}", loaders_str.join(", "), last_option)
}
}
),
}),
Err(error) => Err(Error::ConfigError {
details: format!("Error searching config: {}", error),
}),
}
}
/// Loads a config file without validation for use in tests.
pub fn from_string_for_test(config_string: &str) -> Result<Self> {
let path = PathBuf::from("/virtual/root/virtual_config.json");
let config_file: ConfigFile =
serde_json::from_str(config_string).map_err(|err| Error::ConfigError {
details: format!("Failed to parse config file `{}`: {}", path.display(), err,),
})?;
Self::from_struct(path, config_file, false)
}
/// `validate_fs` disables all filesystem checks for existence of files
fn from_struct(
config_path: PathBuf,
config_file: ConfigFile,
validate_fs: bool,
) -> Result<Self> {
let mut hash = Sha1::new();
serde_json::to_writer(&mut hash, &config_file).unwrap();
let is_multi_project = match config_file {
ConfigFile::MultiProject(_) => true,
ConfigFile::SingleProject(_) => false,
};
let config_file = match config_file {
ConfigFile::MultiProject(config) => *config,
ConfigFile::SingleProject(config) => {
config.create_multi_project_config(&config_path)?
}
};
let MultiProjectConfigFile {
feature_flags: config_file_feature_flags,
projects,
..
} = config_file;
let projects = projects
.into_iter()
.map(|(project_name, config_file_project)| {
let schema_location =
match (config_file_project.schema, config_file_project.schema_dir) {
(Some(schema_file), None) => Ok(SchemaLocation::File(schema_file)),
(None, Some(schema_dir)) => Ok(SchemaLocation::Directory(schema_dir)),
_ => Err(Error::ConfigFileValidation {
config_path: config_path.clone(),
validation_errors: vec![
ConfigValidationError::ProjectNeedsSchemaXorSchemaDir {
project_name,
},
],
}),
}?;
let shard_strip_regex = config_file_project
.shard_strip_regex
.map(|s| Regex::new(&s))
.transpose()
.map_err(|error| Error::ConfigFileValidation {
config_path: config_path.clone(),
validation_errors: vec![ConfigValidationError::InvalidRegex {
key: "shardStripRegex",
project_name,
error,
}],
})?;
let test_path_regex = config_file_project
.test_path_regex
.map(|s| Regex::new(&s))
.transpose()
.map_err(|error| Error::ConfigFileValidation {
config_path: config_path.clone(),
validation_errors: vec![ConfigValidationError::InvalidRegex {
key: "testDirectoryRegex",
project_name,
error,
}],
})?;
let excludes_extensions_set =
config_file_project
.excludes_extensions
.map(move |extensions| {
let mut builder = GlobSetBuilder::new();
for ext in extensions {
builder.add(Glob::new(&ext).unwrap());
}
builder.build().unwrap()
});
let project_config = ProjectConfig {
name: project_name,
base: config_file_project.base,
enabled: true,
schema_extensions: config_file_project.schema_extensions,
extra_artifacts_config: None,
extra: config_file_project.extra,
excludes_extensions: excludes_extensions_set,
output: config_file_project.output,
extra_artifacts_output: config_file_project.extra_artifacts_output,
shard_output: config_file_project.shard_output,
shard_strip_regex,
schema_location,
schema_name: config_file_project.schema_name,
schema_config: config_file_project.schema_config,
typegen_config: config_file_project.typegen_config,
persist: config_file_project.persist,
variable_names_comment: config_file_project.variable_names_comment,
test_path_regex,
feature_flags: Arc::new(
config_file_project
.feature_flags
.unwrap_or_else(|| config_file_feature_flags.clone()),
),
rollout: config_file_project.rollout,
js_module_format: config_file_project.js_module_format,
module_import_config: config_file_project.module_import_config,
diagnostic_report_config: config_file_project.diagnostic_report_config,
resolvers_schema_module: config_file_project.resolvers_schema_module,
codegen_command: config_file_project.codegen_command,
get_custom_path_for_artifact: None,
};
Ok((project_name, project_config))
})
.collect::<Result<FnvIndexMap<_, _>>>()?;
let config_file_dir = config_path.parent().unwrap();
let root_dir = if let Some(config_root) = config_file.root {
canonicalize(config_file_dir.join(config_root)).unwrap()
} else {
config_file_dir.to_owned()
};
let config = Self {
name: config_file.name,
artifact_writer: Box::new(ArtifactFileWriter::new(
match config_file.no_source_control {
Some(true) => None,
_ => source_control_for_root(&root_dir),
},
root_dir.clone(),
)),
status_reporter: Box::new(ConsoleStatusReporter::new(
root_dir.clone(),
is_multi_project,
)),
root_dir,
sources: config_file.sources,
excludes: config_file.excludes,
generated_sources: config_file.generated_sources,
projects,
header: config_file.header,
codegen_command: config_file.codegen_command,
load_saved_state_file: None,
generate_extra_artifacts: None,
generate_virtual_id_file_name: None,
get_artifacts_file_hash_map: None,
saved_state_config: config_file.saved_state_config,
saved_state_loader: None,
saved_state_version: hex::encode(hash.finalize()),
create_operation_persister: None,
compile_everything: false,
repersist_operations: false,
post_artifacts_write: None,
additional_validations: None,
is_dev_variable_name: config_file.is_dev_variable_name,
file_source_config: FileSourceKind::Watchman,
custom_transforms: None,
custom_override_schema_determinator: None,
export_persisted_query_ids_to_file: None,
initialize_resources: None,
update_compiler_state_from_saved_state: None,
has_schema_change_incremental_build: false,
custom_extract_relay_resolvers: None,
should_extract_full_source: None,
transferrable_refetchable_query_directives: vec![],
};
let mut validation_errors = Vec::new();
config.validate_consistency(&mut validation_errors);
if validate_fs {
config.validate_paths(&mut validation_errors);
}
if validation_errors.is_empty() {
Ok(config)
} else {
Err(Error::ConfigFileValidation {
config_path,
validation_errors,
})
}
}
/// Iterator over projects that are enabled.
pub fn enabled_projects(&self) -> impl Iterator<Item = &ProjectConfig> {
self.projects
.values()
.filter(|project_config| project_config.enabled)
}
/// Rayon parallel iterator over projects that are enabled.
pub fn par_enabled_projects(&self) -> impl ParallelIterator<Item = &ProjectConfig> {
self.projects
.par_iter()
.map(|(_project_name, project_config)| project_config)
.filter(|project_config| project_config.enabled)
}
/// Validated internal consistency of the config.
fn validate_consistency(&self, errors: &mut Vec<ConfigValidationError>) {
let mut project_names = FnvHashSet::default();
for (source_dir, project_set) in self.sources.iter() {
for name in project_set.iter() {
if self.projects.get(name).is_none() {
errors.push(ConfigValidationError::ProjectDefinitionMissing {
source_dir: source_dir.clone(),
project_name: *name,
});
}
project_names.insert(*name);
}
}
for (&project_name, project_config) in &self.projects {
// there should be a source for each project matching the project name
if !project_names.contains(&project_name) {
errors.push(ConfigValidationError::ProjectSourceMissing { project_name });
}
// If a base of the project is set, it should exist
if let Some(base_name) = project_config.base {
if self.projects.get(&base_name).is_none() {
errors.push(ConfigValidationError::ProjectBaseMissing {
project_name,
base_project_name: base_name,
})
}
}
}
}
/// Validates that all paths actually exist on disk.
fn validate_paths(&self, errors: &mut Vec<ConfigValidationError>) {
if !self.root_dir.is_dir() {
errors.push(ConfigValidationError::RootNotDirectory {
root_dir: self.root_dir.clone(),
});
// early return, no point in continuing validation
return;
}
// each source should point to an existing directory
for source_dir in self.sources.keys() {
let abs_source_dir = self.root_dir.join(source_dir);
if !abs_source_dir.exists() {
errors.push(ConfigValidationError::SourceNotExistent {
source_dir: abs_source_dir.clone(),
});
} else if !abs_source_dir.is_dir() {
errors.push(ConfigValidationError::SourceNotDirectory {
source_dir: abs_source_dir.clone(),
});
}
}
for (&project_name, project) in &self.projects {
match &project.schema_location {
SchemaLocation::File(schema_file) => {
let abs_schema_file = self.root_dir.join(schema_file);
if !abs_schema_file.exists() {
errors.push(ConfigValidationError::SchemaFileNotExistent {
project_name,
schema_file: abs_schema_file.clone(),
});
} else if !abs_schema_file.is_file() {
errors.push(ConfigValidationError::SchemaFileNotFile {
project_name,
schema_file: abs_schema_file.clone(),
});
}
}
SchemaLocation::Directory(schema_dir) => {
let abs_schema_dir = self.root_dir.join(schema_dir);
if !abs_schema_dir.exists() {
errors.push(ConfigValidationError::SchemaDirNotExistent {
project_name,
schema_dir: abs_schema_dir.clone(),
});
} else if !abs_schema_dir.is_dir() {
errors.push(ConfigValidationError::SchemaDirNotDirectory {
project_name,
schema_dir: abs_schema_dir.clone(),
});
}
}
}
}
}
/// Compute all root paths that we need to query. All files relevant to the
/// compiler should be in these directories.
pub fn get_all_roots(&self) -> Vec<PathBuf> {
let source_roots = self.get_source_roots();
let extra_sources_roots = self.get_generated_sources_roots();
let output_roots = self.get_output_dir_paths();
let extension_roots = self.get_extension_roots();
let schema_file_roots = self.get_schema_file_roots();
let schema_dir_roots = self.get_schema_dir_paths();
unify_roots(
source_roots
.into_iter()
.chain(extra_sources_roots)
.chain(output_roots)
.chain(extension_roots)
.chain(schema_file_roots)
.chain(schema_dir_roots)
.collect(),
)
}
/// Returns all root directories of JS source files for the config.
pub fn get_source_roots(&self) -> Vec<PathBuf> {
self.sources.keys().cloned().collect()
}
/// Returns all root directories of JS source files for the config.
pub fn get_generated_sources_roots(&self) -> Vec<PathBuf> {
self.generated_sources.keys().cloned().collect()
}
/// Returns all root directories of GraphQL schema extension files for the
/// config.
pub fn get_extension_roots(&self) -> Vec<PathBuf> {
self.projects
.values()
.flat_map(|project_config| project_config.schema_extensions.iter().cloned())
.collect()
}
/// Returns all output and extra artifact output directories for the config.
pub fn get_output_dir_paths(&self) -> Vec<PathBuf> {
let output_dirs = self
.projects
.values()
.filter_map(|project_config| project_config.output.clone());
let extra_artifact_output_dirs = self
.projects
.values()
.filter_map(|project_config| project_config.extra_artifacts_output.clone());
output_dirs.chain(extra_artifact_output_dirs).collect()
}
/// Returns all paths that contain GraphQL schema files for the config.
pub fn get_schema_file_paths(&self) -> Vec<PathBuf> {
self.projects
.values()
.filter_map(|project_config| match &project_config.schema_location {
SchemaLocation::File(schema_file) => Some(schema_file.clone()),
SchemaLocation::Directory(_) => None,
})
.collect()
}
/// Returns all GraphQL schema directories for the config.
pub fn get_schema_dir_paths(&self) -> Vec<PathBuf> {
self.projects
.values()
.filter_map(|project_config| match &project_config.schema_location {
SchemaLocation::File(_) => None,
SchemaLocation::Directory(schema_dir) => Some(schema_dir.clone()),
})
.collect()
}
/// Returns root directories that contain GraphQL schema files.
pub fn get_schema_file_roots(&self) -> impl Iterator<Item = PathBuf> {
self.get_schema_file_paths().into_iter().map(|schema_path| {
schema_path
.parent()
.expect("A schema in the project root directory is currently not supported.")
.to_owned()
})
}
}
/// Finds the roots of a set of paths. This filters any paths
/// that are a subdirectory of other paths in the input.
fn unify_roots(mut paths: Vec<PathBuf>) -> Vec<PathBuf> {
paths.sort();
let mut roots = Vec::new();
for path in paths {
match roots.last() {
Some(prev) if path.starts_with(prev) => {
// skip
}
_ => {
roots.push(path);
}
}
}
roots
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_unify_roots() {
assert_eq!(unify_roots(vec![]).len(), 0);
assert_eq!(
unify_roots(vec!["Apps".into(), "Libraries".into()]),
&[PathBuf::from("Apps"), PathBuf::from("Libraries")]
);
assert_eq!(
unify_roots(vec!["Apps".into(), "Apps/Foo".into()]),
&[PathBuf::from("Apps")]
);
assert_eq!(
unify_roots(vec!["Apps/Foo".into(), "Apps".into()]),
&[PathBuf::from("Apps")]
);
assert_eq!(
unify_roots(vec!["Foo".into(), "Foo2".into()]),
&[PathBuf::from("Foo"), PathBuf::from("Foo2"),]
);
}
}
impl fmt::Debug for Config {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Config {
name,
artifact_writer: _,
root_dir,
sources,
excludes,
generated_sources,
compile_everything,
repersist_operations,
projects,
header,
codegen_command,
load_saved_state_file,
generate_extra_artifacts,
saved_state_config,
saved_state_loader,
saved_state_version,
create_operation_persister,
post_artifacts_write,
..
} = self;
fn option_fn_to_string<T>(option: &Option<T>) -> &'static str {
if option.is_some() { "Some(Fn)" } else { "None" }
}
f.debug_struct("Config")
.field("name", name)
.field("root_dir", root_dir)
.field("sources", sources)
.field("excludes", excludes)
.field("generated_sources", generated_sources)
.field("compile_everything", compile_everything)
.field("repersist_operations", repersist_operations)
.field("projects", projects)
.field("header", header)
.field("codegen_command", codegen_command)
.field("load_saved_state_file", load_saved_state_file)
.field("saved_state_config", saved_state_config)
.field(
"create_operation_persister",
&option_fn_to_string(create_operation_persister),
)
.field(
"generate_extra_artifacts",
&option_fn_to_string(generate_extra_artifacts),
)
.field(
"saved_state_loader",
&option_fn_to_string(saved_state_loader),
)
.field("saved_state_version", saved_state_version)
.field(
"post_artifacts_write",
&option_fn_to_string(post_artifacts_write),
)
.finish()
}
}
fn get_default_excludes() -> Vec<String> {
vec![
"**/node_modules/**".to_string(),
"**/__mocks__/**".to_string(),
"**/__generated__/**".to_string(),
]
}
/// Schema of the compiler configuration JSON file.
#[derive(Debug, Serialize, Deserialize, Default, JsonSchema)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct MultiProjectConfigFile {
/// The user may hard-code the JSON Schema for their version of the config.
#[serde(rename = "$schema")]
json_schema: Option<String>,
/// Optional name for this config, might be used for logging or custom extra
/// artifact generator code.
#[serde(default)]
name: Option<String>,
/// Root directory relative to the config file. Defaults to the directory
/// where the config is located.
#[serde(default)]
root: Option<PathBuf>,
#[serde(default)]
header: Vec<String>,
#[serde(default)]
codegen_command: Option<String>,
/// A mapping from directory paths (relative to the root) to a source set.
/// If a path is a subdirectory of another path, the more specific path
/// wins.
#[schemars(with = "FnvIndexMap<PathBuf, DeserializableProjectSet>")]
sources: FnvIndexMap<PathBuf, ProjectSet>,
/// Glob patterns that should not be part of the sources even if they are
/// in the source set directories.
#[serde(default = "get_default_excludes")]
excludes: Vec<String>,
/// Similar to sources but not affected by excludes.
#[serde(default)]
generated_sources: FnvIndexMap<PathBuf, ProjectSet>,
/// Configuration of projects to compile.
projects: FnvIndexMap<ProjectName, ConfigFileProject>,
#[serde(default)]
feature_flags: FeatureFlags,
/// Watchman saved state config.
#[schemars(with = "Option<ScmAwareClockDataJsonSchemaDef>")]
saved_state_config: Option<ScmAwareClockData>,
/// Then name of the global __DEV__ variable to use in generated artifacts
is_dev_variable_name: Option<String>,
/// Opt out of source control checks/integration.
no_source_control: Option<bool>,
}
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields, rename_all = "camelCase", default)]
pub struct SingleProjectConfigFile {
/// The user may hard-code the JSON Schema for their version of the config.
#[serde(rename = "$schema")]
pub json_schema: Option<String>,
#[serde(skip)]
pub project_name: ProjectName,
/// Path to schema.graphql
pub schema: PathBuf,
/// Root directory of application code
pub src: PathBuf,
/// A specific directory to output all artifacts to. When enabling this
/// the babel plugin needs `artifactDirectory` set as well.
pub artifact_directory: Option<PathBuf>,
/// Directories to ignore under src
/// default: ['**/node_modules/**', '**/__mocks__/**', '**/__generated__/**'],
#[serde(alias = "exclude")]
pub excludes: Vec<String>,
/// List of directories with schema extensions.
pub schema_extensions: Vec<PathBuf>,
#[serde(flatten)]
pub typegen_config: TypegenConfig,
/// Query Persist Configuration
/// It contains URL and addition parameters that will be included
/// with the request (think API_KEY, APP_ID, etc...)
pub persist_config: Option<PersistConfig>,
/// We may generate some content in the artifacts that's stripped in production if __DEV__ variable is set
/// This config option is here to define the name of that special variable
pub is_dev_variable_name: Option<String>,
/// Name of the command that runs the relay compiler
pub codegen_command: Option<String>,
/// Formatting style for generated files.
pub js_module_format: JsModuleFormat,
/// Extra configuration for the schema itself.
pub schema_config: SchemaConfig,
/// Configuration for @module
#[serde(default)]
pub module_import_config: ModuleImportConfig,
/// Added in 13.1.1 to customize Final/Compat mode in the single project config file
/// Removed in 14.0.0
#[serde(default)]
pub typegen_phase: Option<Value>,
#[serde(default)]
pub feature_flags: Option<FeatureFlags>,
#[serde(default)]
pub resolvers_schema_module: Option<ResolversSchemaModuleConfig>,
/// Opt out of source control checks/integration.
#[serde(default)]
pub no_source_control: Option<bool>,
}
impl Default for SingleProjectConfigFile {
fn default() -> Self {
Self {
json_schema: None,
project_name: ProjectName::default(),
schema: Default::default(),
src: Default::default(),
artifact_directory: Default::default(),
excludes: get_default_excludes(),
schema_extensions: vec![],
schema_config: Default::default(),
typegen_config: Default::default(),
persist_config: None,
is_dev_variable_name: None,
codegen_command: None,
js_module_format: JsModuleFormat::CommonJS,
typegen_phase: None,
feature_flags: None,
module_import_config: Default::default(),
resolvers_schema_module: Default::default(),
no_source_control: Some(false),
}
}
}
impl SingleProjectConfigFile {
fn get_common_root(
&self,
root_dir: PathBuf,
) -> std::result::Result<PathBuf, ConfigValidationError> {
let mut paths = vec![];
if let Some(artifact_directory_path) = self.artifact_directory.clone() {
paths.push(
canonicalize(root_dir.join(artifact_directory_path.clone())).map_err(|_| {
ConfigValidationError::ArtifactDirectoryNotExistent {
path: artifact_directory_path,
}
})?,
);
}
paths.push(canonicalize(root_dir.join(self.src.clone())).map_err(|_| {
ConfigValidationError::SourceNotExistent {
source_dir: self.src.clone(),
}
})?);
paths.push(
canonicalize(root_dir.join(self.schema.clone())).map_err(|_| {
ConfigValidationError::SchemaFileNotExistent {
project_name: self.project_name,
schema_file: self.schema.clone(),
}
})?,
);
for extension_dir in self.schema_extensions.iter() {
paths.push(
canonicalize(root_dir.join(extension_dir.clone())).map_err(|_| {
ConfigValidationError::ExtensionDirNotExistent {