From 90955a3c021a070a2a09e9b260d9d4e670c9a304 Mon Sep 17 00:00:00 2001 From: William Carroll Date: Sun, 22 Dec 2019 21:32:25 +0000 Subject: Support ensure_{file,dir} in zsh Support functions for ensuring the existence of directories and files. These functions represent the type of small ergonomic wins that I believe libraries should support. --- configs/shared/functions.zsh | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/configs/shared/functions.zsh b/configs/shared/functions.zsh index cae434274e3c..9f1e3a77e9d0 100644 --- a/configs/shared/functions.zsh +++ b/configs/shared/functions.zsh @@ -85,6 +85,19 @@ compliments() { # Filesystem operations ################################################################################ +ensure_dir() { + # Ensures that the directory and its children exist. + # Usage: ensure_dir + mkdir -p $1 +} + +ensure_file() { + # Ensures that the file and the path to that file exist. + # Usage: ensure_dir + # depends ensure_dir + ensure_dir $(dirname $1) && touch $1 +} + tar_dir() { # Tars dir as dir.tar. Removes dir. # compliments untar_dir @@ -204,11 +217,12 @@ wallpaper() { dotfilify() { # Moves a regular, non-symlinked file into my dotfiles. # compliments undotfilify + # depends ensure_dir local original_path=$(realpath $1) - # Trim $HOME prefix - local dotfile_path="${DOTFILES#$HOME/}/configs/shared/${original_path#$HOME/}" - - mv $original_path $dotfile_path && ln --force -s $dotfile_path $original_path + local dotfile_path="${DOTFILES}/configs/shared/${original_path#$HOME/}" + ensure_dir $(dirname $dotfile_path) && \ + mv $original_path $dotfile_path && \ + ln --force -s $dotfile_path $original_path } # TODO: Write more robust, tested dotfile manager application in Elisp. -- cgit 1.4.1