mod add_config;
mod build;
mod clean;
mod config_cmd;
pub(crate) mod analyzers;
mod doctor;
mod graph;
pub(crate) mod processors;
pub(crate) mod sloc;
pub(crate) mod smart;
pub(crate) mod symlink_install;
pub(crate) mod tools;
pub(crate) use add_config::{add_processor, add_analyzer};
use std::borrow::Cow;
use std::collections::HashMap;
use std::path::PathBuf;
use std::time::{Duration, Instant};
use anyhow::Result;
use crate::analyzers::DepAnalyzer;
use crate::cli::{BuildPhase, DisplayOptions};
use crate::color;
use crate::config::*;
use crate::deps_cache::DepsCache;
use crate::errors;
use crate::file_index::FileIndex;
use crate::graph::BuildGraph;
use crate::object_store::{ObjectStore, ObjectStoreOptions};
use crate::processors::{LuaProcessor, ProcessorMap, Processor};
use crate::remote_cache;
use crate::tool_lock;
pub(crate) type PhaseTimings = Vec<(String, Duration)>;
pub enum ValidationSeverity {
Error,
Warning,
}
pub struct ValidationIssue {
pub severity: ValidationSeverity,
pub message: String,
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum GraphBuildMode {
Normal,
ForClean,
}
fn sorted_keys<V>(map: &HashMap<String, V>) -> Vec<&String> {
let mut keys: Vec<&String> = map.keys().collect();
keys.sort();
keys
}
fn phases_debug() -> bool {
crate::runtime_flags::phases_debug()
}
struct ProductStatusLabels<'a> {
current: (Cow<'a, str>, &'static str),
restorable: (Cow<'a, str>, &'static str),
stale: (Cow<'a, str>, &'static str),
new: (Cow<'a, str>, &'static str),
}
struct StatusPrintOptions<'a> {
force: bool,
labels: &'a ProductStatusLabels<'a>,
explain: bool,
display_opts: DisplayOptions,
verbose: bool,
all_processor_names: &'a [&'a str],
native_processors: &'a std::collections::HashSet<&'a str>,
}
pub(crate) fn create_processor_for_instance(
type_name: &str,
config_toml: &toml::Value,
) -> anyhow::Result<Option<Box<dyn Processor>>> {
if let Some(entry) = find_registry_entry(type_name) {
let mut resolved = config_toml.clone();
crate::registries::apply_all_defaults(entry.name, &mut resolved);
return (entry.create)(&resolved).map(Some);
}
Ok(None)
}
pub(crate) fn create_all_default_processors() -> ProcessorMap {
let mut processors: ProcessorMap = HashMap::new();
for entry in registry_entries() {
let mut empty_toml = toml::Value::Table(toml::map::Map::new());
crate::registries::apply_all_defaults(entry.name, &mut empty_toml);
let proc = (entry.create)(&empty_toml).unwrap();
processors.insert(entry.name.to_string(), proc);
}
processors
}
pub struct Builder {
object_store: ObjectStore,
config: Config,
file_index: FileIndex,
}
impl Builder {
pub fn new() -> Result<Self> {
Config::require_config()?;
let config = Config::load()?;
let restore_method = config.cache.restore_method.resolve();
if config.cache.compression && restore_method == crate::config::RestoreMethod::Hardlink {
anyhow::bail!("Cannot use cache compression with hardlink restore method. \
Set restore_method = \"copy\" or disable compression.");
}
let remote_backend = match &config.cache.remote {
Some(url) => Some(remote_cache::create_backend(url)?),
None => None,
};
if !config.cache.mtime_check {
crate::checksum::set_mtime_check(false);
}
let object_store = ObjectStore::new(ObjectStoreOptions {
restore_method,
compression: config.cache.compression,
remote: remote_backend,
remote_push: config.cache.remote_push,
remote_pull: config.cache.remote_pull,
})?;
let file_index = FileIndex::build()?;
Ok(Self {
object_store,
config,
file_index,
})
}
pub fn create_processors(&self) -> Result<ProcessorMap> {
let cfg = &self.config.processor;
let mut processors: ProcessorMap = HashMap::new();
for inst in &cfg.instances {
match create_processor_for_instance(&inst.type_name, &inst.config_toml) {
Ok(Some(proc)) => {
processors.insert(inst.instance_name.clone(), proc);
}
Ok(None) => {
anyhow::bail!("Unknown processor type: '{}'", inst.type_name);
}
Err(e) => {
return Err(e.context(format!(
"Failed to create processor instance '{}'", inst.instance_name
)));
}
}
}
let lua_plugins = LuaProcessor::discover_plugins(
&self.config.plugins.dir,
&cfg.extra,
)?;
for (name, proc) in lua_plugins {
if processors.contains_key(&name) {
anyhow::bail!("Lua plugin '{}' conflicts with processor instance", name);
}
processors.insert(name, Box::new(proc));
}
Ok(processors)
}
fn detect_config_changes(&self, processors: &ProcessorMap, show_all: bool) {
if crate::json_output::is_json_mode() || crate::runtime_flags::quiet() {
return;
}
for name in sorted_keys(processors) {
let processor = processors.get(name).expect(errors::PROCESSOR_NOT_IN_MAP);
let config_json = match processor.config_json() {
Some(json) => json,
None => continue,
};
let config_json = if show_all {
config_json
} else {
Self::filter_output_fields(name, &config_json)
};
if let Ok(Some(old_json)) = self.object_store.store_processor_config(name, &config_json) {
if let Some(diff) = ObjectStore::diff_configs(&old_json, &config_json) {
println!("{}",
color::yellow(&format!("Config changed for [{}]:", name)));
println!("{}", diff);
}
}
}
}
fn filter_output_fields(processor_name: &str, json: &str) -> String {
let output_fields = ProcessorConfig::output_fields_for(processor_name);
let Ok(value) = serde_json::from_str::<serde_json::Value>(json) else {
return json.to_string();
};
let Some(obj) = value.as_object() else {
return json.to_string();
};
match output_fields {
Some(fields) => {
let filtered: serde_json::Map<String, serde_json::Value> = obj.iter()
.filter(|(k, _)| fields.contains(&k.as_str()))
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
serde_json::to_string(&filtered).unwrap_or_else(|_| json.to_string())
}
None => json.to_string(),
}
}
fn create_analyzers(&self, verbose: bool) -> Result<HashMap<String, Box<dyn DepAnalyzer>>> {
let mut analyzers: HashMap<String, Box<dyn DepAnalyzer>> = HashMap::new();
for inst in &self.config.analyzer.instances {
let plugin = crate::registries::find_analyzer_plugin(&inst.type_name)
.ok_or_else(|| anyhow::anyhow!("Unknown analyzer type '{}'", inst.type_name))?;
let analyzer = (plugin.create)(&inst.instance_name, &inst.config_toml, verbose)?;
analyzers.insert(inst.instance_name.clone(), analyzer);
}
Ok(analyzers)
}
fn run_analyzers(&self, graph: &mut BuildGraph, verbose: bool) -> Result<()> {
let analyzers = self.create_analyzers(verbose)?;
let mut deps_cache = DepsCache::open()?;
let active_analyzers: Vec<&String> = sorted_keys(&analyzers).into_iter()
.filter(|name| analyzers[*name].auto_detect(&self.file_index))
.collect();
for name in &active_analyzers {
analyzers[*name].analyze(graph, &mut deps_cache, &self.file_index, verbose)?;
}
Ok(())
}
fn build_graph_with_processors(&self, processors: &ProcessorMap) -> Result<BuildGraph> {
let (graph, _) = self.build_graph_with_processors_impl(processors, GraphBuildMode::Normal, BuildPhase::Build, None, false)?;
Ok(graph)
}
fn build_graph_with_processors_and_phase(&self, processors: &ProcessorMap, stop_after: BuildPhase, processor_filter: Option<&[String]>, verbose: bool) -> Result<(BuildGraph, PhaseTimings)> {
self.build_graph_with_processors_impl(processors, GraphBuildMode::Normal, stop_after, processor_filter, verbose)
}
fn build_graph_for_clean_with_processors(&self, processors: &ProcessorMap) -> Result<BuildGraph> {
let (graph, _) = self.build_graph_with_processors_impl(processors, GraphBuildMode::ForClean, BuildPhase::Build, None, false)?;
Ok(graph)
}
pub fn detected_processors(&self) -> Result<std::collections::HashSet<String>> {
let processors = create_all_default_processors();
let mut detected = std::collections::HashSet::new();
for (name, proc) in &processors {
if proc.auto_detect(&self.file_index) {
detected.insert(name.clone());
}
}
Ok(detected)
}
pub fn detected_and_available_processors(&self) -> Result<std::collections::HashSet<String>> {
let processors = create_all_default_processors();
let mut available = std::collections::HashSet::new();
for (name, proc) in &processors {
if !proc.auto_detect(&self.file_index) {
continue;
}
let tools = proc.required_tools();
if tools.iter().all(|t| which::which(t).is_ok()) {
available.insert(name.clone());
}
}
Ok(available)
}
pub fn no_file_processors(&self) -> Result<Vec<String>> {
let processors = self.create_processors()?;
let graph = self.build_graph_with_processors(&processors)?;
let mut has_products: std::collections::HashSet<&str> = std::collections::HashSet::new();
for product in graph.products() {
has_products.insert(product.processor.as_str());
}
let mut empty: Vec<String> = sorted_keys(&processors)
.into_iter()
.filter(|name| !has_products.contains(name.as_str()))
.cloned()
.collect();
empty.sort();
Ok(empty)
}
fn is_processor_active(&self, _name: &str, _processor: &dyn Processor) -> bool {
true
}
fn discover_products(
&self,
graph: &mut BuildGraph,
processors: &ProcessorMap,
active: &[impl AsRef<str>],
for_clean: bool,
) -> Result<()> {
let mut file_index = self.file_index.clone();
let debug = phases_debug();
for pass in 0..10 {
let before = graph.products().len();
for name in active {
let name = name.as_ref();
if !processors[name].scan_config().enabled {
continue;
}
if for_clean {
processors[name].discover_for_clean(graph, &file_index, name)?;
} else {
processors[name].discover(graph, &file_index, name)?;
}
}
let after = graph.products().len();
if after == before {
break;
}
if pass + 1 >= 10 {
break;
}
let outputs: Vec<PathBuf> = graph.products()[before..after]
.iter()
.flat_map(|p| p.outputs.iter().cloned())
.collect();
let added = file_index.add_virtual_files(&outputs);
if added == 0 {
break;
}
if debug {
eprintln!("{}", color::dim(&format!(
" discover pass {}: {} new products, {} virtual files added",
pass + 1, after - before, added
)));
}
}
Ok(())
}
fn build_graph_with_processors_impl(&self, processors: &ProcessorMap, mode: GraphBuildMode, stop_after: BuildPhase, processor_filter: Option<&[String]>, verbose: bool) -> Result<(BuildGraph, PhaseTimings)> {
if phases_debug() {
eprintln!("{}", color::bold("Phase: Building dependency graph..."));
}
let mut graph = BuildGraph::new();
let mut phase_timings = PhaseTimings::new();
let active_processors: Vec<&String> = sorted_keys(processors).into_iter()
.filter(|name| {
if let Some(filter) = processor_filter
&& !filter.iter().any(|f| f == *name) {
return false;
}
self.is_processor_active(name, processors[*name].as_ref())
})
.collect();
if phases_debug() {
eprintln!("{}", color::dim(" Phase: discover"));
}
let t = Instant::now();
self.discover_products(&mut graph, processors, &active_processors, mode == GraphBuildMode::ForClean)?;
phase_timings.push(("discover".to_string(), t.elapsed()));
if stop_after == BuildPhase::Discover {
return Ok((graph, phase_timings));
}
if mode == GraphBuildMode::Normal {
if phases_debug() {
eprintln!("{}", color::dim(" Phase: add_dependencies"));
}
let t = Instant::now();
self.run_analyzers(&mut graph, verbose)?;
phase_timings.push(("add_dependencies".to_string(), t.elapsed()));
}
if stop_after == BuildPhase::AddDependencies {
return Ok((graph, phase_timings));
}
if phases_debug() {
eprintln!("{}", color::dim(" Phase: apply_tool_version_hashes"));
}
let t = Instant::now();
let tool_hashes = tool_lock::processor_tool_hashes(
processors,
&|_name| true, )?;
if !tool_hashes.is_empty() {
graph.apply_tool_version_hashes(&tool_hashes);
}
phase_timings.push(("tool_version_hashes".to_string(), t.elapsed()));
if phases_debug() {
eprintln!("{}", color::dim(" Phase: resolve_dependencies"));
}
let t = Instant::now();
graph.resolve_dependencies();
phase_timings.push(("resolve".to_string(), t.elapsed()));
Ok((graph, phase_timings))
}
pub fn build_graph_filtered(
&self,
filter_name: Option<&str>,
include_all: bool,
) -> Result<BuildGraph> {
let processors = self.create_processors()?;
let mut graph = BuildGraph::new();
let active_processors: Vec<&String> = sorted_keys(&processors).into_iter()
.filter(|name| {
if let Some(filter) = filter_name
&& name.as_str() != filter {
return false;
}
include_all || self.is_processor_active(name, processors[*name].as_ref())
})
.collect();
self.discover_products(&mut graph, &processors, &active_processors, false)?;
self.run_analyzers(&mut graph, false)?;
graph.resolve_dependencies();
Ok(graph)
}
fn build_graph(&self) -> Result<BuildGraph> {
let processors = self.create_processors()?;
self.build_graph_with_processors(&processors)
}
pub fn build_graph_for_cache(&self) -> Result<BuildGraph> {
self.build_graph()
}
pub fn valid_cache_keys(&self) -> Result<std::collections::HashSet<String>> {
let graph = self.build_graph_for_cache()?;
Ok(graph.products().iter().map(|p| p.cache_key()).collect())
}
pub fn object_store(&self) -> &ObjectStore {
&self.object_store
}
pub fn watch_paths(&self) -> Vec<PathBuf> {
let mut paths: Vec<PathBuf> = Vec::new();
let config_path = PathBuf::from("rsconstruct.toml");
if config_path.exists() {
paths.push(config_path);
}
for dir in self.config.processor.src_dirs() {
let full = PathBuf::from(&dir);
if full.exists() {
paths.push(full);
}
}
for name in &["pyproject.toml", "config"] {
let p = PathBuf::from(name);
if p.exists() {
paths.push(p);
}
}
let plugins = PathBuf::from(&self.config.plugins.dir);
if plugins.exists() {
paths.push(plugins);
}
paths.sort();
paths.dedup();
paths
}
}