Skip to content

Commit

Permalink
Move ProjectConfig and some other things to relay-config
Browse files Browse the repository at this point in the history
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
  • Loading branch information
alunyov authored and facebook-github-bot committed Dec 16, 2021
1 parent bae328b commit 5baad8c
Show file tree
Hide file tree
Showing 14 changed files with 171 additions and 116 deletions.
2 changes: 1 addition & 1 deletion compiler/crates/relay-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
3 changes: 1 addition & 2 deletions compiler/crates/relay-codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion compiler/crates/relay-codegen/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions compiler/crates/relay-compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
108 changes: 3 additions & 105 deletions compiler/crates/relay-compiler/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@ 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;
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;
Expand Down Expand Up @@ -496,96 +494,6 @@ impl fmt::Debug for Config {
}
}

pub struct ProjectConfig {
pub name: ProjectName,
pub base: Option<ProjectName>,
pub output: Option<PathBuf>,
pub extra_artifacts_output: Option<PathBuf>,
pub shard_output: bool,
pub shard_strip_regex: Option<Regex>,
pub schema_extensions: Vec<PathBuf>,
pub enabled: bool,
pub schema_location: SchemaLocation,
pub typegen_config: TypegenConfig,
pub persist: Option<PersistConfig>,
pub variable_names_comment: bool,
pub extra: serde_json::Value,
pub feature_flags: Arc<FeatureFlags>,
pub test_path_regex: Option<Regex>,
pub filename_for_artifact:
Option<Box<dyn (Fn(SourceLocationKey, StringKey) -> String) + Send + Sync>>,
pub skip_types_for_artifact: Option<Box<dyn (Fn(SourceLocationKey) -> 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<Fn>"
} else {
"None"
},
)
.field(
"skip_types_for_artifact",
&if skip_types_for_artifact.is_some() {
"Some<Fn>"
} 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<String> {
vec![
"**/node_modules/**".to_string(),
Expand Down Expand Up @@ -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<String, String>,
}

type PersistId = String;

#[async_trait]
Expand Down
4 changes: 3 additions & 1 deletion compiler/crates/relay-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion compiler/crates/relay-compiler/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
15 changes: 15 additions & 0 deletions compiler/crates/relay-config/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
18 changes: 18 additions & 0 deletions compiler/crates/relay-config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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};
123 changes: 123 additions & 0 deletions compiler/crates/relay-config/src/project_config.rs
Original file line number Diff line number Diff line change
@@ -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<K, V> = IndexMap<K, V, FnvBuildHasher>;

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<String, String>,
}

#[derive(Clone, Debug)]
pub enum SchemaLocation {
File(PathBuf),
Directory(PathBuf),
}

pub struct ProjectConfig {
pub name: ProjectName,
pub base: Option<ProjectName>,
pub output: Option<PathBuf>,
pub extra_artifacts_output: Option<PathBuf>,
pub shard_output: bool,
pub shard_strip_regex: Option<Regex>,
pub schema_extensions: Vec<PathBuf>,
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<PersistConfig>,
pub variable_names_comment: bool,
pub extra: serde_json::Value,
pub feature_flags: Arc<FeatureFlags>,
pub test_path_regex: Option<Regex>,
pub filename_for_artifact:
Option<Box<dyn (Fn(SourceLocationKey, StringKey) -> String) + Send + Sync>>,
pub skip_types_for_artifact: Option<Box<dyn (Fn(SourceLocationKey) -> 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<Fn>"
} else {
"None"
},
)
.field(
"skip_types_for_artifact",
&if skip_types_for_artifact.is_some() {
"Some<Fn>"
} else {
"None"
},
)
.field("rollout", rollout)
.field("js_module_format", js_module_format)
.finish()
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion compiler/crates/relay-typegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
6 changes: 2 additions & 4 deletions compiler/crates/relay-typegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#![deny(rust_2018_idioms)]
#![deny(clippy::all)]

mod config;
mod flow;
mod typescript;
mod writer;
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 5baad8c

Please sign in to comment.