From de7edd403db516c46dc95d056061885554369cad Mon Sep 17 00:00:00 2001 From: lionellloh Date: Tue, 23 Jun 2020 01:39:10 +0800 Subject: [PATCH] brew update-license-data: checks if it is outdated. If so, save new ver --- .../Homebrew/dev-cmd/update-license-data.rb | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 Library/Homebrew/dev-cmd/update-license-data.rb diff --git a/Library/Homebrew/dev-cmd/update-license-data.rb b/Library/Homebrew/dev-cmd/update-license-data.rb new file mode 100644 index 0000000000..afcca90291 --- /dev/null +++ b/Library/Homebrew/dev-cmd/update-license-data.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +require "commands" +require "cli/parser" +require "open-uri" +require "json" + +module Homebrew + module_function + SPDX_FOLDER_PATH = (HOMEBREW_LIBRARY_PATH/"data").freeze + FILE_NAME = "spdx.json".freeze + NEW_FILE_NAME = "spdx_new.json".freeze + SPDX_DATA_URL = "https://raw.githubusercontent.com/spdx/license-list-data/master/json/licenses.json" + + def update_license_data_args + Homebrew::CLI::Parser.new do + usage_banner <<~EOS + `update_license_data` + + Update SPDX license data in the Homebrew repository. + EOS + switch "--fail-if-outdated", + description: "Return a failing status code if current license data's version is different from the upstream. This "\ + "can be used to notify CI when the SPDX license data is out of date." + end + end + + def update_license_data + update_license_data_args.parse + p args + curr_spdx_hash = File.open(SPDX_FOLDER_PATH/FILE_NAME, 'r') do |f| + JSON.parse(f.read) + end + puts "Fetching newest version of SPDX License data..." + updated_spdx_string = open(SPDX_DATA_URL) do |json| + json.read + end + + updated_spdx_hash = JSON.parse(updated_spdx_string) + if curr_spdx_hash["licenseListVersion"] != updated_spdx_hash["licenseListVersion"] + + puts "Current version is #{curr_spdx_hash["licenseListVersion"]} but newest version is #{updated_spdx_hash["licenseListVersion"]}\n"\ + "Updating existing licences data file..." + File.open(SPDX_FOLDER_PATH/FILE_NAME, "wb") do |file| + file.write(updated_spdx_string) + end + if args.fail_if_outdated + Homebrew.failed = true + end + end + else + puts "Current version of license data is updated. No change required" + end + end