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

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

Parameters:

  • package (String)

    The package

  • url (String)

    The url



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`.

Parameters:

  • path (String)

    The path for the file

Returns:

  • (Hash)

    The hash of dependenceis (url => version or ref)



10
11
12
13
14
15
16
# File 'lib/elm_install/elm_package.rb', line 10

def self.dependencies(path, options = { silent: true })
  json = read path, options
  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.

Parameters:

  • message (String)

    The message

  • options (Hash)

    The options



39
40
41
42
43
# File 'lib/elm_install/elm_package.rb', line 39

def self.exit(message, options)
  return {} if options[:silent]
  Logger.arrow message
  Process.exit
end

.read(path, options = { silent: true }) ⇒ Hash

Reads the given file as JSON.

:reek:DuplicateMethodCall

Parameters:

  • path (String)

    The path

Returns:

  • (Hash)

    The json



25
26
27
28
29
30
31
# File 'lib/elm_install/elm_package.rb', line 25

def self.read(path, options = { silent: true })
  JSON.parse(File.read(path))
rescue JSON::ParserError
  exit "Invalid JSON in file: #{path.bold}.", options
rescue Errno::ENOENT
  exit "Could not find file: #{path.bold}.", options
end

.transform_dependencies(raw_dependencies, sources) ⇒ Hash

Transform dependencies from (package name => version) to (url => version or ref) format using the ‘depdendency-sources` field.

Parameters:

  • raw_dependencies (Hash)

    The raw dependencies

  • sources (Hash)

    The sources for the dependencies

Returns:

  • (Hash)

    The dependencies



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

Parameters:

  • package (String)

    The package

  • value (String)

    The version

  • constraint (String)

    The constarint

  • memo (Hash)

    The hash to save the dependency to

Returns:

  • (Hash)

    The memo object



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.

Parameters:

  • key (String)

    The package

Returns:

  • (String)

    The url



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