Skip to content

Commit

Permalink
bazel/updater: Improve extensibility (#872)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Northey <[email protected]>
  • Loading branch information
phlax authored Oct 21, 2023
1 parent 9fc5a60 commit 8ebc85a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 21 deletions.
54 changes: 37 additions & 17 deletions bazel/dependency/bazel-update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,47 @@

set -o pipefail

JQ="$1"
UPDATE_TOOL="$2"
VERSION_FILE="$3"
DEP_DATA="$4"
DEP="$5"
VERSION="$6"
VERSION_FILE="$1"
DEP_DATA="$2"
DEP="$3"
VERSION="$4"

JQ="${JQ_BIN:-jq}"
if [[ -z "$JQ" || ! -x "$JQ" ]]; then
echo "jq binary not found: ${JQ}" >&2
exit 1
fi
if [[ -z "$DEP" || -z "$VERSION" ]]; then
echo "You need to supply dependency and sha to update"
echo "You need to supply dependency and sha to update" >&2
exit 1
fi
if [[ -n "$VERSION_PATH_REPLACE" ]]; then
IFS=':' read -ra path_replace <<< "$VERSION_PATH_REPLACE"
VERSION_FILE="${VERSION_FILE/${path_replace[0]}/${path_replace[1]}/}"
fi

pushd "${BUILD_WORKSPACE_DIRECTORY}" &> /dev/null
VERSION_FILE="$(realpath "${VERSION_FILE}")"
popd &> /dev/null
EXISTING_VERSION="$("${JQ}" -r ".${DEP}.version" "${DEP_DATA}")"
EXISTING_SHA="$("${JQ}" -r ".${DEP}.sha256" "${DEP_DATA}")"
REPO="$(${JQ} -r ".${DEP}.repo" "${DEP_DATA}")"
URL="$(${JQ} -r ".${DEP}.url" "${DEP_DATA}")"
DEP_SEARCH="\"${DEP}\": {"
VERSION_SEARCH="\"version\": \"${EXISTING_VERSION}\","
SHA_SEARCH="\"sha256\": \"${EXISTING_SHA}\","
REPO_SELECTOR="${REPO_SELECTOR:-".__DEP__.repo"}"
REPO_SELECTOR="${REPO_SELECTOR/__DEP__/${DEP}}"
REPO="$("${JQ}" -r "${REPO_SELECTOR}" "${DEP_DATA}")"
SHA_SELECTOR="${SHA_SELECTOR:-".__DEP__.sha256"}"
SHA_SELECTOR="${SHA_SELECTOR/__DEP__/${DEP}}"
EXISTING_SHA="$("${JQ}" -r "${SHA_SELECTOR}" "${DEP_DATA}")"
VERSION_SELECTOR="${VERSION_SELECTOR:-".__DEP__.version"}"
VERSION_SELECTOR="${VERSION_SELECTOR/__DEP__/${DEP}}"
EXISTING_VERSION="$("${JQ}" -r "${VERSION_SELECTOR}" "${DEP_DATA}")"
URL_SELECTOR="${URL_SELECTOR:-".__DEP__.urls[0]"}"
URL_SELECTOR="${URL_SELECTOR/__DEP__/${DEP}}"
URL="$(${JQ} -r "${URL_SELECTOR}" "${DEP_DATA}")"
DEP_SEARCH="${DEP_SEARCH:-"\"__DEP__\": {"}"
DEP_SEARCH="${DEP_SEARCH/__DEP__/${DEP}}"
VERSION_SEARCH="${VERSION_SEARCH:-"\"version\": \"__EXISTING_VERSION__\","}"
VERSION_SEARCH="${VERSION_SEARCH/__EXISTING_VERSION__/$EXISTING_VERSION}"
SHA_SEARCH="${SHA_SEARCH:-"\"sha256\": \"__EXISTING_SHA__\","}"
SHA_SEARCH="${SHA_SEARCH/__EXISTING_SHA__/$EXISTING_SHA}"


get_sha () {
local url sha repo version url
Expand All @@ -32,11 +51,12 @@ get_sha () {
version="$3"
url="${url//\{repo\}/${repo}}"
url="${url//\{version\}/${version}}"
url="${url//${EXISTING_VERSION}/${version}}"
sha="$(curl -sfL "${url}" | sha256sum | cut -d' ' -f1)" || {
echo "Failed to fetch asset (${url})" >&2
exit 1
}
printf "$sha"
printf '%s' "$sha"
}

find_version_line () {
Expand All @@ -54,7 +74,7 @@ find_version_line () {
| head -n1 \
| cut -d: -f1)"
version_match_ln="$((match_ln + version_match_ln - 1))"
printf "$version_match_ln"
printf '%s' "$version_match_ln"
}

find_sha_line () {
Expand All @@ -72,7 +92,7 @@ find_sha_line () {
| head -n1 \
| cut -d: -f1)"
sha_match_ln="$((match_ln + sha_match_ln - 1))"
printf "$sha_match_ln"
printf '%s' "$sha_match_ln"
}

update_sha () {
Expand Down
41 changes: 37 additions & 4 deletions bazel/dependency/macros.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,63 @@ def updater(
version_file,
jq_toolchain = "@jq_toolchains//:resolved_toolchain",
update_script = "@envoy_toolshed//dependency:bazel-update.sh",
sha_updater = "@envoy_toolshed//sha:replace",
data = None,
deps = None,
dep_search = None,
sha_search = None,
version_search = None,
repo_selector = None,
sha_selector = None,
url_selector = None,
version_path_replace = None,
version_selector = None,
toolchains = None,
pydict = False,
**kwargs,
):
toolchains = [jq_toolchain] + (toolchains or [])
data = (data or []) + [
jq_toolchain,
sha_updater,
update_script,
dependencies,
version_file,
]
args = [
"$(JQ_BIN)",
"$(location %s)" % sha_updater,
"$(location %s)" % version_file,
"$(location %s)" % dependencies,
]
env = {
"JQ_BIN": "$(JQ_BIN)",
}
if pydict:
env["DEP_SEARCH"] = "__DEP__ = dict("
env["SHA_SEARCH"] = "sha256 = \"__EXISTING_SHA__\","
env["VERSION_SEARCH"] = "version = \"__EXISTING_VERSION__\","

if dep_search:
env["DEP_SEARCH"] = dep_search
if sha_search:
env["SHA_SEARCH"] = sha_search
if version_search:
env["VERSION_SEARCH"] = version_search
if repo_selector:
env["REPO_SELECTOR"] = repo_selector
if sha_selector:
env["SHA_SELECTOR"] = sha_selector
if url_selector:
env["URL_SELECTOR"] = url_selector
if version_path_replace:
env["VERSION_PATH_REPLACE"] = version_path_replace
if version_selector:
env["VERSION_SELECTOR"] = version_selector

native.sh_binary(
name = name,
srcs = [update_script],
data = data,
env = env,
args = args,
deps = deps or [],
toolchains = toolchains,
**kwargs,
)

0 comments on commit 8ebc85a

Please sign in to comment.