-
Notifications
You must be signed in to change notification settings - Fork 11
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
Added 'rglob' function to stdlib. #242
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
fn dir_glob(dir, pattern, results) { | ||
files <= find $dir -maxdepth 1 -mindepth 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can optimize this a lot passing |
||
files <= split($files, "\n") | ||
|
||
for f in $files { | ||
_, status <= test -d $f | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if asking only directories to |
||
|
||
if $status == "0" { | ||
dir_pattern <= format("%s/%s", $f, $pattern) | ||
current_results <= glob($dir_pattern) | ||
|
||
for r in $current_results { | ||
results <= append($results, $r) | ||
} | ||
|
||
results <= dir_glob($f, $pattern, $results) | ||
} | ||
} | ||
|
||
return $results | ||
} | ||
|
||
fn rglob(pattern) { | ||
working_dir <= pwd | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont think this is the right way to handle the rglob. It will only work in the current directory? I'm not saying we must add support to the '/path/**' syntax, but maybe the signature could be |
||
result <= glob($working_dir+"/"+$pattern) | ||
result <= dir_glob($working_dir, $pattern, $result) | ||
|
||
return $result | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#!/usr/bin/env nash | ||
|
||
import rglob | ||
|
||
fn setup() { | ||
temp_dir <= mktemp -d | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this directory is leaking. |
||
|
||
test_txt = $temp_dir+"/test.txt" | ||
test_sh = $temp_dir+"/test.sh" | ||
|
||
touch $test_txt | ||
touch $test_sh | ||
|
||
hello_dir = $temp_dir+"/hello" | ||
world_dir = $temp_dir+"/world" | ||
|
||
mkdir $hello_dir | ||
mkdir $world_dir | ||
|
||
honda_txt = $hello_dir+"/honda.txt" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. kkkkk |
||
honda_sh = $hello_dir+"/honda.sh" | ||
civic_txt = $world_dir+"/civic.txt" | ||
civic_sh = $world_dir+"/civic.sh" | ||
|
||
touch $honda_txt | ||
touch $honda_sh | ||
touch $civic_txt | ||
touch $civic_sh | ||
|
||
return $temp_dir | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better to return the cleaning function also, something like:
|
||
} | ||
|
||
fn assert_success(expected, got) { | ||
if len($got) != len($expected) { | ||
print("expected length to be '%s' but got '%s'\n", len($expected), len($got)) | ||
exit("1") | ||
} | ||
|
||
found = "0" | ||
|
||
for x in $expected { | ||
for y in $got { | ||
if $x == $y { | ||
found <= echo $found+" + 1" | bc | ||
} | ||
} | ||
} | ||
|
||
if $found != len($expected) { | ||
print("expected [%s] got [%s]\n", $expected, $got) | ||
exit("1") | ||
} | ||
} | ||
|
||
fn test_rglob_txt(dir) { | ||
expected_files = ( | ||
$dir+"/test.txt" | ||
$dir+"/hello/honda.txt" | ||
$dir+"/world/civic.txt" | ||
) | ||
|
||
got_files <= rglob("*.txt") | ||
|
||
assert_success($expected_files, $got_files) | ||
} | ||
|
||
fn test_rglob_sh(dir) { | ||
expected_files = ( | ||
$dir+"/test.sh" | ||
$dir+"/hello/honda.sh" | ||
$dir+"/world/civic.sh" | ||
) | ||
|
||
got_files <= rglob("*.sh") | ||
|
||
assert_success($expected_files, $got_files) | ||
} | ||
|
||
old_dir <= pwd | ||
temp_dir <= setup() | ||
|
||
chdir($temp_dir) | ||
test_rglob_txt($temp_dir) | ||
test_rglob_sh($temp_dir) | ||
chdir($old_dir) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
stdlib functions should never abort the script. We must treat (or ignore in some cases) all errors.