Skip to content

Commit

Permalink
Merge pull request #1130 from sagiegurari/0.37.15
Browse files Browse the repository at this point in the history
0.37.15
  • Loading branch information
sagiegurari authored Jul 29, 2024
2 parents 3eed7ee + edd1c1c commit 92946e4
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 32 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## CHANGELOG

### v0.37.15

* Enhancement: Support env expansion for script runner #1125
* Enhancement: Make LoggerOptions public + add name field #1124 (thanks @SamuelMarks)

### v0.37.14 (2024-07-17)

* Fix: fix the --skip-init-end-tasks argument #1108 (thanks @06393993)
Expand Down
13 changes: 9 additions & 4 deletions src/lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,20 @@ pub(crate) static DEFAULT_LOG_LEVEL: &str = "info";
pub(crate) static DEFAULT_TASK_NAME: &str = "default";
pub(crate) static DEFAULT_OUTPUT_FORMAT: &str = "default";

pub fn run(cli_args: &CliArgs, global_config: &GlobalConfig) -> Result<(), CargoMakeError> {
pub fn run(
cli_args: &CliArgs,
global_config: &GlobalConfig,
logger_options: Option<LoggerOptions>,
) -> Result<(), CargoMakeError> {
let start_time = SystemTime::now();

recursion_level::increment();

logger::init(&LoggerOptions {
logger::init(&logger_options.unwrap_or(LoggerOptions {
name: String::from(env!("CARGO_PKG_NAME")),
level: cli_args.log_level.clone(),
color: !cli_args.disable_color,
});
}));

if recursion_level::is_top() {
info!("{} {}", &cli_args.command, &VERSION);
Expand Down Expand Up @@ -173,6 +178,6 @@ pub fn run_cli(command_name: String, sub_command: bool) -> Result<CliArgs, Cargo

let cli_args = cli_parser::parse(&global_config, &command_name, sub_command)?;

run(&cli_args, &global_config)?;
run(&cli_args, &global_config, None)?;
Ok(cli_args)
}
36 changes: 23 additions & 13 deletions src/lib/cli_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ fn run_makefile_not_found() {
hide_uninteresting: false,
},
&global_config,
None,
)
.unwrap();
}
Expand Down Expand Up @@ -77,6 +78,7 @@ fn run_empty_task() {
hide_uninteresting: false,
},
&global_config,
None,
)
.unwrap();
}
Expand Down Expand Up @@ -115,6 +117,7 @@ fn print_empty_task() {
hide_uninteresting: false,
},
&global_config,
None,
)
.unwrap();
}
Expand Down Expand Up @@ -153,6 +156,7 @@ fn list_empty_task() {
hide_uninteresting: false,
},
&global_config,
None,
)
.unwrap();
}
Expand Down Expand Up @@ -191,6 +195,7 @@ fn run_file_and_task() {
hide_uninteresting: false,
},
&global_config,
None,
)
.unwrap();
}
Expand Down Expand Up @@ -232,6 +237,7 @@ fn run_cwd_with_file() {
hide_uninteresting: false,
},
&global_config,
None,
)
.unwrap();
}
Expand Down Expand Up @@ -271,6 +277,7 @@ fn run_file_not_go_to_project_root() {
hide_uninteresting: false,
},
&global_config,
None,
)
.unwrap();
}
Expand Down Expand Up @@ -310,6 +317,7 @@ fn run_cwd_go_to_project_root_current_dir() {
hide_uninteresting: false,
},
&global_config,
None,
)
.unwrap();
}
Expand Down Expand Up @@ -352,6 +360,7 @@ fn run_cwd_go_to_project_root_child_dir() {
hide_uninteresting: false,
},
&global_config,
None,
)
.unwrap();
}
Expand Down Expand Up @@ -394,6 +403,7 @@ fn run_cwd_task_not_found() {
hide_uninteresting: false,
},
&global_config,
None,
)
.unwrap();
}
Expand All @@ -414,7 +424,7 @@ fn run_bad_subcommand() {
"ParserError { error: InvalidCommandLine(\"Command does not match spec, command line: [\\\"bad\\\"]\") }"
);

// run(&cli_args.unwrap(), &global_config).unwrap();
// run(&cli_args.unwrap(), &global_config, None).unwrap();
}

#[test]
Expand All @@ -441,7 +451,7 @@ fn run_valid() {
)
.unwrap();

assert_eq!(run(&cli_args, &global_config).unwrap(), ());
assert_eq!(run(&cli_args, &global_config, None).unwrap(), ());
}

#[test]
Expand All @@ -460,7 +470,7 @@ fn run_with_global_config() {
)
.unwrap();

run(&cli_args, &global_config).unwrap();
run(&cli_args, &global_config, None).unwrap();
}

#[test]
Expand All @@ -486,7 +496,7 @@ fn run_log_level_override() {
)
.unwrap();

run(&cli_args, &global_config).unwrap();
run(&cli_args, &global_config, None).unwrap();
}

#[test]
Expand Down Expand Up @@ -519,7 +529,7 @@ fn run_set_env_values() {
envmnt::set("ENV2_TEST", "EMPTY");
envmnt::set("ENV3_TEST", "EMPTY");

run(&cli_args, &global_config).unwrap();
run(&cli_args, &global_config, None).unwrap();

assert_eq!(envmnt::get_or_panic("ENV1_TEST"), "TEST1");
assert_eq!(envmnt::get_or_panic("ENV2_TEST"), "TEST2a=TEST2b");
Expand Down Expand Up @@ -550,7 +560,7 @@ fn run_set_env_via_file() {
envmnt::set("ENV2_TEST", "EMPTY");
envmnt::set("ENV3_TEST", "EMPTY");

run(&cli_args.unwrap(), &global_config).unwrap();
run(&cli_args.unwrap(), &global_config, None).unwrap();

assert_eq!(envmnt::get_or_panic("ENV1_TEST"), "TEST1");
assert_eq!(envmnt::get_or_panic("ENV2_TEST"), "TEST2");
Expand Down Expand Up @@ -590,7 +600,7 @@ fn run_set_env_both() {
envmnt::set("ENV5_TEST", "EMPTY");
envmnt::set("ENV6_TEST", "EMPTY");

run(&cli_args.unwrap(), &global_config).unwrap();
run(&cli_args.unwrap(), &global_config, None).unwrap();

assert_eq!(envmnt::get_or_panic("ENV1_TEST"), "TEST1");
assert_eq!(envmnt::get_or_panic("ENV2_TEST"), "TEST2");
Expand Down Expand Up @@ -627,7 +637,7 @@ fn run_print_only() {
create_cli(&global_config, CliSpec::new(), true),
);

run(&cli_args.unwrap(), &global_config).unwrap();
run(&cli_args.unwrap(), &global_config, None).unwrap();
}

#[test]
Expand All @@ -653,7 +663,7 @@ fn run_diff_steps() {
create_cli(&global_config, CliSpec::new(), true),
);

run(&cli_args.unwrap(), &global_config).unwrap();
run(&cli_args.unwrap(), &global_config, None).unwrap();
}

#[test]
Expand All @@ -674,7 +684,7 @@ fn run_protected_flow_example() {
create_cli(&global_config, CliSpec::new(), true),
);

run(&cli_args.unwrap(), &global_config).unwrap();
run(&cli_args.unwrap(), &global_config, None).unwrap();
}

#[test]
Expand All @@ -696,7 +706,7 @@ fn run_no_task_args() {

envmnt::set("CARGO_MAKE_TASK_ARGS", "EMPTY");

run(&cli_args.unwrap(), &global_config).unwrap();
run(&cli_args.unwrap(), &global_config, None).unwrap();

assert_eq!(envmnt::get_or_panic("CARGO_MAKE_TASK_ARGS"), "");
}
Expand All @@ -723,7 +733,7 @@ fn run_set_task_args() {

envmnt::set("CARGO_MAKE_TASK_ARGS", "EMPTY");

run(&cli_args.unwrap(), &global_config).unwrap();
run(&cli_args.unwrap(), &global_config, None).unwrap();

assert_eq!(
envmnt::get_or_panic("CARGO_MAKE_TASK_ARGS"),
Expand All @@ -748,7 +758,7 @@ fn run_set_task_var_args() {

envmnt::set("CARGO_MAKE_TASK_ARGS", "EMPTY");

run(&cli_args, &global_config).unwrap();
run(&cli_args, &global_config, None).unwrap();

assert_eq!(
envmnt::get_or_panic("CARGO_MAKE_TASK_ARGS"),
Expand Down
21 changes: 10 additions & 11 deletions src/lib/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ pub(crate) enum LogLevel {
}

/// The logger options used to initialize the logger
pub(crate) struct LoggerOptions {
pub struct LoggerOptions {
/// Name to prefix each log message: "[name] %s"
pub name: String,
/// The logger level name (verbose, info, error, off)
pub(crate) level: String,
pub level: String,
/// True to printout colorful output
pub(crate) color: bool,
pub color: bool,
}

pub(crate) fn get_level(level_name: &str) -> LogLevel {
Expand All @@ -42,17 +44,16 @@ pub(crate) fn get_level(level_name: &str) -> LogLevel {

/// Returns the current logger level name
pub(crate) fn get_log_level() -> String {
let level = if envmnt::is_equal("CARGO_MAKE_LOG_LEVEL", "off") {
if envmnt::is_equal("CARGO_MAKE_LOG_LEVEL", "off") {
"off"
} else if log_enabled!(Level::Debug) {
"verbose"
} else if log_enabled!(Level::Info) {
"info"
} else {
"error"
};

level.to_string()
}
.to_string()
}

fn get_name_for_filter(filter: &LevelFilter) -> String {
Expand Down Expand Up @@ -139,10 +140,10 @@ pub(crate) fn init(options: &LoggerOptions) {
format!("[{}]", recursion_lvl)
};

let name_fmt = get_formatted_name(&options.name, color);

let result = fern::Dispatch::new()
.format(move |out, message, record| {
let name = env!("CARGO_PKG_NAME");

let record_level = record.level();

if cfg!(test) {
Expand All @@ -151,8 +152,6 @@ pub(crate) fn init(options: &LoggerOptions) {
}
}

let name_fmt = get_formatted_name(&name, color);

let record_level_fmt = get_formatted_log_level(&record_level, color);

out.finish(format_args!(
Expand Down
2 changes: 2 additions & 0 deletions src/lib/logger_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ fn get_formatted_log_level_error_no_color() {
#[should_panic]
fn create_error() {
init(&LoggerOptions {
name: String::from(env!("CARGO_PKG_NAME")),
level: "error".to_string(),
color: false,
});
Expand All @@ -183,6 +184,7 @@ fn update_disable_color_env_var() {
envmnt::remove("CARGO_MAKE_DISABLE_COLOR");

init(&LoggerOptions {
name: String::from(env!("CARGO_PKG_NAME")),
level: "info".to_string(),
color: false,
});
Expand Down
2 changes: 1 addition & 1 deletion src/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ mod functions;
mod installer;
mod io;
mod legacy;
mod logger;
pub mod logger;
mod plugin;
mod profile;
mod proxy_task;
Expand Down
15 changes: 12 additions & 3 deletions src/lib/scriptengine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,21 @@ fn invoke_script(
flow_state: Option<Rc<RefCell<FlowState>>>,
cli_arguments: &Vec<String>,
) -> Result<bool, CargoMakeError> {
let engine_type = get_engine_type(script, &script_runner, &script_extension)?;
let expanded_script_runner = match script_runner {
Some(ref value) => Some(environment::expand_value(value)),
None => None,
};
let engine_type = get_engine_type(script, &expanded_script_runner, &script_extension)?;

match engine_type {
EngineType::OS => {
let script_text = get_script_text(script)?;
os_script::execute(&script_text, script_runner, cli_arguments, validate)
os_script::execute(
&script_text,
expanded_script_runner,
cli_arguments,
validate,
)
}
EngineType::Duckscript => {
let script_text = get_script_text(script)?;
Expand All @@ -286,7 +295,7 @@ fn invoke_script(
let extension = script_extension.clone().unwrap();
generic_script::execute(
&script_text,
script_runner.unwrap(),
expanded_script_runner.unwrap(),
extension,
script_runner_args.clone(),
cli_arguments,
Expand Down
35 changes: 35 additions & 0 deletions src/lib/scriptengine/mod_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,38 @@ fn invoke_generic_runner_error() {

assert!(output);
}

#[test]
fn invoke_shell_to_batch_runner_with_env_expansion() {
envmnt::set("invoke_shell_to_batch_runner_with_env_expansion", "@shell");
let mut task = Task::new();
task.script_runner = Some("${invoke_shell_to_batch_runner_with_env_expansion}".to_string());
task.script = Some(ScriptValue::Text(vec!["echo test".to_string()]));

let output = invoke(
&task,
&test::create_empty_flow_info(),
Rc::new(RefCell::new(FlowState::new())),
)
.unwrap();

assert!(output);
}

#[test]
#[should_panic]
fn invoke_shell_to_batch_runner_with_env_expansion_no_env() {
let mut task = Task::new();
task.script_runner =
Some("${invoke_shell_to_batch_runner_with_env_expansion_no_env}".to_string());
task.script = Some(ScriptValue::Text(vec!["echo test".to_string()]));

let output = invoke(
&task,
&test::create_empty_flow_info(),
Rc::new(RefCell::new(FlowState::new())),
)
.unwrap();

assert!(output);
}
Loading

0 comments on commit 92946e4

Please sign in to comment.