✨ add recursive file searcher methods
This commit is contained in:
parent
cacf0ae86a
commit
2bb9655422
41
std/file.nas
41
std/file.nas
|
@ -85,3 +85,44 @@ var recursive_find_files = func(path) {
|
||||||
unix.closedir(dd);
|
unix.closedir(dd);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var recursive_find_files_flat = func(path) {
|
||||||
|
var tree_files = recursive_find_files(path);
|
||||||
|
if (tree_files==nil) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
var flat = [];
|
||||||
|
var bfs = [tree_files];
|
||||||
|
while(size(bfs)!=0) {
|
||||||
|
var first = pop(bfs);
|
||||||
|
foreach(var file_record; first.files) {
|
||||||
|
if (ishash(file_record)) {
|
||||||
|
append(bfs, file_record);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
append(flat, first.dir~"/"~file_record);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return flat;
|
||||||
|
}
|
||||||
|
|
||||||
|
var recursive_find_files_with_extension = func(path, extensions...) {
|
||||||
|
var in_vec = func(ext) {
|
||||||
|
foreach(var i; extensions) {
|
||||||
|
if (ext==i) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
var files = recursive_find_files_flat(path);
|
||||||
|
var res = [];
|
||||||
|
foreach(var filename; files) {
|
||||||
|
var tmp = split('.', filename);
|
||||||
|
if (size(tmp)>1 and in_vec(tmp[-1])) {
|
||||||
|
append(res, filename);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
|
@ -2,15 +2,15 @@ use std.file;
|
||||||
|
|
||||||
var check = func(dir_name) {
|
var check = func(dir_name) {
|
||||||
var ts = maketimestamp();
|
var ts = maketimestamp();
|
||||||
var f = file.find_all_files_with_extension(dir_name, "nas");
|
var files = file.recursive_find_files_with_extension(dir_name, "nas");
|
||||||
var res = [];
|
var res = [];
|
||||||
foreach(var k; f) {
|
foreach(var f; files) {
|
||||||
ts.stamp();
|
ts.stamp();
|
||||||
if (system("nasal -c "~dir_name~"/"~k~" 1>/dev/null 2>/dev/null")!=0) {
|
if (system("nasal -c "~f~" 1>/dev/null 2>/dev/null")!=0) {
|
||||||
println("\e[31merror\e[0m ", dir_name, "/", k);
|
println("\e[31merror\e[0m ", f);
|
||||||
append(res, dir_name~"/"~k);
|
append(res, f);
|
||||||
}
|
}
|
||||||
println("compiling ", dir_name, "/", k, " in \e[32m", ts.elapsedMSec(), "\e[0m ms");
|
println("compiling ", f, " in \e[32m", ts.elapsedMSec(), "\e[0m ms");
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,26 +19,10 @@ if (size(arg)<1) {
|
||||||
|
|
||||||
var needle = arg[0];
|
var needle = arg[0];
|
||||||
|
|
||||||
var do_flat = func(vec) {
|
|
||||||
var flat = [];
|
|
||||||
var bfs = [vec];
|
|
||||||
while(size(bfs)!=0) {
|
|
||||||
var d = pop(bfs);
|
|
||||||
foreach(var f; d.files) {
|
|
||||||
if (ishash(f)) {
|
|
||||||
append(bfs,f);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
append(flat, d.dir~"/"~f);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sort(flat, func(a, b) {return cmp(a, b)<0});
|
|
||||||
return flat;
|
|
||||||
}
|
|
||||||
|
|
||||||
var result = [];
|
var result = [];
|
||||||
var all_files = file.recursive_find_files(".");
|
var all_files = file.recursive_find_files_flat(".");
|
||||||
foreach(var f; do_flat(all_files)) {
|
sort(all_files, func(a, b) {return cmp(a, b)<=0;});
|
||||||
|
foreach(var f; all_files) {
|
||||||
var pos = find(needle, f);
|
var pos = find(needle, f);
|
||||||
if (pos == -1) {
|
if (pos == -1) {
|
||||||
continue;
|
continue;
|
||||||
|
|
Loading…
Reference in New Issue