From 6d9e734bc7fc1032a89ce9cb4a06607db101fd8c Mon Sep 17 00:00:00 2001 From: Takahiko Kawasaki Date: Fri, 17 May 2024 14:22:46 +0900 Subject: [PATCH 1/3] [new] scripts/update-schema-id.sh This script updates the schema IDs in the following YAML files: - schemas/v{major}.{minor}/schema.yaml - schemas/v{major}.{minor}/schema-base.yaml and generates the following JSON files from the updated YAML files: - schemas/v{major}.{minor}/schema.json - schemas/v{major}.{minor}/schema-base.json --- scripts/update-schema-id.sh | 309 ++++++++++++++++++++++++++++++++++++ 1 file changed, 309 insertions(+) create mode 100755 scripts/update-schema-id.sh diff --git a/scripts/update-schema-id.sh b/scripts/update-schema-id.sh new file mode 100755 index 0000000000..6ebe69b5c5 --- /dev/null +++ b/scripts/update-schema-id.sh @@ -0,0 +1,309 @@ +#!/bin/sh +# +# OVERVIEW +# -------- +# +# This script updates the schema IDs in the following YAML files: +# +# schemas/v{major}.{minor}/schema.yaml +# schemas/v{major}.{minor}/schema-base.yaml +# +# and generates the following JSON files from the updated YAML files: +# +# schemas/v{major}.{minor}/schema.json +# schemas/v{major}.{minor}/schema-base.json +# +# USAGE +# ----- +# +# update-schema-id.sh -v VERSION [options] +# +# -v VERSION +# specifies the target version in the 'major.minor' format. +# For example, '-v 3.1'. This option is mandatory. +# +# -d DATE +# specifies the release date in the 'YYYY-MM-DD' format. +# For example, '-d 2024-05-17'. If omitted, the current date is used. +# +# -n +# turns on the dry-run mode that shows commands to execute but does not +# actually execute them. +# +# -q +# turns on the quiet mode. +# +# -h +# shows this help. +# +# EXAMPLE +# ------- +# +# ./scripts/update-schema-id.sh -v 3.1 +# +# If the above command line is executed on May 7, 2024, the following +# external commands are invoked by the script. +# +# ex -s -c '/^$id:/s/^.*$/$id: '\''https:\/\/spec.openapis.org\/oas\/3.1\/schema\/2024-05-07'\''/' \ +# -c wq schemas/v3.1/schema.yaml +# +# ./scripts/yaml2json/yaml2json.js schemas/v3.1/schema.yaml +# +# ex -s -c '/^$id:/s/^.*$/$id: '\''https:\/\/spec.openapis.org\/oas\/3.1\/schema-base\/2024-05-07'\''/' \ +# -c '/^$ref:/s/^.*$/$ref: '\''https:\/\/spec.openapis.org\/oas\/3.1\/schema\/2024-05-07'\''/' \ +# -c wq schemas/v3.1/schema-base.yaml +# +# ./scripts/yaml2json/yaml2json.js schemas/v3.1/schema-base.yaml +# + + +#------------------------------------------------------------ +# Global Variables +#------------------------------------------------------------ +__var_version= +__var_date=`date -I -u` +__var_dry_run=off +__var_quiet=off + + +#------------------------------------------------------------ +# main +#------------------------------------------------------------ +__main() +{ + # Process the command line options. + __process_options "$@" + + # Update schemas/v{major}.{minor}/schema.yaml + __update_schema_yaml + + # Generate schemas/v{major}.{minor}/schema.json from the updated YAML file. + __update_schema_json + + # Update schemas/v{major}.{minor}/schema-base.yaml + __update_schema_base_yaml + + # Generate schemas/v{major}.{minor}/schema-base.json from the updated YAML file. + __update_schema_base_json +} + + +#------------------------------------------------------------ +# process_options +#------------------------------------------------------------ +__process_options() +{ + while getopts v:d:nqh option + do + case $option in + # Version in the '{major}.{minor}' format. + v) + __var_version="${OPTARG}" + ;; + + # Date in the 'YYYY-MM-DD' format. + d) + __var_date="${OPTARG}" + ;; + + # Dry-Run Mode + n) + __var_dry_run=on + ;; + + # Quiet Mode + q) + __var_quiet=on + ;; + + # Help + h) + __help + exit 0 + ;; + + # Unknown Option + *) + # The error message is printed by 'getopts'. + exit 1 + ;; + esac + done + + __validate_version "${__var_version}" + __validate_date "${__var_date}" +} + + +#------------------------------------------------------------ +# __help +#------------------------------------------------------------ +__help() +{ + cat <<__HELP__ + +USAGE: + + $0 -v VERSION [options] + +OPTIONS: + + -v VERSION + specifies the target version in the 'major.minor' format. + For example, '-v 3.1'. This option is mandatory. + + -d DATE + specifies the release date in the 'YYYY-MM-DD' format. + For example, '-d 2024-05-17'. If omitted, the current date is used. + + -n + turns on the dry-run mode that shows commands to execute but does not + actually execute them. + + -q + turns on the quiet mode. + + -h + shows this help. + +__HELP__ +} + + +#------------------------------------------------------------ +# __error_exit +#------------------------------------------------------------ +__error_exit() +{ + echo "ERROR: $1" >&2 + exit 1 +} + + +#------------------------------------------------------------ +# __validate_version +#------------------------------------------------------------ +__validate_version() +{ + version="$1" + + # If the -v option is not specified. + if [ -z "${version}" ]; then + __error_exit "The '-v VERSION' option must be specified." + fi + + # If the argument of the -v option does not match the expected format. + if ! expr "${version}" : '[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then + __error_exit "The format of the version specified by the -v option is invalid." + fi +} + + +#------------------------------------------------------------ +# __validate_date +#------------------------------------------------------------ +__validate_date() +{ + date="$1" + + # If the argument of the -d option does not match the expected format. + if ! expr "${date}" : '[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]$' >/dev/null; then + __error_exit "The format of the date specified by the -d option is invalid." + fi +} + + +#------------------------------------------------------------ +# __execute_command +#------------------------------------------------------------ +__execute_command() +{ + command=("$@") + + # If the quiet mode is off. + if [ "${__var_quiet}" = "off" ]; then + # Print the command. + echo "${command[@]}" + fi + + # If the dry-run mode is off. + if [ "${__var_dry_run}" = "off" ]; then + # Execute the command. + "${command[@]}" + fi +} + + +#------------------------------------------------------------ +# __update_schema_yaml +#------------------------------------------------------------ +__update_schema_yaml() +{ + file="schemas/v${__var_version}/schema.yaml" + id="https:\\/\\/spec.openapis.org\\/oas\\/${__var_version}\\/schema\\/${__var_date}" + + # Prepare a command line to update the $id in the file. + command=(ex -s -c '/^$id:/s/^.*$/$id: '\'''${id}''\''/' -c wq ${file}) + + # Execute the command line. + __execute_command "${command[@]}" +} + + +#------------------------------------------------------------ +# __update_schema_json +#------------------------------------------------------------ +__update_schema_json() +{ + __yaml2json "schemas/v${__var_version}/schema.yaml" +} + + +#------------------------------------------------------------ +# __update_schema_base_yaml +#------------------------------------------------------------ +__update_schema_base_yaml() +{ + file="schemas/v${__var_version}/schema-base.yaml" + id="https:\\/\\/spec.openapis.org\\/oas\\/${__var_version}\\/schema-base\\/${__var_date}" + ref="https:\\/\\/spec.openapis.org\\/oas\\/${__var_version}\\/schema\\/${__var_date}" + + # Prepare a command line to update the $id and $ref in the file. + command=(ex -s -c '/^$id:/s/^.*$/$id: '\'''${id}''\''/' + -c '/^$ref:/s/^.*$/$ref: '\'''${ref}''\''/' + -c wq ${file}) + + # Execute the command line. + __execute_command "${command[@]}" +} + + +#------------------------------------------------------------ +# __update_schema_base_json +#------------------------------------------------------------ +__update_schema_base_json() +{ + __yaml2json "schemas/v${__var_version}/schema-base.yaml" +} + + +#------------------------------------------------------------ +# __yaml2json +#------------------------------------------------------------ +__yaml2json() +{ + input="$1" + + # Prepare a command line to generate a JSON file from the input file. + command=(./scripts/yaml2json/yaml2json.js ${input}) + + # Execute the command line. + __execute_command "${command[@]}" +} + + +#------------------------------------------------------------ +# Entry Point +#------------------------------------------------------------ +__main "$@" +exit 0 From 31b47b975520553c3bf804fba3413b95c162f3b1 Mon Sep 17 00:00:00 2001 From: Takahiko Kawasaki Date: Fri, 17 May 2024 15:20:15 +0900 Subject: [PATCH 2/3] Fixed comments --- scripts/update-schema-id.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/update-schema-id.sh b/scripts/update-schema-id.sh index 6ebe69b5c5..ec26d6054f 100755 --- a/scripts/update-schema-id.sh +++ b/scripts/update-schema-id.sh @@ -67,7 +67,7 @@ __var_quiet=off #------------------------------------------------------------ -# main +# __main #------------------------------------------------------------ __main() { @@ -89,7 +89,7 @@ __main() #------------------------------------------------------------ -# process_options +# __process_options #------------------------------------------------------------ __process_options() { From b51df13d6561a5d88c73a1fa34e58e6673aa7428 Mon Sep 17 00:00:00 2001 From: Takahiko Kawasaki Date: Mon, 10 Jun 2024 07:31:16 +0900 Subject: [PATCH 3/3] Support version 3.0 --- scripts/update-schema-id.sh | 78 ++++++++++++++++++++++++++++++------- 1 file changed, 64 insertions(+), 14 deletions(-) diff --git a/scripts/update-schema-id.sh b/scripts/update-schema-id.sh index ec26d6054f..335d34e8bd 100755 --- a/scripts/update-schema-id.sh +++ b/scripts/update-schema-id.sh @@ -3,15 +3,31 @@ # OVERVIEW # -------- # -# This script updates the schema IDs in the following YAML files: +# For the version 3.1 and higher: # -# schemas/v{major}.{minor}/schema.yaml -# schemas/v{major}.{minor}/schema-base.yaml +# This script updates the schema IDs in the following YAML files: # -# and generates the following JSON files from the updated YAML files: +# schemas/v{major}.{minor}/schema.yaml +# schemas/v{major}.{minor}/schema-base.yaml # -# schemas/v{major}.{minor}/schema.json -# schemas/v{major}.{minor}/schema-base.json +# and generates the following JSON files from the updated YAML files: +# +# schemas/v{major}.{minor}/schema.json +# schemas/v{major}.{minor}/schema-base.json +# +# For the version 3.0: +# +# This script updates the schema ID in the following YAML file: +# +# schemas/v{major}.{minor}/schema.yaml +# +# and generates the following JSON file from the updated YAML file: +# +# schemas/v{major}.{minor}/schema.json +# +# For versions older than 3.0: +# +# This script does not support versions older than 3.0. # # USAGE # ----- @@ -36,8 +52,8 @@ # -h # shows this help. # -# EXAMPLE -# ------- +# EXAMPLE 1 (3.1 or higher) +# ------------------------- # # ./scripts/update-schema-id.sh -v 3.1 # @@ -55,12 +71,27 @@ # # ./scripts/yaml2json/yaml2json.js schemas/v3.1/schema-base.yaml # +# EXAMPLE 2 (3.0) +# --------------- +# +# ./scripts/update-schema-id.sh -v 3.0 +# +# If the above command line is executed on June 9, 2024, the following +# external commands are invoked by the script. +# +# ex -s -c '/^id:/s/^.*$/id: https:\/\/spec.openapis.org\/oas\/3.0\/schema\/2024-06-09/' \ +# -c wq schemas/v3.0/schema.yaml +# +# ./scripts/yaml2json/yaml2json.js schemas/v3.0/schema.yaml +# #------------------------------------------------------------ # Global Variables #------------------------------------------------------------ __var_version= +__var_major= +__var_minor= __var_date=`date -I -u` __var_dry_run=off __var_quiet=off @@ -80,11 +111,14 @@ __main() # Generate schemas/v{major}.{minor}/schema.json from the updated YAML file. __update_schema_json - # Update schemas/v{major}.{minor}/schema-base.yaml - __update_schema_base_yaml + # If the version is 3.1 or higher. + if [ ${__var_major} -gt 3 ] || [ ${__var_minor} -ge 1 ]; then + # Update schemas/v{major}.{minor}/schema-base.yaml + __update_schema_base_yaml - # Generate schemas/v{major}.{minor}/schema-base.json from the updated YAML file. - __update_schema_base_json + # Generate schemas/v{major}.{minor}/schema-base.json from the updated YAML file. + __update_schema_base_json + fi } @@ -151,6 +185,7 @@ OPTIONS: -v VERSION specifies the target version in the 'major.minor' format. For example, '-v 3.1'. This option is mandatory. + Versions older than 3.0 are not supported. -d DATE specifies the release date in the 'YYYY-MM-DD' format. @@ -196,6 +231,15 @@ __validate_version() if ! expr "${version}" : '[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then __error_exit "The format of the version specified by the -v option is invalid." fi + + # Extract the major number and the minor number. + __var_major=`echo ${version} | cut -f 1 -d .` + __var_minor=`echo ${version} | cut -f 2 -d .` + + # If the major number is less than 3. + if [ ${__var_major} -lt 3 ]; then + __error_exit "This script does not support versions older than 3.0." + fi } @@ -242,8 +286,14 @@ __update_schema_yaml() file="schemas/v${__var_version}/schema.yaml" id="https:\\/\\/spec.openapis.org\\/oas\\/${__var_version}\\/schema\\/${__var_date}" - # Prepare a command line to update the $id in the file. - command=(ex -s -c '/^$id:/s/^.*$/$id: '\'''${id}''\''/' -c wq ${file}) + # If the version is 3.0. + if [ ${__var_major} -eq 3 ] && [ ${__var_minor} -eq 0 ]; then + # Prepare a command line to update the id in the file. + command=(ex -s -c '/^id:/s/^.*$/id: '${id}'/' -c wq ${file}) + else + # Prepare a command line to update the $id in the file. + command=(ex -s -c '/^$id:/s/^.*$/$id: '\'''${id}''\''/' -c wq ${file}) + fi # Execute the command line. __execute_command "${command[@]}"