mirror of
https://github.com/sagiegurari/duckscript
synced 2024-10-14 03:42:32 +00:00
dirname command #6
This commit is contained in:
parent
8c4cdcfe30
commit
9ea74b9e0a
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
### v0.1.2
|
### v0.1.2
|
||||||
|
|
||||||
|
* New **dirname** command #6
|
||||||
* New **canonicalize** command #21
|
* New **canonicalize** command #21
|
||||||
* New **basename** command #5
|
* New **basename** command #5
|
||||||
* New **mkdir** command #13
|
* New **mkdir** command #13
|
||||||
|
|
|
@ -16,7 +16,7 @@ additional_profiles = [
|
||||||
[tasks.generate-sdk-docs]
|
[tasks.generate-sdk-docs]
|
||||||
workspace = false
|
workspace = false
|
||||||
command = "cargo"
|
command = "cargo"
|
||||||
args = [ "run", "--bin", "duckscript", "--", "./sdkdocs.ds", ]
|
args = [ "run", "--bin", "duckscript", "--", "./sdkdocs.ds" ]
|
||||||
|
|
||||||
[tasks.generate-readme]
|
[tasks.generate-readme]
|
||||||
script = [
|
script = [
|
||||||
|
|
28
docs/sdk.md
28
docs/sdk.md
|
@ -18,6 +18,7 @@
|
||||||
* [sdk::fs::CreateDirectory](#sdk__fs__CreateDirectory)
|
* [sdk::fs::CreateDirectory](#sdk__fs__CreateDirectory)
|
||||||
* [sdk::fs::GetCanonicalPath](#sdk__fs__GetCanonicalPath)
|
* [sdk::fs::GetCanonicalPath](#sdk__fs__GetCanonicalPath)
|
||||||
* [sdk::fs::GetFileName](#sdk__fs__GetFileName)
|
* [sdk::fs::GetFileName](#sdk__fs__GetFileName)
|
||||||
|
* [sdk::fs::GetParentDirectory](#sdk__fs__GetParentDirectory)
|
||||||
* [sdk::fs::Print](#sdk__fs__Print)
|
* [sdk::fs::Print](#sdk__fs__Print)
|
||||||
* [sdk::fs::Read](#sdk__fs__Read)
|
* [sdk::fs::Read](#sdk__fs__Read)
|
||||||
* [sdk::fs::Write](#sdk__fs__Write)
|
* [sdk::fs::Write](#sdk__fs__Write)
|
||||||
|
@ -819,6 +820,33 @@ file = basename ./dir/file.txt
|
||||||
#### Aliases:
|
#### Aliases:
|
||||||
basename
|
basename
|
||||||
|
|
||||||
|
<a name="sdk__fs__GetParentDirectory"></a>
|
||||||
|
## sdk::fs::GetParentDirectory
|
||||||
|
```sh
|
||||||
|
var = dirname path
|
||||||
|
```
|
||||||
|
|
||||||
|
This command will return the parent path of the provided path.<br>
|
||||||
|
If the parent path is empty, it will return none.
|
||||||
|
|
||||||
|
#### Parameters
|
||||||
|
|
||||||
|
The path to extract the parent path from.
|
||||||
|
|
||||||
|
#### Return Value
|
||||||
|
|
||||||
|
The parent path or none.
|
||||||
|
|
||||||
|
#### Examples
|
||||||
|
|
||||||
|
```sh
|
||||||
|
directory = dirname ./dir/file.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
#### Aliases:
|
||||||
|
dirname
|
||||||
|
|
||||||
<a name="sdk__fs__Print"></a>
|
<a name="sdk__fs__Print"></a>
|
||||||
## sdk::fs::Print
|
## sdk::fs::Print
|
||||||
```sh
|
```sh
|
||||||
|
|
20
duckscript_sdk/src/sdk/std/fs/dirname/help.md
Normal file
20
duckscript_sdk/src/sdk/std/fs/dirname/help.md
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
```sh
|
||||||
|
var = dirname path
|
||||||
|
```
|
||||||
|
|
||||||
|
This command will return the parent path of the provided path.<br>
|
||||||
|
If the parent path is empty, it will return none.
|
||||||
|
|
||||||
|
#### Parameters
|
||||||
|
|
||||||
|
The path to extract the parent path from.
|
||||||
|
|
||||||
|
#### Return Value
|
||||||
|
|
||||||
|
The parent path or none.
|
||||||
|
|
||||||
|
#### Examples
|
||||||
|
|
||||||
|
```sh
|
||||||
|
directory = dirname ./dir/file.txt
|
||||||
|
```
|
39
duckscript_sdk/src/sdk/std/fs/dirname/mod.rs
Executable file
39
duckscript_sdk/src/sdk/std/fs/dirname/mod.rs
Executable file
|
@ -0,0 +1,39 @@
|
||||||
|
use crate::utils::{io, pckg};
|
||||||
|
use duckscript::types::command::{Command, CommandResult};
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
#[path = "./mod_test.rs"]
|
||||||
|
mod mod_test;
|
||||||
|
|
||||||
|
struct CommandImpl {
|
||||||
|
package: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Command for CommandImpl {
|
||||||
|
fn name(&self) -> String {
|
||||||
|
pckg::concat(&self.package, "GetParentDirectory")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn aliases(&self) -> Vec<String> {
|
||||||
|
vec!["dirname".to_string()]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn help(&self) -> String {
|
||||||
|
include_str!("help.md").to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(&self, arguments: Vec<String>) -> CommandResult {
|
||||||
|
if arguments.is_empty() {
|
||||||
|
CommandResult::Error("Path not provided.".to_string())
|
||||||
|
} else {
|
||||||
|
let parent_path = io::get_parent_directory_name(&arguments[0]);
|
||||||
|
CommandResult::Continue(parent_path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn create(package: &str) -> Box<dyn Command> {
|
||||||
|
Box::new(CommandImpl {
|
||||||
|
package: package.to_string(),
|
||||||
|
})
|
||||||
|
}
|
31
duckscript_sdk/src/sdk/std/fs/dirname/mod_test.rs
Normal file
31
duckscript_sdk/src/sdk/std/fs/dirname/mod_test.rs
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
use super::*;
|
||||||
|
use crate::test;
|
||||||
|
use crate::test::CommandValidation;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn common_functions() {
|
||||||
|
test::test_common_command_functions(create(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn run_no_path_provided() {
|
||||||
|
test::run_script_and_fail(vec![create("")], "dirname");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn run_provided() {
|
||||||
|
test::run_script_and_validate(
|
||||||
|
vec![create("")],
|
||||||
|
"out = dirname ./target/_duckscript/file.txt",
|
||||||
|
CommandValidation::Match("out".to_string(), "./target/_duckscript".to_string()),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn run_file_without_directory_provided() {
|
||||||
|
test::run_script_and_validate(
|
||||||
|
vec![create("")],
|
||||||
|
"out = dirname file.txt",
|
||||||
|
CommandValidation::None,
|
||||||
|
);
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
mod basename;
|
mod basename;
|
||||||
mod canonical;
|
mod canonical;
|
||||||
|
mod dirname;
|
||||||
mod mkdir;
|
mod mkdir;
|
||||||
mod print;
|
mod print;
|
||||||
mod read;
|
mod read;
|
||||||
|
@ -16,6 +17,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
|
||||||
|
|
||||||
commands.set(basename::create(&package))?;
|
commands.set(basename::create(&package))?;
|
||||||
commands.set(canonical::create(&package))?;
|
commands.set(canonical::create(&package))?;
|
||||||
|
commands.set(dirname::create(&package))?;
|
||||||
commands.set(mkdir::create(&package))?;
|
commands.set(mkdir::create(&package))?;
|
||||||
commands.set(print::create(&package))?;
|
commands.set(print::create(&package))?;
|
||||||
commands.set(read::create(&package))?;
|
commands.set(read::create(&package))?;
|
||||||
|
|
|
@ -26,6 +26,24 @@ pub(crate) fn get_base_name(path: &str) -> Option<String> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn get_parent_directory_name(path: &str) -> Option<String> {
|
||||||
|
let file_path = Path::new(path);
|
||||||
|
|
||||||
|
let directory = file_path.parent();
|
||||||
|
match directory {
|
||||||
|
Some(directory_path) => {
|
||||||
|
let directory = directory_path.to_string_lossy().into_owned();
|
||||||
|
|
||||||
|
if directory.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(directory)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn create_directory(directory: &str) -> Result<(), String> {
|
pub(crate) fn create_directory(directory: &str) -> Result<(), String> {
|
||||||
let directory_path = Path::new(directory);
|
let directory_path = Path::new(directory);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue