Skip to content

Commit

Permalink
remove files including test only from generated filelist
Browse files Browse the repository at this point in the history
(refs: #1047)
  • Loading branch information
taichi-ishitani committed Nov 9, 2024
1 parent c52bcfe commit d18a220
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
45 changes: 28 additions & 17 deletions crates/veryl/src/cmd_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -25,7 +25,7 @@ impl CmdBuild {
Self { opt }
}

pub fn exec(&self, metadata: &mut Metadata) -> Result<bool> {
pub fn exec(&self, metadata: &mut Metadata, include_tests: bool) -> Result<bool> {
let paths = metadata.paths(&self.opt.files, true)?;

let mut check_error = CheckError::default();
Expand Down Expand Up @@ -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)
Expand All @@ -148,11 +148,12 @@ impl CmdBuild {
metadata: &Metadata,
paths: &[PathSet],
temp_dir: Option<TempDir>,
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();
Expand Down Expand Up @@ -200,24 +201,34 @@ impl CmdBuild {
Ok(())
}

fn sort_filelist(metadata: &Metadata, paths: &[PathSet]) -> Vec<PathSet> {
fn sort_filelist(metadata: &Metadata, paths: &[PathSet], include_tests: bool) -> Vec<PathSet> {
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);
}
}
}
Expand All @@ -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());
}

Expand Down
2 changes: 1 addition & 1 deletion crates/veryl/src/cmd_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion crates/veryl/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ fn main() -> Result<ExitCode> {
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)?,
Expand Down

0 comments on commit d18a220

Please sign in to comment.