Class: Lono::Configset::Materializer::GemsBuilder

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/lono/configset/materializer/gems_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(*jades) ⇒ GemsBuilder

Returns a new instance of GemsBuilder.



7
8
9
10
# File 'lib/lono/configset/materializer/gems_builder.rb', line 7

def initialize(*jades)
  @jades = jades.flatten
  @build_root = "#{Lono.root}/tmp/configsets"
end

Instance Method Details

#buildObject



12
13
14
15
16
17
18
19
# File 'lib/lono/configset/materializer/gems_builder.rb', line 12

def build
  puts "GemsBuilder#build materializing #{@jades.map(&:name)}" if ENV['LONO_DEBUG']
  remove_gemfile
  gemfile = build_gemfile(@jades)
  return unless gemfile
  write(gemfile)
  bundle
end

#build_gemfile(jades) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/lono/configset/materializer/gems_builder.rb', line 21

def build_gemfile(jades)
  return if jades.empty?

  lines = []
  jades.each do |jade|
    return if local_exist?(jade)
    args = source.gem_args(jade)
    line = %Q|gem #{args}|
    lines << line unless lines.include?(line)
  end
  lines.sort!
  lines.join("\n") + "\n"
end

#bundleObject



35
36
37
38
39
# File 'lib/lono/configset/materializer/gems_builder.rb', line 35

def bundle
  Bundler.with_original_env do
    bundle_install
  end
end

#bundle_installObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lono/configset/materializer/gems_builder.rb', line 41

def bundle_install
  command = "cd #{@build_root} && bundle install"
  puts "=> #{command}" if ENV['LONO_DEBUG']
  stdout, stderr, status = Open3.capture3(command)

  unless status.success?
    names = @jades.map(&:name)
    gem_text = names.size == 1 ? "gem" : "gems"
    puts "Fail to build #{gem_text} #{names.join(', ')}".color(:red)
    puts "Failed running => #{command}"
    puts stdout
    puts stderr
    if stderr.include?("correct access rights")
      puts "Are you sure you have access to the git repo?".color(:yellow)
    end
    exit 1
  end
end

#local_exist?(jade) ⇒ Boolean

When a local version is found, will use that version

Returns:

  • (Boolean)


61
62
63
# File 'lib/lono/configset/materializer/gems_builder.rb', line 61

def local_exist?(jade)
  !!jade.finder.find(jade.name, local_only: true)
end

#remove_gemfileObject



71
72
73
74
# File 'lib/lono/configset/materializer/gems_builder.rb', line 71

def remove_gemfile
  FileUtils.rm_f("#{@build_root}/Gemfile")
  FileUtils.rm_f("#{@build_root}/Gemfile.lock")
end

#sourceObject



76
77
78
# File 'lib/lono/configset/materializer/gems_builder.rb', line 76

def source
  Source.new
end

#write(gemfile) ⇒ Object



65
66
67
68
69
# File 'lib/lono/configset/materializer/gems_builder.rb', line 65

def write(gemfile)
  path = "#{@build_root}/Gemfile"
  FileUtils.mkdir_p(File.dirname(path))
  IO.write(path, gemfile)
end