From 653c89c01cc9c9912e17a5ad6995e7eb7811fbad Mon Sep 17 00:00:00 2001 From: jonathan 'jonthn' buschmann Date: Mon, 8 May 2023 19:32:45 +0800 Subject: [PATCH] Add support for XDG compliant directories and configuration This change the default of : - ~/.rcrc with ~/.dotfiles to - ~/.config/rcm/rcrc with ~/.local/share/rcm/dotfiles (XDG_xxxx) It's an _hard_ error to have both ~/.rcrc and ~/.config/rcm/rcrc, except for _compatibility_ to keep ~/.rcrc as a link to ~/.config/rcm/rcrc Completes #250 --- Makefile.am | 7 +++- NEWS.md.in | 1 + man/lsrc.1 | 8 +++- man/mkrc.1 | 8 +++- man/rcdn.1 | 5 ++- man/rcm.7.mustache | 6 +++ man/rcrc.5 | 3 ++ man/rcup.1 | 8 +++- share/rcm.sh.in | 38 ++++++++++++++++++- test/helper.sh | 2 + test/mkrc-no-dotfiles-dir-found.t | 2 +- test/mkrc-one-dotfile-arg.t | 2 +- test/xdg-compatibilty-rcrc-symlink.t | 11 ++++++ test/xdg-config-with-dotfiles.t | 15 ++++++++ test/xdg-error-both-config.t | 13 +++++++ ...g-favor-rcm_dotfiles-over-tilde_dotfiles.t | 17 +++++++++ test/xdg.t | 19 ++++++++++ 17 files changed, 156 insertions(+), 9 deletions(-) create mode 100644 test/xdg-compatibilty-rcrc-symlink.t create mode 100644 test/xdg-config-with-dotfiles.t create mode 100644 test/xdg-error-both-config.t create mode 100644 test/xdg-favor-rcm_dotfiles-over-tilde_dotfiles.t create mode 100644 test/xdg.t diff --git a/Makefile.am b/Makefile.am index acb217c3..3ac0bb44 100644 --- a/Makefile.am +++ b/Makefile.am @@ -55,7 +55,12 @@ TESTS = \ test/rcup-hooks-failure.t \ test/rcup-hooks-run-in-situ.t \ test/rcup-hooks-run-in-order.t \ - test/rcup-spaces.t + test/rcup-spaces.t \ + test/xdg.t \ + test/xdg-compatibilty-rcrc-symlink.t \ + test/xdg-config-with-dotfiles.t \ + test/xdg-error-both-config.t \ + test/xdg-favor-rcm_dotfiles-over-tilde_dotfiles.t dist_check_SCRIPTS = $(TESTS) dist_check_DATA = test/helper.sh diff --git a/NEWS.md.in b/NEWS.md.in index a6e6e1d0..49bf0278 100644 --- a/NEWS.md.in +++ b/NEWS.md.in @@ -1,6 +1,7 @@ rcm (@PACKAGE_VERSION@) unstable; urgency=low * Feature: rcup/rcdn hooks can bail early (Patrick Brisbin) + * Feature: XDG-compliance ~/.config/rcm/rcrc and ~/local/share/rcm/dotfiles * Documentation improvement (Teo Ljungberg) -- Mike Burns Fri, 30 Dec 2022 11:43:00 -0500 diff --git a/man/lsrc.1 b/man/lsrc.1 index 7242497e..15ec3ab8 100644 --- a/man/lsrc.1 +++ b/man/lsrc.1 @@ -163,10 +163,16 @@ when using an exclude pattern. .Bl -tag -width ".Ev RCRC" .It Ev RCRC User configuration file. Defaults to -.Pa ~/.rcrc . +.Pa ${XDG_CONFIG_HOME}/rcm/rcrc . .El .Sh FILES +.Pp +.Pa ${XDG_DATA_HOME}/rcm/dotfiles +or .Pa ~/.dotfiles +.Pp +.Pa ${XDG_CONFIG_HOME}/rcm/rcrc +or .Pa ~/.rcrc .Sh SEE ALSO .Xr mkrc 1 , diff --git a/man/mkrc.1 b/man/mkrc.1 index 16bd1fa6..849483f2 100644 --- a/man/mkrc.1 +++ b/man/mkrc.1 @@ -79,10 +79,16 @@ section in .Bl -tag -width ".Ev RCRC" .It Ev RCRC User configuration file. Defaults to -.Pa ~/.rcrc . +.Pa ${XDG_CONFIG_HOME}/rcm/rcrc . .El .Sh FILES +.Pp +.Pa ${XDG_DATA_HOME}/rcm/dotfiles +or .Pa ~/.dotfiles +.Pp +.Pa ${XDG_CONFIG_HOME}/rcm/rcrc +or .Pa ~/.rcrc .Sh EXAMPLES .Dl mkrc ~/.vimrc diff --git a/man/rcdn.1 b/man/rcdn.1 index e5d8bf42..e590bb85 100644 --- a/man/rcdn.1 +++ b/man/rcdn.1 @@ -140,9 +140,12 @@ only remove the specified file(s) .Bl -tag -width ".Ev RCRC" .It Ev RCRC User configuration file. Defaults to -.Pa ~/.rcrc . +.Pa ${XDG_CONFIG_HOME}/rcm/rcrc . .El .Sh FILES +.Pp +.Pa ${XDG_CONFIG_HOME}/rcm/rcrc +or .Pa ~/.rcrc .Sh EXAMPLES .Dl rcdn -v diff --git a/man/rcm.7.mustache b/man/rcm.7.mustache index 003ed6df..45c2a7bb 100644 --- a/man/rcm.7.mustache +++ b/man/rcm.7.mustache @@ -286,7 +286,13 @@ share tools and develop this further as a first-class entity. It is also our hope that a common set of tools will encourage others to share their dotfiles, too. .Sh FILES +.Pp +.Pa ${XDG_DATA_HOME}/rcm/dotfiles +or .Pa ~/.dotfiles +.Pp +.Pa ${XDG_CONFIG_HOME}/rcm/rcrc +or .Pa ~/.rcrc .Sh SEE ALSO .Xr lsrc 1 , diff --git a/man/rcrc.5 b/man/rcrc.5 index 6b6dda9b..a6f1a3c7 100644 --- a/man/rcrc.5 +++ b/man/rcrc.5 @@ -74,6 +74,9 @@ under the section . .El .Sh FILES +.Pp +.Pa ${XDG_CONFIG_HOME}/rcm/rcrc +or .Pa ~/.rcrc .Sh EXAMPLES .Dl COPY_ALWAYS="ssh/id_* weechat/* netrc" diff --git a/man/rcup.1 b/man/rcup.1 index 09318779..b9080038 100644 --- a/man/rcup.1 +++ b/man/rcup.1 @@ -247,10 +247,16 @@ The post-up hook is run. .Bl -tag -width ".Ev RCRC" .It Ev RCRC User configuration file. Defaults to -.Pa ~/.rcrc . +.Pa ${XDG_CONFIG_HOME}/rcm/rcrc . .El .Sh FILES +.Pp +.Pa ${XDG_DATA_HOME}/rcm/dotfiles +or .Pa ~/.dotfiles +.Pp +.Pa ${XDG_CONFIG_HOME}/rcm/rcrc +or .Pa ~/.rcrc .Sh SEE ALSO .Xr lsrc 1 , diff --git a/share/rcm.sh.in b/share/rcm.sh.in index 47312d9c..ee188171 100644 --- a/share/rcm.sh.in +++ b/share/rcm.sh.in @@ -8,11 +8,17 @@ PRINT=echo PROMPT=echo_n ERROR=echo_error VERBOSE=: -DEFAULT_DOTFILES_DIR="$HOME/.dotfiles" +DEFAULT_DOTFILES_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/rcm/dotfiles" MKDIR=mkdir INSTALL=rcup ROOT_DIR="$HOME" +# Default is XDG-style +if [ ! -d "$DEFAULT_DOTFILES_DIR" ] && + [ -d "$HOME/.dotfiles" ]; then + DEFAULT_DOTFILES_DIR="$HOME/.dotfiles" +fi + if [ -z "$LOGNAME" ]; then LOGNAME=$(whoami) fi @@ -101,6 +107,8 @@ handle_common_flags() { PRINT=: INSTALL="$INSTALL -q" fi + + $DEBUG "Default dotfiles dir is $DEFAULT_DOTFILES_DIR" } determine_hostname() { @@ -185,7 +193,33 @@ append_variable() { fi } -: ${RCRC:=$HOME/.rcrc} +if [ -z "$RCRC" ]; then + + # Error to have 2 possible conflicting rcrc files + + if [ -r "$HOME/.rcrc" ] && + [ -r "${XDG_CONFIG_HOME:-${HOME}/.config}/rcm/rcrc" ] && + [ -h "$HOME/.rcrc" ] && + diff -q "$HOME/.rcrc" "${XDG_CONFIG_HOME:-${HOME}/.config}/rcm/rcrc" >/dev/null 2>&1; then + + # Allow ~/.rcrc to be a link to ~/.config/rcm/rcrc for compatibility + RCRC="${XDG_CONFIG_HOME:-${HOME}/.config}/rcm/rcrc" + + elif [ -r "$HOME/.rcrc" ] && + [ -r "${XDG_CONFIG_HOME:-${HOME}/.config}/rcm/rcrc" ]; then + + echo "Both $HOME/.rcrc and ${XDG_CONFIG_HOME:-${HOME}/.config}/rcm/rcrc exists. Please remove one to use rcm" >&2 + exit 1 + else + + # To be more XDG-compliant favor ~/.config/rcm/rcrc and set it as `default` + RCRC="${XDG_CONFIG_HOME:-${HOME}/.config}/rcm/rcrc" + if [ -r "${HOME}/.rcrc" ]; then + RCRC="${HOME}/.rcrc" + fi + fi + +fi if [ -r "$RCRC" ]; then . "$RCRC" diff --git a/test/helper.sh b/test/helper.sh index 5016cf86..d22d179e 100644 --- a/test/helper.sh +++ b/test/helper.sh @@ -3,6 +3,8 @@ for bin in lsrc mkrc rcup rcdn; do done export HOME="$PWD" +export XDG_CONFIG_HOME="$PWD/.config" +export XDG_DATA_HOME="$PWD/.local/share" export PATH="$TESTDIR/../bin:$PATH" export RCRC="$HOME/.rcrc" export RCM_LIB="$TESTDIR/../share" diff --git a/test/mkrc-no-dotfiles-dir-found.t b/test/mkrc-no-dotfiles-dir-found.t index 250c99ef..06c4d5c5 100644 --- a/test/mkrc-no-dotfiles-dir-found.t +++ b/test/mkrc-no-dotfiles-dir-found.t @@ -16,7 +16,7 @@ Exit when no location found $ touch .exampleno $ mkrc -d .missing-dotfiles .exampleno >/dev/null - No dotfiles directories found in .missing-dotfiles */.dotfiles (glob) + No dotfiles directories found in .missing-dotfiles */share/rcm/dotfiles (glob) [1] $ refute "should not be a symlink" -h $HOME/.examplenob diff --git a/test/mkrc-one-dotfile-arg.t b/test/mkrc-one-dotfile-arg.t index 11b30dd5..a3590558 100644 --- a/test/mkrc-one-dotfile-arg.t +++ b/test/mkrc-one-dotfile-arg.t @@ -6,7 +6,7 @@ Trying to fool should result in no dir found error $ mkrc -d ".other-dotfiles .another-dotfiles" rcfile - No dotfiles directories found in .other-dotfiles .another-dotfiles /*/.dotfiles (glob) + No dotfiles directories found in .other-dotfiles .another-dotfiles /*/share/rcm/dotfiles (glob) [1] Giving 2 dotfiles dir should result in an error and exit EX_USAGE diff --git a/test/xdg-compatibilty-rcrc-symlink.t b/test/xdg-compatibilty-rcrc-symlink.t new file mode 100644 index 00000000..343a562d --- /dev/null +++ b/test/xdg-compatibilty-rcrc-symlink.t @@ -0,0 +1,11 @@ + $ . "$TESTDIR/helper.sh" + $ unset RCRC + $ unset XDG_CONFIG_HOME + $ unset XDG_DATA_HOME + +Should allow when ~/.rcrc is a symlink to ~/.config/rcm/rcrc + + $ mkdir -p ~/.config/rcm + $ touch ~/.config/rcm/rcrc + $ ln -s ~/.config/rcm/rcrc ~/.rcrc + $ rcup diff --git a/test/xdg-config-with-dotfiles.t b/test/xdg-config-with-dotfiles.t new file mode 100644 index 00000000..4dcdcf37 --- /dev/null +++ b/test/xdg-config-with-dotfiles.t @@ -0,0 +1,15 @@ + $ . "$TESTDIR/helper.sh" + $ unset RCRC + $ unset XDG_CONFIG_HOME + $ unset XDG_DATA_HOME + +Should support ~/.config/rcm/rcrc with ~/.dotfiles + + $ mkdir -p ~/.config/rcm + $ touch ~/.config/rcm/rcrc + $ mkdir -p .dotfiles + > mkdir -p ".dotfiles/config/software2/" + > touch ".dotfiles/config/software2/foobar" + + $ rcup + > assert_linked "$HOME/.config/software2/foobar" "$HOME/.dotfiles/config/software2/foobar" diff --git a/test/xdg-error-both-config.t b/test/xdg-error-both-config.t new file mode 100644 index 00000000..8df5267b --- /dev/null +++ b/test/xdg-error-both-config.t @@ -0,0 +1,13 @@ + $ . "$TESTDIR/helper.sh" + $ unset RCRC + $ unset XDG_CONFIG_HOME + $ unset XDG_DATA_HOME + +Should exit when using both ~/.rcrc and ~/.config/rcm/rcrc + + $ mkdir -p ~/.config/rcm + $ touch ~/.config/rcm/rcrc + $ touch ~/.rcrc + $ rcup + Both \/[\/\d\w_.-]+\.rcrc and \/[\/\d\w_.-]+\.config\/rcm\/rcrc exists\. Please remove one to use rcm (re) + [1] diff --git a/test/xdg-favor-rcm_dotfiles-over-tilde_dotfiles.t b/test/xdg-favor-rcm_dotfiles-over-tilde_dotfiles.t new file mode 100644 index 00000000..f430aebe --- /dev/null +++ b/test/xdg-favor-rcm_dotfiles-over-tilde_dotfiles.t @@ -0,0 +1,17 @@ + $ . "$TESTDIR/helper.sh" + $ unset RCRC + $ unset XDG_CONFIG_HOME + $ unset XDG_DATA_HOME + +Should prefer ~/.local/share/rcm/dotfiles over ~/.dotfiles + + $ mkdir -p .dotfiles + > mkdir -p .dotfiles/config/software3/ + > touch ".dotfiles/config/software3/traditional-config" + $ mkdir -p .local/share/rcm/dotfiles + > mkdir -p .local/share/rcm/dotfiles/config/software3 + > touch ".local/share/rcm/dotfiles/config/software3/xdg-config" + + $ rcup + > assert_linked "$HOME/.config/software3/xdg-config" "$HOME/.local/share/rcm/dotfiles/config/software3/xdg-config" + > refute "used ~/.dotfiles" -r $HOME/.config/software3/traditional-config diff --git a/test/xdg.t b/test/xdg.t new file mode 100644 index 00000000..a4b66982 --- /dev/null +++ b/test/xdg.t @@ -0,0 +1,19 @@ + $ . "$TESTDIR/helper.sh" + $ unset RCRC + $ unset XDG_CONFIG_HOME + $ unset XDG_DATA_HOME + +Should support complete XDG-style directories and configuration + + $ mkdir -p .local/share/rcm/dotfiles + > mkdir -p ".local/share/rcm/dotfiles/config" + > mkdir -p ".local/share/rcm/dotfiles/config/software1" + > mkdir -p ".local/share/rcm/dotfiles/config/software4" + > touch ".local/share/rcm/dotfiles/config/software1/config" + > touch ".local/share/rcm/dotfiles/config/software4/config" + > touch ".local/share/rcm/dotfiles/config/software4/more" + + $ rcup + > assert_linked "$HOME/.config/software1/config" "$HOME/.local/share/rcm/dotfiles/config/software1/config" + > assert_linked "$HOME/.config/software4/config" "$HOME/.local/share/rcm/dotfiles/config/software4/config" + > assert_linked "$HOME/.config/software4/more" "$HOME/.local/share/rcm/dotfiles/config/software4/more"