From 7d114558f8c74d9a71b6ab84a85186c0940065a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=85=E6=88=8E=E6=B0=8F?= Date: Sun, 5 Jun 2022 02:53:33 +0800 Subject: [PATCH] feat(plugins): support plugin specs in slug@branch format after checking out, run a custom function for each plugin. (action-install-plugins-macos.sh): loop over RIME_PLUGINS instead of plugins/* directories; the behaviour is now consistent with the windows script. ci scripts: update version-info.txt to log plugin slugs instead of directories. --- action-install-plugins-macos.sh | 20 +++++++++------ action-install-plugins-windows.bat | 30 +++++++++++++++++++--- install-plugins.sh | 41 +++++++++++++++++++++++++++--- plugins/CMakeLists.txt | 1 + 4 files changed, 76 insertions(+), 16 deletions(-) diff --git a/action-install-plugins-macos.sh b/action-install-plugins-macos.sh index 470b53ae3..d8888c59e 100755 --- a/action-install-plugins-macos.sh +++ b/action-install-plugins-macos.sh @@ -5,14 +5,18 @@ export RIME_ROOT="$(cd "$(dirname "$0")"; pwd)" echo "RIME_PLUGINS=${RIME_PLUGINS}" > version-info.txt echo "librime $(git describe --always)" >> version-info.txt +function action_install_plugin() { + local plugin="$1" + local plugin_dir="$2" + echo "${plugin} $(git -C "${plugin_dir}" describe --always)" >> version-info.txt + if [[ -e "${plugin_dir}/action-install.sh" ]]; then + (cd "${plugin_dir}"; bash ./action-install.sh) + fi +} + +declare -fx action_install_plugin + if [[ -n "${RIME_PLUGINS}" ]]; then # intentionally unquoted: ${RIME_PLUGINS} is a space separated list of slugs - bash ./install-plugins.sh ${RIME_PLUGINS} - for plugin_dir in plugins/*; do - [[ -d "${plugin_dir}" ]] || continue - echo "${plugin_dir} $(git -C "${plugin_dir}" describe --always)" >> version-info.txt - if [[ -e "${plugin_dir}/action-install.sh" ]]; then - (cd "${plugin_dir}"; bash ./action-install.sh) - fi - done + bash ./install-plugins.sh run=action_install_plugin ${RIME_PLUGINS} fi diff --git a/action-install-plugins-windows.bat b/action-install-plugins-windows.bat index c540e971c..323def4f9 100644 --- a/action-install-plugins-windows.bat +++ b/action-install-plugins-windows.bat @@ -2,6 +2,12 @@ setlocal if not defined RIME_ROOT set RIME_ROOT=%CD% +rem for GitHub pull request #1, git checkout 1/merge +set clone_options=^ + --config "remote.origin.fetch=+refs/pull/*:refs/remotes/origin/*" ^ + --depth 1 ^ + --no-single-branch + echo RIME_PLUGINS=%RIME_PLUGINS% > version-info.txt echo librime >> version-info.txt git describe --always >> version-info.txt @@ -12,13 +18,29 @@ if defined RIME_PLUGINS ( exit /b :install_plugin -set slug=%1 -echo plugin: %slug% +set plugin=%1 +echo plugin: %plugin% +for /f "delims=@" %%i in ("%plugin%") do ( + set slug=%%i + goto :got_slug +) +:got_slug +if %slug% == %plugin% ( + set branch= +) else ( + set branch=%plugin:*@=% +) set plugin_project=%slug:*/=% set plugin_dir=plugins\%plugin_project:librime-=% -git clone --depth 1 "https://github.com/%slug%.git" %plugin_dir% +git clone %clone_options% "https://github.com/%slug%.git" %plugin_dir% if errorlevel 1 exit /b -echo %plugin_dir% >> version-info.txt +rem pull request ref doesn't work with git clone --branch +if not [%branch%] == [] ( + git -C %plugin_dir% checkout %branch% + if errorlevel 1 exit /b +) +:action_install_plugin +echo %plugin% >> version-info.txt git -C %plugin_dir% describe --always >> version-info.txt if exist %plugin_dir%\action-install.bat ( pushd %plugin_dir% diff --git a/install-plugins.sh b/install-plugins.sh index 467572fac..719b07c5e 100755 --- a/install-plugins.sh +++ b/install-plugins.sh @@ -1,17 +1,50 @@ #!/bin/bash +set -e + cd "$(dirname "$0")" -for slug in "$@" +clone_options=( + # for GitHub pull request #1, git checkout 1/merge + --config 'remote.origin.fetch=+refs/pull/*:refs/remotes/origin/*' + # shallow clone + --depth 1 + # fetch all branches + --no-single-branch +) + +if [[ "${1}" =~ run=.* ]]; then + custom_install="${1#run=}" + shift +fi + +for plugin in "$@" do + if [[ "${plugin}" =~ @ ]]; then + slug="${plugin%@*}" + branch="${plugin#*@}" + else + slug="${plugin}" + branch='' + fi plugin_project="${slug##*/}" plugin_dir="plugins/${plugin_project#librime-}" if [[ -d "${plugin_dir}" ]] then - echo "Updating plugin: ${plugin_dir}" - git -C "${plugin_dir}" checkout master + echo "Updating ${plugin} in ${plugin_dir}" + if [[ -n "${branch}" ]]; then + git -C "${plugin_dir}" checkout "${branch}" + fi git -C "${plugin_dir}" pull else - git clone --depth 1 "https://github.com/${slug}.git" "${plugin_dir}" + echo "Checking out ${plugin} to ${plugin_dir}" + git clone "${clone_options[@]}" "https://github.com/${slug}.git" "${plugin_dir}" + # pull request ref doesn't work with git clone --branch + if [[ -n "${branch}" ]]; then + git -C "${plugin_dir}" checkout "${branch}" + fi + fi + if [[ -n "${custom_install}" ]]; then + ${custom_install} "${plugin}" "${plugin_dir}" fi done diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index e70438f5a..9e6fe47e0 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -26,6 +26,7 @@ if(DEFINED ENV{RIME_PLUGINS}) set(plugins $ENV{RIME_PLUGINS}) message(STATUS "Prescribed plugins: ${plugins}") if(NOT "${plugins}" STREQUAL "") + string(REGEX REPLACE "@[^ ]*" "" plugins ${plugins}) string(REGEX REPLACE "[^ ]*/(librime-)?" "" plugins ${plugins}) string(REPLACE " " ";" plugins ${plugins}) endif()