Class: ElmInstall::Identifier

Inherits:
Base
  • Object
show all
Defined in:
lib/elm_install/identifier.rb

Overview

Identifies dependencies

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory, options = {}) ⇒ Indentifier

Initialize a new identifier.



17
18
19
20
21
22
# File 'lib/elm_install/identifier.rb', line 17

def initialize(directory, options = {})
  @options = options
  @dependency_sources = dependency_sources directory
  @initial_dependencies = identify directory
  self
end

Instance Attribute Details

#initial_dependenciesArray<Dependency> (readonly)



5
6
7
# File 'lib/elm_install/identifier.rb', line 5

def initial_dependencies
  @initial_dependencies
end

#optionsHash (readonly)



8
9
10
# File 'lib/elm_install/identifier.rb', line 8

def options
  @options
end

Instance Method Details

#dependency_sources(directory) ⇒ Hash

Returns the dependency sources for the given directory.



30
31
32
# File 'lib/elm_install/identifier.rb', line 30

def dependency_sources(directory)
  json(directory)['dependency-sources'].to_h
end

#exit(message) ⇒ Object

Exits the current process and logs a given message.



127
128
129
130
131
# File 'lib/elm_install/identifier.rb', line 127

def exit(message)
  Logger.arrow message
  Process.exit
  nil
end

#identify(directory) ⇒ Array

Identifies dependencies from a directory



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/elm_install/identifier.rb', line 50

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 = @options

    Dependency.new(package, type.source, constraints)
  end
end

#json(directory) ⇒ Hash

Returns the contents of the ‘elm-package.json’ for the given directory.



110
111
112
113
114
115
116
117
118
119
# File 'lib/elm_install/identifier.rb', line 110

def json(directory)
  path = File.join(directory, 'elm-package.json')
  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

#uri_type(url, branch) ⇒ Type

Returns the type from the given arguments.



94
95
96
97
98
99
100
101
102
# File 'lib/elm_install/identifier.rb', line 94

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.



40
41
42
# File 'lib/elm_install/identifier.rb', line 40

def version(directory)
  Semverse::Version.new(json(directory)['version'])
end