diff --git a/src/build.rs b/src/build.rs index 345f403..4d97057 100644 --- a/src/build.rs +++ b/src/build.rs @@ -11,6 +11,7 @@ pub mod read_compile_state; use crate::build::compile::{mark_modules_with_deleted_deps_dirty, mark_modules_with_expired_deps_dirty}; use crate::helpers::emojis::*; use crate::helpers::{self, get_workspace_root}; +use crate::sourcedirs; use ahash::AHashSet; use build_types::*; use console::style; @@ -344,6 +345,7 @@ pub fn incremental_build( let compile_duration = start_compiling.elapsed(); logs::finalize(&build_state.packages); + sourcedirs::print(&build_state); pb.finish(); if !compile_errors.is_empty() { if helpers::contains_ascii_characters(&compile_warnings) { diff --git a/src/lib.rs b/src/lib.rs index e35ee28..b84aed0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,4 +4,5 @@ pub mod cmd; pub mod helpers; pub mod lock; pub mod queue; +pub mod sourcedirs; pub mod watcher; diff --git a/src/main.rs b/src/main.rs index 5001219..30a9f73 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,7 @@ use clap::{Parser, ValueEnum}; use regex::Regex; -pub mod bsconfig; -pub mod build; -pub mod cmd; -pub mod helpers; -pub mod lock; -pub mod queue; -pub mod watcher; +use rewatch::{build, cmd, lock, watcher}; #[derive(Debug, Clone, ValueEnum)] enum Command { diff --git a/src/sourcedirs.rs b/src/sourcedirs.rs new file mode 100644 index 0000000..cf452bf --- /dev/null +++ b/src/sourcedirs.rs @@ -0,0 +1,117 @@ +use crate::build::build_types::BuildState; +use crate::build::packages::Package; +use ahash::{AHashMap, AHashSet}; +use rayon::prelude::*; +use serde::Serialize; +use serde_json::json; +use std::fs::File; +use std::io::prelude::*; + +type Dir = String; +type PackageName = String; +type AbsolutePath = String; +type PackagePath = String; + +#[derive(Serialize, Debug, Clone, PartialEq, Hash)] +pub struct SourceDirs { + pub dirs: Vec, + pub pkgs: Vec<(PackageName, AbsolutePath)>, + pub generated: Vec, +} + +pub fn print(buildstate: &BuildState) { + let (_name, package) = buildstate + .packages + .iter() + .find(|(_name, package)| package.is_root) + .expect("Could not find root package"); + + // First do all child packages + let child_packages = buildstate + .packages + .par_iter() + .filter(|(_name, package)| !package.is_root) + .map(|(_name, package)| { + let path = package.get_bs_build_path(); + + let dirs = package + .dirs + .to_owned() + .unwrap_or(AHashSet::new()) + .iter() + .filter_map(|path| path.to_str().map(|x| x.to_string())) + .collect::>(); + + fn deps_to_pkgs<'a>( + packages: &'a AHashMap, + xs: &'a Option>, + ) -> AHashSet<(String, PackagePath)> { + xs.as_ref() + .unwrap_or(&vec![]) + .iter() + .filter_map(|name| { + packages + .get(&name.to_owned()) + .map(|package| (name.clone(), package.path.clone())) + }) + .collect::>() + } + + let pinned_dependencies = + deps_to_pkgs(&buildstate.packages, &package.bsconfig.pinned_dependencies); + let bs_dependencies = deps_to_pkgs(&buildstate.packages, &package.bsconfig.bs_dependencies); + let bs_dev_dependencies = + deps_to_pkgs(&buildstate.packages, &package.bsconfig.bs_dev_dependencies); + + let mut pkgs = AHashMap::new(); + pkgs.extend(pinned_dependencies); + pkgs.extend(bs_dependencies); + pkgs.extend(bs_dev_dependencies); + + let name = path + "/.sourcedirs.json"; + let _ = File::create(&name).map(|mut file| { + let source_files = SourceDirs { + dirs: dirs.clone().into_iter().collect::>(), + pkgs: pkgs + .clone() + .into_iter() + .collect::>(), + generated: vec![], + }; + + file.write(json!(source_files).to_string().as_bytes()) + }); + let _ = std::fs::copy(package.get_bs_build_path(), package.get_build_path()); + + (&package.path, dirs, pkgs) + }) + .collect::, + AHashMap, + )>>(); + + let mut all_dirs = AHashSet::new(); + let mut all_pkgs: AHashMap = AHashMap::new(); + + child_packages.iter().for_each(|(package_path, dirs, pkgs)| { + dirs.iter().for_each(|dir| { + all_dirs.insert(format!("{package_path}/{dir}")); + }); + + all_pkgs.extend(pkgs.to_owned()); + }); + + let path = package.get_bs_build_path(); + let name = path + "/.sourcedirs.json"; + let _ = File::create(name.clone()).map(|mut file| { + let all_source_files = SourceDirs { + dirs: all_dirs.into_iter().collect::>(), + pkgs: all_pkgs.into_iter().collect::>(), + generated: vec![], + }; + file.write(json!(all_source_files).to_string().as_bytes()) + }); + + let _ = std::fs::copy(package.get_bs_build_path(), package.get_build_path()); +}