Skip to content

Commit

Permalink
Merge SourceSet and ProjectSet
Browse files Browse the repository at this point in the history
Reviewed By: tyao1

Differential Revision: D34252714

fbshipit-source-id: b2979d0457d3f50080df3f1a2412c1ea099205ca
  • Loading branch information
captbaritone authored and facebook-github-bot committed Feb 16, 2022
1 parent 7687c7a commit 72622e5
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 117 deletions.
4 changes: 2 additions & 2 deletions compiler/crates/relay-compiler/src/build_project/build_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

use crate::config::ProjectConfig;
use crate::{compiler_state::SourceSetName, graphql_asts::GraphQLAsts};
use crate::{compiler_state::ProjectName, graphql_asts::GraphQLAsts};
use common::Diagnostic;
use dependency_analyzer::{get_reachable_ast, get_reachable_ir, ReachableAst};
use fnv::FnvHashMap;
Expand Down Expand Up @@ -46,7 +46,7 @@ pub fn build_ir(
project_config: &ProjectConfig,
implicit_dependencies: &DependencyMap,
schema: &SDLSchema,
graphql_asts: &FnvHashMap<SourceSetName, GraphQLAsts>,
graphql_asts: &FnvHashMap<ProjectName, GraphQLAsts>,
is_incremental_build: bool,
) -> Result<BuildIRResult, Vec<Diagnostic>> {
let project_asts = graphql_asts
Expand Down
8 changes: 4 additions & 4 deletions compiler/crates/relay-compiler/src/build_project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod source_control;
mod validate;

use self::log_program_stats::print_stats;
use crate::compiler_state::{ArtifactMapKind, CompilerState, ProjectName, SourceSetName};
use crate::compiler_state::{ArtifactMapKind, CompilerState, ProjectName};
use crate::config::{Config, ProjectConfig};
use crate::errors::BuildProjectError;
use crate::file_source::SourceControlUpdateStatus;
Expand Down Expand Up @@ -70,7 +70,7 @@ impl From<BuildProjectError> for BuildProjectFailure {
pub fn build_raw_program(
project_config: &ProjectConfig,
implicit_dependencies: &DependencyMap,
graphql_asts: &FnvHashMap<SourceSetName, GraphQLAsts>,
graphql_asts: &FnvHashMap<ProjectName, GraphQLAsts>,
schema: Arc<SDLSchema>,
log_event: &impl PerfLogEvent,
is_incremental_build: bool,
Expand Down Expand Up @@ -144,7 +144,7 @@ pub fn build_programs(
config: &Config,
project_config: &ProjectConfig,
compiler_state: &CompilerState,
graphql_asts: &FnvHashMap<SourceSetName, GraphQLAsts>,
graphql_asts: &FnvHashMap<ProjectName, GraphQLAsts>,
schema: Arc<SDLSchema>,
log_event: &impl PerfLogEvent,
perf_logger: Arc<impl PerfLogger + 'static>,
Expand Down Expand Up @@ -206,7 +206,7 @@ pub fn build_project(
config: &Config,
project_config: &ProjectConfig,
compiler_state: &CompilerState,
graphql_asts: &FnvHashMap<SourceSetName, GraphQLAsts>,
graphql_asts: &FnvHashMap<ProjectName, GraphQLAsts>,
perf_logger: Arc<impl PerfLogger + 'static>,
) -> Result<BuildProjectOutput, BuildProjectFailure> {
let log_event = perf_logger.create_event("build_project");
Expand Down
62 changes: 17 additions & 45 deletions compiler/crates/relay-compiler/src/compiler_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ use relay_transforms::DependencyMap;
use schema::SDLSchema;
use schema_diff::{definitions::SchemaChange, detect_changes};
use serde::{Deserialize, Serialize};
use std::env;
use std::{
fmt,
env, fmt,
fs::File as FsFile,
hash::Hash,
io::{BufReader, BufWriter},
Expand All @@ -38,12 +37,9 @@ use zstd::stream::{read::Decoder as ZstdDecoder, write::Encoder as ZstdEncoder};
/// Name of a compiler project.
pub type ProjectName = StringKey;

/// Name of a source set; a source set corresponds to a set fo files
/// that can be shared by multiple compiler projects
pub type SourceSetName = StringKey;

/// Set of project names.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
#[serde(untagged)]
pub enum ProjectSet {
ProjectName(ProjectName),
ProjectNames(Vec<ProjectName>),
Expand Down Expand Up @@ -76,31 +72,11 @@ impl IntoIterator for ProjectSet {
}
}

/// Represents the name of the source set, or list of source sets
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
#[serde(untagged)]
pub enum SourceSet {
SourceSetName(SourceSetName),
SourceSetNames(Vec<SourceSetName>),
}

impl IntoIterator for SourceSet {
type Item = SourceSetName;
type IntoIter = std::vec::IntoIter<Self::Item>;

fn into_iter(self) -> Self::IntoIter {
match self {
SourceSet::SourceSetName(name) => vec![name].into_iter(),
SourceSet::SourceSetNames(names) => names.into_iter(),
}
}
}

impl fmt::Display for SourceSet {
impl fmt::Display for ProjectSet {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SourceSet::SourceSetName(name) => write!(f, "{}", name),
SourceSet::SourceSetNames(names) => write!(
ProjectSet::ProjectName(name) => write!(f, "{}", name),
ProjectSet::ProjectNames(names) => write!(
f,
"{}",
names
Expand Down Expand Up @@ -227,7 +203,7 @@ pub enum ArtifactMapKind {

#[derive(Serialize, Deserialize, Debug)]
pub struct CompilerState {
pub graphql_sources: FnvHashMap<SourceSetName, GraphQLSources>,
pub graphql_sources: FnvHashMap<ProjectName, GraphQLSources>,
pub schemas: FnvHashMap<ProjectName, SchemaSources>,
pub extensions: FnvHashMap<ProjectName, SchemaSources>,
pub artifacts: FnvHashMap<ProjectName, Arc<ArtifactMapKind>>,
Expand Down Expand Up @@ -276,9 +252,9 @@ impl CompilerState {

for (category, files) in categorized {
match category {
FileGroup::Source { source_set } => {
FileGroup::Source { project_set } => {
let log_event = perf_logger.create_event("categorize");
log_event.string("source_set_name", source_set.to_string());
log_event.string("source_set_name", project_set.to_string());
let extract_timer = log_event.start("extract_graphql_strings_from_file_time");
let sources: GraphQLSourceSet = files
.par_iter()
Expand All @@ -299,8 +275,8 @@ impl CompilerState {
.collect::<Result<_>>()?;
log_event.stop(extract_timer);
log_event.complete();
for source_set_name in source_set {
result.set_pending_source_set(source_set_name, sources.clone());
for project_name in project_set {
result.set_pending_source_set(project_name, sources.clone());
}
}
FileGroup::Schema { project_set } => {
Expand Down Expand Up @@ -443,13 +419,13 @@ impl CompilerState {

for (category, files) in categorized {
match category {
FileGroup::Source { source_set } => {
FileGroup::Source { project_set } => {
// TODO: possible optimization to only set this if the
// extracted sources actually differ.
has_changed = true;

let log_event = perf_logger.create_event("categorize");
log_event.string("source_set_name", source_set.to_string());
log_event.string("source_set_name", project_set.to_string());
let extract_timer =
log_event.start("extract_graphql_strings_from_file_time");
let sources: GraphQLSourceSet = files
Expand All @@ -469,9 +445,9 @@ impl CompilerState {
.collect::<Result<_>>()?;
log_event.stop(extract_timer);

for source_set_name in source_set {
for project_name in project_set {
self.graphql_sources
.entry(source_set_name)
.entry(project_name)
.or_default()
.merge_pending_sources(sources.clone());
}
Expand Down Expand Up @@ -629,14 +605,10 @@ impl CompilerState {
!self.pending_file_source_changes.read().unwrap().is_empty()
}

fn set_pending_source_set(
&mut self,
source_set_name: SourceSetName,
source_set: GraphQLSourceSet,
) {
fn set_pending_source_set(&mut self, project_name: ProjectName, source_set: GraphQLSourceSet) {
let pending_entry = &mut self
.graphql_sources
.entry(source_set_name)
.entry(project_name)
.or_default()
.pending;
if pending_entry.is_empty() {
Expand Down
20 changes: 10 additions & 10 deletions compiler/crates/relay-compiler/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::build_project::{
artifact_writer::{ArtifactFileWriter, ArtifactWriter},
AdditionalValidations,
};
use crate::compiler_state::{ProjectName, SourceSet};
use crate::compiler_state::{ProjectName, ProjectSet};
use crate::errors::{ConfigValidationError, Error, Result};
use crate::saved_state::SavedStateLoader;
use crate::status_reporter::{ConsoleStatusReporter, StatusReporter};
Expand Down Expand Up @@ -64,7 +64,7 @@ pub struct Config {
/// 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, SourceSet>,
pub sources: FnvIndexMap<PathBuf, ProjectSet>,
pub excludes: Vec<String>,
pub projects: FnvIndexMap<ProjectName, ProjectConfig>,
pub header: Vec<String>,
Expand Down Expand Up @@ -358,23 +358,23 @@ impl Config {

/// Validated internal consistency of the config.
fn validate_consistency(&self, errors: &mut Vec<ConfigValidationError>) {
let mut source_set_names = FnvHashSet::default();
let mut project_names = FnvHashSet::default();
for value in self.sources.values() {
match value {
SourceSet::SourceSetName(name) => {
source_set_names.insert(*name);
ProjectSet::ProjectName(name) => {
project_names.insert(*name);
}
SourceSet::SourceSetNames(names) => {
ProjectSet::ProjectNames(names) => {
for name in names {
source_set_names.insert(*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 !source_set_names.contains(&project_name) {
if !project_names.contains(&project_name) {
errors.push(ConfigValidationError::ProjectSourceMissing { project_name });
}

Expand Down Expand Up @@ -540,7 +540,7 @@ struct MultiProjectConfigFile {
/// 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.
sources: IndexMap<PathBuf, SourceSet, fnv::FnvBuildHasher>,
sources: IndexMap<PathBuf, ProjectSet, fnv::FnvBuildHasher>,

/// Glob patterns that should not be part of the sources even if they are
/// in the source set directories.
Expand Down Expand Up @@ -769,7 +769,7 @@ impl SingleProjectConfigFile {
let mut sources = FnvIndexMap::default();
let src = normalize_path_from_config(current_dir, common_root_dir.clone(), self.src);

sources.insert(src, SourceSet::SourceSetName(self.project_name));
sources.insert(src, ProjectSet::ProjectName(self.project_name));

Ok(MultiProjectConfigFile {
root: Some(common_root_dir),
Expand Down
Loading

0 comments on commit 72622e5

Please sign in to comment.