b2313ec7493dc127c9712f20654579221d0bcce6

Author
Alex <alexpjohnson@gmail.com>
Committer
Marc Cornellà <hello@mcornella.com>
Date

Message

feat(installer): respect and install in `$ZDOTDIR` if set (#9376)

Fixes #9001
Fixes #10479

Closes #9376

Co-authored-by: Marc Cornellà <hello@mcornella.com>

Diff

  1diff --git a/tools/install.sh b/tools/install.sh
  2index a6538f9d71c1dbd7c5ffbe255a31094bea97896b..4582ed03ee5977a208a8c07dca2d7a1ca4a31609 100755
  3--- a/tools/install.sh
  4+++ b/tools/install.sh
  5@@ -16,6 +16,9 @@
  6 #   ZSH=~/.zsh sh install.sh
  7 #
  8 # Respects the following environment variables:
  9+#   ZDOTDIR - path to Zsh dotfiles directory (default: unset). See [1][2]
 10+#             [1] https://zsh.sourceforge.io/Doc/Release/Parameters.html#index-ZDOTDIR
 11+#             [2] https://zsh.sourceforge.io/Doc/Release/Files.html#index-ZDOTDIR_002c-use-of
 12 #   ZSH     - path to the Oh My Zsh repository folder (default: $HOME/.oh-my-zsh)
 13 #   REPO    - name of the GitHub repo to install from (default: ohmyzsh/ohmyzsh)
 14 #   REMOTE  - full remote URL of the git repo to install (default: GitHub via HTTPS)
 15@@ -53,8 +56,17 @@ HOME="${HOME:-$(eval echo ~$USER)}"
 16 # Track if $ZSH was provided
 17 custom_zsh=${ZSH:+yes}
 18 
 19-# Default settings
 20+# Use $zdot to keep track of where the directory is for zsh dotfiles
 21+# To check if $ZDOTDIR was provided, explicitly check for $ZDOTDIR
 22+zdot="${ZDOTDIR:-$HOME}"
 23+
 24+# Default value for $ZSH
 25+# a) if $ZDOTDIR is supplied: $ZDOTDIR/ohmyzsh
 26+# b) otherwise, $HOME/.oh-my-zsh
 27+ZSH="${ZSH:-${ZDOTDIR:+$ZDOTDIR/ohmyzsh}}"
 28 ZSH="${ZSH:-$HOME/.oh-my-zsh}"
 29+
 30+# Default settings
 31 REPO=${REPO:-ohmyzsh/ohmyzsh}
 32 REMOTE=${REMOTE:-https://github.com/${REPO}.git}
 33 BRANCH=${BRANCH:-master}
 34@@ -311,11 +323,11 @@ setup_zshrc() {
 35   echo "${FMT_BLUE}Looking for an existing zsh config...${FMT_RESET}"
 36 
 37   # Must use this exact name so uninstall.sh can find it
 38-  OLD_ZSHRC=~/.zshrc.pre-oh-my-zsh
 39-  if [ -f ~/.zshrc ] || [ -h ~/.zshrc ]; then
 40+  OLD_ZSHRC="$zdot/.zshrc.pre-oh-my-zsh"
 41+  if [ -f "$zdot/.zshrc" ] || [ -h "$zdot/.zshrc" ]; then
 42     # Skip this if the user doesn't want to replace an existing .zshrc
 43     if [ "$KEEP_ZSHRC" = yes ]; then
 44-      echo "${FMT_YELLOW}Found ~/.zshrc.${FMT_RESET} ${FMT_GREEN}Keeping...${FMT_RESET}"
 45+      echo "${FMT_YELLOW}Found ${zdot}/.zshrc.${FMT_RESET} ${FMT_GREEN}Keeping...${FMT_RESET}"
 46       return
 47     fi
 48     if [ -e "$OLD_ZSHRC" ]; then
 49@@ -327,19 +339,22 @@ setup_zshrc() {
 50       fi
 51       mv "$OLD_ZSHRC" "${OLD_OLD_ZSHRC}"
 52 
 53-      echo "${FMT_YELLOW}Found old ~/.zshrc.pre-oh-my-zsh." \
 54+      echo "${FMT_YELLOW}Found old .zshrc.pre-oh-my-zsh." \
 55         "${FMT_GREEN}Backing up to ${OLD_OLD_ZSHRC}${FMT_RESET}"
 56     fi
 57-    echo "${FMT_YELLOW}Found ~/.zshrc.${FMT_RESET} ${FMT_GREEN}Backing up to ${OLD_ZSHRC}${FMT_RESET}"
 58-    mv ~/.zshrc "$OLD_ZSHRC"
 59+    echo "${FMT_YELLOW}Found ${zdot}/.zshrc.${FMT_RESET} ${FMT_GREEN}Backing up to ${OLD_ZSHRC}${FMT_RESET}"
 60+    mv "$zdot/.zshrc" "$OLD_ZSHRC"
 61   fi
 62 
 63-  echo "${FMT_GREEN}Using the Oh My Zsh template file and adding it to ~/.zshrc.${FMT_RESET}"
 64+  echo "${FMT_GREEN}Using the Oh My Zsh template file and adding it to $zdot/.zshrc.${FMT_RESET}"
 65+
 66+  # Modify $ZSH variable in .zshrc directory to use the literal $ZDOTDIR or $HOME
 67+  omz="$ZSH"
 68+  [ -z "$ZDOTDIR" ] || omz=$(echo "$omz" | sed "s|^$ZDOTDIR/|\$ZDOTDIR/|")
 69+  omz=$(echo "$omz" | sed "s|^$HOME/|\$HOME/|")
 70 
 71-  # Replace $HOME path with '$HOME' in $ZSH variable in .zshrc file
 72-  omz=$(echo "$ZSH" | sed "s|^$HOME/|\$HOME/|")
 73-  sed "s|^export ZSH=.*$|export ZSH=\"${omz}\"|" "$ZSH/templates/zshrc.zsh-template" > ~/.zshrc-omztemp
 74-  mv -f ~/.zshrc-omztemp ~/.zshrc
 75+  sed "s|^export ZSH=.*$|export ZSH=\"${omz}\"|" "$ZSH/templates/zshrc.zsh-template" > "$zdot/.zshrc-omztemp"
 76+  mv -f "$zdot/.zshrc-omztemp" "$zdot/.zshrc"
 77 
 78   echo
 79 }
 80@@ -407,9 +422,9 @@ EOF
 81 
 82   # We're going to change the default shell, so back up the current one
 83   if [ -n "$SHELL" ]; then
 84-    echo "$SHELL" > ~/.shell.pre-oh-my-zsh
 85+    echo "$SHELL" > "$zdot/.shell.pre-oh-my-zsh"
 86   else
 87-    grep "^$USER:" /etc/passwd | awk -F: '{print $7}' > ~/.shell.pre-oh-my-zsh
 88+    grep "^$USER:" /etc/passwd | awk -F: '{print $7}' > "$zdot/.shell.pre-oh-my-zsh"
 89   fi
 90 
 91   echo "Changing your shell to $zsh..."
 92@@ -451,7 +466,7 @@ print_success() {
 93   printf '\n'
 94   printf '\n'
 95   printf "%s %s %s\n" "Before you scream ${FMT_BOLD}${FMT_YELLOW}Oh My Zsh!${FMT_RESET} look over the" \
 96-    "$(fmt_code "$(fmt_link ".zshrc" "file://$HOME/.zshrc" --text)")" \
 97+    "$(fmt_code "$(fmt_link ".zshrc" "file://$zdot/.zshrc" --text)")" \
 98     "file to select plugins, themes, and options."
 99   printf '\n'
100   printf '%s\n' "• Follow us on Twitter: $(fmt_link @ohmyzsh https://twitter.com/ohmyzsh)"