Class: ElmInstall::Populator

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

Overview

This class is responsible for populating the ‘elm-stuff` directory.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(git_resolver) ⇒ Populator

Initializes a new populator.

Parameters:

  • git_resolver (GitResolver)

    The git resolver to use.



7
8
9
# File 'lib/elm_install/populator.rb', line 7

def initialize(git_resolver)
  @git_resolver = git_resolver
end

Class Method Details

.exact_dependencies(solution) ⇒ void

This method returns an undefined value.

Returns the exact dependencies from the solution.

Parameters:

  • solution (Hash)

    The solution



78
79
80
81
82
83
84
# File 'lib/elm_install/populator.rb', line 78

def self.exact_dependencies(solution)
  solution.each_with_object({}) do |(key, value), memo|
    version, = version_and_ref value

    memo[GitCloneUrl.parse(key).path.sub(%r{^/}, '')] = version
  end
end

.version_and_ref(value) ⇒ Array

Retruns the version and the ref from the given string.

Parameters:

  • value (String)

    The input

Returns:

  • (Array)

    The version and the ref as an array



91
92
93
94
95
96
97
98
# File 'lib/elm_install/populator.rb', line 91

def self.version_and_ref(value)
  match = value.match(/(.+)\+(.+)/)

  version = match ? match[1] : value
  ref = match ? match[2] : value

  [version, ref]
end

Instance Method Details

#copy_package(package, package_path) ⇒ void

This method returns an undefined value.

Copies the given package from it’s repository to the given path.

Parameters:

  • package (String)

    The package to copy

  • package_path (String)

    The destination



53
54
55
56
57
58
59
# File 'lib/elm_install/populator.rb', line 53

def copy_package(package, package_path)
  repository_path = File.join(@git_resolver.repository_path(package), '.')

  FileUtils.mkdir_p(package_path)
  FileUtils.cp_r(repository_path, package_path)
  FileUtils.rm_rf(File.join(package_path, '.git'))
end

#populate(solution) ⇒ void

This method returns an undefined value.

Populates ‘elm-stuff` from the given solution.

Parameters:

  • solution (Hash)

    The solution.



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

def populate(solution)
  solution.each do |package, version|
    resolve_package package, version
  end

  write_exact_dependencies(solution)
end

#resolve_package(package, version_str) ⇒ void

This method returns an undefined value.

Resolves and copies a package and it’s version to ‘elm-stuff/packages` directory.

:reek:TooManyStatements { max_statements: 9 }

Parameters:

  • package (String)

    The package

  • version_str (String)

    The resolved version



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/elm_install/populator.rb', line 33

def resolve_package(package, version_str)
  version, ref = self.class.version_and_ref(version_str)

  package_name, package_path = Utils.package_version_path package, version

  @git_resolver.repository(package).checkout(ref)

  Logger.dot "#{package_name.bold} - #{version.bold} (#{ref})"

  FileUtils.rm_rf(package_path) if Dir.exist?(package_path)

  copy_package package, package_path
end

#write_exact_dependencies(solution) ⇒ void

This method returns an undefined value.

Writes the ‘elm-stuff/exact-dependencies.json` file.

Parameters:

  • solution (Hash)

    The solution



66
67
68
69
70
71
# File 'lib/elm_install/populator.rb', line 66

def write_exact_dependencies(solution)
  File.binwrite(
    File.join('elm-stuff', 'exact-dependencies.json'),
    JSON.pretty_generate(self.class.exact_dependencies(solution))
  )
end