Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Elm manager. #1042

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/license_finder/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
1 change: 1 addition & 0 deletions lib/license_finder/package_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
36 changes: 36 additions & 0 deletions lib/license_finder/package_managers/elm.rb
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions lib/license_finder/packages/elm_package.rb
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions spec/lib/license_finder/packages/elm_package_spec.rb
Original file line number Diff line number Diff line change
@@ -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