Class: Velcro::Lockfile

Inherits:
Object
  • Object
show all
Includes:
FileHelpers
Defined in:
lib/velcro/lockfile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FileHelpers

#location, #recursive_search

Constructor Details

#initializeLockfile

Returns a new instance of Lockfile.



10
11
12
# File 'lib/velcro/lockfile.rb', line 10

def initialize
  self.homebrew = Homebrew.new
end

Instance Attribute Details

#homebrewObject

Returns the value of attribute homebrew.



8
9
10
# File 'lib/velcro/lockfile.rb', line 8

def homebrew
  @homebrew
end

Instance Method Details

#generate(dependencies) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/velcro/lockfile.rb', line 14

def generate(dependencies)
  File.open(lockfile_in(Dir.pwd), 'w+') do |lockfile|
    lockfile.write(
      [
        'FORMULA',
        indent(generate_formula(dependencies)),
        '',
        'DEPENDENCIES',
        indent(generate_dependencies(dependencies)),
        ''
      ].flatten.join("\n")
    )
  end
end

#generate_child_dependencies(dependency) ⇒ Object



38
39
40
41
42
# File 'lib/velcro/lockfile.rb', line 38

def generate_child_dependencies(dependency)
  self.homebrew.child_dependencies(dependency).map do |child|
    "#{child.name} (#{child.version})"
  end
end

#generate_dependencies(dependencies) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/velcro/lockfile.rb', line 44

def generate_dependencies(dependencies)
  dependencies.map do |dependency|
    line = "#{dependency.name}"
    line << " (#{dependency.version})" if dependency.version
    line
  end
end

#generate_formula(dependencies) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/velcro/lockfile.rb', line 29

def generate_formula(dependencies)
  dependencies.map do |dependency|
    [
      "#{dependency.name} (#{specified_or_most_recent_version(dependency)})",
      indent(generate_child_dependencies(dependency))
    ]
  end.flatten
end

#indent(lines) ⇒ Object



56
57
58
59
60
# File 'lib/velcro/lockfile.rb', line 56

def indent(lines)
  Array(lines).map do |line|
    "  #{line}"
  end
end

#lockfile_in(directory) ⇒ Object



62
63
64
# File 'lib/velcro/lockfile.rb', line 62

def lockfile_in(directory)
  File.join(directory, 'Brewfile.lock')
end

#specified_or_most_recent_version(dependency) ⇒ Object



52
53
54
# File 'lib/velcro/lockfile.rb', line 52

def specified_or_most_recent_version(dependency)
  dependency.version || self.homebrew.versions(dependency).split.first
end