forked from Orchid/orchid
Infra for debugging and testing
This commit is contained in:
48
xtask/src/check_api_refs.rs
Normal file
48
xtask/src/check_api_refs.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use std::env;
|
||||
use std::ffi::OsStr;
|
||||
use std::fs::{DirEntry, File};
|
||||
use std::io::{self, Read};
|
||||
use std::path::Path;
|
||||
|
||||
use crate::Args;
|
||||
|
||||
pub fn check_api_refs(_args: &Args) -> io::Result<()> {
|
||||
walk_wsp(&mut |_| Ok(true), &mut |file| {
|
||||
if file.path().extension() == Some(OsStr::new("rs")) && file.file_name() != "lib.rs" {
|
||||
let mut contents = String::new();
|
||||
File::open(file.path())?.read_to_string(&mut contents)?;
|
||||
for (l, line) in contents.lines().enumerate() {
|
||||
if !line.trim().starts_with("use") {
|
||||
continue;
|
||||
}
|
||||
let Some(c) = line.find("orchid_api") else { continue };
|
||||
if Some(c) == line.find("orchid_api_") {
|
||||
continue;
|
||||
}
|
||||
let dname = file.path().to_string_lossy().to_string();
|
||||
eprintln!("orchid_api imported in {dname} at {};{}", l + 1, c + 1)
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
fn walk_wsp(
|
||||
dir_filter: &mut impl FnMut(&DirEntry) -> io::Result<bool>,
|
||||
file_handler: &mut impl FnMut(DirEntry) -> io::Result<()>,
|
||||
) -> io::Result<()> {
|
||||
return recurse(&env::current_dir()?, dir_filter, file_handler);
|
||||
fn recurse(
|
||||
dir: &Path,
|
||||
dir_filter: &mut impl FnMut(&DirEntry) -> io::Result<bool>,
|
||||
file_handler: &mut impl FnMut(DirEntry) -> io::Result<()>,
|
||||
) -> io::Result<()> {
|
||||
for file in dir.read_dir()?.collect::<Result<Vec<_>, _>>()? {
|
||||
if file.metadata()?.is_dir() && dir_filter(&file)? {
|
||||
recurse(&file.path(), dir_filter, file_handler)?;
|
||||
}
|
||||
file_handler(file)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,13 @@
|
||||
use std::env;
|
||||
use std::ffi::OsStr;
|
||||
use std::fs::{DirEntry, File};
|
||||
use std::io::{self, Read};
|
||||
use std::path::Path;
|
||||
mod check_api_refs;
|
||||
mod orcx;
|
||||
|
||||
use std::io;
|
||||
use std::process::ExitCode;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
use check_api_refs::check_api_refs;
|
||||
use clap::{Parser, Subcommand};
|
||||
use orcx::orcx;
|
||||
|
||||
#[derive(Parser)]
|
||||
pub struct Args {
|
||||
@@ -19,51 +20,19 @@ pub struct Args {
|
||||
#[derive(Subcommand)]
|
||||
pub enum Commands {
|
||||
CheckApiRefs,
|
||||
Orcx {
|
||||
#[arg(trailing_var_arg = true, num_args = 1..)]
|
||||
subcommand: Vec<String>,
|
||||
},
|
||||
}
|
||||
|
||||
pub static EXIT_OK: AtomicBool = AtomicBool::new(true);
|
||||
|
||||
fn main() -> io::Result<ExitCode> {
|
||||
let args = Args::parse();
|
||||
match args.command {
|
||||
Commands::CheckApiRefs => walk_wsp(&mut |_| Ok(true), &mut |file| {
|
||||
if file.path().extension() == Some(OsStr::new("rs")) && file.file_name() != "lib.rs" {
|
||||
let mut contents = String::new();
|
||||
File::open(file.path())?.read_to_string(&mut contents)?;
|
||||
for (l, line) in contents.lines().enumerate() {
|
||||
if !line.trim().starts_with("use") {
|
||||
continue;
|
||||
}
|
||||
let Some(c) = line.find("orchid_api") else { continue };
|
||||
if Some(c) == line.find("orchid_api_") {
|
||||
continue;
|
||||
}
|
||||
let dname = file.path().to_string_lossy().to_string();
|
||||
eprintln!("orchid_api imported in {dname} at {};{}", l + 1, c + 1)
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})?,
|
||||
match &args.command {
|
||||
Commands::CheckApiRefs => check_api_refs(&args)?,
|
||||
Commands::Orcx { subcommand } => orcx(&args, subcommand)?,
|
||||
}
|
||||
Ok(if EXIT_OK.load(Ordering::Relaxed) { ExitCode::SUCCESS } else { ExitCode::FAILURE })
|
||||
}
|
||||
|
||||
fn walk_wsp(
|
||||
dir_filter: &mut impl FnMut(&DirEntry) -> io::Result<bool>,
|
||||
file_handler: &mut impl FnMut(DirEntry) -> io::Result<()>,
|
||||
) -> io::Result<()> {
|
||||
return recurse(&env::current_dir()?, dir_filter, file_handler);
|
||||
fn recurse(
|
||||
dir: &Path,
|
||||
dir_filter: &mut impl FnMut(&DirEntry) -> io::Result<bool>,
|
||||
file_handler: &mut impl FnMut(DirEntry) -> io::Result<()>,
|
||||
) -> io::Result<()> {
|
||||
for file in dir.read_dir()?.collect::<Result<Vec<_>, _>>()? {
|
||||
if file.metadata()?.is_dir() && dir_filter(&file)? {
|
||||
recurse(&file.path(), dir_filter, file_handler)?;
|
||||
}
|
||||
file_handler(file)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
19
xtask/src/orcx.rs
Normal file
19
xtask/src/orcx.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use std::io;
|
||||
use std::process::Command;
|
||||
use std::sync::atomic::Ordering;
|
||||
|
||||
use crate::{Args, EXIT_OK};
|
||||
|
||||
pub fn orcx(_args: &Args, subcommand: &[String]) -> io::Result<()> {
|
||||
eprintln!("running orcx {}", subcommand.join(" "));
|
||||
let status = Command::new("cargo").args(["build", "-p", "orchid-std"]).status()?;
|
||||
if status.success() {
|
||||
let status = Command::new("cargo")
|
||||
.args(["run", "-p", "orcx", "--"].into_iter().chain(subcommand.iter().map(|s| s.as_str())))
|
||||
.status()?;
|
||||
EXIT_OK.store(status.success(), Ordering::Relaxed);
|
||||
} else {
|
||||
EXIT_OK.store(false, Ordering::Relaxed);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user