Skip to content

Commit

Permalink
Enhancement: Add OS condition #989
Browse files Browse the repository at this point in the history
  • Loading branch information
sagiegurari committed Dec 15, 2023
1 parent 3eb6ccb commit 21fea70
Show file tree
Hide file tree
Showing 12 changed files with 309 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### v0.38.5

* Enhancement: Add OS condition #989
* Enhancement: Enable setting default profile by env variable #996 (thanks @Johnabell)
* Documentation: Add Fig Autocompletion to README.md #993 (thanks @beeinger)
* Maintenance: update dependencies and build
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1823,6 +1823,7 @@ echo "condition was met"
The following condition types are available:

* **profile** - See [profiles](#usage-profiles) for more info
* **os** - List of OS names (Windows, macOS, iOS, Linux, Android, etc... as defined by cfg!(target_os))
* **platforms** - List of platform names (windows, linux, mac)
* **channels** - List of rust channels (stable, beta, nightly)
* **env_set** - List of environment variables that must be defined
Expand Down
1 change: 1 addition & 0 deletions docs/_includes/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -1686,6 +1686,7 @@ echo "condition was met"
The following condition types are available:

* **profile** - See [profiles](#usage-profiles) for more info
* **os** - List of OS names (Windows, macOS, iOS, Linux, Android, etc... as defined by cfg!(target_os))
* **platforms** - List of platform names (windows, linux, mac)
* **channels** - List of rust channels (stable, beta, nightly)
* **env_set** - List of environment variables that must be defined
Expand Down
6 changes: 6 additions & 0 deletions examples/condition.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ script = '''
echo "condition was met"
'''

[tasks.test-os-condition]
condition = { os = ["linux"] }
script = '''
echo "OS condition was met"
'''

[tasks.test-rust-version-condition]
condition = { rust_version = { min = "1.20.0", max = "1.30.0" } }
script = '''
Expand Down
22 changes: 21 additions & 1 deletion src/lib/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,25 @@ fn validate_env_bool(condition: &TaskCondition, truthy: bool) -> bool {
}
}

fn validate_os(condition: &TaskCondition) -> bool {
let os = condition.os.clone();
match os {
Some(os_names) => {
let os_name = envmnt::get_or("CARGO_MAKE_RUST_TARGET_OS", "");
let index = os_names.iter().position(|value| *value == os_name);

match index {
None => {
debug!("Failed OS condition, current OS: {}", &os_name);
false
}
_ => true,
}
}
None => true,
}
}

fn validate_platform(condition: &TaskCondition) -> bool {
let platforms = condition.platforms.clone();
match platforms {
Expand Down Expand Up @@ -395,7 +414,8 @@ fn validate_criteria(flow_info: Option<&FlowInfo>, condition: &Option<TaskCondit
Some(ref condition_struct) => {
debug!("Checking task condition structure.");

validate_platform(&condition_struct)
validate_os(&condition_struct)
&& validate_platform(&condition_struct)
&& validate_profile(&condition_struct)
&& validate_channel(&condition_struct, flow_info)
&& validate_env(&condition_struct)
Expand Down
Loading

0 comments on commit 21fea70

Please sign in to comment.