use std::path::Path;
use fallow_config::{ConfigLoadOptions, EnvSnapshot, FallowConfig, ProductionAnalysis};
use fallow_types::output_format::OutputFormat;
use super::{
ConfigFile, ConfigFileOptions, DuplicationScope, ProductionFlags, ProjectConfig,
ResolveOptions, load_config_file,
};
use crate::EngineResult;
use crate::resolved_run::steps::{
mode_entry, mode_entry_lossy, validate_boundaries_and_rule_packs,
};
impl ResolveOptions {
fn host_default() -> Self {
Self {
output: OutputFormat::Human,
threads: std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get),
no_cache: false,
quiet: true,
}
}
}
impl DuplicationScope {
fn for_dead_code_session(
file: &ConfigFile,
flags: ProductionFlags,
options: ResolveOptions,
env: &EnvSnapshot,
) -> Self {
let mode = |analysis| {
if file.path.is_some() {
flags.effective(analysis, &file.config.production)
} else {
flags.mode(analysis)
}
};
if mode(ProductionAnalysis::Dupes) == mode(ProductionAnalysis::DeadCode) {
return Self::SameAsSession;
}
let dupes = ConfigFile {
config: file.config.clone(),
path: file.path.clone(),
chain: file.chain.clone(),
}
.for_analysis(ProductionAnalysis::Dupes, flags);
Self::Own {
config: Box::new(dupes.config),
options,
env: Box::new(env.clone()),
given: Box::default(),
}
}
}
#[cfg(test)]
#[derive(Debug, Clone, Copy)]
pub struct ProjectConfigOptions {
pub output: OutputFormat,
pub no_cache: bool,
pub threads: usize,
pub production_override: Option<bool>,
pub quiet: bool,
pub analysis: ProductionAnalysis,
pub allow_remote_extends: bool,
}
pub fn config_for_project(root: &Path, config_path: Option<&Path>) -> EngineResult<ProjectConfig> {
config_for_project_with_load_options(root, config_path, ConfigLoadOptions::default())
}
pub fn config_for_project_with_load_options(
root: &Path,
config_path: Option<&Path>,
load_options: ConfigLoadOptions,
) -> EngineResult<ProjectConfig> {
config_for_project_with_production(root, config_path, load_options, ProductionFlags::default())
}
pub fn config_for_project_with_production(
root: &Path,
config_path: Option<&Path>,
load_options: ConfigLoadOptions,
production: ProductionFlags,
) -> EngineResult<ProjectConfig> {
load_project_config(
root,
config_path,
load_options,
production,
&EnvSnapshot::capture(),
)
}
#[cfg(test)]
pub fn config_for_project_with_env(
root: &Path,
config_path: Option<&Path>,
env: &EnvSnapshot,
) -> EngineResult<ProjectConfig> {
load_project_config(
root,
config_path,
ConfigLoadOptions::default(),
ProductionFlags::default(),
env,
)
}
fn load_project_config(
root: &Path,
config_path: Option<&Path>,
load_options: ConfigLoadOptions,
production: ProductionFlags,
env: &EnvSnapshot,
) -> EngineResult<ProjectConfig> {
let file = load_config_file(
root,
config_path,
ConfigFileOptions::new(load_options.allow_remote_extends, env),
)?;
let options = ResolveOptions::host_default();
let duplication = DuplicationScope::for_dead_code_session(&file, production, options, env);
let ConfigFile { config, path, .. } =
file.for_analysis(ProductionAnalysis::DeadCode, production);
if path.is_some() {
validate_boundaries_and_rule_packs(root, &config)?;
}
Ok(mode_entry(root, config, options, env)?.into_project_config(path, duplication))
}
impl ProjectConfig {
#[must_use]
pub fn without_workspace_metadata(mut self) -> Self {
self.workspaces.clear();
self.workspace_diagnostics.clear();
self.workspace_discovery_ms = None;
self
}
}
pub fn default_project_config(root: &Path) -> ProjectConfig {
mode_entry_lossy(
root,
FallowConfig::default(),
ResolveOptions::host_default(),
&EnvSnapshot::capture(),
)
.into_project_config(None, DuplicationScope::SameAsSession)
}
#[cfg(test)]
pub fn config_for_project_analysis(
root: &Path,
config_path: Option<&Path>,
options: ProjectConfigOptions,
) -> EngineResult<ProjectConfig> {
resolve_project_config_analysis(root, config_path, options, &EnvSnapshot::capture())
}
#[cfg(test)]
pub fn config_for_project_analysis_with_env(
root: &Path,
config_path: Option<&Path>,
options: ProjectConfigOptions,
env: &EnvSnapshot,
) -> EngineResult<ProjectConfig> {
resolve_project_config_analysis(root, config_path, options, env)
}
#[cfg(test)]
fn resolve_project_config_analysis(
root: &Path,
config_path: Option<&Path>,
options: ProjectConfigOptions,
env: &EnvSnapshot,
) -> EngineResult<ProjectConfig> {
let file = load_config_file(
root,
config_path,
ConfigFileOptions::new(options.allow_remote_extends, env),
)?
.for_analysis(
options.analysis,
ProductionFlags {
global: options.production_override,
..ProductionFlags::default()
},
);
file.validate(root)?;
let ConfigFile { config, path, .. } = file;
let options = ResolveOptions {
output: options.output,
threads: options.threads,
no_cache: options.no_cache,
quiet: options.quiet,
};
let entry = mode_entry(root, config, options, env)?;
Ok(entry.into_project_config(path, DuplicationScope::SameAsSession))
}
#[cfg(test)]
#[path = "direct_load_tests.rs"]
mod tests;