From 5baad8ca2147bb494532d527fe8a9797ba7802ae Mon Sep 17 00:00:00 2001 From: Andrey Lunyov Date: Thu, 16 Dec 2021 07:57:25 -0800 Subject: [PATCH] Move `ProjectConfig` and some other things to `relay-config` Summary: The goal of this change is to allow different crates to have access to the `project_config` structure. Currently, it's defined on the `relay-compiler` crate, so other `crates` that are dependencies of the relay-compiler cannot import the type of `ProjectConfig` from `relay-compiler`. One of these crates that needs access to the `ProjectConfig` is `relay-transforms`. For now, we just passing pieces of the configuration to the `transforms` - things like project_name, feature flags, etc. With this change, we will be able just to pass a reference to `&ProjectConfig`. An example of this is coming in the next diff. Also, other pieces of `config` from the compiler can be moved to `relay-config`, and I'll finish this in follow-ups. For now, I want to keep the set of changes minimal. Reviewed By: tyao1 Differential Revision: D33151243 fbshipit-source-id: 638529c37da33603ca5e20aae5a9b644908770e1 --- compiler/crates/relay-codegen/Cargo.toml | 2 +- compiler/crates/relay-codegen/src/lib.rs | 3 +- compiler/crates/relay-codegen/src/printer.rs | 2 +- compiler/crates/relay-compiler/Cargo.toml | 1 + compiler/crates/relay-compiler/src/config.rs | 108 +-------------- compiler/crates/relay-compiler/src/lib.rs | 4 +- compiler/crates/relay-compiler/src/main.rs | 3 +- compiler/crates/relay-config/Cargo.toml | 15 +++ .../src/js_module_format.rs | 0 compiler/crates/relay-config/src/lib.rs | 18 +++ .../crates/relay-config/src/project_config.rs | 123 ++++++++++++++++++ .../src/typegen_config.rs} | 0 compiler/crates/relay-typegen/Cargo.toml | 2 +- compiler/crates/relay-typegen/src/lib.rs | 6 +- 14 files changed, 171 insertions(+), 116 deletions(-) create mode 100644 compiler/crates/relay-config/Cargo.toml rename compiler/crates/{relay-codegen => relay-config}/src/js_module_format.rs (100%) create mode 100644 compiler/crates/relay-config/src/lib.rs create mode 100644 compiler/crates/relay-config/src/project_config.rs rename compiler/crates/{relay-typegen/src/config.rs => relay-config/src/typegen_config.rs} (100%) diff --git a/compiler/crates/relay-codegen/Cargo.toml b/compiler/crates/relay-codegen/Cargo.toml index 8b8bd2682722e..8f211b6833b47 100644 --- a/compiler/crates/relay-codegen/Cargo.toml +++ b/compiler/crates/relay-codegen/Cargo.toml @@ -40,9 +40,9 @@ indexmap = { version = "1.7.0", features = ["rayon", "serde-1"] } intern = { path = "../intern" } lazy_static = "1.0" md-5 = "0.8" +relay-config = { path = "../relay-config" } relay-transforms = { path = "../relay-transforms" } schema = { path = "../schema" } -serde = { version = "1.0.126", features = ["derive", "rc"] } [dev-dependencies] fixture-tests = { path = "../fixture-tests" } diff --git a/compiler/crates/relay-codegen/src/lib.rs b/compiler/crates/relay-codegen/src/lib.rs index aba014359185e..65783129e89d9 100644 --- a/compiler/crates/relay-codegen/src/lib.rs +++ b/compiler/crates/relay-codegen/src/lib.rs @@ -13,11 +13,10 @@ mod ast; mod build_ast; mod constants; mod indentation; -mod js_module_format; mod printer; mod utils; pub use ast::{Primitive, QueryID, RequestParameters}; pub use build_ast::{build_request_params, is_static_storage_key_available}; -pub use js_module_format::JsModuleFormat; pub use printer::{print_fragment, print_operation, print_request, print_request_params, Printer}; +pub use relay_config::JsModuleFormat; diff --git a/compiler/crates/relay-codegen/src/printer.rs b/compiler/crates/relay-codegen/src/printer.rs index e4f6a7503f2fa..da879900d2620 100644 --- a/compiler/crates/relay-codegen/src/printer.rs +++ b/compiler/crates/relay-codegen/src/printer.rs @@ -12,8 +12,8 @@ use crate::build_ast::{ }; use crate::constants::CODEGEN_CONSTANTS; use crate::indentation::print_indentation; -use crate::js_module_format::JsModuleFormat; use crate::utils::escape; +use crate::JsModuleFormat; use graphql_ir::{FragmentDefinition, OperationDefinition}; use schema::SDLSchema; diff --git a/compiler/crates/relay-compiler/Cargo.toml b/compiler/crates/relay-compiler/Cargo.toml index d3cd4fa573d64..03b1386b4205a 100644 --- a/compiler/crates/relay-compiler/Cargo.toml +++ b/compiler/crates/relay-compiler/Cargo.toml @@ -43,6 +43,7 @@ persist-query = { path = "../persist-query" } rayon = "1.2" regex = "1.5.4" relay-codegen = { path = "../relay-codegen" } +relay-config = { path = "../relay-config" } relay-schema = { path = "../relay-schema" } relay-transforms = { path = "../relay-transforms" } relay-typegen = { path = "../relay-typegen" } diff --git a/compiler/crates/relay-compiler/src/config.rs b/compiler/crates/relay-compiler/src/config.rs index f2231bc97099e..7391577224ade 100644 --- a/compiler/crates/relay-compiler/src/config.rs +++ b/compiler/crates/relay-compiler/src/config.rs @@ -15,8 +15,7 @@ use crate::errors::{ConfigValidationError, Error, Result}; use crate::saved_state::SavedStateLoader; use crate::status_reporter::{ConsoleStatusReporter, StatusReporter}; use async_trait::async_trait; -use common::{FeatureFlags, Rollout, SourceLocationKey}; -use fmt::Debug; +use common::{FeatureFlags, Rollout}; use fnv::{FnvBuildHasher, FnvHashSet}; use graphql_ir::{OperationDefinition, Program}; use indexmap::IndexMap; @@ -24,10 +23,9 @@ use intern::string_key::{Intern, StringKey}; use persist_query::PersistError; use rayon::prelude::*; use regex::Regex; -use relay_codegen::JsModuleFormat; +use relay_config::{FlowTypegenConfig, JsModuleFormat, TypegenConfig, TypegenLanguage}; +pub use relay_config::{PersistConfig, ProjectConfig, SchemaLocation}; use relay_transforms::ConnectionInterface; -pub use relay_typegen::TypegenLanguage; -use relay_typegen::{FlowTypegenConfig, TypegenConfig}; use serde::de::Error as DeError; use serde::{Deserialize, Deserializer, Serialize}; use serde_json::Value; @@ -496,96 +494,6 @@ impl fmt::Debug for Config { } } -pub struct ProjectConfig { - pub name: ProjectName, - pub base: Option, - pub output: Option, - pub extra_artifacts_output: Option, - pub shard_output: bool, - pub shard_strip_regex: Option, - pub schema_extensions: Vec, - pub enabled: bool, - pub schema_location: SchemaLocation, - pub typegen_config: TypegenConfig, - pub persist: Option, - pub variable_names_comment: bool, - pub extra: serde_json::Value, - pub feature_flags: Arc, - pub test_path_regex: Option, - pub filename_for_artifact: - Option String) + Send + Sync>>, - pub skip_types_for_artifact: Option bool) + Send + Sync>>, - pub rollout: Rollout, - pub js_module_format: JsModuleFormat, -} - -impl Debug for ProjectConfig { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let ProjectConfig { - name, - base, - output, - extra_artifacts_output, - shard_output, - shard_strip_regex, - schema_extensions, - enabled, - schema_location, - typegen_config, - persist, - variable_names_comment, - extra, - feature_flags, - test_path_regex, - filename_for_artifact, - skip_types_for_artifact, - rollout, - js_module_format, - } = self; - f.debug_struct("ProjectConfig") - .field("name", name) - .field("base", base) - .field("output", output) - .field("extra_artifacts_output", extra_artifacts_output) - .field("shard_output", shard_output) - .field("shard_strip_regex", shard_strip_regex) - .field("schema_extensions", schema_extensions) - .field("enabled", enabled) - .field("schema_location", schema_location) - .field("typegen_config", typegen_config) - .field("persist", persist) - .field("variable_names_comment", variable_names_comment) - .field("extra", extra) - .field("feature_flags", feature_flags) - .field("test_path_regex", test_path_regex) - .field( - "filename_for_artifact", - &if filename_for_artifact.is_some() { - "Some" - } else { - "None" - }, - ) - .field( - "skip_types_for_artifact", - &if skip_types_for_artifact.is_some() { - "Some" - } else { - "None" - }, - ) - .field("rollout", rollout) - .field("js_module_format", js_module_format) - .finish() - } -} - -#[derive(Clone, Debug)] -pub enum SchemaLocation { - File(PathBuf), - Directory(PathBuf), -} - fn get_default_excludes() -> Vec { vec![ "**/node_modules/**".to_string(), @@ -904,16 +812,6 @@ struct ConfigFileProject { js_module_format: JsModuleFormat, } -#[derive(Debug, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub struct PersistConfig { - /// URL to send a POST request to to persist. - pub url: String, - /// The document will be in a POST parameter `text`. This map can contain - /// additional parameters to send. - pub params: FnvIndexMap, -} - type PersistId = String; #[async_trait] diff --git a/compiler/crates/relay-compiler/src/lib.rs b/compiler/crates/relay-compiler/src/lib.rs index 10514a5edf38c..e6fcfda0e494a 100644 --- a/compiler/crates/relay-compiler/src/lib.rs +++ b/compiler/crates/relay-compiler/src/lib.rs @@ -32,7 +32,9 @@ pub use build_project::{ transform_program, validate, validate_program, AdditionalValidations, Artifact, ArtifactContent, ArtifactGeneratedTypes, BuildProjectFailure, SourceHashes, }; -pub use config::{FileSourceKind, OperationPersister, PersistConfig}; +pub use config::{ + FileSourceKind, OperationPersister, PersistConfig, ProjectConfig, SchemaLocation, +}; pub use file_source::{ source_for_location, FileCategorizer, FileGroup, FileSource, FileSourceResult, FileSourceSubscription, FileSourceSubscriptionNextChange, FsSourceReader, diff --git a/compiler/crates/relay-compiler/src/main.rs b/compiler/crates/relay-compiler/src/main.rs index ddafe905bd0b7..9e7c9e9c72642 100644 --- a/compiler/crates/relay-compiler/src/main.rs +++ b/compiler/crates/relay-compiler/src/main.rs @@ -10,9 +10,10 @@ use env_logger::Env; use log::{error, info, Level}; use relay_compiler::{ compiler::Compiler, - config::{Config, SingleProjectConfigFile, TypegenLanguage}, + config::{Config, SingleProjectConfigFile}, FileSourceKind, RemotePersister, }; +use relay_typegen::TypegenLanguage; use std::io::Write; use std::{ env::{self, current_dir}, diff --git a/compiler/crates/relay-config/Cargo.toml b/compiler/crates/relay-config/Cargo.toml new file mode 100644 index 0000000000000..e32cff501ac45 --- /dev/null +++ b/compiler/crates/relay-config/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "relay-config" +version = "0.0.0" +authors = ["Facebook"] +edition = "2021" +license = "MIT" + +[dependencies] +common = { path = "../common" } +intern = { path = "../intern" } +serde = { version = "1.0.126", features = ["derive", "rc"] } +serde_json = { version = "1.0.64", features = ["float_roundtrip", "unbounded_depth"] } +fnv = "1.0" +indexmap = { version = "1.7.0", features = ["rayon", "serde-1"] } +regex = "1.5.4" diff --git a/compiler/crates/relay-codegen/src/js_module_format.rs b/compiler/crates/relay-config/src/js_module_format.rs similarity index 100% rename from compiler/crates/relay-codegen/src/js_module_format.rs rename to compiler/crates/relay-config/src/js_module_format.rs diff --git a/compiler/crates/relay-config/src/lib.rs b/compiler/crates/relay-config/src/lib.rs new file mode 100644 index 0000000000000..e8b8dd7fd2d2c --- /dev/null +++ b/compiler/crates/relay-config/src/lib.rs @@ -0,0 +1,18 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#![deny(warnings)] +#![deny(rust_2018_idioms)] +#![deny(clippy::all)] + +mod js_module_format; +mod project_config; +mod typegen_config; + +pub use js_module_format::JsModuleFormat; +pub use project_config::{PersistConfig, ProjectConfig, ProjectName, SchemaLocation}; +pub use typegen_config::{FlowTypegenConfig, FlowTypegenPhase, TypegenConfig, TypegenLanguage}; diff --git a/compiler/crates/relay-config/src/project_config.rs b/compiler/crates/relay-config/src/project_config.rs new file mode 100644 index 0000000000000..23ebd8214b24e --- /dev/null +++ b/compiler/crates/relay-config/src/project_config.rs @@ -0,0 +1,123 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +use fmt::Debug; +use std::{fmt, path::PathBuf, sync::Arc}; + +use common::{FeatureFlags, Rollout, SourceLocationKey}; +use fnv::FnvBuildHasher; +use indexmap::IndexMap; +use intern::string_key::StringKey; +use regex::Regex; +use serde::{Deserialize, Serialize}; + +use crate::{JsModuleFormat, TypegenConfig}; + +type FnvIndexMap = IndexMap; + +pub type ProjectName = StringKey; + +#[derive(Debug, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct PersistConfig { + /// URL to send a POST request to to persist. + pub url: String, + /// The document will be in a POST parameter `text`. This map can contain + /// additional parameters to send. + pub params: FnvIndexMap, +} + +#[derive(Clone, Debug)] +pub enum SchemaLocation { + File(PathBuf), + Directory(PathBuf), +} + +pub struct ProjectConfig { + pub name: ProjectName, + pub base: Option, + pub output: Option, + pub extra_artifacts_output: Option, + pub shard_output: bool, + pub shard_strip_regex: Option, + pub schema_extensions: Vec, + pub enabled: bool, + pub schema_location: SchemaLocation, + // pub schema_config: SchemaConfig, TODO: Add this and use in D33131805 + pub typegen_config: TypegenConfig, + pub persist: Option, + pub variable_names_comment: bool, + pub extra: serde_json::Value, + pub feature_flags: Arc, + pub test_path_regex: Option, + pub filename_for_artifact: + Option String) + Send + Sync>>, + pub skip_types_for_artifact: Option bool) + Send + Sync>>, + pub rollout: Rollout, + pub js_module_format: JsModuleFormat, +} + +impl Debug for ProjectConfig { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let ProjectConfig { + name, + base, + output, + extra_artifacts_output, + shard_output, + shard_strip_regex, + schema_extensions, + enabled, + schema_location, + typegen_config, + persist, + variable_names_comment, + extra, + feature_flags, + test_path_regex, + filename_for_artifact, + skip_types_for_artifact, + rollout, + js_module_format, + } = self; + f.debug_struct("ProjectConfig") + .field("name", name) + .field("base", base) + .field("output", output) + .field("extra_artifacts_output", extra_artifacts_output) + .field("shard_output", shard_output) + .field("shard_strip_regex", shard_strip_regex) + .field("schema_extensions", schema_extensions) + .field("enabled", enabled) + .field("schema_location", schema_location) + .field("typegen_config", typegen_config) + .field("persist", persist) + .field("variable_names_comment", variable_names_comment) + .field("extra", extra) + .field("feature_flags", feature_flags) + .field("test_path_regex", test_path_regex) + .field( + "filename_for_artifact", + &if filename_for_artifact.is_some() { + "Some" + } else { + "None" + }, + ) + .field( + "skip_types_for_artifact", + &if skip_types_for_artifact.is_some() { + "Some" + } else { + "None" + }, + ) + .field("rollout", rollout) + .field("js_module_format", js_module_format) + .finish() + } +} diff --git a/compiler/crates/relay-typegen/src/config.rs b/compiler/crates/relay-config/src/typegen_config.rs similarity index 100% rename from compiler/crates/relay-typegen/src/config.rs rename to compiler/crates/relay-config/src/typegen_config.rs diff --git a/compiler/crates/relay-typegen/Cargo.toml b/compiler/crates/relay-typegen/Cargo.toml index 848b356d87fe6..aa4aeaf300758 100644 --- a/compiler/crates/relay-typegen/Cargo.toml +++ b/compiler/crates/relay-typegen/Cargo.toml @@ -15,9 +15,9 @@ intern = { path = "../intern" } itertools = "0.10.3" lazy_static = "1.0" relay-codegen = { path = "../relay-codegen" } +relay-config = { path = "../relay-config" } relay-transforms = { path = "../relay-transforms" } schema = { path = "../schema" } -serde = { version = "1.0.126", features = ["derive", "rc"] } [dev-dependencies] fixture-tests = { path = "../fixture-tests" } diff --git a/compiler/crates/relay-typegen/src/lib.rs b/compiler/crates/relay-typegen/src/lib.rs index d5a3642431e4a..af38d1bf6e203 100644 --- a/compiler/crates/relay-typegen/src/lib.rs +++ b/compiler/crates/relay-typegen/src/lib.rs @@ -9,7 +9,6 @@ #![deny(rust_2018_idioms)] #![deny(clippy::all)] -mod config; mod flow; mod typescript; mod writer; @@ -18,8 +17,6 @@ use crate::flow::FlowPrinter; use crate::typescript::TypeScriptPrinter; use crate::writer::{GetterSetterPairProp, KeyValuePairProp, SpreadProp, Writer}; use common::NamedItem; -use config::FlowTypegenPhase; -pub use config::{FlowTypegenConfig, TypegenConfig, TypegenLanguage}; use fnv::FnvHashSet; use graphql_ir::{ Condition, Directive, FragmentDefinition, FragmentSpread, InlineFragment, LinkedField, @@ -29,7 +26,8 @@ use indexmap::{map::Entry, IndexMap, IndexSet}; use intern::string_key::{Intern, StringKey}; use itertools::Itertools; use lazy_static::lazy_static; -use relay_codegen::JsModuleFormat; +use relay_config::JsModuleFormat; +pub use relay_config::{FlowTypegenPhase, TypegenConfig, TypegenLanguage}; use relay_transforms::{ ModuleMetadata, RefetchableDerivedFromMetadata, RefetchableMetadata, RelayDirective, RelayResolverSpreadMetadata, RequiredMetadataDirective, CHILDREN_CAN_BUBBLE_METADATA_KEY,