From c6f8f7477f2fd94278e3a0cd2f225cfddddc163e Mon Sep 17 00:00:00 2001 From: agustin Date: Tue, 14 May 2024 16:34:01 -0300 Subject: [PATCH] Add Elm manager. --- lib/license_finder/package.rb | 1 + lib/license_finder/package_manager.rb | 1 + lib/license_finder/package_managers/elm.rb | 36 +++++++++++++++++++ lib/license_finder/packages/elm_package.rb | 29 +++++++++++++++ .../packages/elm_package_spec.rb | 33 +++++++++++++++++ 5 files changed, 100 insertions(+) create mode 100644 lib/license_finder/package_managers/elm.rb create mode 100644 lib/license_finder/packages/elm_package.rb create mode 100644 spec/lib/license_finder/packages/elm_package_spec.rb diff --git a/lib/license_finder/package.rb b/lib/license_finder/package.rb index c10096b5d..84066c37a 100644 --- a/lib/license_finder/package.rb +++ b/lib/license_finder/package.rb @@ -193,3 +193,4 @@ def log_activation(activation) require 'license_finder/packages/composer_package' require 'license_finder/packages/conda_package' require 'license_finder/packages/pubspec_package' +require 'license_finder/packages/elm_package' diff --git a/lib/license_finder/package_manager.rb b/lib/license_finder/package_manager.rb index 3a47ef4b1..34e043457 100644 --- a/lib/license_finder/package_manager.rb +++ b/lib/license_finder/package_manager.rb @@ -181,3 +181,4 @@ def log_to_file(prep_cmd, contents) require 'license_finder/package_managers/composer' require 'license_finder/package_managers/conda' require 'license_finder/package_managers/pub' +require 'license_finder/package_managers/elm' diff --git a/lib/license_finder/package_managers/elm.rb b/lib/license_finder/package_managers/elm.rb new file mode 100644 index 000000000..f0a3602b7 --- /dev/null +++ b/lib/license_finder/package_managers/elm.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +require 'json' + +module LicenseFinder + class Elm < PackageManager + ELM_HOME = (ENV['ELM_HOME'] || '/.elm').freeze + + def current_packages + elm_json_content = JSON.parse File.read(detected_package_path) + elm_version = elm_json_content['elm-version'] + packages = packages_from_json(elm_json_content, elm_version) + end + + def possible_package_paths + [project_path.join('elm.json')] + end + + private + + def packages_from_json(elm_version, elm_json) + packages = (elm_json.dig('dependencies', 'direct') || {}) + .merge(elm_json.dig('dependencies', 'indirect') || {}) + .map do |(author_name, version)| + author, name = author_name.split('/') + package_elm_json = find_package_elm_json(ELM_HOME, elm_version, author, name, version) + ElmPackage.from_elm_json(name, version, author, package_elm_json) + end + end + + def find_package_elm_json(home, elm_version, author, name, version) + path = File.join(home, elm_version, 'packages', @author, name, @version, 'elm.json') + JSON.parse File.open(path) + end + end +end diff --git a/lib/license_finder/packages/elm_package.rb b/lib/license_finder/packages/elm_package.rb new file mode 100644 index 000000000..b67159c04 --- /dev/null +++ b/lib/license_finder/packages/elm_package.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +module LicenseFinder + class ElmPackage < Package + attr_reader :author + + def self.from_elm_json(name, version, author, elm_json_content) + new(name, + version, + author, + spec_licenses: Package.license_names_from_standard_spec(elm_json_content), + summary: elm_json_content['summary'], + ) + end + + def initialize(name, version, author, options = {}) + @author = author + super(name, version, options.merge(authors: [author])) + end + + def package_manager + 'Elm' + end + + def package_url + "https://package.elm-lang.org/packages/#{CGI.escape(author)}/#{CGI.escape(name)}/#{CGI.escape(version)}" + end + end +end diff --git a/spec/lib/license_finder/packages/elm_package_spec.rb b/spec/lib/license_finder/packages/elm_package_spec.rb new file mode 100644 index 000000000..bfb2165d1 --- /dev/null +++ b/spec/lib/license_finder/packages/elm_package_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +module LicenseFinder + describe ElmPackage do + let(:spec) do + { + "type": "package", + "name": "elm/time", + "summary": "Work with POSIX times, time zones, years, months, days, hours, seconds, etc.", + "license": "BSD-3-Clause", + "version": "1.0.0", + "exposed-modules": [ + "Time" + ], + "elm-version": "0.19.0 <= v < 0.20.0", + "dependencies": { + "elm/core": "1.0.0 <= v < 2.0.0" + }, + "test-dependencies": {} + }.to_json + end + + subject do + ElmPackage.from_elm_json("time", "1.0.0", "elm", JSON.parse(spec)) + end + + its(:authors) { is_expected.to eql ['elm'] } + its(:name) { is_expected.to eql 'time' } + its(:licenses) { is_expected.to eql 'BSD-3-Clause' } + its(:package_url) { is_expected.to eql "https://package.elm-lang.org/packages/elm/time/1.0.0" } + its(:package_manager) { 'Elm' } + end +end