@@ -63,6 +63,35 @@ downloader() {
6363 fi
6464}
6565
66+ check_latest_version () {
67+ local _executable=" monorepo-diff-buildkite-plugin"
68+ local _repo=" https://api.github.com/repos/buildkite-plugins/monorepo-diff-buildkite-plugin"
69+ local _version=" "
70+
71+ if check_cmd curl; then
72+ _version=$( curl -s " ${_repo} /releases/latest" | grep -oE ' "tag_name": "v[0-9]+\.[0-9]+\.[0-9]+"' | cut -d' "' -f4)
73+ elif check_cmd wget; then
74+ _version=$( wget -qO- " ${_repo} /releases/latest" | grep -oE ' "tag_name": "v[0-9]+\.[0-9]+\.[0-9]+"' | cut -d' "' -f4)
75+ fi
76+
77+ if [[ " $_version " =~ ^v[0-9]+\. [0-9]+\. [0-9]+ ]]; then
78+ true
79+ else
80+ _version=" "
81+ fi
82+
83+ RETVAL=" $_version "
84+ }
85+
86+ check_binary_version () {
87+ local _executable=" monorepo-diff-buildkite-plugin"
88+ local _version=" "
89+
90+ _version=$( BUILDKITE_PLUGIN_MONOREPO_DIFF_BUILDKITE_PLUGIN_TEST_MODE=true ./${_executable} 2>&1 | grep -oE ' running monorepo-diff-buildkite-plugin ([0-9]+\.[0-9]+\.[0-9]+)' | cut -d' ' -f3)
91+
92+ RETVAL=" v${_version} "
93+ }
94+
6695get_version () {
6796 local _plugin=${BUILDKITE_PLUGINS:- " " }
6897 local _version=" "
@@ -87,17 +116,35 @@ download_binary_and_run() {
87116 local _repo=" https://github.com/buildkite-plugins/monorepo-diff-buildkite-plugin"
88117
89118 get_version || return 1
90- local _version=" $RETVAL "
119+ local _specified_version=" $RETVAL "
120+ local _latest_version=" "
121+ local _binary_version=" "
122+ local test_mode=" ${BUILDKITE_PLUGIN_MONOREPO_DIFF_BUILDKITE_PLUGIN_TEST_MODE:- false} "
123+ local _url=" "
91124
92- if [ -z " ${_version} " ]; then
93- _url=${_repo} /releases/latest/download/${_executable} _${_arch}
94- else
95- _url=${_repo} /releases/download/${_version} /${_executable} _${_arch}
125+ if check_cmd " ./${_executable} " ; then
126+ if check_binary_version; then
127+ _binary_version=" $RETVAL "
128+ else
129+ say " Warning: Could not determine binary version, will download fresh copy"
130+ fi
96131 fi
97132
98- local test_mode=" ${BUILDKITE_PLUGIN_MONOREPO_DIFF_BUILDKITE_PLUGIN_TEST_MODE:- false} "
133+ if [[ " $test_mode " == " true" ]]; then
134+ true
135+ elif [ -z " ${_specified_version} " ]; then
136+ check_latest_version || return 1
137+ _latest_version=" $RETVAL "
138+ if [[ -z " $_binary_version " ]] || [[ " $_binary_version " != " $_latest_version " ]]; then
139+ _url=${_repo} /releases/latest/download/${_executable} _${_arch}
140+ fi
141+ else
142+ if [[ -z " $_binary_version " ]] || [[ " $_binary_version " != " $_specified_version " ]]; then
143+ _url=${_repo} /releases/download/${_specified_version} /${_executable} _${_arch}
144+ fi
145+ fi
99146
100- if [[ " $test_mode " == " false " ] ]; then
147+ if [ -n " ${_url} " ]; then
101148 if ! downloader " $_url " " $_executable " ; then
102149 say " failed to download $_url "
103150 exit 1
0 commit comments