2019-10-09 13:13:56 +02:00
|
|
|
;;; fs.el --- Make working with the filesystem easier -*- lexical-binding: t -*-
|
2020-09-01 11:17:43 +02:00
|
|
|
|
2019-10-09 13:13:56 +02:00
|
|
|
;; Author: William Carroll <wpcarro@gmail.com>
|
2020-09-01 11:17:43 +02:00
|
|
|
;; Version: 0.0.1
|
|
|
|
;; Package-Requires: ((emacs "24.1"))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;; Ergonomic alternatives for working with the filesystem.
|
|
|
|
|
2020-01-16 12:16:22 +01:00
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2019-10-09 13:13:56 +02:00
|
|
|
;; Dependencies
|
2020-01-16 12:16:22 +01:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(require 'dash)
|
2019-10-09 13:13:56 +02:00
|
|
|
(require 'f)
|
2020-09-01 11:17:43 +02:00
|
|
|
(require 's)
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-01-16 12:16:22 +01:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Library
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun fs-ensure-file (path)
|
2019-10-09 13:13:56 +02:00
|
|
|
"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
|
2020-09-01 11:17:43 +02:00
|
|
|
fs-ensure-dir)
|
2019-10-09 13:13:56 +02:00
|
|
|
(f-touch path))
|
|
|
|
|
|
|
|
(f-dirname "/tmp/a/b/file.txt")
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun fs-ensure-dir (path)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Ensure that a directory and its ancestor directories in `PATH' exist."
|
|
|
|
(->> path
|
|
|
|
f-split
|
|
|
|
(apply #'f-mkdir)))
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun fs-ls (dir &optional full-path?)
|
2019-10-09 13:13:56 +02:00
|
|
|
"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
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(ert-deftest fs-test-ensure-file ()
|
2019-10-09 13:13:56 +02:00
|
|
|
(let ((file "/tmp/file/a/b/c/file.txt"))
|
|
|
|
;; Ensure this file doesn't exist first to prevent false-positives.
|
|
|
|
(f-delete file t)
|
2020-09-01 11:17:43 +02:00
|
|
|
(fs-ensure-file file)
|
2019-10-09 13:13:56 +02:00
|
|
|
(should (and (f-exists? file)
|
|
|
|
(f-file? file)))))
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(ert-deftest fs-test-ensure-dir ()
|
2019-10-09 13:13:56 +02:00
|
|
|
(let ((dir "/tmp/dir/a/b/c"))
|
|
|
|
;; Ensure the directory doesn't exist.
|
|
|
|
(f-delete dir t)
|
2020-09-01 11:17:43 +02:00
|
|
|
(fs-ensure-dir dir)
|
2019-10-09 13:13:56 +02:00
|
|
|
(should (and (f-exists? dir)
|
|
|
|
(f-dir? dir)))))
|
|
|
|
|
|
|
|
(provide 'fs)
|
|
|
|
;;; fs.el ends here
|