diff --git a/Library/Homebrew/dev-cmd/audit.rb b/Library/Homebrew/dev-cmd/audit.rb index 45b24c293d..ed990fdc45 100644 --- a/Library/Homebrew/dev-cmd/audit.rb +++ b/Library/Homebrew/dev-cmd/audit.rb @@ -4,6 +4,7 @@ require "formula" require "formula_versions" require "utils/curl" require "utils/notability" +require "utils/spdx" require "extend/ENV" require "formula_cellar_checks" require "cmd/search" @@ -118,8 +119,7 @@ module Homebrew # Check style in a single batch run up front for performance style_results = Style.check_style_json(style_files, options) if style_files # load licenses - spdx = HOMEBREW_LIBRARY_PATH/"data/spdx.json" - spdx_data = JSON.parse(spdx.read) + spdx_data = SPDX.spdx_data new_formula_problem_lines = [] audit_formulae.sort.each do |f| only = only_cops ? ["style"] : args.only diff --git a/Library/Homebrew/dev-cmd/update-license-data.rb b/Library/Homebrew/dev-cmd/update-license-data.rb index 40584b3b4b..7836f069a9 100644 --- a/Library/Homebrew/dev-cmd/update-license-data.rb +++ b/Library/Homebrew/dev-cmd/update-license-data.rb @@ -1,14 +1,11 @@ # frozen_string_literal: true require "cli/parser" -require "utils/github" +require "utils/spdx" module Homebrew module_function - SPDX_PATH = (HOMEBREW_LIBRARY_PATH/"data/spdx.json").freeze - SPDX_API_URL = "https://api.github.com/repos/spdx/license-list-data/releases/latest" - def update_license_data_args Homebrew::CLI::Parser.new do usage_banner <<~EOS @@ -29,16 +26,14 @@ module Homebrew args = update_license_data_args.parse ohai "Updating SPDX license data..." - latest_tag = GitHub.open_api(SPDX_API_URL)["tag_name"] - data_url = "https://raw.githubusercontent.com/spdx/license-list-data/#{latest_tag}/json/licenses.json" - curl_download(data_url, to: SPDX_PATH, partial: false) + SPDX.download_latest_license_data! - Homebrew.failed = system("git", "diff", "--stat", "--exit-code", SPDX_PATH) if args.fail_if_not_changed? + Homebrew.failed = system("git", "diff", "--stat", "--exit-code", SPDX::JSON_PATH) if args.fail_if_not_changed? return unless args.commit? ohai "git add" - safe_system "git", "add", SPDX_PATH + safe_system "git", "add", SPDX::JSON_PATH ohai "git commit" system "git", "commit", "--message", "data/spdx.json: update to #{latest_tag}" end diff --git a/Library/Homebrew/utils/spdx.rb b/Library/Homebrew/utils/spdx.rb new file mode 100644 index 0000000000..a2c5fd4f12 --- /dev/null +++ b/Library/Homebrew/utils/spdx.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require "utils/github" + +module SPDX + module_function + + JSON_PATH = (HOMEBREW_LIBRARY_PATH/"data/spdx.json").freeze + API_URL = "https://api.github.com/repos/spdx/license-list-data/releases/latest" + + def spdx_data + @spdx_data ||= JSON.parse(JSON_PATH.read) + end + + def download_latest_license_data! + latest_tag = GitHub.open_api(API_URL)["tag_name"] + data_url = "https://raw.githubusercontent.com/spdx/license-list-data/#{latest_tag}/json/licenses.json" + curl_download(data_url, to: JSON_PATH, partial: false) + end +end