|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Usage: ./update_west_yml.sh <new-revision> |
| 3 | +# |
| 4 | +# Description: |
| 5 | +# |
| 6 | +# Update a west.yaml for use in a NCS Project to a new NCS revision. |
| 7 | +# This script will: |
| 8 | +# - Fetch the west.yml file from the NCS repository for the specified revision from github. |
| 9 | +# - Fetch the west.yml file for the corresponding Zephyr revision from github. |
| 10 | +# - Update the local west.yml file with the new NCS and Zephyr revisions and update |
| 11 | +# the name-allowlists of both. |
| 12 | +# |
| 13 | +# Requires yq to be installed. It can be installed with: |
| 14 | +# snap install yq |
| 15 | +# |
| 16 | +# Arguments: |
| 17 | +# |
| 18 | +# new-revision The new NCS revision with the format vMAJOR.MINOR.PATCH. |
| 19 | +# |
| 20 | + |
| 21 | +NUM_ARGS=1 |
| 22 | +# Print help text and exit if -h, or --help or insufficient number of arguments |
| 23 | +# was given. |
| 24 | +if [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ $# -lt ${NUM_ARGS} ]; then |
| 25 | + sed -ne '/^#/!q;s/.\{1,2\}//;1d;p' <"$0" |
| 26 | + exit 1 |
| 27 | +fi |
| 28 | + |
| 29 | +# Check that yq is installed |
| 30 | +if ! command -v yq &>/dev/null; then |
| 31 | + echo "yq is not installed. Please install yq to run this script." |
| 32 | + echo "You can install it with: snap install yq" |
| 33 | + exit 1 |
| 34 | +fi |
| 35 | + |
| 36 | +SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)" |
| 37 | +WEST_YML_PATH="${SCRIPT_DIR}/../west.yml" |
| 38 | + |
| 39 | +NEW_NCS_REVISION="${1}" |
| 40 | + |
| 41 | +# Check that the west.yml file exists |
| 42 | +if [ ! -f "${WEST_YML_PATH}" ]; then |
| 43 | + echo "The specified west.yml file does not exist: ${WEST_YML_PATH}" |
| 44 | + exit 1 |
| 45 | +fi |
| 46 | + |
| 47 | +# Validate the argument against the regex |
| 48 | +SEMVER_REGEX="v[0-9]+\.[0-9]+\.[0-9]+" |
| 49 | +if [[ ! ${NEW_NCS_REVISION} =~ ${SEMVER_REGEX} ]]; then |
| 50 | + echo "Invalid revision argument: ${NEW_NCS_REVISION}" |
| 51 | + echo "The revision must be in the format vMAJOR.MINOR.PATCH, e.g. v1.2.3" |
| 52 | + exit 1 |
| 53 | +fi |
| 54 | + |
| 55 | +# Read the existing west.yml file and get the NCS NEW_NCS_REVISION |
| 56 | +current_revision=$(yq '.manifest.projects[] | select(.name == "nrf").revision' "${WEST_YML_PATH}") |
| 57 | + |
| 58 | +echo "Current NCS revision in west.yml: $current_revision" |
| 59 | +echo "Updating to: ${NEW_NCS_REVISION}" |
| 60 | + |
| 61 | +# if the current revision is the same as the new revision, exit |
| 62 | +if [ "${current_revision}" = "${NEW_NCS_REVISION}" ]; then |
| 63 | + echo "The current NCS revision is already set to ${NEW_NCS_REVISION}. No changes made." |
| 64 | + exit 0 |
| 65 | +fi |
| 66 | + |
| 67 | +# Fetch the west.yml from remote NCS repository |
| 68 | +# if fault, exit |
| 69 | +if ! wget -q --output-document _new_ncs_west.yml "https://raw.githubusercontent.com/nrfconnect/sdk-nrf/refs/tags/${NEW_NCS_REVISION}/west.yml"; then |
| 70 | + echo "Failed to fetch the west.yml file from the NCS repository for revision ${NEW_NCS_REVISION}." |
| 71 | + echo "Check if the revision is correct and if the repository is accessible: https://raw.githubusercontent.com/nrfconnect/sdk-nrf/refs/tags/${NEW_NCS_REVISION}/west.yml" |
| 72 | + exit 1 |
| 73 | +fi |
| 74 | + |
| 75 | +# Get the corresponding Zephyr revision from the new NCS west.yml |
| 76 | +NEW_ZEPHYR_REVISION=$(yq '.manifest.projects[] | select(.name == "zephyr").revision' _new_ncs_west.yml) |
| 77 | + |
| 78 | +# Also download the new Zephyr west.yml file |
| 79 | +if ! wget -q --output-document _new_zephyr_west.yml "https://raw.githubusercontent.com/nrfconnect/sdk-zephyr/refs/tags/${NEW_ZEPHYR_REVISION}/west.yml"; then |
| 80 | + echo "Failed to fetch the west.yml file from the Zephyr repository for revision ${NEW_ZEPHYR_REVISION}." |
| 81 | + echo "Check if the revision is correct and if the repository is accessible: https://raw.githubusercontent.com/nrfconnect/sdk-zephyr/refs/tags/${NEW_ZEPHYR_REVISION}/west.yml" |
| 82 | + exit 1 |
| 83 | +fi |
| 84 | + |
| 85 | +# We now have the new NCS west.yml and the new Zephyr west.yml files. |
| 86 | +# For each manifest.projects[].name in the new NCS west.yml, add it to the |
| 87 | +# nrf projects name-allowlist in the local west.yml file. |
| 88 | +# Repeat similarly for zephyr remote project names, adding them to the |
| 89 | +# zephyr projects name-allowlist in the local west.yml file. |
| 90 | + |
| 91 | +# Get what we want from the new NCS west.yml |
| 92 | +yq '.manifest.projects[].name | select(. != "zephyr")' ./_new_ncs_west.yml >_a.txt |
| 93 | +# Sort alphabetically and remove duplicates |
| 94 | +sort -o _a.txt -u _a.txt |
| 95 | +# Now add " - " in front of each line (This gives us a valid "yaml list") |
| 96 | +sed -i 's/^/ - /' _a.txt |
| 97 | +# Place the content of _a.txt into the west.yml file |
| 98 | +yq -i '.manifest.projects[] |= select(.name == "nrf").import.name-allowlist = load("_a.txt")' "${WEST_YML_PATH}" |
| 99 | + |
| 100 | +# repeat the same process for the Zephyr west.yml |
| 101 | +yq '.manifest.projects[].name' ./_new_zephyr_west.yml >_b.txt |
| 102 | +sort -o _b.txt -u _b.txt |
| 103 | +sed -i 's/^/ - /' _b.txt |
| 104 | +yq -i '.manifest.projects[] |= select(.name == "zephyr").import.name-allowlist = load("_b.txt")' "${WEST_YML_PATH}" |
| 105 | + |
| 106 | +# Update the nrf and zephyr revisions in the west.yml file |
| 107 | +yq -i '.manifest.projects[] |= select(.name == "nrf").revision = "'"${NEW_NCS_REVISION}"'"' "${WEST_YML_PATH}" |
| 108 | +yq -i '.manifest.projects[] |= select(.name == "zephyr").revision = "'"${NEW_ZEPHYR_REVISION}"'"' "${WEST_YML_PATH}" |
| 109 | + |
| 110 | +# Cleanup temporary files |
| 111 | +rm _new_ncs_west.yml _new_zephyr_west.yml _a.txt _b.txt |
| 112 | + |
| 113 | +echo "" |
| 114 | +echo "" |
| 115 | +echo "west.yml has been updated to NCS revision ${NEW_NCS_REVISION}." |
| 116 | +echo "Check the file and comment-out all modules in the name-allowlists that are not needed in this project." |
| 117 | +echo "After that, run 'east update' to fetch the new modules." |
| 118 | + |
| 119 | +exit 0 |
0 commit comments