feat(xatu): scaffold source collection
This commit was merged in pull request #66.
This commit is contained in:
8
Cargo.lock
generated
8
Cargo.lock
generated
@@ -251,6 +251,14 @@ dependencies = [
|
|||||||
"windows-link",
|
"windows-link",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "xatu"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"clap",
|
||||||
|
"monorepo",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zmij"
|
name = "zmij"
|
||||||
version = "1.0.23"
|
version = "1.0.23"
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ members = [
|
|||||||
"src/ariadne",
|
"src/ariadne",
|
||||||
"src/machoke",
|
"src/machoke",
|
||||||
"src/monorepo",
|
"src/monorepo",
|
||||||
|
"src/xatu",
|
||||||
]
|
]
|
||||||
resolver = "3"
|
resolver = "3"
|
||||||
|
|
||||||
|
|||||||
18
src/xatu/BUILD
Normal file
18
src/xatu/BUILD
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library")
|
||||||
|
|
||||||
|
rust_library(
|
||||||
|
name = "xatu_lib",
|
||||||
|
crate_name = "xatu",
|
||||||
|
srcs = ["xatu.rs"],
|
||||||
|
)
|
||||||
|
|
||||||
|
rust_binary(
|
||||||
|
name = "xatu",
|
||||||
|
crate_name = "xatu_cli",
|
||||||
|
srcs = ["xatu_main.rs"],
|
||||||
|
deps = [
|
||||||
|
":xatu_lib",
|
||||||
|
"//src/monorepo:monorepo",
|
||||||
|
"//third_party/crates:clap",
|
||||||
|
],
|
||||||
|
)
|
||||||
15
src/xatu/Cargo.toml
Normal file
15
src/xatu/Cargo.toml
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
[package]
|
||||||
|
name = "xatu"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
path = "xatu.rs"
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "xatu"
|
||||||
|
path = "xatu_main.rs"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
clap = { workspace = true, features = ["derive"] }
|
||||||
|
monorepo = { workspace = true }
|
||||||
16
src/xatu/xatu.rs
Normal file
16
src/xatu/xatu.rs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
pub fn run(files: Vec<SourceFile>) -> Result<()> {
|
||||||
|
for file in files {
|
||||||
|
println!("{}", file.path.display());
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type Result<T> = std::result::Result<T, std::io::Error>;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct SourceFile {
|
||||||
|
pub path: PathBuf,
|
||||||
|
pub source: String,
|
||||||
|
}
|
||||||
80
src/xatu/xatu_main.rs
Normal file
80
src/xatu/xatu_main.rs
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
use clap::Parser;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
use xatu::SourceFile;
|
||||||
|
|
||||||
|
fn is_nix_file(path: &Path) -> bool {
|
||||||
|
path.extension().is_some_and(|extension| extension == "nix")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Add configurable exclusions.
|
||||||
|
fn is_excluded_dir(path: &Path) -> bool {
|
||||||
|
path.file_name()
|
||||||
|
.and_then(|name| name.to_str())
|
||||||
|
.is_some_and(|name| {
|
||||||
|
matches!(
|
||||||
|
name,
|
||||||
|
".git"
|
||||||
|
| "bazel-bin"
|
||||||
|
| "bazel-dev"
|
||||||
|
| "bazel-out"
|
||||||
|
| "bazel-testlogs"
|
||||||
|
| "target"
|
||||||
|
| "third_party"
|
||||||
|
) || name == "result"
|
||||||
|
|| name.starts_with("result-")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn collect_source_files(path: &Path) -> xatu::Result<Vec<SourceFile>> {
|
||||||
|
let metadata = std::fs::symlink_metadata(path)?;
|
||||||
|
|
||||||
|
if metadata.file_type().is_dir() {
|
||||||
|
if is_excluded_dir(path) {
|
||||||
|
return Ok(Vec::new());
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut files = Vec::new();
|
||||||
|
|
||||||
|
for entry in std::fs::read_dir(path)? {
|
||||||
|
let entry = entry?;
|
||||||
|
files.extend(collect_source_files(&entry.path())?);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(files);
|
||||||
|
}
|
||||||
|
|
||||||
|
if metadata.file_type().is_file() && is_nix_file(path) {
|
||||||
|
return Ok(vec![SourceFile {
|
||||||
|
path: path.to_path_buf(),
|
||||||
|
source: std::fs::read_to_string(path)?,
|
||||||
|
}]);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Vec::new())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Parser)]
|
||||||
|
struct Args {
|
||||||
|
#[arg(value_name = "PATH", default_value = ".")]
|
||||||
|
paths: Vec<PathBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> xatu::Result<()> {
|
||||||
|
let args = Args::parse();
|
||||||
|
|
||||||
|
let paths: Vec<PathBuf> = args
|
||||||
|
.paths
|
||||||
|
.into_iter()
|
||||||
|
.map(monorepo::workspace::resolve_workspace_path)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let files: Vec<SourceFile> = paths
|
||||||
|
.iter()
|
||||||
|
.map(|path| collect_source_files(path))
|
||||||
|
.collect::<xatu::Result<Vec<_>>>()?
|
||||||
|
.into_iter()
|
||||||
|
.flatten()
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
xatu::run(files)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user