use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::SystemTime;
use ignore::WalkBuilder;
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use crate::modules::clover::Coverage;
use crate::modules::git::Churn;
use crate::modules::kind::{COMMANDS, Kinds, TESTS};
use crate::modules::layout::Layout;
use crate::php::{Analysis, Declared, analyze, count_tests};
use crate::surface::AcceptedSurface;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ModuleState {
On,
Off,
Unknown,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Manifest {
Valid,
InvalidJson {
message: String,
line: usize,
},
NoName,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct KindCount {
pub kind: String,
pub files: u32,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Module {
pub name: String,
pub code: String,
pub dir: PathBuf,
pub state: ModuleState,
pub description: String,
pub priority: i64,
pub manifest: Manifest,
pub surface: Option<AcceptedSurface>,
pub namespace: String,
pub files: u64,
pub lines: u64,
pub kinds: Vec<KindCount>,
pub commands: Vec<String>,
pub tests: u32,
pub coverage: Option<Coverage>,
pub churn: Option<Churn>,
pub uncommitted: Option<u32>,
}
pub(crate) struct FoundModule {
pub(crate) name: String,
pub(crate) dir: PathBuf,
pub(crate) state: ModuleState,
pub(crate) description: String,
pub(crate) priority: i64,
pub(crate) manifest: Manifest,
pub(crate) surface: Option<AcceptedSurface>,
pub(crate) namespace: String,
pub(crate) scan: Scan,
}
#[derive(Debug, Default, Clone)]
pub(crate) struct Scan {
pub(crate) files: u64,
pub(crate) lines: u64,
pub(crate) kinds: Vec<KindCount>,
pub(crate) tests: u32,
pub(crate) commands: BTreeSet<String>,
pub(crate) sources: Vec<Source>,
}
#[derive(Debug, Clone)]
pub(crate) struct Source {
pub(crate) relative: String,
pub(crate) is_test: bool,
pub(crate) analysis: Arc<Analysis>,
}
#[derive(Debug, Clone)]
struct Facts {
size: u64,
modified: Option<SystemTime>,
is_test: bool,
lines: u64,
tests: u32,
analysis: Arc<Analysis>,
}
#[derive(Debug, Default)]
pub struct Progress {
read: AtomicUsize,
total: AtomicUsize,
}
impl Progress {
pub fn get(&self) -> (usize, usize) {
(
self.read.load(Ordering::Relaxed),
self.total.load(Ordering::Relaxed),
)
}
}
#[derive(Debug, Default)]
pub struct ReadCache {
files: HashMap<PathBuf, Facts>,
progress: Arc<Progress>,
}
impl ReadCache {
pub fn clear(&mut self) {
self.files.clear();
}
pub fn progress(&self) -> Arc<Progress> {
Arc::clone(&self.progress)
}
}
pub(crate) fn find_modules(
layout: &Layout,
kinds: &Kinds,
cache: &mut ReadCache,
) -> (Vec<FoundModule>, Vec<String>) {
let statuses = read_statuses(&layout.statuses_file);
let listed: Vec<String> = statuses.keys().cloned().collect();
let Ok(entries) = std::fs::read_dir(&layout.modules_dir) else {
return (Vec::new(), listed);
};
let dirs: Vec<PathBuf> = entries
.filter_map(Result::ok)
.map(|entry| entry.path())
.filter(|dir| dir.join("module.json").is_file())
.collect();
let total: usize = dirs
.iter()
.map(|dir| {
files_of(dir)
.iter()
.filter(|(_, relative)| is_php(relative))
.count()
})
.sum();
cache.progress.read.store(0, Ordering::Relaxed);
cache.progress.total.store(total, Ordering::Relaxed);
let mut modules: Vec<FoundModule> = dirs
.into_iter()
.filter_map(|dir| {
let folder = dir.file_name()?.to_str()?.to_owned();
if folder.starts_with('.') {
return None;
}
let (manifest, fields) = read_manifest(&dir.join("module.json"));
let name = fields.name.unwrap_or(folder);
let state = if !layout.uses_file_activator() {
ModuleState::Unknown
} else if statuses.get(&name) == Some(&serde_json::Value::Bool(true)) {
ModuleState::On
} else {
ModuleState::Off
};
Some(FoundModule {
scan: scan(&dir, kinds, cache),
namespace: read_namespace(&dir, layout, &name),
name,
dir,
state,
description: fields.description,
priority: fields.priority,
surface: fields.surface,
manifest,
})
})
.collect();
modules.sort_by(|a, b| {
a.name
.to_lowercase()
.cmp(&b.name.to_lowercase())
.then_with(|| a.name.cmp(&b.name))
.then_with(|| a.dir.cmp(&b.dir))
});
(modules, listed)
}
#[derive(Default)]
struct ManifestFields {
name: Option<String>,
description: String,
priority: i64,
surface: Option<AcceptedSurface>,
}
fn read_manifest(path: &Path) -> (Manifest, ManifestFields) {
let json = std::fs::read(path).unwrap_or_default();
let value: serde_json::Value = match serde_json::from_slice(&json) {
Ok(value) => value,
Err(error) => {
let text = error.to_string();
let message = text.split(" at line ").next().unwrap_or(&text).to_owned();
let invalid = Manifest::InvalidJson {
message,
line: error.line(),
};
return (invalid, ManifestFields::default());
}
};
let fields = ManifestFields {
name: value
.get("name")
.and_then(|name| name.as_str())
.map(str::to_owned),
description: value
.get("description")
.and_then(|description| description.as_str())
.unwrap_or_default()
.to_owned(),
priority: value
.get("priority")
.and_then(serde_json::Value::as_i64)
.unwrap_or(0),
surface: AcceptedSurface::of_manifest(&value),
};
let manifest = if fields.name.is_some() {
Manifest::Valid
} else {
Manifest::NoName
};
(manifest, fields)
}
fn read_statuses(path: &Path) -> BTreeMap<String, serde_json::Value> {
std::fs::read(path)
.ok()
.and_then(|json| serde_json::from_slice(&json).ok())
.unwrap_or_default()
}
fn read_namespace(dir: &Path, layout: &Layout, name: &str) -> String {
let from_composer = std::fs::read(dir.join("composer.json"))
.ok()
.and_then(|json| serde_json::from_slice::<serde_json::Value>(&json).ok())
.and_then(|composer| {
let prefixes = composer.get("autoload")?.get("psr-4")?.as_object()?;
prefixes.keys().min_by_key(|prefix| prefix.len()).cloned()
})
.map(|prefix| prefix.trim_matches('\\').to_owned())
.filter(|prefix| !prefix.is_empty());
from_composer.unwrap_or_else(|| format!("{}\\{name}", layout.namespace.trim_matches('\\')))
}
struct Entry {
path: PathBuf,
relative: String,
folder: String,
is_test: bool,
size: u64,
modified: Option<SystemTime>,
}
pub(crate) fn files_of(dir: &Path) -> Vec<(PathBuf, String)> {
let walk = WalkBuilder::new(dir)
.require_git(false)
.filter_entry(|entry| {
let is_dir = entry.file_type().is_some_and(|kind| kind.is_dir());
!(is_dir && matches!(entry.file_name().to_str(), Some("vendor" | "node_modules")))
})
.build();
walk.filter_map(Result::ok)
.filter(|entry| entry.file_type().is_some_and(|kind| kind.is_file()))
.map(|entry| {
let path = entry.path();
let relative = path
.strip_prefix(dir)
.unwrap_or(path)
.components()
.map(|part| part.as_os_str().to_string_lossy())
.collect::<Vec<_>>()
.join("/");
(path.to_owned(), relative)
})
.collect()
}
pub(crate) fn command_of(declared: &Declared, folder: &str) -> Option<String> {
declared.command.clone().or_else(|| {
declared
.name_property
.clone()
.filter(|_| folder == COMMANDS)
})
}
pub(crate) fn artisan_command(target: &str) -> Option<&str> {
let mut words = target.split_whitespace();
let php = words.next() == Some("php");
let artisan = words
.next()
.and_then(|word| word.rsplit(['/', '\\']).next())
== Some("artisan");
if !(php && artisan) {
return None;
}
words.next().map(|name| name.trim_matches(['\'', '"']))
}
pub(crate) fn is_php(relative: &str) -> bool {
relative.ends_with(".php") && !relative.ends_with(".blade.php")
}
fn scan(dir: &Path, kinds: &Kinds, cache: &mut ReadCache) -> Scan {
let mut scan = Scan::default();
let mut by_kind: BTreeMap<String, u32> = BTreeMap::new();
let mut php: Vec<Entry> = Vec::new();
for (path, relative) in files_of(dir) {
let path = path.as_path();
let kind = kinds.of(&relative);
let is_test = kind == TESTS;
scan.files += 1;
*by_kind.entry(kind.clone()).or_default() += 1;
if is_php(&relative) {
let metadata = std::fs::metadata(path).ok();
php.push(Entry {
path: path.to_owned(),
relative,
folder: kind,
is_test,
size: metadata.as_ref().map_or(0, std::fs::Metadata::len),
modified: metadata.and_then(|metadata| metadata.modified().ok()),
});
}
}
let changed: Vec<&Entry> = php
.iter()
.filter(|entry| {
!cache.files.get(&entry.path).is_some_and(|facts| {
facts.size == entry.size
&& facts.modified == entry.modified
&& entry.modified.is_some()
&& facts.is_test == entry.is_test
})
})
.collect();
let progress = Arc::clone(&cache.progress);
progress
.read
.fetch_add(php.len() - changed.len(), Ordering::Relaxed);
let read: Vec<(PathBuf, Facts)> = changed
.par_iter()
.map(|entry| {
let source = std::fs::read(&entry.path).unwrap_or_default();
progress.read.fetch_add(1, Ordering::Relaxed);
let facts = Facts {
size: entry.size,
modified: entry.modified,
is_test: entry.is_test,
lines: count_lines(&source),
tests: if entry.is_test {
count_tests(&source)
} else {
0
},
analysis: Arc::new(analyze(&source)),
};
(entry.path.clone(), facts)
})
.collect();
cache.files.extend(read);
for entry in php {
let Some(facts) = cache.files.get(&entry.path) else {
continue;
};
scan.lines += facts.lines;
scan.tests += facts.tests;
if !entry.is_test {
scan.commands.extend(
facts
.analysis
.declares
.iter()
.filter_map(|declared| command_of(declared, &entry.folder)),
);
}
scan.sources.push(Source {
relative: entry.relative,
is_test: entry.is_test,
analysis: Arc::clone(&facts.analysis),
});
}
scan.sources.sort_by(|a, b| a.relative.cmp(&b.relative));
scan.kinds = by_kind
.into_iter()
.map(|(kind, files)| KindCount { kind, files })
.collect();
scan.kinds
.sort_by(|a, b| b.files.cmp(&a.files).then_with(|| a.kind.cmp(&b.kind)));
scan
}
fn count_lines(source: &[u8]) -> u64 {
let ends = source.iter().filter(|byte| **byte == b'\n').count() as u64;
match source.last() {
Some(b'\n') | None => ends,
Some(_) => ends + 1,
}
}
#[cfg(test)]
mod tests {
use super::*;
fn write(path: &Path, content: &str) {
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(path, content).unwrap();
}
fn scan_dir(dir: &Path) -> Scan {
scan(dir, &Kinds::new(None, "app/"), &mut ReadCache::default())
}
#[test]
fn counts_files_but_not_hidden_ignored_or_installed_ones() {
let dir = tempfile::tempdir().unwrap();
let module = dir.path();
write(&module.join("module.json"), "{}");
write(&module.join("app/Models/Post.php"), "<?php");
write(&module.join("resources/views/index.blade.php"), "");
write(&module.join("tests/Unit/.gitkeep"), "");
write(&module.join("node_modules/left-pad/index.js"), "");
write(&module.join("vendor/autoload.php"), "<?php");
write(&module.join(".gitignore"), "/build\n");
write(&module.join("build/app.js"), "");
assert_eq!(scan_dir(module).files, 3);
}
#[test]
fn counts_lines_of_php_kinds_and_tests() {
let dir = tempfile::tempdir().unwrap();
let module = dir.path();
write(&module.join("module.json"), "{\n}\n");
write(
&module.join("app/Models/Post.php"),
"<?php\n\nclass Post {}\n",
);
write(&module.join("app/Models/Tag.php"), "<?php\nclass Tag {}");
write(
&module.join("app/Http/Controllers/PostController.php"),
"<?php\n",
);
write(
&module.join("resources/views/index.blade.php"),
"<p>\n</p>\n<?php echo 1; ?>\n",
);
write(
&module.join("tests/Unit/PostTest.php"),
"<?php\nclass PostTest { protected $signature = 'post:test';\n function testTitle() {}\n function testSlug() {}\n}\n",
);
write(
&module.join("app/Support/Check.php"),
"<?php class Check { protected $signature = 'check:run {--all}'; function testThis() {} }",
);
let scan = scan_dir(module);
assert_eq!(scan.files, 7);
assert_eq!(scan.lines, 12);
assert_eq!(scan.tests, 2);
assert_eq!(scan.commands, BTreeSet::from(["check:run".to_owned()]));
let kinds: Vec<_> = scan
.kinds
.iter()
.map(|kind| (kind.kind.as_str(), kind.files))
.collect();
assert_eq!(
kinds,
[
("Models", 2),
("Controllers", 1),
("Other", 1),
("Support", 1),
("Tests", 1),
("Views", 1)
]
);
}
#[test]
fn a_file_that_did_not_change_is_not_read_again() {
let dir = tempfile::tempdir().unwrap();
let file = dir.path().join("app/Models/Post.php");
write(&file, "<?php\nclass Post {}\n");
let kinds = Kinds::new(None, "app/");
let mut cache = ReadCache::default();
assert_eq!(scan(dir.path(), &kinds, &mut cache).lines, 2);
cache.files.get_mut(&file).unwrap().lines = 99;
assert_eq!(scan(dir.path(), &kinds, &mut cache).lines, 99);
write(&file, "<?php\nclass Post {}\n// more\n");
assert_eq!(scan(dir.path(), &kinds, &mut cache).lines, 3);
cache.clear();
assert_eq!(scan(dir.path(), &kinds, &mut cache).lines, 3);
write(
&dir.path().join("spec/Unit/PostTest.php"),
"<?php class PostTest { function testTitle() {} }",
);
assert_eq!(scan(dir.path(), &kinds, &mut cache).tests, 0);
let config = crate::php::read_config(
br#"<?php return ['paths' => ['generator' => ['test-unit' => 'spec/Unit']]];"#,
)
.unwrap();
let moved = Kinds::new(Some(&config), "app/");
assert_eq!(scan(dir.path(), &moved, &mut cache).tests, 1);
}
#[test]
fn counts_a_last_line_without_a_line_end() {
assert_eq!(count_lines(b""), 0);
assert_eq!(count_lines(b"a"), 1);
assert_eq!(count_lines(b"a\n"), 1);
assert_eq!(count_lines(b"a\nb"), 2);
assert_eq!(count_lines(b"a\r\nb\r\n"), 2);
}
#[test]
fn reads_the_fields_the_package_reads() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("module.json");
write(
&path,
r#"{"name": "Blog", "description": "Posts", "priority": 5}"#,
);
let (manifest, fields) = read_manifest(&path);
assert_eq!(manifest, Manifest::Valid);
assert_eq!(fields.name.as_deref(), Some("Blog"));
assert_eq!((fields.description.as_str(), fields.priority), ("Posts", 5));
write(&path, r#"{"alias": "blog"}"#);
assert_eq!(read_manifest(&path).0, Manifest::NoName);
write(&path, "{\n \"name\": \"Blog\",\n");
let (manifest, fields) = read_manifest(&path);
assert_eq!(
manifest,
Manifest::InvalidJson {
message: "EOF while parsing a value".into(),
line: 3
}
);
assert_eq!(fields.name, None);
}
#[test]
fn a_statuses_file_that_is_missing_or_not_an_object_has_no_entries() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("modules_statuses.json");
assert!(read_statuses(&path).is_empty());
for content in ["", "[]", "[true]", "not json", "null"] {
write(&path, content);
assert!(read_statuses(&path).is_empty(), "{content:?}");
}
write(&path, r#"{"Blog": true}"#);
assert_eq!(read_statuses(&path).len(), 1);
}
}