From d4f9e5f8a28d1feb42f1f6800aca8c3570c23482 Mon Sep 17 00:00:00 2001 From: Takahiko Kawasaki Date: Wed, 8 May 2024 23:06:46 +0900 Subject: [PATCH 1/2] [new] scripts/update-schema-latest This script does the following. 1. Switch to the 'gh-pages' branch. 2. Copy "schemas/v{major}.{minor}/schema.json" in the 'main' branch to "oas/{major}.{minor}/schema/{YYYY-MM-DD}" in the 'gh-pages' branch. The value of {YYYY-MM-DD} is extracted from the '$id' property in the schema.json file. 3. Create a symbolic link "latest" under "oas/{major}.{minor}/schema" that points to the "{YYYY-MM-DD}" file. 4. Copy "schemas/v{major}.{minor}/schema-base.json" in the 'main' branch to "oas/{major}.{minor}/schema-base/{YYYY-MM-DD}" in the 'gh-pages' branch. The value of {YYYY-MM-DD} is extracted from the '$id' property in the schema-base.json file. 5. Create a symbolic link "latest" under "oas/{major}.{minor}/schema-base" that points to the "{YYYY-MM-DD}" file. --- scripts/update-schema-latest | 261 +++++++++++++++++++++++++++++++++++ 1 file changed, 261 insertions(+) create mode 100755 scripts/update-schema-latest diff --git a/scripts/update-schema-latest b/scripts/update-schema-latest new file mode 100755 index 0000000000..5376cd8e11 --- /dev/null +++ b/scripts/update-schema-latest @@ -0,0 +1,261 @@ +#!/usr/bin/env ruby +# +# OVERVIEW +# -------- +# +# This script does the following. +# +# 1. Switch to the 'gh-pages' branch. +# +# 2. Copy "schemas/v{major}.{minor}/schema.json" in the 'main' branch to +# "oas/{major}.{minor}/schema/{YYYY-MM-DD}" in the 'gh-pages' branch. +# The value of {YYYY-MM-DD} is extracted from the '$id' property in +# the schema.json file. +# +# 3. Create a symbolic link "latest" under "oas/{major}.{minor}/schema" +# that points to the "{YYYY-MM-DD}" file. +# +# 4. Copy "schemas/v{major}.{minor}/schema-base.json" in the 'main' branch to +# "oas/{major}.{minor}/schema-base/{YYYY-MM-DD}" in the 'gh-pages' branch. +# The value of {YYYY-MM-DD} is extracted from the '$id' property in +# the schema-base.json file. +# +# 5. Create a symbolic link "latest" under "oas/{major}.{minor}/schema-base" +# that points to the "{YYYY-MM-DD}" file. +# +# USAGE +# ----- +# +# update-schema-latest +# --version VERSION # -v VERSION (required) +# --dry-run # -n +# --quiet # -q +# --help # -h +# +# EXAMPLE +# ------- +# +# ./scripts/update-schema-latest -v 3.1 +# +# If the above command line is executed, the following external commands +# are invoked by the script. This example assumes that the value of the +# '$id' property in the schema files ends with '2022-10-07'. +# +# git checkout gh-pages +# +# git show main:schemas/v3.1/schema.json > oas/3.1/schema/2022-10-07 +# +# (cd oas/3.1/schema && ln -sf 2022-10-07 latest) +# +# git show main:schemas/v3.1/schema-base.json > oas/3.1/schema-base/2022-10-07 +# +# (cd oas/3.1/schema-base && ln -sf 2022-10-07 latest) +# +# NOTE +# ---- +# +# This script requires the following external commands. +# +# - git +# - jq +# + + +require 'bundler/inline' + +gemfile do + source 'https://rubygems.org' + gem 'optparse' +end + + +#------------------------------------------------------------ +# main +#------------------------------------------------------------ +def main(args) + # Process the command line options. + options = Options.process(args) + + # 1. Check out the 'gh-pages' branch. + checkout_gh_pages(options) + + # 2. Copy main:schemas/v{major}.{minor}/schema.json to + # oas/{major}.{minor}/schema/YYYY-MM-DD. + # + # 3. Update oas/{major}.{minor}/schema/latest (symlink) to + # make it point to YYYY-MM-DD. + update_schema(options) + + # 4. Copy main:schemas/v{major}.{minor}/schema-base.json to + # oas/{major}.{minor}/schema-base/YYYY-MM-DD. + # + # 5. Update oas/{major}.{minor}/schema-base/latest (symlink) to + # make it point to YYYY-MM-DD. + update_schema_base(options) +end + + +#------------------------------------------------------------ +# Ver +#------------------------------------------------------------ +class Ver + attr_reader :major, :minor + + def initialize(version) + m = /^(\d+)\.(\d+)$/.match(version) + + @major = m[1] + @minor = m[2] + end +end + + +#------------------------------------------------------------ +# Options +#------------------------------------------------------------ +class Options < OptionParser + DESC_VERSION = "specifies the target version in the 'major.minor' format. For example, '3.1'. This option is mandatory." + DESC_DRY_RUN = "turns on the dry-run mode that shows external commands to execute but does not actually execute them." + DESC_QUIET = "turns on the quiet mode." + + attr_reader :version, :dry_run, :quiet + + def initialize + super + + @version = nil + @dry_run = false + @quiet = false + + self.on('-v VERSION', '--version VERSION', DESC_VERSION) do |value| + check_pattern(value, 'version', /^\d+\.\d+$/) + @version = Ver.new(value) + end + + self.on('-n', '--dry-run', DESC_DRY_RUN) do + @dry_run = true + end + + self.on('-q', '--quiet', DESC_QUIET) do + @quiet = true + end + end + + private + + def check_pattern(value, option, pattern) + if value !~ pattern then + error_exit "ERROR: The given string '#{value}' does not match the expected #{option} pattern." + end + end + + def error_if_missing(value, option) + if value.nil? + error_exit "ERROR: '#{option}' is missing." + end + end + + public + + def validate + error_if_missing(@version, '--version VERSION') + end + + def self.process(args) + options = Options.new + options.parse(args) + options.validate() + + return options + end +end + + +#------------------------------------------------------------ +# error_exit +#------------------------------------------------------------ +def error_exit(message) + $stderr.puts message + exit false +end + + +#------------------------------------------------------------ +# execute_command +#------------------------------------------------------------ +def execute_command(options, command) + # Print the command unless the quiet mode is on. + puts command unless options.quiet + + # Execute the command unless the dry-run mode is on. + system command unless options.dry_run + + if !options.dry_run && $?.exitstatus != 0 + error_exit "ERROR: The command failed." + end +end + + +#------------------------------------------------------------ +# checkout_gh_pages +#------------------------------------------------------------ +def checkout_gh_pages(options) + # The command to checkout the 'gh-pages' branch. + command = "git checkout gh-pages" + + # Execute the command. + execute_command(options, command) +end + + +#------------------------------------------------------------ +# update_schema +#------------------------------------------------------------ +def update_schema(options) + update(options, "schema") +end + + +#------------------------------------------------------------ +# update_schema_base +#------------------------------------------------------------ +def update_schema_base(options) + update(options, "schema-base") +end + + +#------------------------------------------------------------ +# update +#------------------------------------------------------------ +def update(options, file_base) + # Version + ver = options.version + + # The source file in the 'main' branch. + src_file = "schemas/v#{ver.major}.#{ver.minor}/#{file_base}.json" + + # Extract the 'YYYY-MM-DD' part of the "$id" property. + date = `git show main:#{src_file} | jq -rj '."$id"|split("/")|last'` + + # The destination directory in the 'gh-pages' branch. + dst_dir = "oas/#{ver.major}.#{ver.minor}/#{file_base}" + + # The destination file in the 'gh-pages' branch. + dst_file = "#{dst_dir}/#{date}" + + # The command to copy the source file to the destination file. + copy_command = "git show main:#{src_file} > #{dst_file}" + + # The command to update the 'latest' file. + link_command = "(cd #{dst_dir} && ln -sf #{date} latest)" + + # Execute the commands. + execute_command(options, copy_command) + execute_command(options, link_command) +end + + +#------------------------------------------------------------ +# Entry Point +#------------------------------------------------------------ +main(ARGV) From 25de0fc84e0b88e0bf0246effbe8e7703990e839 Mon Sep 17 00:00:00 2001 From: Takahiko Kawasaki Date: Mon, 13 May 2024 14:34:15 +0900 Subject: [PATCH 2/2] Replace error_exit with abort --- scripts/update-schema-latest | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/scripts/update-schema-latest b/scripts/update-schema-latest index 5376cd8e11..ea8ce324af 100755 --- a/scripts/update-schema-latest +++ b/scripts/update-schema-latest @@ -145,13 +145,13 @@ class Options < OptionParser def check_pattern(value, option, pattern) if value !~ pattern then - error_exit "ERROR: The given string '#{value}' does not match the expected #{option} pattern." + abort "ERROR: The given string '#{value}' does not match the expected #{option} pattern." end end def error_if_missing(value, option) if value.nil? - error_exit "ERROR: '#{option}' is missing." + abort "ERROR: '#{option}' is missing." end end @@ -171,15 +171,6 @@ class Options < OptionParser end -#------------------------------------------------------------ -# error_exit -#------------------------------------------------------------ -def error_exit(message) - $stderr.puts message - exit false -end - - #------------------------------------------------------------ # execute_command #------------------------------------------------------------ @@ -191,7 +182,7 @@ def execute_command(options, command) system command unless options.dry_run if !options.dry_run && $?.exitstatus != 0 - error_exit "ERROR: The command failed." + abort "ERROR: The command failed." end end