New semver_parse, semver_is_equal and semver_is_newer commands #129

This commit is contained in:
sagie gur ari 2020-08-26 21:30:07 +00:00
parent 595d1d3ee6
commit fb13d11187
16 changed files with 544 additions and 0 deletions

View File

@ -3,6 +3,7 @@
### v0.6.7
* New --get-exit-code flag for exec command #127
* New semver_parse, semver_is_equal and semver_is_newer commands #129
### v0.6.6 (2020-08-14)

View File

@ -132,6 +132,9 @@
* [std::scope::Clear (clear_scope)](#std__scope__Clear)
* [std::scope::PopStack (scope_pop_stack)](#std__scope__PopStack)
* [std::scope::PushStack (scope_push_stack)](#std__scope__PushStack)
* [std::semver::IsEqual (semver_is_equal)](#std__semver__IsEqual)
* [std::semver::IsNewer (semver_is_newer)](#std__semver__IsNewer)
* [std::semver::Parse (semver_parse)](#std__semver__Parse)
* [std::string::Base64 (base64)](#std__string__Base64)
* [std::string::Base64Decode (base64_decode)](#std__string__Base64Decode)
* [std::string::Base64Encode (base64_encode)](#std__string__Base64Encode)
@ -4929,6 +4932,105 @@ echo ${defined}
#### Aliases:
scope_push_stack
<a name="std__semver__IsEqual"></a>
## std::semver::IsEqual
```sh
output = semver_is_equal value1 value2
```
Returns true if both semver values are valid and equal.
#### Parameters
Two semver values to compare.
#### Return Value
True if both semver values are valid and equal, else false.
#### Examples
```sh
equal = semver_is_equal 1.2.3 1.2.3
assert ${equal}
equal = semver_is_equal 1.2.3 2.2.3
assert_false ${equal}
```
#### Aliases:
semver_is_equal
<a name="std__semver__IsNewer"></a>
## std::semver::IsNewer
```sh
output = semver_is_newer newer older
```
Returns true if both semver values are valid and first value is newer.
#### Parameters
* The expected newer value
* The expected older value
#### Return Value
True if both semver values are valid and first value is newer, else false.
#### Examples
```sh
newer = semver_is_newer 3.2.3 2.2.3
assert ${newer}
newer = semver_is_newer 1.2.3 2.2.3
assert_false ${newer}
newer = semver_is_newer 1.2.3 1.2.3
assert_false ${newer}
```
#### Aliases:
semver_is_newer
<a name="std__semver__Parse"></a>
## std::semver::Parse
```sh
base = semver_parse value
```
Parses the provided value and sets the major, minor and patch variables.<br>
The variable names are based on the output variable name, for example if the output variable name is out:
* out.major - Holds the output major version
* out.minor - Holds the output minor version
* out.patch - Holds the output patch version
#### Parameters
The semver value.
#### Return Value
The major, minor and patch values.
#### Examples
```sh
version = semver_parse 1.2.3
echo ${version.major}
echo ${version.minor}
echo ${version.patch}
```
#### Aliases:
semver_parse
<a name="std__string__Base64"></a>
## std::string::Base64

View File

@ -36,6 +36,7 @@ java-properties = "^1"
meval = "^0.2"
num_cpus = "^1"
rand = "^0.7"
semver = "^0.10"
serde_json = "1"
walkdir = "^2"
which = { version = "^4", default-features = false }

View File

@ -18,6 +18,7 @@ mod process;
mod read;
pub(crate) mod release;
pub(crate) mod scope;
mod semver;
pub(crate) mod string;
mod test;
mod thread;
@ -51,6 +52,7 @@ pub(crate) fn load(commands: &mut Commands) -> Result<(), ScriptError> {
on_error::load(commands, PACKAGE)?;
process::load(commands, PACKAGE)?;
scope::load(commands, PACKAGE)?;
semver::load(commands, PACKAGE)?;
string::load(commands, PACKAGE)?;
test::load(commands, PACKAGE)?;
thread::load(commands, PACKAGE)?;

View File

@ -0,0 +1,23 @@
```sh
output = semver_is_equal value1 value2
```
Returns true if both semver values are valid and equal.
#### Parameters
Two semver values to compare.
#### Return Value
True if both semver values are valid and equal, else false.
#### Examples
```sh
equal = semver_is_equal 1.2.3 1.2.3
assert ${equal}
equal = semver_is_equal 1.2.3 2.2.3
assert_false ${equal}
```

View File

@ -0,0 +1,54 @@
use crate::utils::pckg;
use duckscript::types::command::{Command, CommandResult};
use semver::Version;
#[cfg(test)]
#[path = "./mod_test.rs"]
mod mod_test;
#[derive(Clone)]
pub(crate) struct CommandImpl {
package: String,
}
impl Command for CommandImpl {
fn name(&self) -> String {
pckg::concat(&self.package, "IsEqual")
}
fn aliases(&self) -> Vec<String> {
vec!["semver_is_equal".to_string()]
}
fn help(&self) -> String {
include_str!("help.md").to_string()
}
fn clone_and_box(&self) -> Box<dyn Command> {
Box::new((*self).clone())
}
fn run(&self, arguments: Vec<String>) -> CommandResult {
if arguments.len() < 2 {
CommandResult::Error("Missing semver values to compare.".to_string())
} else {
match Version::parse(&arguments[0]) {
Ok(version1) => match Version::parse(&arguments[1]) {
Ok(version2) => {
let result = if version1 == version2 { true } else { false };
CommandResult::Continue(Some(result.to_string()))
}
Err(error) => CommandResult::Error(error.to_string()),
},
Err(error) => CommandResult::Error(error.to_string()),
}
}
}
}
pub(crate) fn create(package: &str) -> Box<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}

View File

@ -0,0 +1,26 @@
use super::*;
use crate::test;
#[test]
fn common_functions() {
test::test_common_command_functions(create(""));
}
#[test]
fn run_no_args() {
test::run_script_and_error(vec![create("")], "out = semver_is_equal", "out");
}
#[test]
fn run_single_arg() {
test::run_script_and_error(vec![create("")], "out = semver_is_equal 1.2.3", "out");
}
#[test]
fn run_invalid_args() {
test::run_script_and_error(
vec![create("")],
"out = semver_is_equal abc_123 123_test",
"out",
);
}

View File

@ -0,0 +1,27 @@
```sh
output = semver_is_newer newer older
```
Returns true if both semver values are valid and first value is newer.
#### Parameters
* The expected newer value
* The expected older value
#### Return Value
True if both semver values are valid and first value is newer, else false.
#### Examples
```sh
newer = semver_is_newer 3.2.3 2.2.3
assert ${newer}
newer = semver_is_newer 1.2.3 2.2.3
assert_false ${newer}
newer = semver_is_newer 1.2.3 1.2.3
assert_false ${newer}
```

View File

@ -0,0 +1,58 @@
use crate::utils::pckg;
use duckscript::types::command::{Command, CommandResult};
use semver::Version;
#[cfg(test)]
#[path = "./mod_test.rs"]
mod mod_test;
#[derive(Clone)]
pub(crate) struct CommandImpl {
package: String,
}
impl Command for CommandImpl {
fn name(&self) -> String {
pckg::concat(&self.package, "IsNewer")
}
fn aliases(&self) -> Vec<String> {
vec!["semver_is_newer".to_string()]
}
fn help(&self) -> String {
include_str!("help.md").to_string()
}
fn clone_and_box(&self) -> Box<dyn Command> {
Box::new((*self).clone())
}
fn run(&self, arguments: Vec<String>) -> CommandResult {
if arguments.len() < 2 {
CommandResult::Error("Missing semver values to compare.".to_string())
} else {
match Version::parse(&arguments[0]) {
Ok(newer_version) => match Version::parse(&arguments[1]) {
Ok(older_version) => {
let result = if newer_version > older_version {
true
} else {
false
};
CommandResult::Continue(Some(result.to_string()))
}
Err(error) => CommandResult::Error(error.to_string()),
},
Err(error) => CommandResult::Error(error.to_string()),
}
}
}
}
pub(crate) fn create(package: &str) -> Box<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}

View File

@ -0,0 +1,26 @@
use super::*;
use crate::test;
#[test]
fn common_functions() {
test::test_common_command_functions(create(""));
}
#[test]
fn run_no_args() {
test::run_script_and_error(vec![create("")], "out = semver_is_newer", "out");
}
#[test]
fn run_single_arg() {
test::run_script_and_error(vec![create("")], "out = semver_is_newer 1.2.3", "out");
}
#[test]
fn run_invalid_args() {
test::run_script_and_error(
vec![create("")],
"out = semver_is_newer abc_123 123_test",
"out",
);
}

View File

@ -0,0 +1,19 @@
mod is_equal;
mod is_newer;
mod parse;
use crate::utils::pckg;
use duckscript::types::command::Commands;
use duckscript::types::error::ScriptError;
static PACKAGE: &str = "semver";
pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptError> {
let package = pckg::concat(parent, PACKAGE);
commands.set(is_equal::create(&package))?;
commands.set(is_newer::create(&package))?;
commands.set(parse::create(&package))?;
Ok(())
}

View File

@ -0,0 +1,28 @@
```sh
base = semver_parse value
```
Parses the provided value and sets the major, minor and patch variables.<br>
The variable names are based on the output variable name, for example if the output variable name is out:
* out.major - Holds the output major version
* out.minor - Holds the output minor version
* out.patch - Holds the output patch version
#### Parameters
The semver value.
#### Return Value
The major, minor and patch values.
#### Examples
```sh
version = semver_parse 1.2.3
echo ${version.major}
echo ${version.minor}
echo ${version.patch}
```

View File

@ -0,0 +1,81 @@
use crate::utils::pckg;
use duckscript::types::command::{Command, CommandResult, Commands};
use duckscript::types::instruction::Instruction;
use duckscript::types::runtime::StateValue;
use semver::Version;
use std::collections::HashMap;
#[cfg(test)]
#[path = "./mod_test.rs"]
mod mod_test;
#[derive(Clone)]
pub(crate) struct CommandImpl {
package: String,
}
impl Command for CommandImpl {
fn name(&self) -> String {
pckg::concat(&self.package, "Parse")
}
fn aliases(&self) -> Vec<String> {
vec!["semver_parse".to_string()]
}
fn help(&self) -> String {
include_str!("help.md").to_string()
}
fn clone_and_box(&self) -> Box<dyn Command> {
Box::new((*self).clone())
}
fn requires_context(&self) -> bool {
true
}
fn run_with_context(
&self,
arguments: Vec<String>,
_state: &mut HashMap<String, StateValue>,
variables: &mut HashMap<String, String>,
output_variable: Option<String>,
_instructions: &Vec<Instruction>,
_commands: &mut Commands,
_line: usize,
) -> CommandResult {
if arguments.is_empty() {
CommandResult::Error("No semver value provided.".to_string())
} else {
match Version::parse(&arguments[0]) {
Ok(version) => match output_variable {
Some(name) => {
variables.insert(
format!("{}.major", &name).to_string(),
version.major.to_string(),
);
variables.insert(
format!("{}.minor", &name).to_string(),
version.minor.to_string(),
);
variables.insert(
format!("{}.patch", &name).to_string(),
version.patch.to_string(),
);
CommandResult::Continue(None)
}
None => CommandResult::Continue(None),
},
Err(error) => CommandResult::Error(error.to_string()),
}
}
}
}
pub(crate) fn create(package: &str) -> Box<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}

View File

@ -0,0 +1,45 @@
use super::*;
use crate::test;
use crate::test::CommandValidation;
#[test]
fn common_functions() {
test::test_common_command_functions(create(""));
}
#[test]
fn run_no_args() {
test::run_script_and_error(vec![create("")], "out = semver_parse", "out");
}
#[test]
fn run_invalid() {
test::run_script_and_error(vec![create("")], "out = semver_parse aaa_bbb", "out");
}
#[test]
fn run_major() {
test::run_script_and_validate(
vec![create("")],
"out = semver_parse 1.2.3",
CommandValidation::Match("out.major".to_string(), "1".to_string()),
);
}
#[test]
fn run_minor() {
test::run_script_and_validate(
vec![create("")],
"out = semver_parse 1.2.3",
CommandValidation::Match("out.minor".to_string(), "2".to_string()),
);
}
#[test]
fn run_patch() {
test::run_script_and_validate(
vec![create("")],
"out = semver_parse 1.2.3",
CommandValidation::Match("out.patch".to_string(), "3".to_string()),
);
}

View File

@ -0,0 +1,42 @@
fn test_equal
equal = semver_is_equal 1.2.3 1.2.3
assert ${equal}
newer = semver_is_newer 1.2.3 1.2.3
assert_false ${newer}
end
fn test_not_equal_major
equal = semver_is_equal 1.2.3 2.2.3
assert_false ${equal}
newer = semver_is_newer 1.2.3 2.2.3
assert_false ${newer}
newer = semver_is_newer 3.2.3 2.2.3
assert ${newer}
end
fn test_not_equal_minor
equal = semver_is_equal 1.2.3 1.3.3
assert_false ${equal}
newer = semver_is_newer 1.2.3 1.3.3
assert_false ${newer}
newer = semver_is_newer 1.3.3 1.2.3
assert ${newer}
end
fn test_not_equal_patch
equal = semver_is_equal 1.2.3 1.2.4
assert_false ${equal}
newer = semver_is_newer 1.2.3 1.2.4
assert_false ${newer}
newer = semver_is_newer 1.2.4 1.2.3
assert ${newer}
end

View File

@ -0,0 +1,9 @@
fn test_valid
version = semver_parse 1.2.3
assert_eq ${version.major} 1
assert_eq ${version.minor} 2
assert_eq ${version.patch} 3
end