xref: /aosp_15_r20/external/google-cloud-java/generation/update_versions.sh (revision 55e87721aa1bc457b326496a7ca40f3ea1a63287)
1#!/bin/bash
2
3set -e
4
5function retry_with_backoff {
6  attempts_left=$1
7  sleep_seconds=$2
8  shift 2
9  command=$@
10
11  # store current flag state
12  flags=$-
13
14  # allow a failures to continue
15  set +e
16  ${command}
17  exit_code=$?
18
19  # restore "e" flag
20  if [[ ${flags} =~ e ]]
21  then set -e
22  else set +e
23  fi
24
25  if [[ $exit_code == 0 ]]
26  then
27    return 0
28  fi
29
30  # failure
31  if [[ ${attempts_left} -gt 0 ]]
32  then
33    echo "failure (${exit_code}), sleeping ${sleep_seconds}..."
34    sleep ${sleep_seconds}
35    new_attempts=$((${attempts_left} - 1))
36    new_sleep=$((${sleep_seconds} * 2))
37    retry_with_backoff ${new_attempts} ${new_sleep} ${command}
38  fi
39
40  return $exit_code
41}
42
43snapshot_flag=''
44while getopts 's' flag; do
45  case "${flag}" in
46    s) snapshot_flag='true' ;;
47    *) snapshot_flag='false' ;;
48  esac
49done
50
51if [[ "${snapshot_flag}" = "true" ]]; then
52  echo "Bump the current version to -SNAPSHOT"
53else
54  echo "Updating current version to latest from Maven"
55fi
56
57count=0
58missing_artifacts=()
59
60
61path=.
62versions_array=($(grep -E "^.*:[0-9]+\.[0-9]+\.[0-9]+.*:[0-9]+\.[0-9]+\.[0-9]+.*$" "${path}/versions.txt"))
63
64for line in "${versions_array[@]}"; do
65  artifactId=$(echo "${line}" | cut -d ":" -f1)
66
67  if [[ "${artifactId}" =~ .*grafeas.* ]]; then
68    maven_url="https://repo1.maven.org/maven2/io/grafeas/${artifactId}/maven-metadata.xml"
69  elif [[ "${artifactId}" =~ .*area120.* ]] && [[ "${artifactId}" =~ ^google- ]]; then
70    maven_url="https://repo1.maven.org/maven2/com/google/area120/${artifactId}/maven-metadata.xml"
71  elif [[ "${artifactId}" =~ .*analytics-.* ]] && [[ "${artifactId}" =~ ^google- ]]; then
72    maven_url="https://repo1.maven.org/maven2/com/google/analytics/${artifactId}/maven-metadata.xml"
73  elif [[ "${artifactId}" =~ ^(google-|gapic) ]]; then
74    maven_url="https://repo1.maven.org/maven2/com/google/cloud/${artifactId}/maven-metadata.xml"
75  else
76    maven_url="https://repo1.maven.org/maven2/com/google/api/grpc/${artifactId}/maven-metadata.xml"
77  fi
78
79  count=$((count + 1))
80  echo "Module #${count} -- Downloading ${artifactId} from ${maven_url}"
81  # Check if the artifact exists in Maven Central, otherwise add to missing_artifacts
82  if curl --output /dev/null --silent --head --fail "${maven_url}"; then
83    metadata_file=$(retry_with_backoff 3 10 curl -s "${maven_url}" -H "Accept:application/xml" --limit-rate 200k)
84
85    # Versioning of artifacts in Maven Central follow SemVer (Major.Minor.Patch-{alpha|beta})
86    # This keeps track of the additional versioning after the PATCH value (alpha/beta)
87    # `cut` normally returns the entire string if the delimiter DNE. The `-s` makes cut return nothing
88    # maven_latest_version stores Major.Minor.Patch or the entire version
89    # maven_latest_trailing stores alpha/beta/etc. or nothing
90    maven_metadata_version=$(echo "${metadata_file}" | grep 'latest' | cut -d '>' -f 2 | cut -d '<' -f 1)
91    maven_latest_version=$(echo "${maven_metadata_version}" | cut -d "-" -f1)
92    maven_latest_trailing=$(echo "${maven_metadata_version}" | cut -s -d "-" -f2-)
93
94    major_version=$(echo "${maven_latest_version}" | cut -d "." -f1)
95    minor_version=$(echo "${maven_latest_version}" | cut -d "." -f2)
96    patch_version=$(echo "${maven_latest_version}" | cut -d "." -f3)
97    patch_version_bump=$((patch_version + 1))
98    if [[ -z "${maven_latest_trailing}" ]]; then
99      maven_version_bump="${major_version}.${minor_version}.${patch_version_bump}"
100    else
101      maven_version_bump="${major_version}.${minor_version}.${patch_version_bump}-${maven_latest_trailing}"
102    fi
103    if [[ "${snapshot_flag}" = "true" ]]; then
104      new_version="${artifactId}:${maven_metadata_version}:${maven_version_bump}-SNAPSHOT"
105    else
106      new_version="${artifactId}:${maven_metadata_version}:${maven_metadata_version}"
107    fi
108
109    sed -i.bak "s/${line}/${new_version}/g" "${path}/versions.txt" && rm "${path}/versions.txt.bak"
110  else
111    missing_artifacts+=("${artifactId}")
112  fi
113done
114
115echo "These artifacts don't exist: ${missing_artifacts[*]}"