Skip to content

Commit

Permalink
Merge branch 'master' of github.com:janet-lang/spork
Browse files Browse the repository at this point in the history
  • Loading branch information
bakpakin committed Sep 4, 2022
2 parents 4470e25 + 0fac59e commit 510d4cf
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
68 changes: 68 additions & 0 deletions spork/sh.janet
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,71 @@
nil nil # do nothing if file does not exist
# Default, try to remove
(os/rm path)))

(defn exists?
"Check if the given file or directory exists. (Follows symlinks)"
[path]
(not (nil? (os/stat path))))

(defn scan-directory
"Scan a directory recursively, applying the given function on all files and
directories in a depth-first manner. This function has no effect if the
directory does not exist."
[dir func]
(def names (map (fn [name] (path/join dir name))
(try (os/dir dir) ([err] @[]))))
(defn filter-names [mode]
(filter
(fn [name] (= mode (os/stat name :mode)))
names))
(def files (filter-names :file))
(def dirs (filter-names :directory))
(each dir dirs
(scan-directory dir func))
(each file files
(func file))
(each dir dirs
(func dir)))

(defn list-all-files
"List the files in the given directory recursively. Return the paths to all
files found, relative to the current working directory if the given path is a
relative path, or as an absolute path otherwise."
[dir]
(def files @[])
(scan-directory dir (fn [file]
(when (= :file (os/stat file :mode))
(array/push files file))))
files)

(defn create-dirs
"Create all directories in path specified as string including itself."
[dir-path]
(def dirs @[])
(each part (path/parts dir-path)
(array/push dirs part)
(let [path (path/join ;dirs)]
(if (not (os/lstat path))
(os/mkdir path)))))

(defn make-new-file
"Create and open a file, creating all the directories leading to the file if
they do not exist, and return it."
[file-path]
(let [parent-path (path/dirname file-path)]
(when (and (not (exists? file-path))
(not (exists? parent-path)))
(create-dirs parent-path)))
(file/open file-path :w))

(defn copy-file
"Copy a file from source to destination. Creates all directories in the path
to the destination file if they do not exist."
[src-path dst-path]
(def buf-size 4096)
(def buf (buffer/new buf-size))
(with [src (file/open src-path :rb)]
(with [dst (make-new-file dst-path)]
(while (def bytes (file/read src buf-size buf))
(file/write dst bytes)
(buffer/clear buf)))))
1 change: 1 addition & 0 deletions test/assets/17/test.file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this is a test file for suite 17
38 changes: 38 additions & 0 deletions test/suite0017.janet
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
(use ../spork/test)
(import ../spork/sh)
(import ../spork/path)

(start-suite 17)

(def base-path "test")

(do
(assert (deep= (os/dir (path/join base-path "assets/17"))
@["test.file"])
"test files are wrong, assets/17 should only contain test.file")
(sh/copy-file (path/join base-path "assets/17/test.file")
(path/join base-path "assets/17/test2.file"))
(def new_file (slurp (path/join base-path "assets/17/test2.file")))
(assert (deep= (sort (sh/list-all-files (path/join base-path "assets")))
(map |(path/join base-path $0)
@["assets/17/test.file" "assets/17/test2.file"]))
"sh/list-all-files didn't list the correct files")
(sh/rm (path/join base-path "assets/17/test2.file"))
(assert (deep= (os/dir (path/join base-path "assets/17"))
@["test.file"])
"file test2.file was not removed by sh/rm")
(assert (deep= (slurp (path/join base-path "assets/17/test.file"))
new_file)
"file copied with sh/copy-file is not the same"))

(do
(sh/create-dirs (path/join base-path "assets/17/some/more/directories/to/test"))
(assert (= ((os/stat (path/join base-path "assets/17/some/more/directories/to/test")) :mode)
:directory)
"sh/create-dirs failed")
(sh/rm (path/join base-path "assets/17/some"))
(assert (= (os/stat (path/join base-path "assets/17/some/more/directories/to/test"))
nil)
"sh/rm didn't work correctly"))

(end-suite)

0 comments on commit 510d4cf

Please sign in to comment.