diff --git a/CHANGELOG.md b/CHANGELOG.md index 9247c00..4245fa4 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### v0.1.9 +* New get_home_dir command. * New array_join command. * The read_properties command now support **--prefix** flag. * New array_concat command. diff --git a/docs/sdk.md b/docs/sdk.md index f495cd9..31b52da 100644 --- a/docs/sdk.md +++ b/docs/sdk.md @@ -30,6 +30,7 @@ * [std::debug::DumpInstructions (dump_instructions)](#std__debug__DumpInstructions) * [std::debug::DumpState (dump_state)](#std__debug__DumpState) * [std::debug::DumpVariables (dump_variables)](#std__debug__DumpVariables) +* [std::env::GetHomeDirectory (get_home_dir)](#std__env__GetHomeDirectory) * [std::env::GetVar (get_env)](#std__env__GetVar) * [std::env::PrintCurrentDirectory (pwd)](#std__env__PrintCurrentDirectory) * [std::env::SetCurrentDirectory (cd, set_current_dir)](#std__env__SetCurrentDirectory) @@ -833,28 +834,49 @@ array_is_empty ## std::collections::ArrayJoin ```sh -handle = array_concat [handle]* +var = array_join handle separator ``` -Concats all provided arrays and returns a handle to a new array with all items. +Joins all values in the provided array with the provided separator in between each value. #### Parameters -Any number of array handles. +* An array handle +* The separator to put between each item pair #### Return Value -A handle to the new array. +The joined string value #### Examples ```sh -input1 = range 1 4 -input2 = range 4 6 -input3 = range 6 8 +function test_to_string + arr = array hello world + string = array_join ${arr} ", " -# new array will contain values from 1-7 -arr = array_concat ${input1} ${input2} ${input3} + release ${arr} + + assert_eq ${string} "hello, world" +end + +function test_numbers + arr = range 1 5 + string = array_join ${arr} ", " + + release ${arr} + + assert_eq ${string} "1, 2, 3, 4" +end + +function test_empty_separator + arr = range 1 5 + string = array_join ${arr} "" + + release ${arr} + + assert_eq ${string} "1234" +end ``` @@ -1264,6 +1286,33 @@ assert found #### Aliases: dump_variables + +## std::env::GetHomeDirectory +```sh +var = get_home_dir +``` + +Returns the user home directory path.
+In case of any error, false will be returned. + +#### Parameters + +None + +#### Return Value + +The user home directory path or false in case of any error. + +#### Examples + +```sh +directory = get_home_dir +``` + + +#### Aliases: +get_home_dir + ## std::env::GetVar ```sh diff --git a/duckscript_sdk/src/sdk/std/collections/array_join/help.md b/duckscript_sdk/src/sdk/std/collections/array_join/help.md index d8bde49..621f1cb 100644 --- a/duckscript_sdk/src/sdk/std/collections/array_join/help.md +++ b/duckscript_sdk/src/sdk/std/collections/array_join/help.md @@ -1,24 +1,45 @@ ```sh -handle = array_concat [handle]* +var = array_join handle separator ``` -Concats all provided arrays and returns a handle to a new array with all items. +Joins all values in the provided array with the provided separator in between each value. #### Parameters -Any number of array handles. +* An array handle +* The separator to put between each item pair #### Return Value -A handle to the new array. +The joined string value #### Examples ```sh -input1 = range 1 4 -input2 = range 4 6 -input3 = range 6 8 +function test_to_string + arr = array hello world + string = array_join ${arr} ", " -# new array will contain values from 1-7 -arr = array_concat ${input1} ${input2} ${input3} + release ${arr} + + assert_eq ${string} "hello, world" +end + +function test_numbers + arr = range 1 5 + string = array_join ${arr} ", " + + release ${arr} + + assert_eq ${string} "1, 2, 3, 4" +end + +function test_empty_separator + arr = range 1 5 + string = array_join ${arr} "" + + release ${arr} + + assert_eq ${string} "1234" +end ``` diff --git a/duckscript_sdk/src/sdk/std/env/get_home_dir/help.md b/duckscript_sdk/src/sdk/std/env/get_home_dir/help.md new file mode 100644 index 0000000..6a47996 --- /dev/null +++ b/duckscript_sdk/src/sdk/std/env/get_home_dir/help.md @@ -0,0 +1,20 @@ +```sh +var = get_home_dir +``` + +Returns the user home directory path.
+In case of any error, false will be returned. + +#### Parameters + +None + +#### Return Value + +The user home directory path or false in case of any error. + +#### Examples + +```sh +directory = get_home_dir +``` diff --git a/duckscript_sdk/src/sdk/std/env/get_home_dir/mod.rs b/duckscript_sdk/src/sdk/std/env/get_home_dir/mod.rs new file mode 100755 index 0000000..e9917e2 --- /dev/null +++ b/duckscript_sdk/src/sdk/std/env/get_home_dir/mod.rs @@ -0,0 +1,46 @@ +use crate::utils::pckg; +use duckscript::types::command::{Command, CommandResult}; +use home; + +#[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, "GetHomeDirectory") + } + + fn aliases(&self) -> Vec { + vec!["get_home_dir".to_string()] + } + + fn help(&self) -> String { + include_str!("help.md").to_string() + } + + fn clone_and_box(&self) -> Box { + Box::new((*self).clone()) + } + + fn run(&self, _arguments: Vec) -> CommandResult { + match home::home_dir() { + Some(directory) => { + let directory_str = directory.to_string_lossy().into_owned(); + CommandResult::Continue(Some(directory_str)) + } + None => CommandResult::Error("Unable to find user home directory.".to_string()), + } + } +} + +pub(crate) fn create(package: &str) -> Box { + Box::new(CommandImpl { + package: package.to_string(), + }) +} diff --git a/duckscript_sdk/src/sdk/std/env/get_home_dir/mod_test.rs b/duckscript_sdk/src/sdk/std/env/get_home_dir/mod_test.rs new file mode 100644 index 0000000..cd043b5 --- /dev/null +++ b/duckscript_sdk/src/sdk/std/env/get_home_dir/mod_test.rs @@ -0,0 +1,18 @@ +use super::*; +use crate::test; +use crate::test::CommandValidation; + +#[test] +fn common_functions() { + test::test_common_command_functions(create("")); +} + +#[test] +fn run_valid() { + let directory = home::home_dir().unwrap().to_string_lossy().into_owned(); + test::run_script_and_validate( + vec![create("")], + "out = get_home_dir", + CommandValidation::Match("out".to_string(), directory), + ); +} diff --git a/duckscript_sdk/src/sdk/std/env/mod.rs b/duckscript_sdk/src/sdk/std/env/mod.rs index 8bc5aca..65910ba 100755 --- a/duckscript_sdk/src/sdk/std/env/mod.rs +++ b/duckscript_sdk/src/sdk/std/env/mod.rs @@ -1,5 +1,6 @@ mod cd; mod get; +mod get_home_dir; mod pwd; mod set; mod unset; @@ -15,6 +16,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr commands.set(cd::create(&package))?; commands.set(get::create(&package))?; + commands.set(get_home_dir::create(&package))?; commands.set(pwd::create(&package))?; commands.set(set::create(&package))?; commands.set(unset::create(&package))?; diff --git a/test/std/collections/array_join_test.ds b/test/std/collections/array_join_test.ds index c02a16e..48b29ab 100644 --- a/test/std/collections/array_join_test.ds +++ b/test/std/collections/array_join_test.ds @@ -26,7 +26,7 @@ function test_empty_array assert_eq ${string} "" end -function test_separator_array +function test_empty_separator arr = range 1 5 string = array_join ${arr} "" diff --git a/test/std/env/get_home_dir_test.ds b/test/std/env/get_home_dir_test.ds new file mode 100644 index 0000000..4019cda --- /dev/null +++ b/test/std/env/get_home_dir_test.ds @@ -0,0 +1,9 @@ + +function test_get + directory = get_home_dir + empty = is_empty ${directory} + not_false = not equals false ${directory} + + assert_false ${empty} + assert ${not_false} +end