Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aesthetic script changes #543

Merged
merged 6 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions build.ab
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Check if such directory exists
$test -f "Cargo.toml" > /dev/null$ failed {
$ test -f "Cargo.toml" > /dev/null $ failed {
echo "Please run this command in the project root directory"
trust $exit 1$
trust $ exit 1 $
}

trust {
if status == 0 {
// Recompile installer scripts
$cargo run -- setup/install.ab setup/install.sh$
$cargo run -- setup/uninstall.ab setup/uninstall.sh$
$ cargo run -- setup/install.ab setup/install.sh $
$ cargo run -- setup/uninstall.ab setup/uninstall.sh $
} else {
echo "Please run this command in the project root directory"
}
Expand Down
30 changes: 15 additions & 15 deletions setup/install.ab
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ let target = "amber"
let archive = "amber.tar.xz"
let agent = has_failed("uname -a")
then "unknown"
else trust $uname -a$
else trust $ uname -a $

echo ""

fun get_latest_release_tag() {
let tag_url = "https://api.github.com/repos/Ph0enixKM/{name}/releases/latest"
let tag_json = $curl -sL "{tag_url}"$?
let tag_json = $ curl -sL "{tag_url}" $?
// Get the tag name from the JSON
let tag = $echo "\${nameof tag_json}" \
let tag = $ echo "\${nameof tag_json}" \
| grep -Eo "tag_name\\"[^\\"]*\\"([^\\"]+)\\"" \
| grep -Eo "\\"[^\\"]+\\"\$" \
| grep -Eo "[^\\"\\s]+"$?
| grep -Eo "[^\\"\\s]+" $?
return tag
}

Expand All @@ -33,7 +33,7 @@ main(args) {
let bins_folder = get_bins_folder(user_only_install)

// Check if such directory exists
trust $test -d "{place}"$
trust $ test -d "{place}" $

if status == 0 {
echo "Amber already installed"
Expand All @@ -56,7 +56,7 @@ main(args) {
// this decreases the chance that our script results in partial installation leaving assets behind
let sudo = user_only_install then "" else "sudo"
// Create directory for amber
silent ${sudo} mkdir -p "{place}"$ failed {
silent $ {sudo} mkdir -p "{place}" $ failed {
echo "Failed to create directory for amber."
if user_only_install {
echo "Please make sure that root user can access {place} directory."
Expand All @@ -66,7 +66,7 @@ main(args) {
exit 1
}
if user_only_install {
silent $mkdir -p "{bins_folder}"$ failed {
silent $ mkdir -p "{bins_folder}" $ failed {
echo "Failed to create directory for amber bin at {bins_folder}."
exit 1
}
Expand All @@ -82,50 +82,50 @@ main(args) {
let url = "https://github.com/Ph0enixKM/{name}/releases/download/{tag}/amber-{arch}-{os}.tar.xz"

// Download amber
silent $curl -L -o "{archive}" "{url}"$ failed {
silent $ curl -L -o "{archive}" "{url}" $ failed {
echo "Curl failed to download amber."
echo "Something went wrong. Please try again later."
exit 1
}

// Move archived version of amber
${sudo} mv "{archive}" "{place}/{archive}"$ failed {
$ {sudo} mv "{archive}" "{place}/{archive}" $ failed {
echo "Failed to move amber to the installation directory."
echo "Please make sure that root user can access {place} directory."
exit 1
}

// Unarchive amber
silent ${sudo} tar --strip-components=1 -xvf {place}/{archive} -C {place}$ failed {
silent $ {sudo} tar --strip-components=1 -xvf {place}/{archive} -C {place} $ failed {
echo "Failed to unarchive amber at {place}/{archive}"
echo "Please make sure that you have `tar` command installed."
exit 1
}

// Delete the archive
${sudo} rm {place}/{archive}$ failed {
$ {sudo} rm {place}/{archive} $ failed {
echo "Failed to remove downloaded archive at {place}/{archive}"
exit 1
}

// Give permissions to execute amber
${sudo} chmod +x "{place}/{target}"$ failed {
$ {sudo} chmod +x "{place}/{target}" $ failed {
echo "Failed to give permissions to execute amber."
echo "Please make sure that root user can access {place} directory."
exit 1
}

// Delete the previous symbolic link
if file_exist("{bins_folder}/{target}") {
${sudo} rm "{bins_folder}/{target}"$ failed {
$ {sudo} rm "{bins_folder}/{target}" $ failed {
echo "Failed to remove the previous amber symbol link."
echo "Please make sure that root user can access {bins_folder} directory."
exit 1
}
}

// Create amber symbol link
${sudo} ln -s "{place}/{target}" "{bins_folder}/{target}"$ failed {
$ {sudo} ln -s "{place}/{target}" "{bins_folder}/{target}" $ failed {
echo "Failed to create amber symbol link."
echo "Please make sure that root user can access {bins_folder} directory."
exit 1
Expand All @@ -134,7 +134,7 @@ main(args) {
let nickname = input("Would you like to help improve Amber by sharing your OS info with our developer database? Enter your GitHub nickname (or any nickname) or type `no`:")
if (nickname != "no") {
// Send feedback to the server
trust silent $curl -G --data-urlencode "agent={agent}" --data-urlencode "nickname={nickname}" --data-urlencode "name=download" "https://amber-lang.com/api/visit"$
trust silent $ curl -G --data-urlencode "agent={agent}" --data-urlencode "nickname={nickname}" --data-urlencode "name=download" "https://amber-lang.com/api/visit" $
}

// Send success message
Expand Down
10 changes: 5 additions & 5 deletions setup/shared.ab
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { includes } from "std/array"

pub fun get_os(): Text {
// Determine OS type
let os_type = $uname -s$ failed {
let os_type = $ uname -s $ failed {
echo "Failed to determine OS type (using `uname` command)."
echo "Please try again or make sure you have it installed."
exit 1
Expand All @@ -25,7 +25,7 @@ pub fun get_os(): Text {

pub fun get_arch(): Text {
// Determine architecture
let arch_type = $uname -m$ failed {
let arch_type = $ uname -m $ failed {
echo "Failed to determine architecture."
echo "Please try again or use another download method."
exit 1
Expand All @@ -39,7 +39,7 @@ pub fun get_arch(): Text {
}

fun get_home(): Text {
let home = $echo \$HOME$ failed {
let home = $ echo \$HOME $ failed {
echo "User installation requested, but unable to retrieve home directory from $HOME environment."
exit 1
}
Expand All @@ -56,8 +56,8 @@ pub fun get_bins_folder(user_only: Bool): Text {
} else {
// Ensure /usr/local/bin exists for non-user installations
let bins_folder = "/usr/local/bin"
silent $test -d "{bins_folder}"$ failed {
silent $sudo mkdir -p "{bins_folder}"$ failed {
silent $ test -d "{bins_folder}" $ failed {
silent $ sudo mkdir -p "{bins_folder}" $ failed {
echo "Failed to create {bins_folder} directory."
exit 1
}
Expand Down
6 changes: 3 additions & 3 deletions setup/uninstall.ab
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ main(args) {
let place = get_place(user_only_install)
let bins_folder = get_bins_folder(user_only_install)

trust $test -d "{place}" > /dev/null$
trust $ test -d "{place}" > /dev/null $

if status == 0 {
let sudo = user_only_install then "" else "sudo"
${sudo} rm -rf "{place}"$ failed {
$ {sudo} rm -rf "{place}" $ failed {
echo "Failed to remove Amber from {place}"
echo "Make sure root has the correct permissions to access this directory"
exit 1
}
${sudo} rm "{bins_folder}/amber"$ failed {
$ {sudo} rm "{bins_folder}/amber" $ failed {
echo "Failed to remove Amber symlink from {bins_folder}"
echo "Make sure root has the correct permissions to access this directory"
exit 1
Expand Down
10 changes: 5 additions & 5 deletions src/std/date.ab
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,18 @@
/// ```
pub fun date_posix(format: Text = "", date: Text = "", utc: Bool = false): Text? {
if format == "": format = "%FT%T%Z"
if date == "": date = trust $date +"%FT%T%Z"$
if date == "": date = trust $ date +"%FT%T%Z" $
if (utc) {
return $date --utc -d "{date}" +"{format}"$?
return $ date --utc -d "{date}" +"{format}" $?
} else {
return $date -d "{date}" +"{format}"$?
return $ date -d "{date}" +"{format}" $?
}
}

/// Returns the current timestamp (seconds since the Epoch (1970-01-01 00:00 UTC)).
#[allow_absurd_cast]
pub fun now(): Num {
return trust $date +%s$ as Num
return trust $ date +%s $ as Num
}

/// ### EXPERIMENTAL
Expand All @@ -101,7 +101,7 @@ pub fun now(): Num {
/// - minutes
/// - seconds
pub fun date_add(add:Text, date:Text = "", utc: Bool = false): Text? {
if date == "": date = trust $date +"%FT%T%Z"$
if date == "": date = trust $ date +"%FT%T%Z" $
return date_posix("", "{date_posix("%F", date, utc)?} {add} {date_posix("%T", date, utc)?}", utc)?
}

Expand Down
46 changes: 23 additions & 23 deletions src/std/env.ab
Original file line number Diff line number Diff line change
Expand Up @@ -3,76 +3,76 @@ import * from "std/text"

/// Retrieves the value of an environment variable, optionally sourcing it from a file if not already set.
pub fun get_env_var(var: Text, file: Text = ".env"): Text {
let _var = trust $echo "\$\{!var}"$
let _var = trust $ echo "\$\{!var}" $
if _var != "" {
return _var
}

if file_exist(file) {
trust $source "{file}"$
return trust $echo "\$\{!var}"$
trust $ source "{file}" $
return trust $ echo "\$\{!var}" $
}

return ""
}

/// Loads the env file in the environment, using `xargs`.
pub fun load_env_file(file: Text = ".env"): Null {
trust $export "\$(xargs < {file})" > /dev/null$
trust $ export "\$(xargs < {file})" > /dev/null $
}

/// Checks if a variable inside the shell session exists.
pub fun shell_isset(name: Text): Bool {
$[[ ! -z \$\{!{nameof name}+z} ]]$ failed {
$ [[ ! -z \$\{!{nameof name}+z} ]] $ failed {
return false
}
return true
}

/// Sets a constant inside the shell session.
pub fun shell_constant_set(name: Text, val: Text): Null? {
$readonly \${nameof name}="\${nameof val}" 2> /dev/null$?
$ readonly \${nameof name}="\${nameof val}" 2> /dev/null $?
}

/// Gets a constant inside the shell session.
pub fun shell_constant_get(name: Text): Text? {
return $echo \$\{!{nameof name}}$?
return $ echo \$\{!{nameof name}} $?
}

/// Sets a constant inside the shell session.
pub fun shell_var_set(name: Text, val: Text): Null? {
$export \${nameof name}="\${nameof val}" 2> /dev/null$?
$ export \${nameof name}="\${nameof val}" 2> /dev/null $?
}

/// Gets a constant inside the shell session.
pub fun shell_var_get(name: Text): Text? {
return $echo \$\{!{nameof name}}$?
return $ echo \$\{!{nameof name}} $?
}

/// Removes a variable inside the shell session.
pub fun shell_unset(name: Text): Null? {
$unset {name}$?
$ unset {name} $?
}

/// Checks if a command exists.
pub fun is_command(command: Text): Bool {
$[ -x "\$(command -v {command})" ]$ failed {
$ [ -x "\$(command -v {command})" ] $ failed {
return false
}
return true
}

/// Creates a prompt and returns the value.
pub fun input(prompt: Text): Text {
trust $read -p "\${nameof prompt}"$
trust $ read -p "\${nameof prompt}" $
return "\$REPLY"
}

/// Creates a prompt, hides any user input and returns the value.
pub fun input_hidden(prompt: Text): Text {
trust {
$read -s -p "\${nameof prompt}"$
$echo "" >&2$
$ read -s -p "\${nameof prompt}" $
$ echo "" >&2 $
}
return "\$REPLY"
}
Expand All @@ -83,23 +83,23 @@ pub fun input_hidden(prompt: Text): Text {
pub fun confirm(prompt: Text, default_yes: Bool = false): Bool {
let choice_default = default_yes then " [\x1b[1mY/\x1b[0mn]" else " [y/\x1b[1mN\x1b[0m]"
trust {
$printf "\x1b[1m{prompt}\x1b[0m{choice_default}"$
$read -s -n 1$
$printf "\n"$
$ printf "\x1b[1m{prompt}\x1b[0m{choice_default}" $
$ read -s -n 1 $
$ printf "\n" $
}
let result = lower(trust $echo \$REPLY$)
let result = lower(trust $ echo \$REPLY $)
return result == "y" or (result == "" and default_yes)
}

/// Checks if the command has failed.
pub fun has_failed(command: Text): Bool {
trust silent $eval {command}$
trust silent $ eval {command} $
return status != 0
}

/// Checks if the script is running with a user with root permission.
pub fun is_root(): Bool {
if trust $id -u$ == "0" {
if trust $ id -u $ == "0" {
return true
}

Expand All @@ -108,13 +108,13 @@ pub fun is_root(): Bool {

/// `printf` the text following the arguments.
pub fun printf(format: Text, args: [Text] = [""]): Null {
trust ${nameof args}=("{format}" "\$\{{nameof args}[@]}")$
trust $printf "\$\{{nameof args}[@]}"$
trust $ {nameof args}=("{format}" "\$\{{nameof args}[@]}") $
trust $ printf "\$\{{nameof args}[@]}" $
}

/// Escapes the text to be used with `printf`.
pub fun printf_escape(text: Text): Text {
return trust $echo \${nameof text} | sed -e 's/\\\\/\\\\\\\\/g' -e "s/%/%%/g"$
return trust $ echo \${nameof text} | sed -e 's/\\\\/\\\\\\\\/g' -e "s/%/%%/g" $
}

/// Prepares a text with formatting options for `printf`.
Expand Down
Loading