-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move stdlib to different files (#291)
* feat(rust): porting of @b1ek code * feat(test): updated * fix(cargo): missing crate * fix(cargo): missing crate * fix(resolve): remove std to search it * fix(test): done * fix(std): renamed action * Update src/modules/imports/import.rs Co-authored-by: Phoenix Himself <[email protected]> * Update src/modules/imports/import.rs Co-authored-by: Phoenix Himself <[email protected]> * Update src/modules/imports/import.rs Co-authored-by: Phoenix Himself <[email protected]> * Update src/modules/imports/import_string.rs Co-authored-by: Phoenix Himself <[email protected]> * Update src/std/array.ab Co-authored-by: Phoenix Himself <[email protected]> * Update src/std/array.ab Co-authored-by: Phoenix Himself <[email protected]> * Update src/std/fs.ab Co-authored-by: Phoenix Himself <[email protected]> * Update src/std/shell.ab Co-authored-by: Phoenix Himself <[email protected]> * feat(review): moved shell to env * feat(review): moved shell to env * feat(review): moved shell to env * Update setup/install.ab Co-authored-by: Phoenix Himself <[email protected]> * fix(install): done * fix(test): input version by @CymDeveloppement * fix(test): input * fix(test): input * fix(test): input * fix(test): input * fix(test): input --------- Co-authored-by: Phoenix Himself <[email protected]>
- Loading branch information
Showing
69 changed files
with
393 additions
and
379 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,7 +173,4 @@ impl AmberCompiler { | |
}) | ||
} | ||
|
||
pub fn import_std() -> String { | ||
[include_str!("std/main.ab")].join("\n") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ mod modules; | |
mod rules; | ||
mod translate; | ||
mod utils; | ||
mod stdlib; | ||
|
||
#[cfg(test)] | ||
pub mod tests; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
pub fun array_first_index(array, value): Num { | ||
loop index, element in array { | ||
if value as Text == element as Text { | ||
return index | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
pub fun array_search(array, value): [Num] { | ||
let result = [Num] | ||
loop index, element in array { | ||
if value as Text == element as Text { | ||
result += [index] | ||
} | ||
} | ||
return result | ||
} | ||
|
||
pub fun in_array(array, value): Bool { | ||
let result = array_first_index(array, value) | ||
return result >= 0 | ||
} | ||
|
||
pub fun includes(arr, value) { | ||
loop v in arr { | ||
if v == value { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import * from "std/fs" | ||
|
||
pub fun get_env_var(var: Text): Text { | ||
let _var = unsafe $echo "\$\{!var}"$ | ||
if _var != "" { | ||
return _var | ||
} | ||
|
||
if file_exist(".env") { | ||
unsafe $source ".env"$ | ||
return unsafe $echo "\$\{!var}"$ | ||
} | ||
|
||
return "" | ||
} | ||
|
||
pub fun load_env_file(): Null { | ||
unsafe $export "\$(xargs < .env)" > /dev/null$ | ||
} | ||
|
||
pub fun shell_isset(name: Text): Bool { | ||
$[[ ! -z \$\{!{nameof name}+z} ]]$ failed { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
pub fun shell_constant_set(name: Text, val: Text): Null { | ||
$readonly \${nameof name}="\${nameof val}" 2> /dev/null$? | ||
} | ||
|
||
pub fun shell_constant_get(name: Text): Text { | ||
return $echo \$\{!{nameof name}}$? | ||
} | ||
|
||
pub fun shell_var_set(name: Text, val: Text): Null { | ||
$export \${nameof name}="\${nameof val}" 2> /dev/null$? | ||
} | ||
|
||
pub fun shell_var_get(name: Text): Text { | ||
return $echo \$\{!{nameof name}}$? | ||
} | ||
|
||
pub fun shell_unset(name: Text): Null { | ||
$unset {name}$? | ||
} | ||
|
||
pub fun is_command(command: Text): Bool { | ||
$[ -x "\$(command -v {command})" ]$ failed { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
pub fun input(prompt: Text): Text { | ||
unsafe $printf "\${nameof prompt}"$ | ||
unsafe $read$ | ||
return "\$REPLY" | ||
} | ||
|
||
pub fun has_failed(command: Text): Bool { | ||
unsafe silent $eval {command}$ | ||
return status != 0 | ||
} | ||
|
||
pub fun exit(code: Num): Null { | ||
unsafe $exit "{code}"$ | ||
} | ||
|
||
pub fun is_root(): Bool { | ||
if unsafe $id -u$ == "0" { | ||
return true | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
pub fun dir_exist(path) { | ||
$[ -d "{path}" ]$ failed { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
pub fun file_exist(path) { | ||
$[ -f "{path}" ]$ failed { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
pub fun file_read(path) { | ||
return $< "{path}"$? | ||
} | ||
|
||
pub fun file_write(path, content) { | ||
return $echo "{content}" > "{path}"$? | ||
} | ||
|
||
pub fun file_append(path, content) { | ||
return $echo "{content}" >> "{path}"$? | ||
} | ||
|
||
pub fun create_symbolic_link(origin: Text, destination: Text): Bool { | ||
if file_exist(origin) { | ||
unsafe $ln -s "{origin}" "{destination}"$ | ||
return true | ||
} | ||
|
||
echo "The file {origin} doesn't exist!" | ||
return false | ||
} | ||
|
||
pub fun create_dir(path: Text): Null { | ||
if not dir_exist(path) { | ||
unsafe $mkdir -p "{path}"$ | ||
} | ||
} | ||
|
||
pub fun make_executable(path: Text): Bool { | ||
if file_exist(path) { | ||
unsafe $chmod +x "{path}"$ | ||
return true | ||
} | ||
|
||
echo "The file {path} doesn't exist!" | ||
return false | ||
} | ||
|
||
pub fun change_owner(user: Text, path: Text): Bool { | ||
if file_exist(path) or dir_exist(path) { | ||
unsafe $chown -R "{user}" "{path}"$ | ||
return true | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import * from "std/env" | ||
|
||
pub fun download(url: Text, path: Text): Bool { | ||
if { | ||
is_command("curl") { | ||
unsafe $curl -L -o "{path}" "{url}"$ | ||
} | ||
is_command("wget") { | ||
unsafe $wget "{url}" -P "{path}"$ | ||
} | ||
is_command("aria2c") { | ||
unsafe $aria2c "{url}" -d "{path}"$ | ||
} | ||
else { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} |
Oops, something went wrong.