about summary refs log tree commit diff
path: root/emacs/.emacs.d/wpc/fs.el
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-01-30T16·00+0000
committerWilliam Carroll <wpcarro@gmail.com>2020-01-30T16·00+0000
commit578ed1ba98510058cf48f897a9bf4e3391684120 (patch)
tree5eadabd3d232151c75ac2424eaa8771ab25e927e /emacs/.emacs.d/wpc/fs.el
parent3684adf23f847114e1beeaab162398628ba571aa (diff)
Move move .emacs.d out of configs/shared
Moving all of my Emacs-related files into their own directory at the root of
this repository.
Diffstat (limited to 'emacs/.emacs.d/wpc/fs.el')
-rw-r--r--emacs/.emacs.d/wpc/fs.el65
1 files changed, 65 insertions, 0 deletions
diff --git a/emacs/.emacs.d/wpc/fs.el b/emacs/.emacs.d/wpc/fs.el
new file mode 100644
index 000000000000..b1a79e280a57
--- /dev/null
+++ b/emacs/.emacs.d/wpc/fs.el
@@ -0,0 +1,65 @@
+;;; fs.el --- Make working with the filesystem easier -*- lexical-binding: t -*-
+;; Author: William Carroll <wpcarro@gmail.com>
+
+;;; Commentary:
+;; Ergonomic alternatives for working with the filesystem.
+
+;;; Code:
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; Dependencies
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+(require 'f)
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; Library
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+(defun fs/ensure-file (path)
+  "Ensure that a file and its directories in `PATH' exist.
+Will error for inputs with a trailing slash."
+  (when (s-ends-with? "/" path)
+    (error (format "Input path has trailing slash: %s" path)))
+  (->> path
+       f-dirname
+       fs/ensure-dir)
+  (f-touch path))
+
+(f-dirname "/tmp/a/b/file.txt")
+
+(defun fs/ensure-dir (path)
+  "Ensure that a directory and its ancestor directories in `PATH' exist."
+  (->> path
+       f-split
+       (apply #'f-mkdir)))
+
+(defun fs/ls (dir &optional full-path?)
+  "List the files in `DIR' one-level deep.
+Should behave similarly in spirit to the Unix command, ls.
+If `FULL-PATH?' is set, return the full-path of the files."
+  (-drop 2 (directory-files dir full-path?)))
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; Tests
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;; TODO: Support `refute' function / macro.
+(ert-deftest fs/test/ensure-file ()
+  (let ((file "/tmp/file/a/b/c/file.txt"))
+    ;; Ensure this file doesn't exist first to prevent false-positives.
+    (f-delete file t)
+    (fs/ensure-file file)
+    (should (and (f-exists? file)
+                 (f-file? file)))))
+
+(ert-deftest fs/test/ensure-dir ()
+  (let ((dir "/tmp/dir/a/b/c"))
+    ;; Ensure the directory doesn't exist.
+    (f-delete dir t)
+    (fs/ensure-dir dir)
+    (should (and (f-exists? dir)
+                 (f-dir? dir)))))
+
+(provide 'fs)
+;;; fs.el ends here