Module: Ji2p::Bundler

Extended by:
Bundler
Included in:
Bundler
Defined in:
lib/ji2p/bundler.rb

Instance Method Summary collapse

Instance Method Details

#bundler_arguments(options = {}) ⇒ Array<String>

build Bundler::CLI.start arguments array from the given options hash

Parameters:

  • option (Hash)

    the invoke! options hash

Returns:

  • (Array<String>)

    Bundler::CLI.start string arguments array



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ji2p/bundler.rb', line 121

def bundler_arguments(options = {})
  arguments = []

  if options[:install]
    arguments << "install"
    arguments << "--clean" if options[:clean]
    if options[:local]
      arguments << "--local"
      arguments << "--no-prune" # From bundler docs: Don't remove stale gems from the cache.
    end
  elsif options[:update]
    arguments << "update"
    arguments << options[:update]
    arguments << "--local" if options[:local]
  elsif options[:clean]
    arguments << "clean"
  elsif options[:package]
    arguments << "package"
    arguments << "--all" if options[:all]
  end

  arguments << "--verbose" if options[:verbose]

  arguments.flatten
end

#capture_stdout(&block) ⇒ String, Exception

capture any $stdout from the passed block. also trap any exception in that block, in which case the trapped exception will be returned

Parameters:

  • the (Proc)

    code block to execute

Returns:

  • (String, Exception)

    the captured $stdout string and any trapped exception or nil if none



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/ji2p/bundler.rb', line 150

def capture_stdout(&block)
  old_stdout = $stdout
  $stdout = StringIO.new("", "w")
  begin
    block.call
  rescue => e
    return [$stdout.string, e]
  end

  [$stdout.string, nil]
ensure
  $stdout = old_stdout
end

#debug?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/ji2p/bundler.rb', line 114

def debug?
  ENV["DEBUG"]
end

#execute_bundler(options) ⇒ Object



109
110
111
112
# File 'lib/ji2p/bundler.rb', line 109

def execute_bundler(options)
  ::Bundler.reset!
  ::Bundler::CLI.start(bundler_arguments(options))
end

#execute_bundler_with_retry(options) ⇒ Object



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/ji2p/bundler.rb', line 76

def execute_bundler_with_retry(options)
  try = 0
  # capture_stdout also traps any raised exception and pass them back as the function return [output, exception]
  output, exception = capture_stdout do
    loop do
      begin
        execute_bundler(options)
        break
      rescue ::Bundler::VersionConflict => e
        $stderr.puts("Plugin version conflict, aborting")
        raise(e)
      rescue ::Bundler::GemNotFound => e
        $stderr.puts("Plugin not found, aborting")
        raise(e)
      rescue => e
        if try >= options[:max_tries]
          $stderr.puts("Too many retries, aborting, caused by #{e.class}")
          $stderr.puts(e.message) if ENV["DEBUG"]
          raise(e)
        end

        try += 1
        $stderr.puts("Error #{e.class}, retrying #{try}/#{options[:max_tries]}")
        $stderr.puts(e.message)
        sleep(0.5)
      end
    end
  end
  raise exception if exception

  return output
end

#invoke!(options = {}) ⇒ String, Exception

execute bundle install and capture any $stdout output. any raised exception in the process will be trapped and returned. logs errors to $stdout.

Parameters:

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

    invoke options with default values, :max_tries => 10, :clean => false, :install => false, :update => false

Options Hash (options):

  • :max_tries (Boolean)

    The number of times bundler is going to try the installation before failing (default: 10)

  • :clean (Boolean)

    It cleans the unused gems (default: false)

  • :install (Boolean)

    Run the installation of a set of gems defined in a Gemfile (default: false)

  • :update (Boolean, String, Array)

    Update the current environment, must be either false or a String or an Array of String (default: false)

  • :local (Boolean)

    Do not attempt to fetch gems remotely and use the gem cache instead (default: false)

  • :package (Boolean)

    Locks and then caches all dependencies to be reused later on (default: false)

  • :all (Boolean)

    It packages dependencies defined with :git or :path (default: false)

  • :without (Array)

    Exclude gems that are part of the specified named group (default: [:development])

Returns:

  • (String, Exception)

    the installation captured output and any raised exception or nil if none



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ji2p/bundler.rb', line 42

def invoke!(options = {})
  options = {:max_tries => 10, :clean => false, :install => false, :update => false, :local => false,
             :jobs => 12, :all => false, :package => false, :without => [:development]}.merge(options)
  options[:without] = Array(options[:without])
  options[:update] = Array(options[:update]) if options[:update]

  ::Gem.clear_paths
  ENV['GEM_HOME'] = ENV['GEM_PATH'] = Ji2p::Environment.ji2p_gem_home
  ::Gem.paths = ENV
  # set BUNDLE_GEMFILE ENV before requiring bundler to avoid bundler recurse and load unrelated Gemfile(s).
  # in the context of calling Bundler::CLI this is not really required since Bundler::CLI will look at
  # Bundler.settings[:gemfile] unlike Bundler.setup. For the sake of consistency and defensive/future proofing, let's keep it here.
  ENV["BUNDLE_GEMFILE"] = Ji2p::Environment::GEMFILE_PATH

  require 'bundler'
  require 'bundler/cli'

  # force Rubygems sources to our Gemfile sources
  ::Gem.sources = ::Gem::SourceList.from(options[:rubygems_source]) if options[:rubygems_source]

  ::Bundler.settings.set_local(:path, Ji2p::Environment::BUNDLE_DIR)
  ::Bundler.settings.set_local(:gemfile, Ji2p::Environment::GEMFILE_PATH)
  ::Bundler.settings.set_local(:without, options[:without])
  ::Bundler.settings.set_local(:force, options[:force])

  if !debug?
    # Will deal with transient network errors
    execute_bundler_with_retry(options)
  else
    options[:verbose] = true
    execute_bundler(options)
  end
end

#setup!(options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ji2p/bundler.rb', line 7

def setup!(options = {})
  options = {:without => [:development]}.merge(options)
  options[:without] = Array(options[:without])

  ::Gem.clear_paths
  ENV['GEM_HOME'] = ENV['GEM_PATH'] = Environment.ji2p_gem_home
  ::Gem.paths = ENV

  # set BUNDLE_GEMFILE ENV before requiring bundler to avoid bundler recurse and load unrelated Gemfile(s)
  ENV["BUNDLE_GEMFILE"] = Environment::GEMFILE_PATH

  require 'bundler'

  ::Bundler.settings.set_local(:path, Environment::BUNDLE_DIR)
  ::Bundler.settings.set_local(:without, options[:without])
  # in the context of Bundler.setup it looks like this is useless here because Gemfile path can only be specified using
  # the ENV, see https://github.com/bundler/bundler/blob/v1.8.3/lib/bundler/shared_helpers.rb#L103
  ::Bundler.settings.set_local(:gemfile, Environment::GEMFILE_PATH)

  ::Bundler.reset!
  ::Bundler.setup
end