在 Oh My Zsh 中自动更新自定义文件夹

Auto update custom folder in Oh My Zsh

我有自己的 Oh My Zsh 自定义主题,位于 .oh-my-zsh/custom 文件夹中。我将其托管在 GitHub 上(如 Version control of customizations 下的 OMZ 文档中所述)所以我所要做的就是

cd ~/.oh-my-zsh/custom ; git pull ; popd

并且更新正常,但我必须访问每个 machine/VM 我使用此自定义主题和 运行 手动命令的地方。如何绑定 Oh My Zsh 的自动更新系统来更新我的自定义主题?

在尝试贡献一个几乎被忽略的拉取请求之后,我找到了一种方法来绑定 Oh My Zsh 的更新。通过观察 .git 的 FETCH_HEAD 文件的 mod 日期,我可以在 Oh My Zsh 从 github 获取时触发我的更新,这对我来说绰绰有余使用。

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
# self-update

ts_file=~/.zsh-custom-update

upgrade_custom() {
  printf '3[0;34m%s3[0m\n' "Upgrading the custom files"
  cd "$ZSH_CUSTOM"
  if git pull --rebase --stat origin master
  then
    printf '3[0;34m%s3[0m\n' 'Hooray! The custom files have been updated and/or are at the current version.'
  else
    printf '3[0;31m%s3[0m\n' 'There was an error updating. Try again later?'
  fi
}

upgrade_custom_update() {
  echo -n "" >! $ts_file
}

upgrade_custom_check() {
  if [[ "$OSTYPE" == darwin* ]]; then
    ts=$(stat -f '%Sm' $ZSH/.git/FETCH_HEAD || echo 'missing' )
  else
    ts=$(stat -c %y $ZSH/.git/FETCH_HEAD || echo 'missing' )
  fi

  if [[ "$ts" == $( cat $ts_file || echo 'missing' ) ]] ; then
    #echo 'up to date'
  else
    upgrade_custom_update "$ts"
    upgrade_custom    
  fi
}

upgrade_custom_check

我没有编写 zsh 脚本的经验,因此欢迎提供反馈。我同时使用 Mac 和 Linux 但我不确定 stat 命令是否适用于 BSD。

您可以使用 OhMyZsh Full-autoupdate 插件。
它同时更新插件和主题。