Module: ElmInstall::ElmPackage
- Defined in:
- lib/elm_install/elm_package.rb
Overview
This is a class for reading the ‘elm-package`.json file and transform it’s ‘dependecies` field to a unified format.
Class Method Summary collapse
-
.check_path(package, url) ⇒ void
Checks if the given url matches the given package.
-
.dependencies(path, options = { silent: true }) ⇒ Hash
Returns the dependencies for the given ‘elm-package`.
-
.exit(message, options) ⇒ void
Exits the current process with the given message.
-
.read(path, options = { silent: true }) ⇒ Hash
Reads the given file as JSON.
-
.transform_dependencies(raw_dependencies, sources) ⇒ Hash
Transform dependencies from (package name => version) to (url => version or ref) format using the ‘depdendency-sources` field.
-
.transform_dependency(package, value, constraint, memo) ⇒ Hash
Transforms a dependecy.
-
.transform_package(key) ⇒ String
Transforms a package to it’s url for if needed.
Class Method Details
.check_path(package, url) ⇒ void
This method returns an undefined value.
Checks if the given url matches the given package.
:reek:DuplicateMethodCall
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/elm_install/elm_package.rb', line 90 def self.check_path(package, url) uri = GitCloneUrl.parse(url) path = uri.path.sub(%r{^/}, '') return if path == package puts " The source of package #{package.bold} is set to #{url.bold} which would be install to #{"elm-stuff/#{path}".bold}. This would cause a conflict when trying to compile anything. The name of a package must match the source url's path. #{package.bold} <=> #{path.bold} " Process.exit end |
.dependencies(path, options = { silent: true }) ⇒ Hash
Returns the dependencies for the given ‘elm-package`.
10 11 12 13 14 15 16 |
# File 'lib/elm_install/elm_package.rb', line 10 def self.dependencies(path, = { silent: true }) json = read path, transform_dependencies( json['dependencies'].to_h, json['dependency-sources'].to_h ) end |
.exit(message, options) ⇒ void
This method returns an undefined value.
Exits the current process with the given message.
39 40 41 42 43 |
# File 'lib/elm_install/elm_package.rb', line 39 def self.exit(, ) return {} if [:silent] Logger.arrow Process.exit end |
.read(path, options = { silent: true }) ⇒ Hash
Reads the given file as JSON.
:reek:DuplicateMethodCall
25 26 27 28 29 30 31 |
# File 'lib/elm_install/elm_package.rb', line 25 def self.read(path, = { silent: true }) JSON.parse(File.read(path)) rescue JSON::ParserError exit "Invalid JSON in file: #{path.bold}.", rescue Errno::ENOENT exit "Could not find file: #{path.bold}.", end |
.transform_dependencies(raw_dependencies, sources) ⇒ Hash
Transform dependencies from (package name => version) to (url => version or ref) format using the ‘depdendency-sources` field.
52 53 54 55 56 57 58 |
# File 'lib/elm_install/elm_package.rb', line 52 def self.transform_dependencies(raw_dependencies, sources) raw_dependencies.each_with_object({}) do |(package, constraint), memo| value = sources.fetch(package, package) transform_dependency package, value, constraint, memo end end |
.transform_dependency(package, value, constraint, memo) ⇒ Hash
Transforms a dependecy.
:reek:LongParameterList :reek:DuplicateMethodCall
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/elm_install/elm_package.rb', line 71 def self.transform_dependency(package, value, constraint, memo) if value.is_a?(Hash) check_path package, value['url'] memo[value['url']] = value['ref'] else url = transform_package(value) check_path package, url memo[url] = constraint end end |
.transform_package(key) ⇒ String
Transforms a package to it’s url for if needed.
113 114 115 116 117 |
# File 'lib/elm_install/elm_package.rb', line 113 def self.transform_package(key) GitCloneUrl.parse(key).to_s.gsub(/\.git$/, '') rescue "https://github.com/#{key}" end |