Skip to content

Commit

Permalink
wip on work & pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
millergarym committed Apr 14, 2023
1 parent 512ec6b commit db5f48a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
16 changes: 15 additions & 1 deletion rust/compiler/src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -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),
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
35 changes: 35 additions & 0 deletions rust/compiler/src/cli/workspace.rs
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit db5f48a

Please sign in to comment.