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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

Mte90
Copy link
Member

@Mte90 Mte90 commented Nov 12, 2024

Ref: #449

So I implemented the extract function in amber, required a new function contains_multiple

@Mte90 Mte90 changed the title feat(stdlib): yeah feat(stdlib): extract function Nov 12, 2024
src/std/fs.ab Outdated Show resolved Hide resolved
src/std/text.ab Outdated Show resolved Hide resolved
src/std/text.ab Outdated Show resolved Hide resolved
src/std/fs.ab Outdated Show resolved Hide resolved
src/std/fs.ab Outdated Show resolved Hide resolved
src/std/fs.ab Outdated Show resolved Hide resolved
src/tests/stdlib/contains_multiple.ab Outdated Show resolved Hide resolved
src/tests/stdlib/contains_multiple.ab Outdated Show resolved Hide resolved
src/tests/stdlib/extract.ab Outdated Show resolved Hide resolved
src/tests/stdlib/extract.ab Outdated Show resolved Hide resolved
@Mte90 Mte90 requested a review from hdwalters November 14, 2024 10:03
@Mte90
Copy link
Member Author

Mte90 commented Nov 14, 2024

Implemented new functions for contains and also the regex one with their tests.

@@ -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" $
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed the issue with shellcheck, I don't think that we need a PR just for this

let package = tmpdir + "/" + "filename.tar.gz"

if (extract(package, tmpdir)) {
if dir_exist(tmpdir + "/" + tmpdir) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the package include the full path for this reason the check is like that

src/std/fs.ab Outdated Show resolved Hide resolved
Co-authored-by: Phoenix Himself <[email protected]>
@Mte90 Mte90 requested a review from Ph0enixKM November 14, 2024 14:26
src/std/fs.ab Outdated Show resolved Hide resolved
Co-authored-by: Phoenix Himself <[email protected]>
@Mte90 Mte90 requested a review from Ph0enixKM November 14, 2024 16:40
src/std/text.ab Show resolved Hide resolved
let flag = status == 0 then "-r" else "-E"
output = $ echo "{source}" | sed "{flag}" -e "{search}" $
} else {
output = $ echo "{source}" | sed -e "{search}" $
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to use sed -n (suppress automatic printing), and /.../p to print matching lines:

...
    output = $ echo "{source}" | sed "{flag}" -ne "/{search}/p" $
} else {
    output = $ echo "{source}" | sed -ne "/{search}/p" $
...

} else {
output = $ echo "{source}" | sed -e "{search}" $
}
if output == "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to return true if sed output is not empty:

if output != "" {
    return true
}

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_any_regex(path, ["/.tar.bz2$/g", "/.tbz$/g", "/.tbz2$/g"]): $ tar xvjf "{path}" -C "{target}" $?
Copy link
Contributor

@hdwalters hdwalters Nov 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Several suggestions: (i) forward slashes should be moved into the standard library function, (ii) you don't need the "global" specifier /.../g at all, (iii) \. should be escaped, and (iv) you can merge the multiple extensions into a single expression, with escaped grouping operators \(, \| and \) (as we're using basic not extended sed regular expressions for maximum compatibility):

contains_regex(path, "\(\.tar\.bz2\|\.tbz\|\.tbz2\)$"): $ tar xvjf "{path}" -C "{target}" $?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In which case, we don't really need a contains_any_regex function after all.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that is better to leave to the user how to build the regular expression. In sed you can use also # or other symbols as separatore instead of /, also you can use different endings instead of /g so as it is a regex solution I was thinking that is better to leave to the user how to do what they prefer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Respectfully, I disagree. But I'll have to spend some time composing my reasoning. Will get back to you this evening.

src/tests/stdlib/contains_any.ab Show resolved Hide resolved
src/tests/stdlib/contains_all.ab Show resolved Hide resolved
echo "{label}: {result}"
}

main {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove / and /g:

main {
    test_multiple("Empty", "Hello World", [])
    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"])
}

@@ -0,0 +1,7 @@
import { contains_regex } from "std/text"

main {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove / and /g.

Copy link
Member

@Ph0enixKM Ph0enixKM left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @hdwalters suggestions

@Mte90
Copy link
Member Author

Mte90 commented Nov 15, 2024

I have some doubts about removing /../ as I think that should be the user to choose how to write the regular expression.

@Mte90 Mte90 requested a review from Ph0enixKM November 15, 2024 10:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants