Skip to content

Commit

Permalink
Merge pull request #16 from calcit-lang/glob
Browse files Browse the repository at this point in the history
add glob! function
  • Loading branch information
NoEgAm authored Jan 16, 2023
2 parents 927a211 + 871201b commit 0d4e665
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 6 deletions.
9 changes: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "calcit_std"
version = "0.0.18"
version = "0.0.19"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2021"

Expand All @@ -24,6 +24,7 @@ rand = "0.8.5"
ctrlc = "3.2.4"
md5 = "0.7.0"
walkdir = "2"
glob = "0.3.0"

# [profile.release]
# lto = true
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ calcit.std.fs/check-write-file! a b
calcit.std.fs/path-exists? a
calcit.std.fs/read-dir! a
calcit.std.fs/walk-dir! a
calcit.std.fs/glob! |src/*.rs
calcit.std.fs/create-dir! path
calcit.std.fs/create-dir-all! path
Expand Down
27 changes: 26 additions & 1 deletion calcit.cirru

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions compact.cirru
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

{} (:package |calcit.std)
:configs $ {} (:init-fn |calcit.std.test/main!) (:reload-fn |calcit.std.test/reload!) (:version |0.0.18)
:configs $ {} (:init-fn |calcit.std.test/main!) (:reload-fn |calcit.std.test/reload!) (:version |0.0.19)
:modules $ []
:entries $ {}
:files $ {}
Expand Down Expand Up @@ -59,6 +59,9 @@
|create-dir-all! $ quote
defn create-dir-all! (name)
&call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"create_dir_all" name
|glob! $ quote
defn glob! (name)
&call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"glob_call" name
|path-exists? $ quote
defn path-exists? (name)
&call-dylib-edn (get-dylib-path "\"/dylibs/libcalcit_std") "\"path_exists" name
Expand Down Expand Up @@ -230,14 +233,15 @@
println $ read-dir! "\"./"
println "\"dirs:" $ execute! ([] "\"ls")
println "\"all paths size:" $ count (walk-dir! "\"target")
println "\"rs files:" $ glob! "\"src/*.rs"
create-dir! "\"target/dir1"
rename! "\"target/dir1" "\"target/dir4"
create-dir-all! "\"target/dir2/dir3"
check-write-file! "\"target/dir8/dir9/file.text" "\"TODO"
:ns $ quote
ns calcit.std.test.fs $ :require
calcit.std.$meta :refer $ calcit-filename calcit-dirname
calcit.std.fs :refer $ read-file! write-file! path-exists? read-dir! create-dir! create-dir-all! rename! check-write-file! walk-dir!
calcit.std.fs :refer $ read-file! write-file! path-exists? read-dir! create-dir! create-dir-all! rename! check-write-file! walk-dir! glob!
calcit.std.process :refer $ execute!
|calcit.std.test.json $ {}
:defs $ {}
Expand Down
26 changes: 26 additions & 0 deletions src/fs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! wraped some functions from std::fs https://doc.rust-lang.org/std/fs/index.html
use cirru_edn::Edn;
use glob::glob;
use std::fs;
use std::path::Path;
use walkdir::WalkDir;
Expand Down Expand Up @@ -189,3 +190,28 @@ pub fn walk_dir(args: Vec<Edn>) -> Result<Edn, String> {
Err(format!("walk-dir expected 1 argument, got: {args:?}"))
}
}

/// use glob to match paths recursively
#[no_mangle]
pub fn glob_call(args: Vec<Edn>) -> Result<Edn, String> {
if args.len() == 1 {
if let Edn::Str(name) = &args[0] {
let mut content: Vec<Edn> = vec![];
for entry in glob(name).expect("expand glob result") {
match entry {
Ok(entry) => {
if entry.is_file() {
content.push(Edn::Str(format!("{}", entry.display()).into_boxed_str()));
}
}
Err(e) => return Err(format!("Failed to read: {}", e)),
}
}
Ok(Edn::List(content))
} else {
Err(format!("glob expected a string, got: {}", &args[0]))
}
} else {
Err(format!("glob expected 1 argument, got: {args:?}"))
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod random;
mod time;

pub use date::{add_duration, format_time, now_bang, parse_time};
pub use fs::{path_exists, read_dir, read_file, write_file};
pub use fs::{glob_call, path_exists, read_dir, read_file, walk_dir, write_file};
pub use json::{parse_json, stringify_json};
pub use path::join_path;
pub use process::execute_command;
Expand Down

0 comments on commit 0d4e665

Please sign in to comment.