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

feat(stdlib): extract function #587

Merged
merged 19 commits into from
Nov 30, 2024
33 changes: 32 additions & 1 deletion src/std/fs.ab
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { join, replace_regex, split } from "std/text"
import { join, replace_regex, split, contains_multiple } from "std/text"

/// Checks if a directory exists.
pub fun dir_exist(path) {
Expand Down Expand Up @@ -108,3 +108,34 @@ pub fun glob_multiple(paths: [Text]): [Text]? {
pub fun glob(path: Text): [Text]? {
return glob_multiple([path])?
}

/// Extract the file detecting from the filename the extension
/// Supports: bz2, gz, xz, bz2, deb, rar, rpm, tar(gz/xz/bz), zip(war/jar), 7z
/// Note: Not all the commands supports the output folder path
pub fun extract(path: Text, target: Text): Bool{
if file_exist(path) {
if {
contains_multiple(path, [".tar.bz2", ".tbz", ".tbz2"]): trust $tar xvjf "{path}" -C "{target}"$
contains_multiple(path, [".tar.gz", ".tgz"]): trust $tar xzf "{path}" -C "{target}"$
contains_multiple(path, [".tar.xz", ".txz"]): trust $tar xJf "{path}" -C "{target}"$
contains_multiple(path, [".bz2"]): trust $bunzip2 "{path}"$
contains_multiple(path, [".deb"]): trust $dpkg-deb -xv "{path}" "{target}"$
contains_multiple(path, [".gz"]): trust $gunzip "{path}"$
contains_multiple(path, [".rar"]): trust $unrar x "{path}" "{target}"$
contains_multiple(path, [".rpm"]): trust $rpm2cpio "{path}"| cpio -idm{{}}$
contains_multiple(path, [".tar"]): trust $tar xf "{path}"-C "{target}"$
contains_multiple(path, [".xz"]): trust $xz --decompress "{path}"$
contains_multiple(path, [".7z"]): trust $7z -y "{path}" -o "{target}"$
contains_multiple(path, [".zip", ".war", ".jar"]): trust $unzip "{path}" -d "{target}"$
else {
echo "Error: Unsupported file type"
return false
}
}
} else {
echo "Error: File not found"
return false
}

return true
}
13 changes: 12 additions & 1 deletion src/std/text.ab
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fun chars(text: Text): [Text] {
return chars
}

/// Checks if some text contains a value/
/// Checks if some text contains a value.
pub fun contains(text: Text, phrase: Text): Bool {
let result = trust $ if [[ "{text}" == *"{phrase}"* ]]; then
echo 1
Expand All @@ -100,6 +100,17 @@ pub fun contains(text: Text, phrase: Text): Bool {
return result == "1"
}

/// Checks if an array value is in the text.
pub fun contains_multiple(text, terms): Bool {
for index, term in terms {
if contains(text, term) {
return true
}
}

return false
}

/// Reverses text using `rev`.
pub fun reverse(text: Text): Text {
return trust $ echo "{text}" | rev $
Expand Down
6 changes: 6 additions & 0 deletions src/tests/stdlib/contains_multiple.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { contains_multiple } from "std/text"
main {
if contains_multiple("Hello World", ["World", "Something"]) {
echo "Succeeded"
}
}
15 changes: 15 additions & 0 deletions src/tests/stdlib/extract.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * from "std/fs"
import { includes } from "std/array"
main {
let tmpdir = trust $ mktemp -d /tmp/amber-XXXX $
cd tmpdir
trust $touch test.txt$
silent trust $tar -czf "filename.tar.gz" "{tmpdir}/test.txt"$
trust $rm "test.txt"$
let package = tmpdir + "/" + "filename.tar.gz"

if (extract(package, tmpdir)) {
echo "Succeeded"
}
trust $rm -rf "{tmpdir}"$
}