From 3a87b1b82e8412b302cd5afa90b34b09b9b9dc64 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Tue, 12 Nov 2024 12:17:58 +0100 Subject: [PATCH 01/19] feat(stdlib): yeah --- src/std/fs.ab | 33 ++++++++++++++++++++++++++- src/std/text.ab | 13 ++++++++++- src/tests/stdlib/contains_multiple.ab | 6 +++++ src/tests/stdlib/extract.ab | 15 ++++++++++++ 4 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 src/tests/stdlib/contains_multiple.ab create mode 100644 src/tests/stdlib/extract.ab diff --git a/src/std/fs.ab b/src/std/fs.ab index abddddc0..d63672f0 100644 --- a/src/std/fs.ab +++ b/src/std/fs.ab @@ -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) { @@ -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 +} diff --git a/src/std/text.ab b/src/std/text.ab index 5529293f..d25a9148 100644 --- a/src/std/text.ab +++ b/src/std/text.ab @@ -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 @@ -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 $ diff --git a/src/tests/stdlib/contains_multiple.ab b/src/tests/stdlib/contains_multiple.ab new file mode 100644 index 00000000..1b395263 --- /dev/null +++ b/src/tests/stdlib/contains_multiple.ab @@ -0,0 +1,6 @@ +import { contains_multiple } from "std/text" +main { + if contains_multiple("Hello World", ["World", "Something"]) { + echo "Succeeded" + } +} diff --git a/src/tests/stdlib/extract.ab b/src/tests/stdlib/extract.ab new file mode 100644 index 00000000..144767ac --- /dev/null +++ b/src/tests/stdlib/extract.ab @@ -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}"$ +} From 4415196a5705bcafa85b7b0d2831232001f7dd28 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Thu, 14 Nov 2024 10:14:51 +0100 Subject: [PATCH 02/19] feat(review): new contains_all --- src/std/fs.ab | 26 +++++++++++++------------- src/std/text.ab | 21 +++++++++++++++++++-- src/tests/stdlib/contains_all.ab | 14 ++++++++++++++ src/tests/stdlib/contains_any.ab | 7 +++++++ src/tests/stdlib/contains_multiple.ab | 6 ------ src/tests/stdlib/extract.ab | 7 ++++--- 6 files changed, 57 insertions(+), 24 deletions(-) create mode 100644 src/tests/stdlib/contains_all.ab create mode 100644 src/tests/stdlib/contains_any.ab delete mode 100644 src/tests/stdlib/contains_multiple.ab diff --git a/src/std/fs.ab b/src/std/fs.ab index d63672f0..8a3792c8 100644 --- a/src/std/fs.ab +++ b/src/std/fs.ab @@ -1,4 +1,4 @@ -import { join, replace_regex, split, contains_multiple } from "std/text" +import { contains_all, join, replace_regex, split } from "std/text" /// Checks if a directory exists. pub fun dir_exist(path) { @@ -115,18 +115,18 @@ pub fun glob(path: Text): [Text]? { 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}"$ + contains_all(path, [".tar.bz2", ".tbz", ".tbz2"]): trust $ tar xvjf "{path}" -C "{target}" $ + contains_all(path, [".tar.gz", ".tgz"]): trust $ tar xzf "{path}" -C "{target}" $ + contains_all(path, [".tar.xz", ".txz"]): trust $ tar xJf "{path}" -C "{target}" $ + contains_all(path, [".bz2"]): trust $ bunzip2 "{path}" $ + contains_all(path, [".deb"]): trust $ dpkg-deb -xv "{path}" "{target}" $ + contains_all(path, [".gz"]): trust $ gunzip "{path}" $ + contains_all(path, [".rar"]): trust $ unrar x "{path}" "{target}" $ + contains_all(path, [".rpm"]): trust $ rpm2cpio "{path}"| cpio -idm{{}} $ + contains_all(path, [".tar"]): trust $ tar xf "{path}" -C "{target}" $ + contains_all(path, [".xz"]): trust $ xz --decompress "{path}" $ + contains_all(path, [".7z"]): trust $ 7z -y "{path}" -o "{target}" $ + contains_all(path, [".zip", ".war", ".jar"]): trust $ unzip "{path}" -d "{target}" $ else { echo "Error: Unsupported file type" return false diff --git a/src/std/text.ab b/src/std/text.ab index d25a9148..9c26a44c 100644 --- a/src/std/text.ab +++ b/src/std/text.ab @@ -101,8 +101,8 @@ pub fun contains(text: Text, phrase: Text): Bool { } /// Checks if an array value is in the text. -pub fun contains_multiple(text, terms): Bool { - for index, term in terms { +pub fun contains_any(text: Text, terms: [Text]): Bool { + for term in terms { if contains(text, term) { return true } @@ -111,6 +111,23 @@ pub fun contains_multiple(text, terms): Bool { return false } +/// Checks if all the arrays values are in the string +pub fun contains_all(text: Text, terms: [Text]): Bool { + let total = len terms + let i = 0 + for term in terms { + if contains(text, term) { + i++ + } + } + + if i == total { + return true + } + + return false +} + /// Reverses text using `rev`. pub fun reverse(text: Text): Text { return trust $ echo "{text}" | rev $ diff --git a/src/tests/stdlib/contains_all.ab b/src/tests/stdlib/contains_all.ab new file mode 100644 index 00000000..972944eb --- /dev/null +++ b/src/tests/stdlib/contains_all.ab @@ -0,0 +1,14 @@ +import { contains_all } from "std/text" + +// Output +// Succeeded +// Succeeded + +main { + if ! contains_all("Hello World", ["World", "Something"]) { + echo "Succeeded" + } + if contains_all("Hello World", ["World", "Hello"]) { + echo "Succeeded" + } +} diff --git a/src/tests/stdlib/contains_any.ab b/src/tests/stdlib/contains_any.ab new file mode 100644 index 00000000..fdc759f1 --- /dev/null +++ b/src/tests/stdlib/contains_any.ab @@ -0,0 +1,7 @@ +import { contains_any } from "std/text" + +main { + if contains_any("Hello World", ["World", "Something"]) { + echo "Succeeded" + } +} diff --git a/src/tests/stdlib/contains_multiple.ab b/src/tests/stdlib/contains_multiple.ab deleted file mode 100644 index 1b395263..00000000 --- a/src/tests/stdlib/contains_multiple.ab +++ /dev/null @@ -1,6 +0,0 @@ -import { contains_multiple } from "std/text" -main { - if contains_multiple("Hello World", ["World", "Something"]) { - echo "Succeeded" - } -} diff --git a/src/tests/stdlib/extract.ab b/src/tests/stdlib/extract.ab index 144767ac..944e794d 100644 --- a/src/tests/stdlib/extract.ab +++ b/src/tests/stdlib/extract.ab @@ -1,15 +1,16 @@ import * from "std/fs" import { includes } from "std/array" + main { let tmpdir = trust $ mktemp -d /tmp/amber-XXXX $ cd tmpdir - trust $touch test.txt$ + trust $ touch test.txt $ silent trust $tar -czf "filename.tar.gz" "{tmpdir}/test.txt"$ - trust $rm "test.txt"$ + trust $ rm "test.txt" $ let package = tmpdir + "/" + "filename.tar.gz" if (extract(package, tmpdir)) { echo "Succeeded" } - trust $rm -rf "{tmpdir}"$ + trust $ rm -rf "{tmpdir}" $ } From 9292c2d34c048e52adc42678fdb6418d602c3762 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Thu, 14 Nov 2024 10:23:55 +0100 Subject: [PATCH 03/19] feat(review): new contains_all --- src/std/fs.ab | 26 +++++++++++++------------- src/std/text.ab | 2 +- src/tests/stdlib/contains_all.ab | 3 ++- src/tests/stdlib/contains_any.ab | 20 ++++++++++++++++---- src/tests/stdlib/extract.ab | 4 +++- 5 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/std/fs.ab b/src/std/fs.ab index 8a3792c8..c6895f08 100644 --- a/src/std/fs.ab +++ b/src/std/fs.ab @@ -1,4 +1,4 @@ -import { contains_all, join, replace_regex, split } from "std/text" +import { contains_any, join, replace_regex, split } from "std/text" /// Checks if a directory exists. pub fun dir_exist(path) { @@ -115,18 +115,18 @@ pub fun glob(path: Text): [Text]? { pub fun extract(path: Text, target: Text): Bool{ if file_exist(path) { if { - contains_all(path, [".tar.bz2", ".tbz", ".tbz2"]): trust $ tar xvjf "{path}" -C "{target}" $ - contains_all(path, [".tar.gz", ".tgz"]): trust $ tar xzf "{path}" -C "{target}" $ - contains_all(path, [".tar.xz", ".txz"]): trust $ tar xJf "{path}" -C "{target}" $ - contains_all(path, [".bz2"]): trust $ bunzip2 "{path}" $ - contains_all(path, [".deb"]): trust $ dpkg-deb -xv "{path}" "{target}" $ - contains_all(path, [".gz"]): trust $ gunzip "{path}" $ - contains_all(path, [".rar"]): trust $ unrar x "{path}" "{target}" $ - contains_all(path, [".rpm"]): trust $ rpm2cpio "{path}"| cpio -idm{{}} $ - contains_all(path, [".tar"]): trust $ tar xf "{path}" -C "{target}" $ - contains_all(path, [".xz"]): trust $ xz --decompress "{path}" $ - contains_all(path, [".7z"]): trust $ 7z -y "{path}" -o "{target}" $ - contains_all(path, [".zip", ".war", ".jar"]): trust $ unzip "{path}" -d "{target}" $ + contains_any(path, [".tar.bz2", ".tbz", ".tbz2"]): trust $ tar xvjf "{path}" -C "{target}" $ + contains_any(path, [".tar.gz", ".tgz"]): trust $ tar xzf "{path}" -C "{target}" $ + contains_any(path, [".tar.xz", ".txz"]): trust $ tar xJf "{path}" -C "{target}" $ + contains_any(path, [".bz2"]): trust $ bunzip2 "{path}" $ + contains_any(path, [".deb"]): trust $ dpkg-deb -xv "{path}" "{target}" $ + contains_any(path, [".gz"]): trust $ gunzip "{path}" $ + contains_any(path, [".rar"]): trust $ unrar x "{path}" "{target}" $ + contains_any(path, [".rpm"]): trust $ rpm2cpio "{path}" | cpio -idm{{}} $ + contains_any(path, [".tar"]): trust $ tar xf "{path}" -C "{target}" $ + contains_any(path, [".xz"]): trust $ xz --decompress "{path}" $ + contains_any(path, [".7z"]): trust $ 7z -y "{path}" -o "{target}" $ + contains_any(path, [".zip", ".war", ".jar"]): trust $ unzip "{path}" -d "{target}" $ else { echo "Error: Unsupported file type" return false diff --git a/src/std/text.ab b/src/std/text.ab index 9c26a44c..d37c0236 100644 --- a/src/std/text.ab +++ b/src/std/text.ab @@ -117,7 +117,7 @@ pub fun contains_all(text: Text, terms: [Text]): Bool { let i = 0 for term in terms { if contains(text, term) { - i++ + i += 1 } } diff --git a/src/tests/stdlib/contains_all.ab b/src/tests/stdlib/contains_all.ab index 972944eb..74e7dcbe 100644 --- a/src/tests/stdlib/contains_all.ab +++ b/src/tests/stdlib/contains_all.ab @@ -5,9 +5,10 @@ import { contains_all } from "std/text" // Succeeded main { - if ! contains_all("Hello World", ["World", "Something"]) { + if contains_all("Hello World", ["World", "Something"]) == false { echo "Succeeded" } + if contains_all("Hello World", ["World", "Hello"]) { echo "Succeeded" } diff --git a/src/tests/stdlib/contains_any.ab b/src/tests/stdlib/contains_any.ab index fdc759f1..8e39c451 100644 --- a/src/tests/stdlib/contains_any.ab +++ b/src/tests/stdlib/contains_any.ab @@ -1,7 +1,19 @@ import { contains_any } from "std/text" +// Output +// None: 0 +// Left: 1 +// Right: 1 +// Both: 1 + +fun test_multiple(label, text, terms) { + let result = contains_any(text, terms) + echo "{label}: {result}" +} + main { - if contains_any("Hello World", ["World", "Something"]) { - echo "Succeeded" - } -} + test_multiple("None", "Hello World", ["Other", "Other"]) + test_multiple("Left", "Hello World", ["Hello", "Other"]) + test_multiple("Right", "Hello World", ["Other", "World"]) + test_multiple("Both", "Hello World", ["Hello", "World"]) +} diff --git a/src/tests/stdlib/extract.ab b/src/tests/stdlib/extract.ab index 944e794d..2484db0f 100644 --- a/src/tests/stdlib/extract.ab +++ b/src/tests/stdlib/extract.ab @@ -10,7 +10,9 @@ main { let package = tmpdir + "/" + "filename.tar.gz" if (extract(package, tmpdir)) { - echo "Succeeded" + if dir_exist(tmpdir + "/" + tmpdir) { + echo "Succeeded" + } } trust $ rm -rf "{tmpdir}" $ } From a39b756864a66053e760a5482df4b1b6077712c1 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Thu, 14 Nov 2024 11:03:35 +0100 Subject: [PATCH 04/19] feat(stdlib): extract improved --- src/std/fs.ab | 26 +++++++++--------- src/std/text.ab | 37 +++++++++++++++++++++++++- src/tests/stdlib/contains_any_regex.ab | 19 +++++++++++++ src/tests/stdlib/contains_regex.ab | 7 +++++ 4 files changed, 75 insertions(+), 14 deletions(-) create mode 100644 src/tests/stdlib/contains_any_regex.ab create mode 100644 src/tests/stdlib/contains_regex.ab diff --git a/src/std/fs.ab b/src/std/fs.ab index c6895f08..68ed0673 100644 --- a/src/std/fs.ab +++ b/src/std/fs.ab @@ -1,4 +1,4 @@ -import { contains_any, join, replace_regex, split } from "std/text" +import { contains_any_regex, join, replace_regex, split } from "std/text" /// Checks if a directory exists. pub fun dir_exist(path) { @@ -115,18 +115,18 @@ pub fun glob(path: Text): [Text]? { pub fun extract(path: Text, target: Text): Bool{ if file_exist(path) { if { - contains_any(path, [".tar.bz2", ".tbz", ".tbz2"]): trust $ tar xvjf "{path}" -C "{target}" $ - contains_any(path, [".tar.gz", ".tgz"]): trust $ tar xzf "{path}" -C "{target}" $ - contains_any(path, [".tar.xz", ".txz"]): trust $ tar xJf "{path}" -C "{target}" $ - contains_any(path, [".bz2"]): trust $ bunzip2 "{path}" $ - contains_any(path, [".deb"]): trust $ dpkg-deb -xv "{path}" "{target}" $ - contains_any(path, [".gz"]): trust $ gunzip "{path}" $ - contains_any(path, [".rar"]): trust $ unrar x "{path}" "{target}" $ - contains_any(path, [".rpm"]): trust $ rpm2cpio "{path}" | cpio -idm{{}} $ - contains_any(path, [".tar"]): trust $ tar xf "{path}" -C "{target}" $ - contains_any(path, [".xz"]): trust $ xz --decompress "{path}" $ - contains_any(path, [".7z"]): trust $ 7z -y "{path}" -o "{target}" $ - contains_any(path, [".zip", ".war", ".jar"]): trust $ unzip "{path}" -d "{target}" $ + contains_any_regex(path, ["/.tar.bz2$/g", "/.tbz$/g", "/.tbz2$/g"]): trust $ tar xvjf "{path}" -C "{target}" $ + contains_any_regex(path, ["/.tar.gz$/g", "/.tgz$/g"]): trust $ tar xzf "{path}" -C "{target}" $ + contains_any_regex(path, ["/.tar.xz$/g", "/.txz$/g"]): trust $ tar xJf "{path}" -C "{target}" $ + contains_any_regex(path, ["/.bz2$/g"]): trust $ bunzip2 "{path}" $ + contains_any_regex(path, ["/.deb$/g"]): trust $ dpkg-deb -xv "{path}" "{target}" $ + contains_any_regex(path, ["/.gz$/g"]): trust $ gunzip "{path}" $ + contains_any_regex(path, ["/.rar$/g"]): trust $ unrar x "{path}" "{target}" $ + contains_any_regex(path, ["/.rpm$/g"]): trust $ rpm2cpio "{path}" | cpio -idm $ + contains_any_regex(path, ["/.tar$/g"]): trust $ tar xf "{path}" -C "{target}" $ + contains_any_regex(path, ["/.xz$/g"]): trust $ xz --decompress "{path}" $ + contains_any_regex(path, ["/.7z$/g"]): trust $ 7z -y "{path}" -o "{target}" $ + contains_any_regex(path, ["/.zip$/g", "/.war$/g", "/.jar$/g"]): trust $ unzip "{path}" -d "{target}" $ else { echo "Error: Unsupported file type" return false diff --git a/src/std/text.ab b/src/std/text.ab index d37c0236..2363359c 100644 --- a/src/std/text.ab +++ b/src/std/text.ab @@ -21,7 +21,7 @@ pub fun replace_regex(source: Text, search: Text, replace: Text, extended: Bool // contains "GNU sed". $ re='\bCopyright\b.+\bFree Software Foundation\b'; [[ \$(sed --version 2>/dev/null) =~ \$re ]] $ let flag = status == 0 then "-r" else "-E" - return $ echo "{source}" | sed {flag} -e "s/{search}/{replace}/g" $ + return $ echo "{source}" | sed "{flag}" -e "s/{search}/{replace}/g" $ } else { return $ echo "{source}" | sed -e "s/{search}/{replace}/g" $ } @@ -128,6 +128,41 @@ pub fun contains_all(text: Text, terms: [Text]): Bool { return false } +/// Search all occurences of a regex pattern in the content. +/// +/// Function uses `sed` +pub fun contains_regex(source: Text, search: Text, extended: Bool = false): Bool { + trust { + let output = "" + if extended { + // GNU sed versions 4.0 through 4.2 support extended regex syntax, + // but only via the "-r" option; use that if the version information + // contains "GNU sed". + $ re='\bCopyright\b.+\bFree Software Foundation\b'; [[ \$(sed --version 2>/dev/null) =~ \$re ]] $ + let flag = status == 0 then "-r" else "-E" + output = $ echo "{source}" | sed "{flag}" -e "{search}" $ + } else { + output = $ echo "{source}" | sed -e "{search}" $ + } + if output == "" { + echo output + return true + } + } + return false +} + +/// Checks if an array value (with regular expression) is in the text. +pub fun contains_any_regex(text: Text, terms: [Text]): Bool { + for term in terms { + if contains_regex(text, term) { + return true + } + } + + return false +} + /// Reverses text using `rev`. pub fun reverse(text: Text): Text { return trust $ echo "{text}" | rev $ diff --git a/src/tests/stdlib/contains_any_regex.ab b/src/tests/stdlib/contains_any_regex.ab new file mode 100644 index 00000000..b2f1838c --- /dev/null +++ b/src/tests/stdlib/contains_any_regex.ab @@ -0,0 +1,19 @@ +import { contains_any_regex } from "std/text" + +// Output +// None: 0 +// Right: 1 +// Left: 1 +// Both: 1 + +fun test_multiple(label, text, terms) { + let result = contains_any_regex(text, terms) + echo "{label}: {result}" +} + +main { + test_multiple("None", "Hello World", ["/Other/g", "/Other$/g"]) + test_multiple("Right", "Hello World", ["/Other/g", "/World$/g"]) + test_multiple("Left", "Hello World", ["/^Hello/g", "/Other/g"]) + test_multiple("Both", "Hello World", ["/^Hello/g", "/$World/g"]) +} diff --git a/src/tests/stdlib/contains_regex.ab b/src/tests/stdlib/contains_regex.ab new file mode 100644 index 00000000..573f2716 --- /dev/null +++ b/src/tests/stdlib/contains_regex.ab @@ -0,0 +1,7 @@ +import { contains_regex } from "std/text" + +main { + if contains_regex("Hello World", "/World$/g") { + echo "Succeeded" + } +} From 1bd0421fa04be98eda46a864f7c9bc7f7b22a8fd Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Thu, 14 Nov 2024 11:08:01 +0100 Subject: [PATCH 05/19] feat(stdlib): extract improved --- src/std/text.ab | 1 - 1 file changed, 1 deletion(-) diff --git a/src/std/text.ab b/src/std/text.ab index 2363359c..afd8e373 100644 --- a/src/std/text.ab +++ b/src/std/text.ab @@ -145,7 +145,6 @@ pub fun contains_regex(source: Text, search: Text, extended: Bool = false): Bool output = $ echo "{source}" | sed -e "{search}" $ } if output == "" { - echo output return true } } From 2368aef74a4f253c8863ab39d0cd9db0baa80a9b Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Thu, 14 Nov 2024 15:26:32 +0100 Subject: [PATCH 06/19] Update src/std/fs.ab Co-authored-by: Phoenix Himself --- src/std/fs.ab | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/std/fs.ab b/src/std/fs.ab index 68ed0673..57d71e39 100644 --- a/src/std/fs.ab +++ b/src/std/fs.ab @@ -115,18 +115,18 @@ pub fun glob(path: Text): [Text]? { pub fun extract(path: Text, target: Text): Bool{ if file_exist(path) { if { - contains_any_regex(path, ["/.tar.bz2$/g", "/.tbz$/g", "/.tbz2$/g"]): trust $ tar xvjf "{path}" -C "{target}" $ - contains_any_regex(path, ["/.tar.gz$/g", "/.tgz$/g"]): trust $ tar xzf "{path}" -C "{target}" $ - contains_any_regex(path, ["/.tar.xz$/g", "/.txz$/g"]): trust $ tar xJf "{path}" -C "{target}" $ - contains_any_regex(path, ["/.bz2$/g"]): trust $ bunzip2 "{path}" $ - contains_any_regex(path, ["/.deb$/g"]): trust $ dpkg-deb -xv "{path}" "{target}" $ - contains_any_regex(path, ["/.gz$/g"]): trust $ gunzip "{path}" $ - contains_any_regex(path, ["/.rar$/g"]): trust $ unrar x "{path}" "{target}" $ - contains_any_regex(path, ["/.rpm$/g"]): trust $ rpm2cpio "{path}" | cpio -idm $ - contains_any_regex(path, ["/.tar$/g"]): trust $ tar xf "{path}" -C "{target}" $ - contains_any_regex(path, ["/.xz$/g"]): trust $ xz --decompress "{path}" $ - contains_any_regex(path, ["/.7z$/g"]): trust $ 7z -y "{path}" -o "{target}" $ - contains_any_regex(path, ["/.zip$/g", "/.war$/g", "/.jar$/g"]): trust $ unzip "{path}" -d "{target}" $ + contains_any_regex(path, ["/.tar.bz2$/g", "/.tbz$/g", "/.tbz2$/g"]): $ tar xvjf "{path}" -C "{target}" $? + contains_any_regex(path, ["/.tar.gz$/g", "/.tgz$/g"]): $ tar xzf "{path}" -C "{target}" $? + contains_any_regex(path, ["/.tar.xz$/g", "/.txz$/g"]): $ tar xJf "{path}" -C "{target}" $? + contains_any_regex(path, ["/.bz2$/g"]): $ bunzip2 "{path}" $? + contains_any_regex(path, ["/.deb$/g"]): $ dpkg-deb -xv "{path}" "{target}" $? + contains_any_regex(path, ["/.gz$/g"]): $ gunzip "{path}" $? + contains_any_regex(path, ["/.rar$/g"]): $ unrar x "{path}" "{target}" $? + contains_any_regex(path, ["/.rpm$/g"]): $ rpm2cpio "{path}" | cpio -idm $? + contains_any_regex(path, ["/.tar$/g"]): $ tar xf "{path}" -C "{target}" $? + contains_any_regex(path, ["/.xz$/g"]): $ xz --decompress "{path}" $? + contains_any_regex(path, ["/.7z$/g"]): $ 7z -y "{path}" -o "{target}" $? + contains_any_regex(path, ["/.zip$/g", "/.war$/g", "/.jar$/g"]): $ unzip "{path}" -d "{target}" $? else { echo "Error: Unsupported file type" return false From c1c1c443d2614634dbcabe07b6a76353606d46b5 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Thu, 14 Nov 2024 17:39:19 +0100 Subject: [PATCH 07/19] Update src/std/fs.ab Co-authored-by: Phoenix Himself --- src/std/fs.ab | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/std/fs.ab b/src/std/fs.ab index 57d71e39..ae775193 100644 --- a/src/std/fs.ab +++ b/src/std/fs.ab @@ -112,7 +112,7 @@ pub fun glob(path: Text): [Text]? { /// 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{ +pub fun extract(path: Text, target: Text): Bool? { if file_exist(path) { if { contains_any_regex(path, ["/.tar.bz2$/g", "/.tbz$/g", "/.tbz2$/g"]): $ tar xvjf "{path}" -C "{target}" $? From 0739645bf7f93b1df1539d9f1f5680fa272f8b9a Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Thu, 14 Nov 2024 17:46:13 +0100 Subject: [PATCH 08/19] fix(test): done --- src/tests/stdlib/extract.ab | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/tests/stdlib/extract.ab b/src/tests/stdlib/extract.ab index 2484db0f..b3ac1361 100644 --- a/src/tests/stdlib/extract.ab +++ b/src/tests/stdlib/extract.ab @@ -9,10 +9,13 @@ main { trust $ rm "test.txt" $ let package = tmpdir + "/" + "filename.tar.gz" - if (extract(package, tmpdir)) { - if dir_exist(tmpdir + "/" + tmpdir) { - echo "Succeeded" - } + extract(package, tmpdir) failed { + echo "Error" } + + if dir_exist(tmpdir + "/" + tmpdir) { + echo "Succeeded" + } + trust $ rm -rf "{tmpdir}" $ } From cd3fc4baebb0dfcdcccd953276351bef8c054cdf Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Fri, 15 Nov 2024 10:46:48 +0100 Subject: [PATCH 09/19] feat(tests): imprroved --- src/std/text.ab | 12 +++--------- src/tests/stdlib/contains_any.ab | 2 ++ src/tests/stdlib/contains_any_regex.ab | 2 ++ 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/std/text.ab b/src/std/text.ab index afd8e373..e56504e4 100644 --- a/src/std/text.ab +++ b/src/std/text.ab @@ -113,19 +113,13 @@ pub fun contains_any(text: Text, terms: [Text]): Bool { /// Checks if all the arrays values are in the string pub fun contains_all(text: Text, terms: [Text]): Bool { - let total = len terms - let i = 0 for term in terms { - if contains(text, term) { - i += 1 + if not contains(text, term) { + return false } } - if i == total { - return true - } - - return false + return true } /// Search all occurences of a regex pattern in the content. diff --git a/src/tests/stdlib/contains_any.ab b/src/tests/stdlib/contains_any.ab index 8e39c451..9c9b4898 100644 --- a/src/tests/stdlib/contains_any.ab +++ b/src/tests/stdlib/contains_any.ab @@ -1,6 +1,7 @@ import { contains_any } from "std/text" // Output +// Empty: 1 // None: 0 // Left: 1 // Right: 1 @@ -12,6 +13,7 @@ fun test_multiple(label, text, terms) { } main { + test_multiple("Empty", "Hello World", [Text]) test_multiple("None", "Hello World", ["Other", "Other"]) test_multiple("Left", "Hello World", ["Hello", "Other"]) test_multiple("Right", "Hello World", ["Other", "World"]) diff --git a/src/tests/stdlib/contains_any_regex.ab b/src/tests/stdlib/contains_any_regex.ab index b2f1838c..da6cd2cf 100644 --- a/src/tests/stdlib/contains_any_regex.ab +++ b/src/tests/stdlib/contains_any_regex.ab @@ -1,6 +1,7 @@ import { contains_any_regex } from "std/text" // Output +// Empty: 1 // None: 0 // Right: 1 // Left: 1 @@ -12,6 +13,7 @@ fun test_multiple(label, text, terms) { } main { + test_multiple("Empty", "Hello World", [Text]) test_multiple("None", "Hello World", ["/Other/g", "/Other$/g"]) test_multiple("Right", "Hello World", ["/Other/g", "/World$/g"]) test_multiple("Left", "Hello World", ["/^Hello/g", "/Other/g"]) From 1a4f581360c95c90571b2dd7f7ce421d9c999055 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Fri, 15 Nov 2024 10:51:41 +0100 Subject: [PATCH 10/19] feat(tests): imprroved --- src/tests/stdlib/contains_any.ab | 2 +- src/tests/stdlib/contains_any_regex.ab | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tests/stdlib/contains_any.ab b/src/tests/stdlib/contains_any.ab index 9c9b4898..47992e38 100644 --- a/src/tests/stdlib/contains_any.ab +++ b/src/tests/stdlib/contains_any.ab @@ -1,7 +1,7 @@ import { contains_any } from "std/text" // Output -// Empty: 1 +// Empty: 0 // None: 0 // Left: 1 // Right: 1 diff --git a/src/tests/stdlib/contains_any_regex.ab b/src/tests/stdlib/contains_any_regex.ab index da6cd2cf..e7a80ecd 100644 --- a/src/tests/stdlib/contains_any_regex.ab +++ b/src/tests/stdlib/contains_any_regex.ab @@ -1,7 +1,7 @@ import { contains_any_regex } from "std/text" // Output -// Empty: 1 +// Empty: 0 // None: 0 // Right: 1 // Left: 1 From 80a7ad4c9ce0b156a5e0e3604196a770ee336a2b Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Fri, 15 Nov 2024 17:03:13 +0100 Subject: [PATCH 11/19] Update src/std/fs.ab Co-authored-by: Phoenix Himself --- src/std/fs.ab | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/std/fs.ab b/src/std/fs.ab index ae775193..c902f581 100644 --- a/src/std/fs.ab +++ b/src/std/fs.ab @@ -112,7 +112,7 @@ pub fun glob(path: Text): [Text]? { /// 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? { +pub fun extract(path: Text, target: Text): Null? { if file_exist(path) { if { contains_any_regex(path, ["/.tar.bz2$/g", "/.tbz$/g", "/.tbz2$/g"]): $ tar xvjf "{path}" -C "{target}" $? From add743c375340d03bbc50e9e7835766207bf434f Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Fri, 15 Nov 2024 17:03:25 +0100 Subject: [PATCH 12/19] Update src/std/fs.ab Co-authored-by: Phoenix Himself --- src/std/fs.ab | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/std/fs.ab b/src/std/fs.ab index c902f581..d66503ea 100644 --- a/src/std/fs.ab +++ b/src/std/fs.ab @@ -129,13 +129,11 @@ pub fun extract(path: Text, target: Text): Null? { contains_any_regex(path, ["/.zip$/g", "/.war$/g", "/.jar$/g"]): $ unzip "{path}" -d "{target}" $? else { echo "Error: Unsupported file type" - return false + fail 3 } } } else { echo "Error: File not found" - return false + fail 2 } - - return true } From a2b477577c61f62ac5c5bcaba8c418cd17c3873b Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Fri, 15 Nov 2024 17:12:14 +0100 Subject: [PATCH 13/19] feat(regex): g match --- src/std/fs.ab | 24 ++++++++++++------------ src/std/text.ab | 12 ++++++------ src/tests/stdlib/contains_any_regex.ab | 8 ++++---- src/tests/stdlib/contains_regex.ab | 2 +- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/std/fs.ab b/src/std/fs.ab index d66503ea..83625aec 100644 --- a/src/std/fs.ab +++ b/src/std/fs.ab @@ -115,18 +115,18 @@ pub fun glob(path: Text): [Text]? { pub fun extract(path: Text, target: Text): Null? { if file_exist(path) { if { - contains_any_regex(path, ["/.tar.bz2$/g", "/.tbz$/g", "/.tbz2$/g"]): $ tar xvjf "{path}" -C "{target}" $? - contains_any_regex(path, ["/.tar.gz$/g", "/.tgz$/g"]): $ tar xzf "{path}" -C "{target}" $? - contains_any_regex(path, ["/.tar.xz$/g", "/.txz$/g"]): $ tar xJf "{path}" -C "{target}" $? - contains_any_regex(path, ["/.bz2$/g"]): $ bunzip2 "{path}" $? - contains_any_regex(path, ["/.deb$/g"]): $ dpkg-deb -xv "{path}" "{target}" $? - contains_any_regex(path, ["/.gz$/g"]): $ gunzip "{path}" $? - contains_any_regex(path, ["/.rar$/g"]): $ unrar x "{path}" "{target}" $? - contains_any_regex(path, ["/.rpm$/g"]): $ rpm2cpio "{path}" | cpio -idm $? - contains_any_regex(path, ["/.tar$/g"]): $ tar xf "{path}" -C "{target}" $? - contains_any_regex(path, ["/.xz$/g"]): $ xz --decompress "{path}" $? - contains_any_regex(path, ["/.7z$/g"]): $ 7z -y "{path}" -o "{target}" $? - contains_any_regex(path, ["/.zip$/g", "/.war$/g", "/.jar$/g"]): $ unzip "{path}" -d "{target}" $? + contains_any_regex(path, ["tar.bz2$", "tbz$", "tbz2$"]): $ tar xvjf "{path}" -C "{target}" $? + contains_any_regex(path, ["tar.gz$", "tgz$"]): $ tar xzf "{path}" -C "{target}" $? + contains_any_regex(path, ["tar.xz$", "txz$"]): $ tar xJf "{path}" -C "{target}" $? + contains_any_regex(path, ["bz2$"]): $ bunzip2 "{path}" $? + contains_any_regex(path, ["deb$"]): $ dpkg-deb -xv "{path}" "{target}" $? + contains_any_regex(path, ["gz$"]): $ gunzip "{path}" $? + contains_any_regex(path, ["rar$"]): $ unrar x "{path}" "{target}" $? + contains_any_regex(path, ["rpm$"]): $ rpm2cpio "{path}" | cpio -idm $? + contains_any_regex(path, ["tar$"]): $ tar xf "{path}" -C "{target}" $? + contains_any_regex(path, ["xz$"]): $ xz --decompress "{path}" $? + contains_any_regex(path, ["7z$"]): $ 7z -y "{path}" -o "{target}" $? + contains_any_regex(path, ["zip$", "war$", "jar$"]): $ unzip "{path}" -d "{target}" $? else { echo "Error: Unsupported file type" fail 3 diff --git a/src/std/text.ab b/src/std/text.ab index e56504e4..459b5a25 100644 --- a/src/std/text.ab +++ b/src/std/text.ab @@ -122,10 +122,10 @@ pub fun contains_all(text: Text, terms: [Text]): Bool { return true } -/// Search all occurences of a regex pattern in the content. +/// Match all occurences of a regex pattern. /// /// Function uses `sed` -pub fun contains_regex(source: Text, search: Text, extended: Bool = false): Bool { +pub fun contains_regex(source: Text, search: Text, extended: Bool = false, match: Text = "g"): Bool { trust { let output = "" if extended { @@ -134,9 +134,9 @@ pub fun contains_regex(source: Text, search: Text, extended: Bool = false): Bool // contains "GNU sed". $ re='\bCopyright\b.+\bFree Software Foundation\b'; [[ \$(sed --version 2>/dev/null) =~ \$re ]] $ let flag = status == 0 then "-r" else "-E" - output = $ echo "{source}" | sed "{flag}" -e "{search}" $ + output = $ echo "{source}" | sed "{flag}" -e "/{search}/{match}" $ } else { - output = $ echo "{source}" | sed -e "{search}" $ + output = $ echo "{source}" | sed -e "/{search}/{match}" $ } if output == "" { return true @@ -146,9 +146,9 @@ pub fun contains_regex(source: Text, search: Text, extended: Bool = false): Bool } /// Checks if an array value (with regular expression) is in the text. -pub fun contains_any_regex(text: Text, terms: [Text]): Bool { +pub fun contains_any_regex(text: Text, terms: [Text], match: Text = "g"): Bool { for term in terms { - if contains_regex(text, term) { + if contains_regex(text, term, false, match) { return true } } diff --git a/src/tests/stdlib/contains_any_regex.ab b/src/tests/stdlib/contains_any_regex.ab index e7a80ecd..c01b2354 100644 --- a/src/tests/stdlib/contains_any_regex.ab +++ b/src/tests/stdlib/contains_any_regex.ab @@ -14,8 +14,8 @@ fun test_multiple(label, text, terms) { main { test_multiple("Empty", "Hello World", [Text]) - test_multiple("None", "Hello World", ["/Other/g", "/Other$/g"]) - test_multiple("Right", "Hello World", ["/Other/g", "/World$/g"]) - test_multiple("Left", "Hello World", ["/^Hello/g", "/Other/g"]) - test_multiple("Both", "Hello World", ["/^Hello/g", "/$World/g"]) + test_multiple("None", "Hello World", ["Other", "Other$"]) + test_multiple("Right", "Hello World", ["Other", "World$"]) + test_multiple("Left", "Hello World", ["^Hello", "Other"]) + test_multiple("Both", "Hello World", ["^Hello", "$World"]) } diff --git a/src/tests/stdlib/contains_regex.ab b/src/tests/stdlib/contains_regex.ab index 573f2716..614b1cd5 100644 --- a/src/tests/stdlib/contains_regex.ab +++ b/src/tests/stdlib/contains_regex.ab @@ -1,7 +1,7 @@ import { contains_regex } from "std/text" main { - if contains_regex("Hello World", "/World$/g") { + if contains_regex("Hello World", "World$") { echo "Succeeded" } } From 96282f4c435c6983095c8c9c001b16b49b6ae28d Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Tue, 19 Nov 2024 10:33:41 +0100 Subject: [PATCH 14/19] feat(stdlib): match --- src/std/text.ab | 2 +- src/tests/stdlib/contains_regex.ab | 7 ------- src/tests/stdlib/match_regex.ab | 7 +++++++ 3 files changed, 8 insertions(+), 8 deletions(-) delete mode 100644 src/tests/stdlib/contains_regex.ab create mode 100644 src/tests/stdlib/match_regex.ab diff --git a/src/std/text.ab b/src/std/text.ab index 459b5a25..2f8db3c9 100644 --- a/src/std/text.ab +++ b/src/std/text.ab @@ -125,7 +125,7 @@ pub fun contains_all(text: Text, terms: [Text]): Bool { /// Match all occurences of a regex pattern. /// /// Function uses `sed` -pub fun contains_regex(source: Text, search: Text, extended: Bool = false, match: Text = "g"): Bool { +pub fun match_regex(source: Text, search: Text, extended: Bool = false, match: Text = "g"): Bool { trust { let output = "" if extended { diff --git a/src/tests/stdlib/contains_regex.ab b/src/tests/stdlib/contains_regex.ab deleted file mode 100644 index 614b1cd5..00000000 --- a/src/tests/stdlib/contains_regex.ab +++ /dev/null @@ -1,7 +0,0 @@ -import { contains_regex } from "std/text" - -main { - if contains_regex("Hello World", "World$") { - echo "Succeeded" - } -} diff --git a/src/tests/stdlib/match_regex.ab b/src/tests/stdlib/match_regex.ab new file mode 100644 index 00000000..0b4788db --- /dev/null +++ b/src/tests/stdlib/match_regex.ab @@ -0,0 +1,7 @@ +import { match_regex } from "std/text" + +main { + if match_regex("Hello World", "World$") { + echo "Succeeded" + } +} From 28cc7d1ba966dcec43c7f28ac565242524df5e72 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Tue, 19 Nov 2024 10:39:34 +0100 Subject: [PATCH 15/19] feat(stdlib): match --- src/std/text.ab | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/std/text.ab b/src/std/text.ab index 2f8db3c9..7b0941ac 100644 --- a/src/std/text.ab +++ b/src/std/text.ab @@ -148,7 +148,7 @@ pub fun match_regex(source: Text, search: Text, extended: Bool = false, match: T /// Checks if an array value (with regular expression) is in the text. pub fun contains_any_regex(text: Text, terms: [Text], match: Text = "g"): Bool { for term in terms { - if contains_regex(text, term, false, match) { + if match_regex(text, term, false, match) { return true } } From 27e40596939eb0311de4b2a539f378dd7da9fa32 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Wed, 20 Nov 2024 11:22:30 +0100 Subject: [PATCH 16/19] feat(review): done --- src/std/fs.ab | 26 +++++++++---------- src/std/text.ab | 10 +++---- src/tests/stdlib/contains_all.ab | 23 +++++++++------- ...ntains_any_regex.ab => match_any_regex.ab} | 4 +-- 4 files changed, 34 insertions(+), 29 deletions(-) rename src/tests/stdlib/{contains_any_regex.ab => match_any_regex.ab} (82%) diff --git a/src/std/fs.ab b/src/std/fs.ab index 83625aec..1c0bcf06 100644 --- a/src/std/fs.ab +++ b/src/std/fs.ab @@ -1,4 +1,4 @@ -import { contains_any_regex, join, replace_regex, split } from "std/text" +import { match_any_regex, match_regex, join, replace_regex, split } from "std/text" /// Checks if a directory exists. pub fun dir_exist(path) { @@ -115,18 +115,18 @@ pub fun glob(path: Text): [Text]? { pub fun extract(path: Text, target: Text): Null? { if file_exist(path) { if { - contains_any_regex(path, ["tar.bz2$", "tbz$", "tbz2$"]): $ tar xvjf "{path}" -C "{target}" $? - contains_any_regex(path, ["tar.gz$", "tgz$"]): $ tar xzf "{path}" -C "{target}" $? - contains_any_regex(path, ["tar.xz$", "txz$"]): $ tar xJf "{path}" -C "{target}" $? - contains_any_regex(path, ["bz2$"]): $ bunzip2 "{path}" $? - contains_any_regex(path, ["deb$"]): $ dpkg-deb -xv "{path}" "{target}" $? - contains_any_regex(path, ["gz$"]): $ gunzip "{path}" $? - contains_any_regex(path, ["rar$"]): $ unrar x "{path}" "{target}" $? - contains_any_regex(path, ["rpm$"]): $ rpm2cpio "{path}" | cpio -idm $? - contains_any_regex(path, ["tar$"]): $ tar xf "{path}" -C "{target}" $? - contains_any_regex(path, ["xz$"]): $ xz --decompress "{path}" $? - contains_any_regex(path, ["7z$"]): $ 7z -y "{path}" -o "{target}" $? - contains_any_regex(path, ["zip$", "war$", "jar$"]): $ unzip "{path}" -d "{target}" $? + match_regex(path, "\.\(tar\.bz2\|tbz\|tbz2\)$"): $ tar xvjf "{path}" -C "{target}" $? + match_any_regex(path, ["tar.gz$", "tgz$"]): $ tar xzf "{path}" -C "{target}" $? + match_any_regex(path, ["tar.xz$", "txz$"]): $ tar xJf "{path}" -C "{target}" $? + match_any_regex(path, ["bz2$"]): $ bunzip2 "{path}" $? + match_any_regex(path, ["deb$"]): $ dpkg-deb -xv "{path}" "{target}" $? + match_any_regex(path, ["gz$"]): $ gunzip "{path}" $? + match_any_regex(path, ["rar$"]): $ unrar x "{path}" "{target}" $? + match_any_regex(path, ["rpm$"]): $ rpm2cpio "{path}" | cpio -idm $? + match_any_regex(path, ["tar$"]): $ tar xf "{path}" -C "{target}" $? + match_any_regex(path, ["xz$"]): $ xz --decompress "{path}" $? + match_any_regex(path, ["7z$"]): $ 7z -y "{path}" -o "{target}" $? + match_any_regex(path, ["zip$", "war$", "jar$"]): $ unzip "{path}" -d "{target}" $? else { echo "Error: Unsupported file type" fail 3 diff --git a/src/std/text.ab b/src/std/text.ab index 7b0941ac..559fb55c 100644 --- a/src/std/text.ab +++ b/src/std/text.ab @@ -125,7 +125,7 @@ pub fun contains_all(text: Text, terms: [Text]): Bool { /// Match all occurences of a regex pattern. /// /// Function uses `sed` -pub fun match_regex(source: Text, search: Text, extended: Bool = false, match: Text = "g"): Bool { +pub fun match_regex(source: Text, search: Text, extended: Bool = false, match: Text = "p"): Bool { trust { let output = "" if extended { @@ -134,11 +134,11 @@ pub fun match_regex(source: Text, search: Text, extended: Bool = false, match: T // contains "GNU sed". $ re='\bCopyright\b.+\bFree Software Foundation\b'; [[ \$(sed --version 2>/dev/null) =~ \$re ]] $ let flag = status == 0 then "-r" else "-E" - output = $ echo "{source}" | sed "{flag}" -e "/{search}/{match}" $ + output = $ echo "{source}" | sed "{flag}" -ne "/{search}/{match}" $ } else { - output = $ echo "{source}" | sed -e "/{search}/{match}" $ + output = $ echo "{source}" | sed -ne "/{search}/{match}" $ } - if output == "" { + if output != "" { return true } } @@ -146,7 +146,7 @@ pub fun match_regex(source: Text, search: Text, extended: Bool = false, match: T } /// Checks if an array value (with regular expression) is in the text. -pub fun contains_any_regex(text: Text, terms: [Text], match: Text = "g"): Bool { +pub fun match_any_regex(text: Text, terms: [Text], match: Text = "p"): Bool { for term in terms { if match_regex(text, term, false, match) { return true diff --git a/src/tests/stdlib/contains_all.ab b/src/tests/stdlib/contains_all.ab index 74e7dcbe..99ad1dfc 100644 --- a/src/tests/stdlib/contains_all.ab +++ b/src/tests/stdlib/contains_all.ab @@ -1,15 +1,20 @@ import { contains_all } from "std/text" // Output -// Succeeded -// Succeeded +// Empty: 0 +// None: 0 +// Left: 1 +// Right: 1 +// Both: 1 -main { - if contains_all("Hello World", ["World", "Something"]) == false { - echo "Succeeded" - } +fun test_multiple(label, text, terms) { + let result = contains_all(text, terms) + echo "{label}: {result}" +} - if contains_all("Hello World", ["World", "Hello"]) { - echo "Succeeded" - } +main { + test_multiple("None", "Hello World", ["Other", "Other"]) + test_multiple("Left", "Hello World", ["World", "Something"]) + test_multiple("Right", "Hello World", ["World", "Hello"]) + test_multiple("Both", "Hello World", ["Hello", "World"]) } diff --git a/src/tests/stdlib/contains_any_regex.ab b/src/tests/stdlib/match_any_regex.ab similarity index 82% rename from src/tests/stdlib/contains_any_regex.ab rename to src/tests/stdlib/match_any_regex.ab index c01b2354..cefa20a8 100644 --- a/src/tests/stdlib/contains_any_regex.ab +++ b/src/tests/stdlib/match_any_regex.ab @@ -1,4 +1,4 @@ -import { contains_any_regex } from "std/text" +import { match_any_regex } from "std/text" // Output // Empty: 0 @@ -8,7 +8,7 @@ import { contains_any_regex } from "std/text" // Both: 1 fun test_multiple(label, text, terms) { - let result = contains_any_regex(text, terms) + let result = match_any_regex(text, terms) echo "{label}: {result}" } From 0fa3d883f29e068b9e5d31fc7bb466afdc96237b Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Wed, 20 Nov 2024 11:27:18 +0100 Subject: [PATCH 17/19] feat(review): done --- src/tests/stdlib/contains_all.ab | 1 - 1 file changed, 1 deletion(-) diff --git a/src/tests/stdlib/contains_all.ab b/src/tests/stdlib/contains_all.ab index 99ad1dfc..e0e45fda 100644 --- a/src/tests/stdlib/contains_all.ab +++ b/src/tests/stdlib/contains_all.ab @@ -1,7 +1,6 @@ import { contains_all } from "std/text" // Output -// Empty: 0 // None: 0 // Left: 1 // Right: 1 From 47f22b16c78f299f68d3c0ac13b1de2e936c26ba Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Wed, 20 Nov 2024 11:32:22 +0100 Subject: [PATCH 18/19] feat(review): done --- src/tests/stdlib/contains_all.ab | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tests/stdlib/contains_all.ab b/src/tests/stdlib/contains_all.ab index e0e45fda..491f447b 100644 --- a/src/tests/stdlib/contains_all.ab +++ b/src/tests/stdlib/contains_all.ab @@ -2,7 +2,7 @@ import { contains_all } from "std/text" // Output // None: 0 -// Left: 1 +// One: 0 // Right: 1 // Both: 1 @@ -13,7 +13,7 @@ fun test_multiple(label, text, terms) { main { test_multiple("None", "Hello World", ["Other", "Other"]) - test_multiple("Left", "Hello World", ["World", "Something"]) + test_multiple("One", "Hello World", ["World", "Something"]) test_multiple("Right", "Hello World", ["World", "Hello"]) test_multiple("Both", "Hello World", ["Hello", "World"]) } From b7ceaf86da1faea1e4a1d9fbff66273217776843 Mon Sep 17 00:00:00 2001 From: Daniele Scasciafratte Date: Mon, 25 Nov 2024 11:19:37 +0100 Subject: [PATCH 19/19] feat(review): done --- src/std/fs.ab | 24 ++++++++++++------------ src/std/text.ab | 11 ++++++----- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/std/fs.ab b/src/std/fs.ab index 1c0bcf06..f4b0749a 100644 --- a/src/std/fs.ab +++ b/src/std/fs.ab @@ -1,4 +1,4 @@ -import { match_any_regex, match_regex, join, replace_regex, split } from "std/text" +import { match_regex, join, replace_regex, split } from "std/text" /// Checks if a directory exists. pub fun dir_exist(path) { @@ -116,17 +116,17 @@ pub fun extract(path: Text, target: Text): Null? { if file_exist(path) { if { match_regex(path, "\.\(tar\.bz2\|tbz\|tbz2\)$"): $ tar xvjf "{path}" -C "{target}" $? - match_any_regex(path, ["tar.gz$", "tgz$"]): $ tar xzf "{path}" -C "{target}" $? - match_any_regex(path, ["tar.xz$", "txz$"]): $ tar xJf "{path}" -C "{target}" $? - match_any_regex(path, ["bz2$"]): $ bunzip2 "{path}" $? - match_any_regex(path, ["deb$"]): $ dpkg-deb -xv "{path}" "{target}" $? - match_any_regex(path, ["gz$"]): $ gunzip "{path}" $? - match_any_regex(path, ["rar$"]): $ unrar x "{path}" "{target}" $? - match_any_regex(path, ["rpm$"]): $ rpm2cpio "{path}" | cpio -idm $? - match_any_regex(path, ["tar$"]): $ tar xf "{path}" -C "{target}" $? - match_any_regex(path, ["xz$"]): $ xz --decompress "{path}" $? - match_any_regex(path, ["7z$"]): $ 7z -y "{path}" -o "{target}" $? - match_any_regex(path, ["zip$", "war$", "jar$"]): $ unzip "{path}" -d "{target}" $? + match_regex(path, "\.\(tar\.gz\|tgz\)$"): $ tar xzf "{path}" -C "{target}" $? + match_regex(path, "\.\(tar\.xz\|txz$\)$"): $ tar xJf "{path}" -C "{target}" $? + match_regex(path, "\.bz2$"): $ bunzip2 "{path}" $? + match_regex(path, "\.deb$"): $ dpkg-deb -xv "{path}" "{target}" $? + match_regex(path, "\.gz$"): $ gunzip "{path}" $? + match_regex(path, "\.rar$"): $ unrar x "{path}" "{target}" $? + match_regex(path, "\.rpm$"): $ rpm2cpio "{path}" | cpio -idm $? + match_regex(path, "\.tar$"): $ tar xf "{path}" -C "{target}" $? + match_regex(path, "\.xz$"): $ xz --decompress "{path}" $? + match_regex(path, "\.7z$"): $ 7z -y "{path}" -o "{target}" $? + match_regex(path, "\.\(zip\|war\|jar\)$"): $ unzip "{path}" -d "{target}" $? else { echo "Error: Unsupported file type" fail 3 diff --git a/src/std/text.ab b/src/std/text.ab index 559fb55c..19cdc981 100644 --- a/src/std/text.ab +++ b/src/std/text.ab @@ -125,8 +125,9 @@ pub fun contains_all(text: Text, terms: [Text]): Bool { /// Match all occurences of a regex pattern. /// /// Function uses `sed` -pub fun match_regex(source: Text, search: Text, extended: Bool = false, match: Text = "p"): Bool { +pub fun match_regex(source: Text, search: Text, extended: Bool = false): Bool { trust { + search = replace(search, "/", "\/") let output = "" if extended { // GNU sed versions 4.0 through 4.2 support extended regex syntax, @@ -134,9 +135,9 @@ pub fun match_regex(source: Text, search: Text, extended: Bool = false, match: T // contains "GNU sed". $ re='\bCopyright\b.+\bFree Software Foundation\b'; [[ \$(sed --version 2>/dev/null) =~ \$re ]] $ let flag = status == 0 then "-r" else "-E" - output = $ echo "{source}" | sed "{flag}" -ne "/{search}/{match}" $ + output = $ echo "{source}" | sed "{flag}" -ne "/{search}/p" $ } else { - output = $ echo "{source}" | sed -ne "/{search}/{match}" $ + output = $ echo "{source}" | sed -ne "/{search}/p" $ } if output != "" { return true @@ -146,9 +147,9 @@ pub fun match_regex(source: Text, search: Text, extended: Bool = false, match: T } /// Checks if an array value (with regular expression) is in the text. -pub fun match_any_regex(text: Text, terms: [Text], match: Text = "p"): Bool { +pub fun match_any_regex(text: Text, terms: [Text]): Bool { for term in terms { - if match_regex(text, term, false, match) { + if match_regex(text, term, false) { return true } }