From 70e84ddfe2cd29f7a77ebdac099cb2f987605af3 Mon Sep 17 00:00:00 2001 From: sagie gur ari Date: Sun, 16 Jul 2017 16:48:35 +0000 Subject: [PATCH] New --no-workspace cli arg --- Cargo.toml | 2 +- README.md | 37 +++++++++++++-- docs/_includes/content.md | 37 +++++++++++++-- docs/api/src/cargo_make/cli.rs.html | 16 +++++-- docs/api/src/cargo_make/runner.rs.html | 16 +++++-- src/cli.rs | 11 +++-- src/cli_test.rs | 11 +++-- src/runner.rs | 13 ++++-- src/runner_test.rs | 65 +++++++++++++++++++++++--- 9 files changed, 171 insertions(+), 37 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 80ef9524..39e4b006 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cargo-make" -version = "0.3.24" +version = "0.3.25" authors = ["Sagie Gur-Ari "] description = "Rust task runner and build tool." license = "Apache-2.0" diff --git a/README.md b/README.md index 9316987f..0df34070 100644 --- a/README.md +++ b/README.md @@ -509,6 +509,33 @@ The order of the members is defined by the member attribute in the workspace Car We can use this capability to run same functionality on all workspace member crates, for example if we want to format all crates, we can run in the workspace directory: ````cargo make format````. +In case you wish to run the tasks on the workspace level and not on the members, use the ````--no-workspace```` cli flag when running cargo make, for example: + +````sh +cargo make --no-workspace mytask +```` + +You can define a composite flow that runs both workspace level tasks and member level tasks using this flag.
+This is an example of a workspace level Makefile.toml which enables to run such a flow: + +````toml +[tasks.composite] +dependencies = ["member_flow", "workspace_flow"] + +[tasks.member_flow] +command = "cargo" +args = ["make", "member_task"] + +[tasks.workspace_flow] +#run some workspace level command or flow +```` + +You can start this composite flow as follows: + +````sh +cargo make --no-workspace composite +```` + ### Init and End tasks Every task or flow that is executed by the cargo-make has additional 2 tasks.
@@ -544,10 +571,11 @@ USAGE: cargo make [FLAGS] [OPTIONS] [TASK] FLAGS: - -h, --help Prints help information - --print-steps Only prints the steps of the build in the order they will be invoked but without invoking them - -v, --verbose Sets the log level to verbose (shorthand for --loglevel verbose) - -V, --version Prints version information + -h, --help Prints help information + --no-workspace Disable workspace support (tasks are triggered on workspace and not on members) + --print-steps Only prints the steps of the build in the order they will be invoked but without invoking them + -v, --verbose Sets the log level to verbose (shorthand for --loglevel verbose) + -V, --version Prints version information OPTIONS: --cwd Will set the current working directory. The search for the makefile will be from this directory if defined. @@ -728,6 +756,7 @@ See [contributing guide](.github/CONTRIBUTING.md) | Date | Version | Description | | ----------- | ------- | ----------- | +| 2017-07-16 | v0.3.25 | New --no-workspace cli arg | | 2017-07-15 | v0.3.24 | Workspace support | | 2017-07-14 | v0.3.23 | Added codecov task in default toml | | 2017-07-14 | v0.3.20 | Added coverage task in default toml | diff --git a/docs/_includes/content.md b/docs/_includes/content.md index 0366b754..26ab6a72 100644 --- a/docs/_includes/content.md +++ b/docs/_includes/content.md @@ -480,6 +480,33 @@ The order of the members is defined by the member attribute in the workspace Car We can use this capability to run same functionality on all workspace member crates, for example if we want to format all crates, we can run in the workspace directory: ````cargo make format````. +In case you wish to run the tasks on the workspace level and not on the members, use the ````--no-workspace```` cli flag when running cargo make, for example: + +````sh +cargo make --no-workspace mytask +```` + +You can define a composite flow that runs both workspace level tasks and member level tasks using this flag.
+This is an example of a workspace level Makefile.toml which enables to run such a flow: + +````toml +[tasks.composite] +dependencies = ["member_flow", "workspace_flow"] + +[tasks.member_flow] +command = "cargo" +args = ["make", "member_task"] + +[tasks.workspace_flow] +#run some workspace level command or flow +```` + +You can start this composite flow as follows: + +````sh +cargo make --no-workspace composite +```` + ### Init and End tasks Every task or flow that is executed by the cargo-make has additional 2 tasks.
@@ -515,10 +542,11 @@ USAGE: cargo make [FLAGS] [OPTIONS] [TASK] FLAGS: - -h, --help Prints help information - --print-steps Only prints the steps of the build in the order they will be invoked but without invoking them - -v, --verbose Sets the log level to verbose (shorthand for --loglevel verbose) - -V, --version Prints version information + -h, --help Prints help information + --no-workspace Disable workspace support (tasks are triggered on workspace and not on members) + --print-steps Only prints the steps of the build in the order they will be invoked but without invoking them + -v, --verbose Sets the log level to verbose (shorthand for --loglevel verbose) + -V, --version Prints version information OPTIONS: --cwd Will set the current working directory. The search for the makefile will be from this directory if defined. @@ -699,6 +727,7 @@ See [contributing guide](https://github.com/sagiegurari/cargo-make/blob/master/. | Date | Version | Description | | ----------- | ------- | ----------- | +| 2017-07-16 | v0.3.25 | New --no-workspace cli arg | | 2017-07-15 | v0.3.24 | Workspace support | | 2017-07-14 | v0.3.23 | Added codecov task in default toml | | 2017-07-14 | v0.3.20 | Added coverage task in default toml | diff --git a/docs/api/src/cargo_make/cli.rs.html b/docs/api/src/cargo_make/cli.rs.html index 8da38502..1ded2728 100644 --- a/docs/api/src/cargo_make/cli.rs.html +++ b/docs/api/src/cargo_make/cli.rs.html @@ -164,6 +164,11 @@ 119 120 121 +122 +123 +124 +125 +126
 //! # cli
 //!
@@ -193,6 +198,7 @@
     task: &str,
     log_level: &str,
     cwd: Option<&str>,
+    disable_workspace: bool,
     print_only: bool,
 ) {
     let logger = log::create(log_level);
@@ -210,9 +216,9 @@
     environment::setup_env(&logger, &config, &task);
 
     if print_only {
-        runner::print(&logger, &config, &task);
+        runner::print(&logger, &config, &task, disable_workspace);
     } else {
-        runner::run(&logger, &config, &task);
+        runner::run(&logger, &config, &task, disable_workspace);
     }
 }
 
@@ -232,8 +238,9 @@
             };
 
             let print_only = cmd_matches.is_present("print-steps");
+            let disable_workspace = cmd_matches.is_present("no-workspace");
 
-            run(build_file, task, log_level, cwd, print_only);
+            run(build_file, task, log_level, cwd, disable_workspace, print_only);
         }
         None => panic!("cargo-{} not invoked via cargo command.", NAME),
     }
@@ -263,6 +270,9 @@
             .arg(Arg::with_name("cwd").long("--cwd").value_name("DIRECTORY").help(
                 "Will set the current working directory. The search for the makefile will be from this directory if defined."
             ))
+            .arg(Arg::with_name("no-workspace").long("--no-workspace").help(
+                "Disable workspace support (tasks are triggered on workspace and not on members)"
+            ))
             .arg(
                 Arg::from_usage("-l, --loglevel=[LOG LEVEL] 'The log level'")
                     .possible_values(&["verbose", "info", "error"])
diff --git a/docs/api/src/cargo_make/runner.rs.html b/docs/api/src/cargo_make/runner.rs.html
index 2d9dcc03..f4e1d795 100644
--- a/docs/api/src/cargo_make/runner.rs.html
+++ b/docs/api/src/cargo_make/runner.rs.html
@@ -280,6 +280,9 @@
 235
 236
 237
+238
+239
+240
 
 //! # runner
 //!
@@ -420,6 +423,7 @@
     logger: &Logger,
     config: &Config,
     task: &str,
+    disable_workspace: bool,
 ) -> ExecutionPlan {
     let mut task_names = HashSet::new();
     let mut steps = Vec::new();
@@ -445,12 +449,12 @@
     // load crate info and look for workspace info
     let crate_info = CrateInfo::load(&logger);
 
-    if crate_info.workspace.is_some() {
+    if disable_workspace || crate_info.workspace.is_none() {
+        create_execution_plan_for_step(&logger, &config, &task, &mut steps, &mut task_names, true);
+    } else {
         let workspace_task = create_workspace_task(crate_info, task);
 
         steps.push(Step { name: "workspace".to_string(), config: workspace_task });
-    } else {
-        create_execution_plan_for_step(&logger, &config, &task, &mut steps, &mut task_names, true);
     }
 
     // always add end task even if already executed due to some depedency
@@ -484,10 +488,11 @@
     logger: &Logger,
     config: &Config,
     task: &str,
+    disable_workspace: bool,
 ) {
     let start_time = SystemTime::now();
 
-    let execution_plan = create_execution_plan(&logger, &config, &task);
+    let execution_plan = create_execution_plan(&logger, &config, &task, disable_workspace);
     logger.verbose("Created execution plan: ", &[], Some(&execution_plan));
 
     run_task_flow(logger, &execution_plan);
@@ -512,8 +517,9 @@
     logger: &Logger,
     config: &Config,
     task: &str,
+    disable_workspace: bool,
 ) {
-    let execution_plan = create_execution_plan(&logger, &config, &task);
+    let execution_plan = create_execution_plan(&logger, &config, &task, disable_workspace);
     logger.verbose("Created execution plan: ", &[], Some(&execution_plan));
 
     println!("{:#?}", &execution_plan);
diff --git a/src/cli.rs b/src/cli.rs
index 1dc6cacc..88e34231 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -26,6 +26,7 @@ fn run(
     task: &str,
     log_level: &str,
     cwd: Option<&str>,
+    disable_workspace: bool,
     print_only: bool,
 ) {
     let logger = log::create(log_level);
@@ -43,9 +44,9 @@ fn run(
     environment::setup_env(&logger, &config, &task);
 
     if print_only {
-        runner::print(&logger, &config, &task);
+        runner::print(&logger, &config, &task, disable_workspace);
     } else {
-        runner::run(&logger, &config, &task);
+        runner::run(&logger, &config, &task, disable_workspace);
     }
 }
 
@@ -65,8 +66,9 @@ fn run_for_args(matches: ArgMatches) {
             };
 
             let print_only = cmd_matches.is_present("print-steps");
+            let disable_workspace = cmd_matches.is_present("no-workspace");
 
-            run(build_file, task, log_level, cwd, print_only);
+            run(build_file, task, log_level, cwd, disable_workspace, print_only);
         }
         None => panic!("cargo-{} not invoked via cargo command.", NAME),
     }
@@ -96,6 +98,9 @@ fn create_cli<'a, 'b>() -> App<'a, 'b> {
             .arg(Arg::with_name("cwd").long("--cwd").value_name("DIRECTORY").help(
                 "Will set the current working directory. The search for the makefile will be from this directory if defined."
             ))
+            .arg(Arg::with_name("no-workspace").long("--no-workspace").help(
+                "Disable workspace support (tasks are triggered on workspace and not on members)"
+            ))
             .arg(
                 Arg::from_usage("-l, --loglevel=[LOG LEVEL] 'The log level'")
                     .possible_values(&["verbose", "info", "error"])
diff --git a/src/cli_test.rs b/src/cli_test.rs
index 4ad6c81c..c938c112 100644
--- a/src/cli_test.rs
+++ b/src/cli_test.rs
@@ -4,17 +4,17 @@ use std::path::Path;
 
 #[test]
 fn run_empty_task() {
-    run("bad.toml", "empty", "error", None, false);
+    run("bad.toml", "empty", "error", None, false, false);
 }
 
 #[test]
 fn print_empty_task() {
-    run("bad.toml", "empty", "error", None, true);
+    run("bad.toml", "empty", "error", None, false, true);
 }
 
 #[test]
 fn run_file_and_task() {
-    run("./examples/dependencies.toml", "A", "error", None, false);
+    run("./examples/dependencies.toml", "A", "error", None, false, false);
 }
 
 #[test]
@@ -22,7 +22,7 @@ fn run_cwd_with_file() {
     let directory = Path::new("./examples");
     assert!(env::set_current_dir(&directory).is_ok());
 
-    run("./examples/dependencies.toml", "A", "error", Some(".."), false);
+    run("./examples/dependencies.toml", "A", "error", Some(".."), false, false);
 }
 
 #[test]
@@ -31,7 +31,7 @@ fn run_cwd_task_not_found() {
     let directory = Path::new("./examples");
     assert!(env::set_current_dir(&directory).is_ok());
 
-    run("./dependencies.toml", "A", "error", Some(".."), false);
+    run("./dependencies.toml", "A", "error", Some(".."), false, false);
 }
 
 #[test]
@@ -81,6 +81,7 @@ fn run_for_args_print_only() {
         "A",
         "-l",
         "error",
+        "--no-workspace",
         "--print-steps",
     ]);
 
diff --git a/src/runner.rs b/src/runner.rs
index 285eb92f..56d86c4b 100644
--- a/src/runner.rs
+++ b/src/runner.rs
@@ -137,6 +137,7 @@ fn create_execution_plan(
     logger: &Logger,
     config: &Config,
     task: &str,
+    disable_workspace: bool,
 ) -> ExecutionPlan {
     let mut task_names = HashSet::new();
     let mut steps = Vec::new();
@@ -162,12 +163,12 @@ fn create_execution_plan(
     // load crate info and look for workspace info
     let crate_info = CrateInfo::load(&logger);
 
-    if crate_info.workspace.is_some() {
+    if disable_workspace || crate_info.workspace.is_none() {
+        create_execution_plan_for_step(&logger, &config, &task, &mut steps, &mut task_names, true);
+    } else {
         let workspace_task = create_workspace_task(crate_info, task);
 
         steps.push(Step { name: "workspace".to_string(), config: workspace_task });
-    } else {
-        create_execution_plan_for_step(&logger, &config, &task, &mut steps, &mut task_names, true);
     }
 
     // always add end task even if already executed due to some depedency
@@ -201,10 +202,11 @@ pub fn run(
     logger: &Logger,
     config: &Config,
     task: &str,
+    disable_workspace: bool,
 ) {
     let start_time = SystemTime::now();
 
-    let execution_plan = create_execution_plan(&logger, &config, &task);
+    let execution_plan = create_execution_plan(&logger, &config, &task, disable_workspace);
     logger.verbose("Created execution plan: ", &[], Some(&execution_plan));
 
     run_task_flow(logger, &execution_plan);
@@ -229,8 +231,9 @@ pub fn print(
     logger: &Logger,
     config: &Config,
     task: &str,
+    disable_workspace: bool,
 ) {
-    let execution_plan = create_execution_plan(&logger, &config, &task);
+    let execution_plan = create_execution_plan(&logger, &config, &task, disable_workspace);
     logger.verbose("Created execution plan: ", &[], Some(&execution_plan));
 
     println!("{:#?}", &execution_plan);
diff --git a/src/runner_test.rs b/src/runner_test.rs
index 4637977e..246e0377 100644
--- a/src/runner_test.rs
+++ b/src/runner_test.rs
@@ -1,6 +1,7 @@
 use super::*;
 use log;
 use std::collections::HashMap;
+use std::env;
 use types::{ConfigSection, CrateInfo, PlatformOverrideTask, Task, Workspace};
 
 #[test]
@@ -66,28 +67,45 @@ fn get_task_name_platform_alias() {
 #[test]
 fn create_execution_plan_single() {
     let logger = log::create("error");
-    let mut config = Config { config: ConfigSection::new(), env: HashMap::new(), tasks: HashMap::new() };
+    let mut config_section = ConfigSection::new();
+    config_section.init_task = Some("init".to_string());
+    config_section.end_task = Some("end".to_string());
+    let mut config = Config { config: config_section, env: HashMap::new(), tasks: HashMap::new() };
+
+    config.tasks.insert("init".to_string(), Task::new());
+    config.tasks.insert("end".to_string(), Task::new());
 
     let task = Task::new();
 
     config.tasks.insert("test".to_string(), task);
 
-    let execution_plan = create_execution_plan(&logger, &config, "test");
-    assert_eq!(execution_plan.steps.len(), 1);
+    let execution_plan = create_execution_plan(&logger, &config, "test", false);
+    assert_eq!(execution_plan.steps.len(), 3);
+    assert_eq!(execution_plan.steps[0].name, "init");
+    assert_eq!(execution_plan.steps[1].name, "test");
+    assert_eq!(execution_plan.steps[2].name, "end");
 }
 
 #[test]
 fn create_execution_plan_single_disabled() {
     let logger = log::create("error");
-    let mut config = Config { config: ConfigSection::new(), env: HashMap::new(), tasks: HashMap::new() };
+    let mut config_section = ConfigSection::new();
+    config_section.init_task = Some("init".to_string());
+    config_section.end_task = Some("end".to_string());
+    let mut config = Config { config: config_section, env: HashMap::new(), tasks: HashMap::new() };
+
+    config.tasks.insert("init".to_string(), Task::new());
+    config.tasks.insert("end".to_string(), Task::new());
 
     let mut task = Task::new();
     task.disabled = Some(true);
 
     config.tasks.insert("test".to_string(), task);
 
-    let execution_plan = create_execution_plan(&logger, &config, "test");
-    assert_eq!(execution_plan.steps.len(), 0);
+    let execution_plan = create_execution_plan(&logger, &config, "test", false);
+    assert_eq!(execution_plan.steps.len(), 2);
+    assert_eq!(execution_plan.steps[0].name, "init");
+    assert_eq!(execution_plan.steps[1].name, "end");
 }
 
 #[test]
@@ -135,10 +153,43 @@ fn create_execution_plan_platform_disabled() {
 
     config.tasks.insert("test".to_string(), task);
 
-    let execution_plan = create_execution_plan(&logger, &config, "test");
+    let execution_plan = create_execution_plan(&logger, &config, "test", false);
     assert_eq!(execution_plan.steps.len(), 0);
 }
 
+#[test]
+fn create_execution_plan_workspace() {
+    let logger = log::create("error");
+    let mut config = Config { config: ConfigSection::new(), env: HashMap::new(), tasks: HashMap::new() };
+
+    let task = Task::new();
+
+    config.tasks.insert("test".to_string(), task);
+
+    env::set_current_dir("./examples/workspace").unwrap();
+    let execution_plan = create_execution_plan(&logger, &config, "test", false);
+    env::set_current_dir("../../").unwrap();
+    assert_eq!(execution_plan.steps.len(), 1);
+    assert_eq!(execution_plan.steps[0].name, "workspace");
+}
+
+
+#[test]
+fn create_execution_plan_noworkspace() {
+    let logger = log::create("error");
+    let mut config = Config { config: ConfigSection::new(), env: HashMap::new(), tasks: HashMap::new() };
+
+    let task = Task::new();
+
+    config.tasks.insert("test".to_string(), task);
+
+    env::set_current_dir("./examples/workspace").unwrap();
+    let execution_plan = create_execution_plan(&logger, &config, "test", true);
+    env::set_current_dir("../../").unwrap();
+    assert_eq!(execution_plan.steps.len(), 1);
+    assert_eq!(execution_plan.steps[0].name, "test");
+}
+
 #[test]
 fn create_workspace_task_no_members() {
     let mut crate_info = CrateInfo::new();