From d18a220c39a7b1ddcfe20059fd89c5e94cb0259f Mon Sep 17 00:00:00 2001 From: Taichi Ishitani Date: Sat, 9 Nov 2024 01:08:11 +0900 Subject: [PATCH] remove files including test only from generated filelist (refs: veryl-lang/veryl#1047) --- crates/veryl/src/cmd_build.rs | 45 ++++++++++++++++++++++------------- crates/veryl/src/cmd_test.rs | 2 +- crates/veryl/src/main.rs | 2 +- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/crates/veryl/src/cmd_build.rs b/crates/veryl/src/cmd_build.rs index e9d0fe4b..785c9670 100644 --- a/crates/veryl/src/cmd_build.rs +++ b/crates/veryl/src/cmd_build.rs @@ -10,7 +10,7 @@ use std::path::{Path, PathBuf}; use tempfile::TempDir; use veryl_analyzer::namespace::Namespace; use veryl_analyzer::symbol::SymbolKind; -use veryl_analyzer::{type_dag, Analyzer}; +use veryl_analyzer::{symbol_table, type_dag, Analyzer}; use veryl_emitter::Emitter; use veryl_metadata::{FilelistType, Metadata, SourceMapTarget, Target}; use veryl_parser::{resource_table, veryl_token::TokenSource, Parser}; @@ -25,7 +25,7 @@ impl CmdBuild { Self { opt } } - pub fn exec(&self, metadata: &mut Metadata) -> Result { + pub fn exec(&self, metadata: &mut Metadata, include_tests: bool) -> Result { let paths = metadata.paths(&self.opt.files, true)?; let mut check_error = CheckError::default(); @@ -124,7 +124,7 @@ impl CmdBuild { } } - self.gen_filelist(metadata, &paths, temp_dir)?; + self.gen_filelist(metadata, &paths, temp_dir, include_tests)?; let _ = check_error.check_all()?; Ok(true) @@ -148,11 +148,12 @@ impl CmdBuild { metadata: &Metadata, paths: &[PathSet], temp_dir: Option, + include_tests: bool, ) -> Result<()> { let filelist_path = metadata.filelist_path(); let base_path = metadata.project_path(); - let paths = Self::sort_filelist(metadata, paths); + let paths = Self::sort_filelist(metadata, paths, include_tests); let text = if let Target::Bundle { path } = &metadata.build.target { let temp_dir = temp_dir.unwrap(); @@ -200,24 +201,34 @@ impl CmdBuild { Ok(()) } - fn sort_filelist(metadata: &Metadata, paths: &[PathSet]) -> Vec { + fn sort_filelist(metadata: &Metadata, paths: &[PathSet], include_tests: bool) -> Vec { let mut table = HashMap::new(); for path in paths { table.insert(path.src.clone(), path); } - // Remove files which are not connected from project - let connected_components = type_dag::connected_components(); + // Collect files connected from project let mut prj_namespace = Namespace::new(); prj_namespace.push(resource_table::insert_str(&metadata.project.name)); - for symbols in &connected_components { - let used = symbols.iter().any(|x| x.namespace.included(&prj_namespace)); - if !used { - for symbol in symbols { - if let TokenSource::File(x) = symbol.token.source { - let path = PathBuf::from(format!("{}", x)); - table.remove(&path); - } + + let mut candidate_symbols: Vec<_> = type_dag::connected_components() + .into_iter() + .flatten() + .filter(|symbol| symbol.namespace.included(&prj_namespace)) + .collect(); + if include_tests { + candidate_symbols.extend(symbol_table::get_all().into_iter().filter(|symbol| { + matches!(symbol.kind, SymbolKind::Test(_)) + && symbol.namespace.included(&prj_namespace) + })); + } + + let mut used_paths = HashMap::new(); + for symbol in &candidate_symbols { + if let TokenSource::File(x) = symbol.token.source { + let path = PathBuf::from(format!("{}", x)); + if let Some(x) = table.remove(&path) { + used_paths.insert(path, x); } } } @@ -231,14 +242,14 @@ impl CmdBuild { ) { if let TokenSource::File(x) = symbol.token.source { let path = PathBuf::from(format!("{}", x)); - if let Some(x) = table.remove(&path) { + if let Some(x) = used_paths.remove(&path) { ret.push(x.clone()); } } } } - for path in table.into_values() { + for path in used_paths.into_values() { ret.push(path.clone()); } diff --git a/crates/veryl/src/cmd_test.rs b/crates/veryl/src/cmd_test.rs index 7c52ade3..2bc9d927 100644 --- a/crates/veryl/src/cmd_test.rs +++ b/crates/veryl/src/cmd_test.rs @@ -23,7 +23,7 @@ impl CmdTest { let build = CmdBuild::new(OptBuild { files: self.opt.files.clone(), }); - build.exec(metadata)?; + build.exec(metadata, true)?; let tests: Vec<_> = symbol_table::get_all() .into_iter() diff --git a/crates/veryl/src/main.rs b/crates/veryl/src/main.rs index deb73905..83c4c52d 100644 --- a/crates/veryl/src/main.rs +++ b/crates/veryl/src/main.rs @@ -317,7 +317,7 @@ fn main() -> Result { Commands::Init(x) => cmd_init::CmdInit::new(x).exec()?, Commands::Fmt(x) => cmd_fmt::CmdFmt::new(x).exec(&mut metadata)?, Commands::Check(x) => cmd_check::CmdCheck::new(x).exec(&mut metadata)?, - Commands::Build(x) => cmd_build::CmdBuild::new(x).exec(&mut metadata)?, + Commands::Build(x) => cmd_build::CmdBuild::new(x).exec(&mut metadata, false)?, Commands::Clean(x) => cmd_clean::CmdClean::new(x).exec(&mut metadata)?, Commands::Update(x) => cmd_update::CmdUpdate::new(x).exec(&mut metadata)?, Commands::Publish(x) => cmd_publish::CmdPublish::new(x).exec(&mut metadata)?,