From f53e38d790ec67d99ec8204af97273a53b73a9dc Mon Sep 17 00:00:00 2001 From: Jan Allersma Date: Dec 19 2018 14:54:07 +0000 Subject: Resolve dep-tree by building project recursively. --- diff --git a/src/backend/build.rs b/src/backend/build.rs index 5c8a4db..c2d4f6c 100644 --- a/src/backend/build.rs +++ b/src/backend/build.rs @@ -25,9 +25,37 @@ pub fn build(config_file: PathBuf) -> Result { Ok(String::from("Build succeeded!")) } +pub fn build_rec(config_file: PathBuf) -> Result { + let dep_tree = super::deptree::print(&super::system::OS::current(), config_file); + + // Inconventient, should be changed later + match &dep_tree { + Ok(result) => println!("{}", result), + Err(e) => return Err(e.to_string()) + } + + let dep_tree = dep_tree.unwrap(); + + for node in &dep_tree.depends_on { + println!("Building module '{}'..", &node.dep_name); + match build_rec(node.path.clone()) { + Ok(_) => {}, + Err(e) => return Err(e) + } + } + + println!("Building current project '{}'..", &dep_tree.dep_name); + match build(dep_tree.path) { + Ok(result) => println!("{}", result), + Err(e) => return Err(e) + } + + Ok(String::from("Project built!")) +} + #[cfg(target_os="linux")] fn build_module(config: serde_json::Value) -> Result { - println!("Building project.."); + println!("Building module.."); let build_cmd = &config["build"]["linux"]; @@ -40,7 +68,7 @@ fn build_module(config: serde_json::Value) -> Result { #[cfg(target_os="macos")] fn build_module(config: serde_json::Value) -> Result { - println!("Building project.."); + println!("Building module.."); let build_cmd = &config["build"]["os-x"]; @@ -53,7 +81,7 @@ fn build_module(config: serde_json::Value) -> Result { #[cfg(target_os="windows")] fn build_module(config: serde_json::Value) -> Result { - println!("Building project.."); + println!("Building module.."); let build_cmd = &config["build"]["windows"]; diff --git a/src/backend/dep.rs b/src/backend/dep.rs index e3662f2..7643de3 100644 --- a/src/backend/dep.rs +++ b/src/backend/dep.rs @@ -6,8 +6,6 @@ use super::system::OS; pub fn json(config: String) -> Result { let config_json: serde_json::Value; - println!("Reading module configuration.."); - match serde_json::from_str(&config) { Ok(json) => config_json = json, Err(e) => { diff --git a/src/backend/deptree.rs b/src/backend/deptree.rs index d3c6076..d38bc6a 100644 --- a/src/backend/deptree.rs +++ b/src/backend/deptree.rs @@ -6,9 +6,9 @@ use std::path::PathBuf; use std::fmt; pub struct Node { - dep_name: String, - path: PathBuf, - depends_on: Vec + pub dep_name: String, + pub path: PathBuf, + pub depends_on: Vec } impl fmt::Display for Node { @@ -31,8 +31,6 @@ impl fmt::Display for Node { } } - dependency = self.clone(); - try!(write!(f, "{}", string)); Ok(()) } @@ -71,7 +69,7 @@ pub fn print(os: &OS, path: PathBuf) -> Result { let root = Node { dep_name: String::from(path.file_name().unwrap().to_str().unwrap()), - path: super::filesystem::get_module_root().unwrap(), + path: path, depends_on: nodes }; diff --git a/src/backend/project.rs b/src/backend/project.rs index 9e611da..5333f3f 100644 --- a/src/backend/project.rs +++ b/src/backend/project.rs @@ -16,10 +16,20 @@ pub fn init(args: &mut env::Args) { } } -pub fn build() -> Result { - match super::filesystem::get_project_root() { - Some(dir) => super::build::build(dir), - None => Err(String::from("not in a project (sub)directory.")) +pub fn build(args: &mut env::Args) -> Result { + match args.next() { + Some(ref module) if module.as_str() == "--module" => { + match super::filesystem::get_module_root() { + Some(dir) => super::build::build(dir), + None => Err(String::from("not in a project (sub)directory.")) + } + }, + Some(_) | None => { + match super::filesystem::get_project_root() { + Some(dir) => super::build::build_rec(dir), + None => Err(String::from("not in a project (sub)directory.")) + } + } } } @@ -76,7 +86,7 @@ pub fn exe(args: &mut env::Args) -> Result { pub fn run(args: &mut env::Args) -> Result { println!("Building project.."); - match build() { + match build(args) { Ok(output) => println!("{}", output), Err(e) => return Err(e) } @@ -109,7 +119,7 @@ pub fn help() { println!(""); println!("init [DIRECTORY]\t\t Initialize new project in specified directory. Defaults to current directory."); - println!("build\t\t\t\t Build current project."); + println!("build [--module]\t\t Build current project if module flag is not specified, otherwise only the module will be built."); println!("run [ARGUMENTS]\t\t\t Build and run current project with ARGUMENTS to run project with."); println!("exe [ARGUMENTS]\t\t\t Run current project with ARGUMENTS. The project won't be built."); println!("add NAME COMMAND [ARGUMENTS]\t Add dependency with NAME to module and is built through COMMAND with ARGUMENTS."); diff --git a/src/main.rs b/src/main.rs index 6dcd39b..b4b1b95 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,7 +27,7 @@ fn parse() { backend::project::init(&mut argv); } else if &argument == "build" { - match backend::project::build() { + match backend::project::build(&mut argv) { Ok(result) => println!("{}", result), Err(e) => println!("Build failed: {}", e) }