|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | +# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 | + |
| 7 | +# Glean SDK metrics build script. |
| 8 | +# |
| 9 | +# More about Glean at https://mozilla.github.io/glean |
| 10 | +# |
| 11 | +# This script generates metrics and pings as defined in user-provided files |
| 12 | +# and generates Swift code to be included in the final build. |
| 13 | +# It uses the `glean_parser`. |
| 14 | +# See https://mozilla.github.io/glean_parser/ for details. |
| 15 | +# |
| 16 | +# To use it in a Swift project, follow these steps: |
| 17 | +# 1. Import the `sdk_generator.sh` script into your project. |
| 18 | +# 2. Add your `metrics.yaml` and (optionally) `pings.yaml` and `tags.yaml` to your project. |
| 19 | +# 3. Add a new "Run Script" build step and set the command to `bash $PWD/sdk_generator.sh` |
| 20 | +# 4. Add your definition files (`metrics.yaml`, `pings.yaml`, `tags.yaml`) as Input Files for the "Run Script" step. |
| 21 | +# 5. Run the build. |
| 22 | +# 6. Add the files in the `Generated` folder to your project. |
| 23 | +# 7. Add the same files from the `Generated` folder as Output Files of the newly created "Run Script" step. |
| 24 | +# 8. Start using the generated metrics. |
| 25 | + |
| 26 | +set -e |
| 27 | + |
| 28 | +GLEAN_PARSER_VERSION=18.0 |
| 29 | + |
| 30 | +# CMDNAME is used in the usage text below. |
| 31 | +# shellcheck disable=SC2034 |
| 32 | +CMDNAME=$(basename "$0") |
| 33 | +USAGE=$(cat <<'HEREDOC' |
| 34 | +$(CMDNAME) |
| 35 | + |
| 36 | +
|
| 37 | +Glean SDK metrics build script. |
| 38 | +
|
| 39 | +More about Glean at https://mozilla.github.io/glean |
| 40 | +
|
| 41 | +This script generates metrics and pings as defined in user-provided files |
| 42 | +and generates Swift code to be included in the final build. |
| 43 | +It uses the `glean_parser`. |
| 44 | +See https://mozilla.github.io/glean_parser/ for details. |
| 45 | +
|
| 46 | +This script should be executed as a "Run Build Script" phase from Xcode. |
| 47 | +
|
| 48 | +USAGE: |
| 49 | + ${CMDNAME} [OPTIONS] [PATH ...] |
| 50 | +
|
| 51 | +ARGS: |
| 52 | + <PATH>... Explicit list of definition files to parse. |
| 53 | + If not specified the plugin will use the \$SCRIPT_INPUT_FILE_{N} environment variables. |
| 54 | +
|
| 55 | +OPTIONS: |
| 56 | + -a, --allow-reserved Allow reserved names. |
| 57 | + -o, --output <PATH> Folder to place generated code in. Default: \$SOURCE_ROOT/\$PROJECT/Generated |
| 58 | + -g, --glean-namespace <NAME> The Glean namespace to use in generated code. |
| 59 | + -m, --markdown <PATH> Generate markdown documentation in provided directory. |
| 60 | + -b, --build-date <TEXT> Set a specific build date or disable build date generation with `0`. |
| 61 | + --expire-by-version <INTEGER> Expire metrics by version, with the provided major version. |
| 62 | + -h, --help Display this help message. |
| 63 | +HEREDOC |
| 64 | +) |
| 65 | + |
| 66 | +helptext() { |
| 67 | + echo "$USAGE" |
| 68 | +} |
| 69 | + |
| 70 | +declare -a PARAMS=() |
| 71 | +ALLOW_RESERVED="" |
| 72 | +GLEAN_NAMESPACE=Glean |
| 73 | +DOCS_DIRECTORY="" |
| 74 | +BUILD_DATE="" |
| 75 | +EXPIRE_VERSION="" |
| 76 | +declare -a YAML_FILES=() |
| 77 | +OUTPUT_DIR="${SOURCE_ROOT}/${PROJECT}/Generated" |
| 78 | + |
| 79 | +while (( "$#" )); do |
| 80 | + case "$1" in |
| 81 | + -a|--allow-reserved) |
| 82 | + ALLOW_RESERVED="--allow-reserved" |
| 83 | + shift |
| 84 | + ;; |
| 85 | + -o|--output) |
| 86 | + OUTPUT_DIR=$2 |
| 87 | + shift 2 |
| 88 | + ;; |
| 89 | + -g|--glean-namespace) |
| 90 | + GLEAN_NAMESPACE=$2 |
| 91 | + shift 2 |
| 92 | + ;; |
| 93 | + -m|--markdown) |
| 94 | + DOCS_DIRECTORY=$2 |
| 95 | + shift 2 |
| 96 | + ;; |
| 97 | + -b|--build-date) |
| 98 | + BUILD_DATE="--option build_date=$2" |
| 99 | + shift 2 |
| 100 | + ;; |
| 101 | + --expire-by-version) |
| 102 | + EXPIRE_VERSION="--expire-by-version $2" |
| 103 | + shift 2 |
| 104 | + ;; |
| 105 | + -h|--help) |
| 106 | + helptext |
| 107 | + exit 0 |
| 108 | + ;; |
| 109 | + --) # end argument parsing |
| 110 | + shift |
| 111 | + break |
| 112 | + ;; |
| 113 | + --*=|-*) # unsupported flags |
| 114 | + echo "Error: Unsupported flag $1" >&2 |
| 115 | + exit 1 |
| 116 | + ;; |
| 117 | + *) # preserve positional arguments |
| 118 | + PARAMS+=("$1") |
| 119 | + shift |
| 120 | + ;; |
| 121 | + esac |
| 122 | +done |
| 123 | + |
| 124 | +if [ "$ACTION" = "indexbuild" ]; then |
| 125 | + echo "Skipping code generation in 'indexbuild' build. See https://bugzilla.mozilla.org/show_bug.cgi?id=1744504 for more info." |
| 126 | + exit 0 |
| 127 | +fi |
| 128 | + |
| 129 | +if [ "${#PARAMS[@]}" -gt 0 ]; then |
| 130 | + YAML_FILES=("${PARAMS[@]}") |
| 131 | +else |
| 132 | + # Check if at least one input file and/or one input file list has been specified |
| 133 | + if [[ (-z "$SCRIPT_INPUT_FILE_COUNT" || "$SCRIPT_INPUT_FILE_COUNT" -eq 0) && (-z "$SCRIPT_INPUT_FILE_LIST_COUNT" || "$SCRIPT_INPUT_FILE_LIST_COUNT" -eq 0) ]]; then |
| 134 | + echo "warning: No input files specified." |
| 135 | + exit 0 |
| 136 | + fi |
| 137 | + |
| 138 | + if [ "$SCRIPT_INPUT_FILE_COUNT" -gt 0 ]; then |
| 139 | + echo "Processing $SCRIPT_INPUT_FILE_COUNT single file inputs..." |
| 140 | + # Append the content of single input files |
| 141 | + for i in $(seq 0 $((SCRIPT_INPUT_FILE_COUNT - 1))); do |
| 142 | + infilevar="SCRIPT_INPUT_FILE_${i}" |
| 143 | + infile="${!infilevar}" |
| 144 | + YAML_FILES+=("${infile}") |
| 145 | + done |
| 146 | + fi |
| 147 | + |
| 148 | + if [ "$SCRIPT_INPUT_FILE_LIST_COUNT" -gt 0 ]; then |
| 149 | + echo "Processing $SCRIPT_INPUT_FILE_LIST_COUNT file lists..." |
| 150 | + # Append the content of any file lists (lists of file paths) |
| 151 | + for i in $(seq 0 $((SCRIPT_INPUT_FILE_LIST_COUNT - 1))); do |
| 152 | + infilevar="SCRIPT_INPUT_FILE_LIST_${i}" |
| 153 | + infile="${!infilevar}" |
| 154 | + |
| 155 | + while read -r line; do |
| 156 | + YAML_FILES+=("${line}") |
| 157 | + done <"$infile" |
| 158 | + done |
| 159 | + fi |
| 160 | + |
| 161 | + echo "Discovered YAML input files: ${YAML_FILES[*]}" |
| 162 | +fi |
| 163 | + |
| 164 | +if [ -z "$SOURCE_ROOT" ]; then |
| 165 | + echo "Error: No \$SOURCE_ROOT defined." |
| 166 | + echo "Execute this script as a build step in Xcode." |
| 167 | + exit 2 |
| 168 | +fi |
| 169 | + |
| 170 | +if [ -z "$PROJECT" ]; then |
| 171 | + echo "Error: No \$PROJECT defined." |
| 172 | + echo "Execute this script as a build step in Xcode." |
| 173 | + exit 2 |
| 174 | +fi |
| 175 | + |
| 176 | +VENVDIR="${SOURCE_ROOT}/.venv" |
| 177 | + |
| 178 | +[ -x "${VENVDIR}/bin/python" ] || python3 -m venv "${VENVDIR}" |
| 179 | +# We need at least pip 20.3 for Big Sur support, see https://pip.pypa.io/en/stable/news/#id48 |
| 180 | +# Latest pip is 21.0.1 |
| 181 | +"${VENVDIR}"/bin/pip install "pip>=20.3" |
| 182 | +"${VENVDIR}"/bin/pip install --upgrade "glean_parser~=$GLEAN_PARSER_VERSION" |
| 183 | + |
| 184 | +# Run the glinter |
| 185 | +# Turn its warnings into warnings visible in Xcode (but don't do for the success message) |
| 186 | +"${VENVDIR}"/bin/python -m glean_parser \ |
| 187 | + glinter \ |
| 188 | + $ALLOW_RESERVED \ |
| 189 | + "${YAML_FILES[@]}" 2>&1 \ |
| 190 | + | sed 's/^\(.\)/warning: \1/' \ |
| 191 | + | sed '/Your metrics are Glean/s/^warning: //' |
| 192 | + |
| 193 | +# Any of the below variables might be empty, so by not quoting them we ensure they are just left out as arguments |
| 194 | +# shellcheck disable=SC2086 |
| 195 | +PARSER_OUTPUT=$("${VENVDIR}"/bin/python -m glean_parser \ |
| 196 | + translate \ |
| 197 | + -f "swift" \ |
| 198 | + -o "${OUTPUT_DIR}" \ |
| 199 | + -s "glean_namespace=${GLEAN_NAMESPACE}" \ |
| 200 | + $BUILD_DATE \ |
| 201 | + $EXPIRE_VERSION \ |
| 202 | + $ALLOW_RESERVED \ |
| 203 | + "${YAML_FILES[@]}" 2>&1) || { echo "$PARSER_OUTPUT"; echo "error: glean_parser failed. See errors above."; exit 1; } |
| 204 | + |
| 205 | +if [ -n "$DOCS_DIRECTORY" ]; then |
| 206 | + "${VENVDIR}"/bin/python -m glean_parser \ |
| 207 | + translate \ |
| 208 | + -f "markdown" \ |
| 209 | + -o "${DOCS_DIRECTORY}" \ |
| 210 | + $ALLOW_RESERVED \ |
| 211 | + "${YAML_FILES[@]}" |
| 212 | +fi |
| 213 | + |
| 214 | +exit 0 |
0 commit comments