Module: Ji2p::Bundler
Instance Method Summary collapse
-
#bundler_arguments(options = {}) ⇒ Array<String>
build Bundler::CLI.start arguments array from the given options hash.
-
#capture_stdout(&block) ⇒ String, Exception
capture any $stdout from the passed block.
- #debug? ⇒ Boolean
- #execute_bundler(options) ⇒ Object
- #execute_bundler_with_retry(options) ⇒ Object
-
#invoke!(options = {}) ⇒ String, Exception
execute bundle install and capture any $stdout output.
- #setup!(options = {}) ⇒ Object
Instance Method Details
#bundler_arguments(options = {}) ⇒ Array<String>
build Bundler::CLI.start arguments array from the given options hash
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( = {}) arguments = [] if [:install] arguments << "install" arguments << "--clean" if [:clean] if [:local] arguments << "--local" arguments << "--no-prune" # From bundler docs: Don't remove stale gems from the cache. end elsif [:update] arguments << "update" arguments << [:update] arguments << "--local" if [:local] elsif [:clean] arguments << "clean" elsif [:package] arguments << "package" arguments << "--all" if [:all] end arguments << "--verbose" if [: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
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
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() ::Bundler.reset! ::Bundler::CLI.start(bundler_arguments()) 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() 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() 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 >= [:max_tries] $stderr.puts("Too many retries, aborting, caused by #{e.class}") $stderr.puts(e.) if ENV["DEBUG"] raise(e) end try += 1 $stderr.puts("Error #{e.class}, retrying #{try}/#{[:max_tries]}") $stderr.puts(e.) 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.
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!( = {}) = {:max_tries => 10, :clean => false, :install => false, :update => false, :local => false, :jobs => 12, :all => false, :package => false, :without => [:development]}.merge() [:without] = Array([:without]) [:update] = Array([:update]) if [: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([:rubygems_source]) if [: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, [:without]) ::Bundler.settings.set_local(:force, [:force]) if !debug? # Will deal with transient network errors execute_bundler_with_retry() else [:verbose] = true execute_bundler() 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!( = {}) = {:without => [:development]}.merge() [:without] = Array([: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, [: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 |