From db5f48ae12ec65ea5e19d884320496e1e31acf42 Mon Sep 17 00:00:00 2001 From: Gary Miller Date: Fri, 14 Apr 2023 17:08:56 +1000 Subject: [PATCH] wip on work & pkg --- rust/compiler/src/cli/mod.rs | 16 +++++++++++++- rust/compiler/src/cli/workspace.rs | 35 ++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 rust/compiler/src/cli/workspace.rs diff --git a/rust/compiler/src/cli/mod.rs b/rust/compiler/src/cli/mod.rs index 839e5a6b..6d9c94a0 100644 --- a/rust/compiler/src/cli/mod.rs +++ b/rust/compiler/src/cli/mod.rs @@ -1,16 +1,21 @@ +use std::path::{Path, PathBuf}; + use anyhow::{Error, anyhow}; use clap::{Args, Parser}; -use std::{path::PathBuf, str::FromStr}; +use std::str::{FromStr}; +// use std::path{Path, PathBuf}; pub mod ast; pub mod rust; pub mod tsgen; pub mod verify; +pub mod workspace; pub fn run_cli() -> i32 { let cli = Cli::parse(); let r = match cli.command { + Command::Gen(opts) => workspace::workspace(&opts), Command::Verify(opts) => verify::verify(&opts), Command::Ast(opts) => ast::ast(&opts), Command::Rust(opts) => rust::rust(&opts), @@ -38,6 +43,8 @@ struct Cli { #[derive(Debug, Parser)] pub enum Command { + /// generate source based on Workspace & Packages files (adl.work.json & adl.pkg.json) + Gen(GenOpts), /// verify ADL Verify(VerifyOpts), /// generate the json AST for some ADL modules @@ -51,6 +58,13 @@ pub enum Command { WriteStdlib(DumpStdlibOpts), } +#[derive(Debug, Args)] +pub struct GenOpts { + /// The module where the code is generated, relative to crate root + #[arg(default_value_t={".".to_string()})] + pub dir: String, +} + #[derive(Debug, Args)] pub struct DumpStdlibOpts { /// writes generated code to the specified directory diff --git a/rust/compiler/src/cli/workspace.rs b/rust/compiler/src/cli/workspace.rs new file mode 100644 index 00000000..c5e4014e --- /dev/null +++ b/rust/compiler/src/cli/workspace.rs @@ -0,0 +1,35 @@ +use std::fs; +use std::path::{Path, PathBuf}; +use std::env; + +pub(crate) fn workspace(opts: &super::GenOpts) -> Result<(), anyhow::Error> { + let pkg_defs = collect_work_and_pkg(&opts.dir); + println!("{:?}", pkg_defs); + Ok(()) +} + +const ADL_PKG_FILES: &[(&str, PkgDef)] = &[("adl.pkg.json", PkgDef::Pkg), ("adl.work.json", PkgDef::Work)]; + +#[derive(Debug, Copy, Clone, PartialEq)] +enum PkgDef { + Pkg, + Work, +} + +fn collect_work_and_pkg(start_dir: &String) -> Vec<(PkgDef,PathBuf)> { + let mut res = vec![]; + let mut current_dir = PathBuf::from(start_dir); + + loop { + for f in ADL_PKG_FILES { + let file_path = current_dir.join(f.0); + if file_path.exists() { + res.push((f.1, current_dir.clone())); + } + } + if !current_dir.pop() { + break; + } + } + res +} \ No newline at end of file