Class: ElmInstall::Identifier
- Defined in:
- lib/elm_install/identifier.rb
Overview
Identifies dependencies
Instance Attribute Summary collapse
-
#initial_dependencies ⇒ Array<Dependency>
readonly
The initial dependencies.
-
#initial_elm_version ⇒ String
readonly
The initial Elm version.
-
#options ⇒ Hash
readonly
The options.
Instance Method Summary collapse
-
#dependency_sources(directory) ⇒ Hash
Returns the dependency sources for the given directory.
-
#elm_version(directory) ⇒ String
Returns the Elm version for a package.
-
#identify(directory) ⇒ Array
Identifies dependencies from a directory.
-
#initialize(directory, options = {}) ⇒ Indentifier
constructor
Initialize a new identifier.
-
#json(directory) ⇒ Hash
Returns the contents of the ‘elm-package.json’ for the given directory.
-
#uri_type(url, branch) ⇒ Type
Returns the type from the given arguments.
-
#version(directory) ⇒ Semverse::Version
Returns the version of a package in the given directory.
-
#warn(message) ⇒ Hash
Exits the current process and logs a given message.
Constructor Details
#initialize(directory, options = {}) ⇒ Indentifier
Initialize a new identifier.
20 21 22 23 24 25 26 |
# File 'lib/elm_install/identifier.rb', line 20 def initialize(directory, = {}) @options = @dependency_sources = dependency_sources directory @initial_elm_version = elm_version directory @initial_dependencies = identify directory self end |
Instance Attribute Details
#initial_dependencies ⇒ Array<Dependency> (readonly)
Returns The initial dependencies.
5 6 7 |
# File 'lib/elm_install/identifier.rb', line 5 def initial_dependencies @initial_dependencies end |
#initial_elm_version ⇒ String (readonly)
Returns The initial Elm version.
11 12 13 |
# File 'lib/elm_install/identifier.rb', line 11 def initial_elm_version @initial_elm_version end |
#options ⇒ Hash (readonly)
Returns The options.
8 9 10 |
# File 'lib/elm_install/identifier.rb', line 8 def @options end |
Instance Method Details
#dependency_sources(directory) ⇒ Hash
Returns the dependency sources for the given directory.
34 35 36 |
# File 'lib/elm_install/identifier.rb', line 34 def dependency_sources(directory) json(directory)['dependency-sources'].to_h end |
#elm_version(directory) ⇒ String
Returns the Elm version for a package
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/elm_install/identifier.rb', line 54 def elm_version(directory) if json(directory)['elm-version'] =~ /<\s*0\.19/ '0.18' elsif json(directory)['elm-version'] =~ /<\s*0\.18/ '0.17' elsif json(directory)['elm-version'] =~ /<\s*0\.17/ '0.16' else '' end end |
#identify(directory) ⇒ Array
Identifies dependencies from a directory
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/elm_install/identifier.rb', line 72 def identify(directory) raw = json(directory) dependencies = raw['dependencies'].to_h dependency_sources = raw['dependency-sources'] .to_h .merge(@dependency_sources) dependencies.map do |package, constraint| constraints = Utils.transform_constraint constraint type = if dependency_sources.key?(package) source = dependency_sources[package] case source when Hash uri_type source['url'], Branch::Just(source['ref']) when String if File.exist?(source) Type::Directory(Pathname.new(source)) else uri_type source, Branch::Just('master') end end else Type::Git(Uri::Github(package), Branch::Nothing()) end type.source.identifier = self type.source. = @options Dependency.new(package, type.source, constraints) end end |
#json(directory) ⇒ Hash
Returns the contents of the ‘elm-package.json’ for the given directory.
132 133 134 135 136 137 138 139 |
# File 'lib/elm_install/identifier.rb', line 132 def json(directory) path = File.join(directory, 'elm-package.json') JSON.parse(File.read(path)) rescue JSON::ParserError warn "Invalid JSON in file: #{path.bold}" rescue Errno::ENOENT warn "Could not find file: #{path.bold}" end |
#uri_type(url, branch) ⇒ Type
Returns the type from the given arguments.
116 117 118 119 120 121 122 123 124 |
# File 'lib/elm_install/identifier.rb', line 116 def uri_type(url, branch) uri = GitCloneUrl.parse(url) case uri when URI::SshGit::Generic Type::Git(Uri::Ssh(uri), branch) when URI::HTTP Type::Git(Uri::Http(uri), branch) end end |
#version(directory) ⇒ Semverse::Version
Returns the version of a package in the given directory.
44 45 46 |
# File 'lib/elm_install/identifier.rb', line 44 def version(directory) Semverse::Version.new(json(directory)['version']) end |