Skip to content

Commit dfdab2c

Browse files
committed
Auto detect latest tag if no version is specified.
1 parent 6e47610 commit dfdab2c

File tree

4 files changed

+115
-16
lines changed

4 files changed

+115
-16
lines changed

scripts/examples/analyzeAntDesign.sh

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@
55

66
# Note: The first (and only) parameter is the version of "ant-design" to analyze.
77
# Note: This script is meant to be started in the root directory of this repository.
8+
# Note: This script requires "cURL" ( https://curl.se ) to be installed.
89

910
# Fail on any error ("-e" = exit on first error, "-o pipefail" exist on errors within piped commands)
1011
set -o errexit -o pipefail
1112

12-
# Read the first input argument containing the version of the artifacts
13-
if [ "$#" -ne 1 ]; then
14-
echo "analyzerAntDesign Error: Usage: $0 <version>" >&2
15-
exit 1
16-
fi
13+
# Read the first input argument containing the version of the project
1714
projectVersion=$1
15+
if [ -z "${projectVersion}" ]; then
16+
echo "analyzerAntDesign: Optional parameter <version> is not specified. Detecting latest version..." >&2
17+
echo "analyzerAntDesign: Usage example: $0 <version>" >&2
18+
projectVersion=$( ./../../scripts/examples/detectLatestGitTag.sh --url "https://github.com/ant-design/ant-design.git" )
19+
echo "analyzerAntDesign: Using latest version: ${projectVersion}" >&2
20+
fi
1821

1922
# Check if environment variable is set
2023
if [ -z "${NEO4J_INITIAL_PASSWORD}" ]; then

scripts/examples/analyzeAxonFramework.sh

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99
# Fail on any error ("-e" = exit on first error, "-o pipefail" exist on errors within piped commands)
1010
set -o errexit -o pipefail
1111

12-
# Read the first input argument containing the version of the artifacts
13-
if [ "$#" -ne 1 ]; then
14-
echo "analyzeAxonFramework Error: Usage: $0 <version>" >&2
15-
exit 1
16-
fi
1712
artifactsVersion=$1
13+
if [ -z "${artifactsVersion}" ]; then
14+
echo "analyzeAxonFramework: Optional parameter <version> is not specified. Detecting latest version..." >&2
15+
echo "analyzeAxonFramework: Usage example: $0 <version>" >&2
16+
artifactsVersion=$( ./../../scripts/examples/detectLatestGitTag.sh --url "https://github.com/AxonFramework/AxonFramework.git" --prefix "axon-")
17+
echo "analyzeAxonFramework: Using latest version: ${artifactsVersion}" >&2
18+
fi
1819

1920
# Check if environment variable is set
2021
if [ -z "${NEO4J_INITIAL_PASSWORD}" ]; then
21-
echo "analyzeAxonFramework: Error: Requires environment variable NEO4J_INITIAL_PASSWORD to be set first. Use 'export NEO4J_INITIAL_PASSWORD=<your-own-password>'."
22+
echo "analyzeAxonFramework: Error: Requires environment variable NEO4J_INITIAL_PASSWORD to be set first. Use 'export NEO4J_INITIAL_PASSWORD=<your-own-password>'." >&2
2223
exit 1
2324
fi
2425

scripts/examples/analyzeReactRouter.sh

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
# Fail on any error ("-e" = exit on first error, "-o pipefail" exist on errors within piped commands)
1010
set -o errexit -o pipefail
1111

12-
# Read the first input argument containing the version of the artifacts
13-
if [ "$#" -ne 1 ]; then
14-
echo "analyzerReactRouter Error: Usage: $0 <version>" >&2
15-
exit 1
16-
fi
12+
# Read the first input argument containing the version of the project
1713
projectVersion=$1
14+
if [ -z "${projectVersion}" ]; then
15+
echo "analyzerReactRouter: Optional parameter <version> is not specified. Detecting latest version..." >&2
16+
echo "analyzerReactRouter: Usage example: $0 <version>" >&2
17+
projectVersion=$( ./../../scripts/examples/detectLatestGitTag.sh --url "https://github.com/remix-run/react-router.git" )
18+
echo "analyzerReactRouter: Using latest version: ${projectVersion}" >&2
19+
fi
1820

1921
# Check if environment variable is set
2022
if [ -z "${NEO4J_INITIAL_PASSWORD}" ]; then
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/env bash
2+
3+
# Returns the latest tag of a remote repository given by its url.
4+
5+
# Fail on any error ("-e" = exit on first error, "-o pipefail" exist on errors within piped commands)
6+
set -o errexit -o pipefail
7+
8+
# Display how this command is intended to be used including an example when wrong input parameters were detected
9+
usage() {
10+
echo "" >&2
11+
echo "Usage: $0 --url <git-clone-url> [--prefix <tag prefix to ignore>]" >&2
12+
echo "Example: $0 --url https://github.com/ant-design/ant-design.git" >&2
13+
exit 1
14+
}
15+
16+
# Default command line option values
17+
url=""
18+
prefix=""
19+
20+
# Parse command line options
21+
while [[ $# -gt 0 ]]; do
22+
key="${1}"
23+
value="${2}"
24+
25+
case "${key}" in
26+
--url)
27+
url="${value}"
28+
shift
29+
;;
30+
--prefix)
31+
prefix="${value}"
32+
shift
33+
;;
34+
*)
35+
echo "detectLatestVersion Error: Unknown option: ${key}" >&2
36+
usage
37+
;;
38+
esac
39+
shift
40+
done
41+
42+
if [ -n "${url}" ]; then
43+
# url specified -> check if it is a valid URL
44+
if ! curl --head --fail --max-time 20 "${url}" >/dev/null 2>&1; then
45+
echo "detectLatestVersion Error: Invalid URL: ${url}" >&2
46+
exit 1
47+
fi
48+
else
49+
echo "detectLatestVersion: Error: Please specify an url." >&2
50+
fi
51+
52+
if [ -n "${prefix}" ]; then
53+
echo "detectLatestVersion: Ignoring tag prefix '${prefix}'." >&2
54+
fi
55+
56+
57+
# Check if the command succeeded
58+
if ! projectVersion=$( git ls-remote --sort "-version:refname" --tags "${url}" \
59+
| grep -E "refs/tags/${prefix:-.*}[0-9]+.*\w$" \
60+
| sed -n '1p' \
61+
| sed "s/.*\/${prefix}//" \
62+
)
63+
then
64+
redColor='\033[0;31m'
65+
noColor='\033[0m'
66+
echo -e "${redColor}detectLatestVersion: Error: Failed to detect latest git tag for ${url}($?):${noColor}" >&2
67+
echo -e "${redColor}${projectVersion}${noColor}" >&2
68+
exit 1
69+
fi
70+
# echo "detectLatestVersion: Detected latest version ${projectVersion} for ${url}." >&2
71+
72+
# # Retrieve latest release from GitHub API
73+
# # Extract the owner and repo of the url
74+
# url_without_github_domain="${url#https://github.com/}"
75+
# owner_and_repo="${url_without_github_domain%.*}"
76+
# github_latest_release_endpoint_url="https://api.github.com/repos/${owner_and_repo}/releases/latest"
77+
#
78+
# if ! projectVersion=$( curl --silent --fail-with-body "${github_latest_release_endpoint_url}" \
79+
# | grep '"tag_name":' \
80+
# | sed -E 's/.*"([^"]+)".*/\1/'\
81+
# )
82+
# then
83+
# redColor='\033[0;31m'
84+
# noColor='\033[0m'
85+
# echo -e "${redColor}detectLatestVersion: Error: Failed to detect latest version for ${github_latest_release_endpoint_url}($?):${noColor}" >&2
86+
# echo -e "${redColor}${projectVersion}${noColor}" >&2
87+
# exit 1
88+
# fi
89+
#
90+
# echo "detectLatestVersion: Detected latest GitHub release ${projectVersion} for ${github_latest_release_endpoint_url}." >&2
91+
92+
# Print the latest version as result
93+
echo "${projectVersion}"

0 commit comments

Comments
 (0)