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

Implement standard library glob function #477

Closed
Closed
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: 8 additions & 0 deletions src/std/fs.ab
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * from "std/text"

/// Checks if a directory exists.
pub fun dir_exist(path) {
$[ -d "{path}" ]$ failed {
Expand Down Expand Up @@ -76,3 +78,9 @@ pub fun change_owner(user: Text, path: Text): Bool {

return false
}

/// Finds all files or directories matching a file glob.
pub fun glob(path: Text): [Text]? {
let files = $eval "for file in {path}; do [ -e \\\"\\\$file\\\" ] && echo \\\"\\\$file\\\"; done"$?
return split(files, "\n")
}
22 changes: 22 additions & 0 deletions src/tests/stdlib/glob_missing_file.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * from "std/fs"
import * from "std/text"

// Output
// glob failed

main {
let tmpdir = unsafe $ mktemp -d $
unsafe {
$ touch {tmpdir}/file1 $
$ touch {tmpdir}/file2 $
$ touch {tmpdir}/first\ file\ with\ spaces $
$ touch {tmpdir}/second\ file\ with\ spaces $
}

let files = glob("{tmpdir}/missing*") failed { echo "glob failed" }
loop file in files {
echo replace_regex(file, "^.+\/", "[tmpdir]\/", true)
}

unsafe $ rm -rf {tmpdir} $
}
23 changes: 23 additions & 0 deletions src/tests/stdlib/glob_no_spaces.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * from "std/fs"
import * from "std/text"

// Output
// [tmpdir]/file1
// [tmpdir]/file2

main {
let tmpdir = unsafe $ mktemp -d $
unsafe {
$ touch {tmpdir}/file1 $
$ touch {tmpdir}/file2 $
$ touch {tmpdir}/first\ file\ with\ spaces $
$ touch {tmpdir}/second\ file\ with\ spaces $
}

let files = glob("{tmpdir}/file*") failed { echo "glob failed" }
loop file in files {
echo replace_regex(file, "^.+\/", "[tmpdir]\/", true)
}

unsafe $ rm -rf {tmpdir} $
}
23 changes: 23 additions & 0 deletions src/tests/stdlib/glob_with_spaces.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * from "std/fs"
import * from "std/text"

// Output
// [tmpdir]/first file with spaces
// [tmpdir]/second file with spaces

main {
let tmpdir = unsafe $ mktemp -d $
unsafe {
$ touch {tmpdir}/file1 $
$ touch {tmpdir}/file2 $
$ touch {tmpdir}/first\ file\ with\ spaces $
$ touch {tmpdir}/second\ file\ with\ spaces $
}

let files = glob("{tmpdir}/*with\ spaces*") failed { echo "glob failed" }
loop file in files {
echo replace_regex(file, "^.+\/", "[tmpdir]\/", true)
}

unsafe $ rm -rf {tmpdir} $
}
Loading