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.

Parameters:

  • directory (Dir)

    The initial directory

  • options (Hash) (defaults to: {})

    The options



20
21
22
23
24
25
26
# File 'lib/elm_install/identifier.rb', line 20

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

Instance Attribute Details

#initial_dependenciesArray<Dependency> (readonly)

Returns The initial dependencies.

Returns:

  • (Array<Dependency>)

    The initial dependencies



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

def initial_dependencies
  @initial_dependencies
end

#initial_elm_versionString (readonly)

Returns The initial Elm version.

Returns:

  • (String)

    The initial Elm version



11
12
13
# File 'lib/elm_install/identifier.rb', line 11

def initial_elm_version
  @initial_elm_version
end

#optionsHash (readonly)

Returns The options.

Returns:

  • (Hash)

    The options



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.

Parameters:

  • directory (Dir)

    The directory

Returns:

  • (Hash)

    The directory sources



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

Parameters:

  • directory (Dir)

    The directory

Returns:

  • (String)

    The version



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

Parameters:

  • directory (Dir)

    The directory

Returns:

  • (Array)

    The dependencies



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

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

#json(directory) ⇒ Hash

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

Parameters:

  • directory (Dir)

    The directory

Returns:

  • (Hash)

    The contents



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.

Parameters:

  • url (String)

    The base url

  • branch (Branch)

    The branch

Returns:

  • (Type)

    The type



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.

Parameters:

  • directory (Dir)

    The directory

Returns:



44
45
46
# File 'lib/elm_install/identifier.rb', line 44

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

#warn(message) ⇒ Hash

Exits the current process and logs a given message.

Parameters:

  • message (String)

    The message

Returns:

  • (Hash)

    An empty hash



147
148
149
150
# File 'lib/elm_install/identifier.rb', line 147

def warn(message)
  Logger.arrow message if @options[:vebose]
  {}
end