use std::path::{Path, PathBuf};
use fallow_config::{EntryPointRole, ResolvedConfig};
use fallow_types::discover::{DiscoveredFile, EntryPoint, EntryPointSource};
use fallow_types::path_util::is_absolute_path_any_platform;
use rustc_hash::FxHashSet;
use super::super::walk::SOURCE_EXTENSIONS;
use super::{CategorizedEntryPoints, relative_paths_for};
#[must_use]
pub fn discover_plugin_entry_points(
plugin_result: &crate::plugins::AggregatedPluginResult,
config: &ResolvedConfig,
files: &[DiscoveredFile],
) -> Vec<EntryPoint> {
discover_plugin_entry_point_sets(plugin_result, config, files).all
}
#[must_use]
pub fn discover_plugin_entry_point_sets(
plugin_result: &crate::plugins::AggregatedPluginResult,
config: &ResolvedConfig,
files: &[DiscoveredFile],
) -> CategorizedEntryPoints {
discover_plugin_entry_point_sets_timed(plugin_result, config, files).entries
}
pub struct TimedPluginEntryPoints {
pub entries: CategorizedEntryPoints,
pub build_ms: f64,
pub match_ms: f64,
}
pub fn discover_plugin_entry_point_sets_timed(
plugin_result: &crate::plugins::AggregatedPluginResult,
config: &ResolvedConfig,
files: &[DiscoveredFile],
) -> TimedPluginEntryPoints {
let mut entries = CategorizedEntryPoints::default();
let build_start = std::time::Instant::now();
let relative_paths = relative_paths_for(files, &config.root);
let production_skip = production_skipped_plugins(config);
let (glob_set, glob_meta) = build_plugin_glob_meta(plugin_result, production_skip.as_ref());
let build_ms = build_start.elapsed().as_secs_f64() * 1000.0;
let match_start = std::time::Instant::now();
if let Some(glob_set) = glob_set.filter(|set| !set.is_empty()) {
match_plugin_entry_files(&mut entries, &glob_set, &glob_meta, &relative_paths, files);
}
let match_ms = match_start.elapsed().as_secs_f64() * 1000.0;
push_plugin_setup_files(&mut entries, &config.root, plugin_result);
TimedPluginEntryPoints {
entries: entries.dedup(),
build_ms,
match_ms,
}
}
fn production_skipped_plugins(config: &ResolvedConfig) -> Option<FxHashSet<&str>> {
if !config.production {
return None;
}
let mut names: FxHashSet<&str> = config
.external_plugins
.iter()
.filter(|plugin| plugin.has_explicit_support_role())
.map(|plugin| plugin.name.as_str())
.collect();
if names.is_empty() {
return None;
}
for reserved in crate::plugins::registry::builtin_plugin_names()
.into_iter()
.chain(INTERNAL_ENTRY_SOURCES.iter().copied())
{
names.remove(reserved);
}
(!names.is_empty()).then_some(names)
}
const INTERNAL_ENTRY_SOURCES: &[&str] = &["scripts", "script-tests"];
fn build_plugin_glob_meta<'a>(
plugin_result: &'a crate::plugins::AggregatedPluginResult,
production_skip: Option<&FxHashSet<&str>>,
) -> (Option<globset::GlobSet>, Vec<CompiledEntryRule<'a>>) {
let mut builder = globset::GlobSetBuilder::new();
let mut glob_meta: Vec<CompiledEntryRule<'a>> = Vec::new();
for (rule, pname) in plugin_result.unique_entry_patterns() {
if let Some((include, mut compiled)) = compile_entry_rule(rule, pname, plugin_result) {
compiled.production_excluded = compiled.role == EntryPointRole::Support
&& production_skip.is_some_and(|names| names.contains(pname.as_str()));
builder.add(include);
glob_meta.push(compiled);
}
}
for (pattern, pname) in plugin_result
.discovered_always_used
.iter()
.chain(plugin_result.always_used.iter())
.chain(plugin_result.fixture_patterns.iter())
{
if let Ok(glob) = fallow_config::compile_path_glob(pattern) {
builder.add(glob);
if let Some(path) = crate::plugins::CompiledPathRule::for_entry_rule(
&crate::plugins::PathRule::new(pattern.clone()),
"support entry pattern",
) {
glob_meta.push(CompiledEntryRule {
path,
plugin_name: pname,
role: EntryPointRole::Support,
production_excluded: false,
});
}
}
}
(
super::super::plugin_glob_set_or_warn(builder.build()),
glob_meta,
)
}
fn match_plugin_entry_files(
entries: &mut CategorizedEntryPoints,
glob_set: &globset::GlobSet,
glob_meta: &[CompiledEntryRule<'_>],
relative_paths: &[String],
files: &[DiscoveredFile],
) {
for (idx, rel) in relative_paths.iter().enumerate() {
let matches: Vec<usize> = glob_set
.matches(rel)
.into_iter()
.filter(|match_idx| glob_meta[*match_idx].matches(rel))
.collect();
if matches.is_empty() {
continue;
}
let name = glob_meta[matches[0]].plugin_name;
let entry = EntryPoint {
path: files[idx].path.clone(),
source: EntryPointSource::Plugin {
name: name.to_string(),
},
};
categorize_plugin_match(entries, entry, glob_meta, &matches);
}
}
fn categorize_plugin_match(
entries: &mut CategorizedEntryPoints,
entry: EntryPoint,
glob_meta: &[CompiledEntryRule<'_>],
matches: &[usize],
) {
if matches
.iter()
.all(|&match_idx| glob_meta[match_idx].production_excluded)
{
entries.production_excluded.push(entry.path);
return;
}
let mut has_runtime = false;
let mut has_test = false;
let mut has_support = false;
for &match_idx in matches {
match glob_meta[match_idx].role {
EntryPointRole::Runtime => has_runtime = true,
EntryPointRole::Test => has_test = true,
EntryPointRole::Support => has_support = true,
}
}
if has_runtime {
entries.push_runtime(entry.clone());
}
if has_test {
entries.push_test(entry.clone());
}
if has_support || (!has_runtime && !has_test) {
entries.push_support(entry);
}
}
fn push_plugin_setup_files(
entries: &mut CategorizedEntryPoints,
root: &Path,
plugin_result: &crate::plugins::AggregatedPluginResult,
) {
for (setup_file, pname) in &plugin_result.setup_files {
let resolved = resolve_plugin_setup_file(root, setup_file);
if resolved.exists() {
entries.push_support(EntryPoint {
path: resolved,
source: EntryPointSource::Plugin {
name: pname.clone(),
},
});
continue;
}
for ext in SOURCE_EXTENSIONS {
let with_ext = resolved.with_extension(ext);
if with_ext.exists() {
entries.push_support(EntryPoint {
path: with_ext,
source: EntryPointSource::Plugin {
name: pname.clone(),
},
});
break;
}
}
}
}
pub(super) fn resolve_plugin_setup_file(root: &Path, setup_file: &Path) -> PathBuf {
if is_absolute_path_any_platform(setup_file) {
setup_file.to_path_buf()
} else {
root.join(setup_file)
}
}
struct CompiledEntryRule<'a> {
path: crate::plugins::CompiledPathRule,
plugin_name: &'a str,
role: EntryPointRole,
production_excluded: bool,
}
impl CompiledEntryRule<'_> {
fn matches(&self, path: &str) -> bool {
self.path.matches(path)
}
}
fn compile_entry_rule<'a>(
rule: &'a crate::plugins::PathRule,
plugin_name: &'a str,
plugin_result: &'a crate::plugins::AggregatedPluginResult,
) -> Option<(globset::Glob, CompiledEntryRule<'a>)> {
let include = match crate::plugins::entry_rule_glob(&rule.pattern) {
Ok(glob) => glob,
Err(err) => {
tracing::warn!("invalid entry pattern '{}': {err}", rule.pattern);
return None;
}
};
let role = plugin_result
.entry_point_roles
.get(plugin_name)
.copied()
.unwrap_or(EntryPointRole::Support);
Some((
include,
CompiledEntryRule {
path: crate::plugins::CompiledPathRule::for_entry_rule(rule, "entry pattern")?,
plugin_name,
role,
production_excluded: false,
},
))
}