summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Mike Burns <mike@mike-burns.com>2013-08-16 15:42:56 +0200
committerGravatar Mike Burns <mike@mike-burns.com>2013-08-16 16:11:36 +0200
commit975b347b7206a0e9caeeea8349e4765b4db654f9 (patch)
tree7e993414fce578e351b38d0a761670ee551f22b0
parent526fb25aff2b27b46aaa4754f5b613ca1c97f8c3 (diff)
Pre-up, post-up, pre-down, and post-down hooks
These are programs that, if they exist, will run before or after the syncronization/removal is run. Three use cases caused this: 1. The thoughtbot dotfiles will run a vundle installation set of commands after intitial synchronization. 2. I changed the location of `.bash_history` to `.bash/history` and wanted to move `.bash_history` to `.bash/history` after up to preserve existing history. 3. Moving from an existing old-style custom install script to `rcup` might require some cleanup; this happened in practice, and required a simple script.
-rw-r--r--NEWS.md.in1
-rwxr-xr-xbin/lsrc2
-rwxr-xr-xbin/rcdn5
-rwxr-xr-xbin/rcup9
-rw-r--r--man/rcdn.111
-rw-r--r--man/rcup.126
-rw-r--r--share/rcm.sh.in15
7 files changed, 63 insertions, 6 deletions
diff --git a/NEWS.md.in b/NEWS.md.in
index 2934419..0e7c8df 100644
--- a/NEWS.md.in
+++ b/NEWS.md.in
@@ -6,6 +6,7 @@ rcm (@PACKAGE_VERSION@) unstable; urgency=low
* Copy instead of symlink with -C.
* Always copy the files listed in COPY_ALWAYS.
* Show whether the file is a copy or symlink using lsrc -F.
+ * Add hooks for pre-up, post-up, pre-down, and post-down.
-- Mike Burns <mburns@thoughtbot.com> Mon, 05 Aug 2013 16:43:33 +0200
diff --git a/bin/lsrc b/bin/lsrc
index f7f4826..1ea2e2c 100755
--- a/bin/lsrc
+++ b/bin/lsrc
@@ -140,7 +140,7 @@ is_metafile() {
host_portion=`echo $1 | sed -e 's/host-.*/host-/'`
tag_portion=`echo $1 | sed -e 's/tag-.*/tag-/'`
- [ x$host_portion = 'xhost-' -o x$tag_portion = 'xtag-' ]
+ [ x$host_portion = 'xhost-' -o x$tag_portion = 'xtag-' -o "x$1" = "xhooks" ]
}
dotfiles_dir_excludes() {
diff --git a/bin/rcdn b/bin/rcdn
index d809796..cae9dc8 100755
--- a/bin/rcdn
+++ b/bin/rcdn
@@ -70,6 +70,9 @@ if [ -e $HOME/.rcrc ]; then
fi
handle_command_line $*
+: ${DOTFILES_DIRS:=$DOTFILES_DIRS $DEFAULT_DOTFILES_DIR}
+
+run_hooks pre down
for dest_and_src in `lsrc $LS_ARGS`; do
saved_ifs=$IFS
@@ -81,3 +84,5 @@ for dest_and_src in `lsrc $LS_ARGS`; do
remove_link $dest $dest $sigil
done
+
+run_hooks post down
diff --git a/bin/rcup b/bin/rcup
index 5f51192..5a42a81 100755
--- a/bin/rcup
+++ b/bin/rcup
@@ -119,13 +119,13 @@ handle_command_line() {
handle_common_flags rcup $version $verbosity
tags=${arg_tags:-$TAGS}
- dotfiles_dirs=${dotfiles_dirs:-$DOTFILES_DIRS}
+ DOTFILES_DIRS=${dotfiles_dirs:-$DOTFILES_DIRS}
files=$@
for tag in $tags; do
LS_ARGS="$LS_ARGS -t $tag"
done
- for dotfiles_dir in $dotfiles_dirs; do
+ for dotfiles_dir in $DOTFILES_DIRS; do
LS_ARGS="$LS_ARGS -d $dotfiles_dir"
done
for exclude in $excludes; do
@@ -146,6 +146,9 @@ if [ -e $HOME/.rcrc ]; then
fi
handle_command_line $*
+: ${DOTFILES_DIRS:=$DOTFILES_DIRS $DEFAULT_DOTFILES_DIR}
+
+run_hooks pre up
for dest_and_src in `lsrc $LS_ARGS`; do
saved_ifs=$IFS
@@ -162,3 +165,5 @@ for dest_and_src in `lsrc $LS_ARGS`; do
handle_file $src $dest $sigil
done
+
+run_hooks post up
diff --git a/man/rcdn.1 b/man/rcdn.1
index 6c641c9..24ca1a2 100644
--- a/man/rcdn.1
+++ b/man/rcdn.1
@@ -30,6 +30,17 @@ is, that directory is removed. If a rc file is not a symlink but is
listed in
.Va COPY_ALWAYS
the file is removed.
+.Pp
+The
+.Nm
+program will run hooks before and after removing files, if these hooks
+exist. Hooks are executable programs inside the dotfiles directory, with
+the following names:
+.Pa hooks/pre-down and
+.Pa hooks/post-down .
+These hooks are run each time
+.Nm
+is run and therefore must be idempotent.
.
.Bl -tag
.It Fl d Ar DIR
diff --git a/man/rcup.1 b/man/rcup.1
index 99fa364..4140205 100644
--- a/man/rcup.1
+++ b/man/rcup.1
@@ -84,7 +84,8 @@ option in
.Xr rcrc 5
can be used to list files that must only be copied.
.Pp
-Two meta files are supported: host-specific files and tagged files.
+Three meta files are supported: host-specific files, tagged files,
+hooks.
.Pp
Host-specific files go in a directory named for the host, prefixed with
.Pa host- .
@@ -93,8 +94,7 @@ For example,
contains files specific to the computer with hostname
.Pa scarlett
, and these files will only be installed on the computer with hostname
-.Pa scarlett
-\&.
+.Pa scarlett .
.Pp
Tagged files go in a directory named for the tag, prefixed with
.Li tag- .
@@ -103,18 +103,33 @@ Therefore, files under
are only installed when installing using the
.Li git
tag.
+.Pp
+Hooks go in a directory named
+.Pa hooks .
+Two hooks are supported by
+.Nm rcup :
+pre-up and post-up . These go in files with predictable filenames:
+.Pa .dotfiles/hooks/pre-up and
+.Pa .dotfiles/hooks/post-up .
+These files must be executable. They are run every time
+.Nm
+is run, and therefore must be idempotent.
.Sh ALGORITHM
It is instructive to understand the process
.Nm rcup
uses when synchronizing your rc files:
.Bl -enum
.It
+The pre-up hook is run.
+.
+.It
All non-host, non-tag files without a dot prefix are symlinked to the
dotted filename in your home directory. So,
.Pa .dotfiles/tigrc
is
symlinked to
.Pa ~/.tigrc .
+.
.It
All non-host, non-tag directories have their structure copied to your
home directory, then a non-dotted symlink is created within. So for
@@ -125,12 +140,14 @@ causes the
directory to be created, then
.Pa haskell.vim
is symlinked within.
+.
.It
Steps (1) and (2) are applied to host-specific files. These are files
under a directory named
.Sm off
.Pa host- Va $HOSTNAME .
.Sm on
+.
.It
Steps (1) and (2) are applied to tag-specific files. These are files
under directories named
@@ -142,6 +159,9 @@ where
is the name of each specified tag in turn, taken from the command line
or from
.Xr rcrc 5 .
+.
+.It
+The post-up hook is run.
.El
.
.Sh FILES
diff --git a/share/rcm.sh.in b/share/rcm.sh.in
index ea35e47..36b6762 100644
--- a/share/rcm.sh.in
+++ b/share/rcm.sh.in
@@ -99,3 +99,18 @@ handle_metadata_flags() {
$DEBUG "TAGS: $TAGS"
$DEBUG "DOTFILES_DIRS: $DOTFILES_DIRS"
}
+
+run_hooks() {
+ $DEBUG "run_hooks $1 $2"
+ $DEBUG " with DOTFILES_DIRS: $DOTFILES_DIRS"
+ local when=$1
+ local direction=$2
+
+ for dotfiles_dir in $DOTFILES_DIRS; do
+ $DEBUG "hook: $dotfiles_dir/hooks/$when-$direction"
+ if [ -x "$dotfiles_dir/hooks/$when-$direction" ]; then
+ $VERBOSE "running $when-$direction hooks for $dotfiles_dir"
+ $dotfiles_dir/hooks/$when-$direction
+ fi
+ done
+}