Browse Source

used the new collect/icollect in a few spots

main
Jesse Laprade 4 months ago
parent
commit
b9e24e948c
  1. 21
      README.md
  2. 60
      ffs.fnl

21
README.md

@ -16,7 +16,6 @@ A collection of file system utilities for [Fennel](https://fennel-lang.org/).
* [Shell functions](#shell-functions)
* [shell->sequence](#shell-sequence)
* [Path functions](#path-functions)
* [path-exists?](#path-exists)
* [path-copy](#path-copy)
* [path-delete](#path-delete)
* [paths-missing](#paths-missing)
@ -99,26 +98,6 @@ Takes a shell command and returns the output as a sequence.
This section describes path-related functions.
### path-exists?
Takes a path and returns `true` if the path exists, or `false` if it
doesn't exist.
```
(ffs.path-exists? path) -> true or false
```
* **path** type: `string`
* **return** type: `true` or `false`
**Example**: How to print a message if a file or directory exists or doesn't exist:
```
(if (ffs.path-exists? "Documents")
(print "Found 'Documents'!")
(print "Didn't find 'Documents'"))
```
### path-copy
Takes a source path and copies it to the destination path.

60
ffs.fnl

@ -6,31 +6,15 @@
(local ffs {})
(fn ffs.shell->sequence [command]
(let [seq []]
(with-open [file-handle (io.popen command :r)]
(each [i (file-handle:lines)]
(table.insert seq i)))
seq))
(fn ffs.path-exists? [path]
(let [file-handle (io.open path :r)]
(if file-handle
(file-handle:close)
false)))
(fn ffs.path-copy [source dest]
(os.execute (string.format "cp -r %s %s" source dest)))
(fn ffs.path-delete [path]
(os.execute (string.format "rm -rf %s" path)))
(with-open [file-handle (io.popen command :r)]
(icollect [i (file-handle:lines)] i)))
;; The (if ...) here makes sure a "/" is appended to the path
(fn ffs.directory-exists? [directory]
(let [(str items-found) (string.gsub directory "/$" "")
dir-to-check (if (> 0 items-found)
directory
(.. str "/"))]
(ffs.path-exists? dir-to-check)))
(let [command (string.format "test -d %s && echo exists" directory)]
(if (. (ffs.shell->sequence command) 1)
true ;; This prevents "exists" from being returned
false)))
(fn ffs.directory-create [dir]
(os.execute (string.format "mkdir -p %s" dir)))
@ -39,8 +23,10 @@
(ffs.shell->sequence (string.format "ls %s" dir)))
(fn ffs.file-exists? [file]
(and (ffs.path-exists? file)
(not (ffs.directory-exists? file))))
(let [command (string.format "test -f %s && echo exists" file)]
(if (. (ffs.shell->sequence command) 1)
true ;; This prevents "exists" from being returned
false)))
(fn ffs.file-create [file]
(io.close (io.open file :w)))
@ -50,20 +36,24 @@
(file-out:write data)))
(fn ffs.file->lines [file]
(let [seq []]
(each [line (io.lines file)]
(table.insert seq line))
seq))
(icollect [line (io.lines file)] line))
(fn ffs.path-exists? [path]
(or (ffs.file-exists? path)
(ffs.directory-exists? path)))
(fn ffs.path-copy [source dest]
(os.execute (string.format "cp -r %s %s" source dest)))
(fn ffs.path-delete [path]
(os.execute (string.format "rm -rf %s" path)))
(fn ffs.paths-missing [mode required-paths]
(let [seq []
func (match mode
(let [func (match mode
:files ffs.file-exists?
:directories ffs.directory-exists?
:paths ffs.path-exists?)]
(each [_ path (pairs required-paths)]
:directories ffs.directory-exists?)]
(icollect [_ path (ipairs required-paths)]
(when (not (func path))
(table.insert seq path)))
seq))
path))))
ffs
Loading…
Cancel
Save