Class: Bundler::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/bundler/cli.rb

Constant Summary

Constants inherited from Thor

Thor::HELP_MAPPINGS, Thor::THOR_RESERVED_WORDS, Thor::VERSION

Instance Attribute Summary

Attributes included from Thor::Actions

#behavior

Attributes included from Thor::Base

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Thor::Actions

#action, #append_file, #apply, #chmod, #copy_file, #create_file, #destination_root, #destination_root=, #directory, #empty_directory, #find_in_source_paths, #get, #gsub_file, #in_root, included, #inject_into_class, #inject_into_file, #inside, #prepend_file, #relative_to_original_destination_root, #remove_file, #run, #run_ruby_script, #source_paths, #template, #thor

Methods inherited from Thor

check_unknown_options!, check_unknown_options?, default_task, desc, help, long_desc, map, method_option, method_options, printable_tasks, subcommand, subcommands, task_help

Methods included from Thor::Base

included, register_klass_file, shell, shell=, subclass_files, subclasses

Constructor Details

#initializeCLI

Returns a new instance of CLI.



14
15
16
17
18
19
20
# File 'lib/bundler/cli.rb', line 14

def initialize(*)
  super
  the_shell = (options["no-color"] ? Thor::Shell::Basic.new : shell)
  Bundler.ui = UI::Shell.new(the_shell)
  Bundler.ui.debug! if options["verbose"]
  Gem::DefaultUserInteraction.ui = UI::RGProxy.new(Bundler.ui)
end

Class Method Details

.source_rootObject



501
502
503
# File 'lib/bundler/cli.rb', line 501

def self.source_root
  File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
end

Instance Method Details

#cacheObject



311
312
313
314
315
316
317
318
319
320
# File 'lib/bundler/cli.rb', line 311

def cache
  Bundler.definition.resolve_with_cache!
  Bundler.load.cache
  Bundler.settings[:no_prune] = true if options["no-prune"]
  Bundler.load.lock
rescue GemNotFound => e
  Bundler.ui.error(e.message)
  Bundler.ui.warn "Run `bundle install` to install missing gems."
  exit 128
end

#checkObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/bundler/cli.rb', line 100

def check
  ENV['BUNDLE_GEMFILE'] = File.expand_path(options[:gemfile]) if options[:gemfile]
  begin
    not_installed = Bundler.definition.missing_specs
  rescue GemNotFound, VersionConflict
    Bundler.ui.error "Your Gemfile's dependencies could not be satisfied"
    Bundler.ui.warn  "Install missing gems with `bundle install`"
    exit 1
  end

  if not_installed.any?
    Bundler.ui.error "The following gems are missing"
    not_installed.each { |s| Bundler.ui.error " * #{s.name} (#{s.version})" }
    Bundler.ui.warn "Install missing gems with `bundle install`"
    exit 1
  else
    Bundler.load.lock
    Bundler.ui.info "The Gemfile's dependencies are satisfied"
  end
end

#config(name = nil, *args) ⇒ Object



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/bundler/cli.rb', line 373

def config(name = nil, *args)
  values = ARGV.dup
  values.shift # remove config
  values.shift # remove the name

  unless name
    Bundler.ui.confirm "Settings are listed in order of priority. The top value will be used.\n"

    Bundler.settings.all.each do |setting|
      Bundler.ui.confirm "#{setting}"
      with_padding do
        Bundler.settings.pretty_values_for(setting).each do |line|
          Bundler.ui.info line
        end
      end
      Bundler.ui.confirm ""
    end
    return
  end

  if values.empty?
    Bundler.ui.confirm "Settings for `#{name}` in order of priority. The top value will be used"
    with_padding do
      Bundler.settings.pretty_values_for(name).each { |line| Bundler.ui.info line }
    end
  else
    locations = Bundler.settings.locations(name)

    if local = locations[:local]
      Bundler.ui.info "Your application has set #{name} to #{local.inspect}. This will override the " \
        "system value you are currently setting"
    end

    if global = locations[:global]
      Bundler.ui.info "You are replacing the current system value of #{name}, which is currently #{global}"
    end

    if env = locations[:env]
      Bundler.ui.info "You have set a bundler environment variable for #{env}. This will take precedence " \
        "over the system value you are setting"
    end

    Bundler.settings.set_global(name, values.join(" "))
  end
end

#console(group = nil) ⇒ Object



435
436
437
438
439
440
441
# File 'lib/bundler/cli.rb', line 435

def console(group = nil)
  group ? Bundler.require(:default, *(group.split.map! {|g| g.to_sym })) : Bundler.require
  ARGV.clear

  require 'irb'
  IRB.start
end

#execObject



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/bundler/cli.rb', line 343

def exec(*)
  ARGV.delete("exec")

  Bundler.setup

  begin
    # Run
    Kernel.exec(*ARGV)
  rescue Errno::EACCES
    Bundler.ui.error "bundler: not executable: #{ARGV.first}"
    exit 126
  rescue Errno::ENOENT
    Bundler.ui.error "bundler: command not found: #{ARGV.first}"
    Bundler.ui.warn  "Install missing gem binaries with `bundle install`"
    exit 127
  end
end

#gem(name) ⇒ Object



481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/bundler/cli.rb', line 481

def gem(name)
  target = File.join(Dir.pwd, name)
  constant_name = name.split('_').map{|p| p.capitalize}.join
  constant_name = constant_name.split('-').map{|q| q.capitalize}.join('::') if constant_name =~ /-/
  constant_array = constant_name.split('::')
  FileUtils.mkdir_p(File.join(target, 'lib', name))
  opts = {:name => name, :constant_name => constant_name, :constant_array => constant_array}
  template(File.join("newgem/Gemfile.tt"),               File.join(target, "Gemfile"),                opts)
  template(File.join("newgem/Rakefile.tt"),              File.join(target, "Rakefile"),               opts)
  template(File.join("newgem/gitignore.tt"),             File.join(target, ".gitignore"),             opts)
  template(File.join("newgem/newgem.gemspec.tt"),        File.join(target, "#{name}.gemspec"),        opts)
  template(File.join("newgem/lib/newgem.rb.tt"),         File.join(target, "lib/#{name}.rb"),         opts)
  template(File.join("newgem/lib/newgem/version.rb.tt"), File.join(target, "lib/#{name}/version.rb"), opts)
  if options[:bin]
    template(File.join("newgem/bin/newgem.tt"),          File.join(target, 'bin', name),              opts)
  end
  Bundler.ui.info "Initializating git repo in #{target}"
  Dir.chdir(target) { `git init`; `git add .` }
end

#help(cli = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bundler/cli.rb', line 28

def help(cli = nil)
  case cli
  when "gemfile" then command = "gemfile.5"
  when nil       then command = "bundle"
  else command = "bundle-#{cli}"
  end

  manpages = %w(
      bundle
      bundle-config
      bundle-exec
      bundle-install
      bundle-package
      bundle-update
      gemfile.5)

  if manpages.include?(command)
    root = File.expand_path("../man", __FILE__)

    if have_groff? && root !~ %r{^file:/.+!/META-INF/jruby.home/.+}
      groff   = "groff -Wall -mtty-char -mandoc -Tascii"
      pager   = ENV['MANPAGER'] || ENV['PAGER'] || 'less -R'

      Kernel.exec "#{groff} #{root}/#{command} | #{pager}"
    else
      puts File.read("#{root}/#{command}.txt")
    end
  else
    super
  end
end

#initObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/bundler/cli.rb', line 67

def init
  opts = options.dup
  if File.exist?("Gemfile")
    Bundler.ui.error "Gemfile already exists at #{Dir.pwd}/Gemfile"
    exit 1
  end

  if opts[:gemspec]
    gemspec = File.expand_path(opts[:gemspec])
    unless File.exist?(gemspec)
      Bundler.ui.error "Gem specification #{gemspec} doesn't exist"
      exit 1
    end
    spec = Gem::Specification.load(gemspec)
    puts "Writing new Gemfile to #{Dir.pwd}/Gemfile"
    File.open('Gemfile', 'wb') do |file|
      file << "# Generated from #{gemspec}\n"
      file << spec.to_gemfile
    end
  else
    puts "Writing new Gemfile to #{Dir.pwd}/Gemfile"
    FileUtils.cp(File.expand_path('../templates/Gemfile', __FILE__), 'Gemfile')
  end
end

#install(path = nil) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/bundler/cli.rb', line 156

def install(path = nil)
  opts = options.dup
  opts[:without] ||= []
  if opts[:without].size == 1
    opts[:without].map!{|g| g.split(" ") }
    opts[:without].flatten!
  end
  opts[:without].map!{|g| g.to_sym }

  ENV['BUNDLE_GEMFILE'] = File.expand_path(opts[:gemfile]) if opts[:gemfile]
  ENV['RB_USER_INSTALL'] = '1' if Bundler::FREEBSD

  # Just disable color in deployment mode
  Bundler.ui.shell = Thor::Shell::Basic.new if opts[:deployment]

  if (path || opts[:path] || opts[:deployment]) && opts[:system]
    Bundler.ui.error "You have specified both a path to install your gems to, \n" \
                     "as well as --system. Please choose."
    exit 1
  end

  if path && opts[:path]
    Bundler.ui.error "You have specified a path via `bundle install #{path}` as well as\n" \
                     "by `bundle install --path #{options[:path]}`. These options are\n" \
                     "equivalent, so please use one or the other."
    exit 1
  end

  if opts["disable-shared-gems"]
    Bundler.ui.error "The disable-shared-gem option is no longer available.\n\n" \
                     "Instead, use `bundle install` to install to your system,\n" \
                     "or `bundle install --path path/to/gems` to install to an isolated\n" \
                     "location. Bundler will resolve relative paths relative to\n" \
                     "your `Gemfile`."
    exit 1
  end

  if opts[:deployment] || opts[:frozen]
    unless Bundler.default_lockfile.exist?
      flag = opts[:deployment] ? '--deployment' : '--frozen'
      raise ProductionError, "The #{flag} flag requires a Gemfile.lock. Please make " \
                             "sure you have checked your Gemfile.lock into version control " \
                             "before deploying."
    end

    if Bundler.root.join("vendor/cache").exist?
      opts[:local] = true
    end

    Bundler.settings[:frozen] = '1'
  end

  # When install is called with --no-deployment, disable deployment mode
  if opts[:deployment] == false
    Bundler.settings.delete(:frozen)
    opts[:system] = true
  end

  # Can't use Bundler.settings for this because settings needs gemfile.dirname
  Bundler.settings[:path] = nil if opts[:system]
  Bundler.settings[:path] = "vendor/bundle" if opts[:deployment]
  Bundler.settings[:path] = path if path
  Bundler.settings[:path] = opts[:path] if opts[:path]
  Bundler.settings[:bin] = opts["binstubs"] if opts[:binstubs]
  Bundler.settings[:no_prune] = true if opts["no-prune"]
  Bundler.settings[:disable_shared_gems] = Bundler.settings[:path] ? '1' : nil
  Bundler.settings.without = opts[:without] unless opts[:without].empty?
  Bundler.ui.be_quiet! if opts[:quiet]

  Installer.install(Bundler.root, Bundler.definition, opts)
  Bundler.load.cache if Bundler.root.join("vendor/cache").exist? && !options["no-cache"]

  if Bundler.settings[:path]
    relative_path = Bundler.settings[:path]
    relative_path = "./" + relative_path unless relative_path[0] == ?/
    Bundler.ui.confirm "Your bundle is complete! " +
      "It was installed into #{relative_path}"
  else
    Bundler.ui.confirm "Your bundle is complete! " +
      "Use `bundle show [gemname]` to see where a bundled gem is installed."
  end

  if path
    Bundler.ui.warn "The path argument to `bundle install` is deprecated. " +
      "It will be removed in version 1.1. " +
      "Please use `bundle install --path #{path}` instead."
  end
rescue GemNotFound => e
  if opts[:local]
    Bundler.ui.warn "Some gems seem to be missing from your vendor/cache directory."
  end

  if Bundler.definition.no_sources?
    Bundler.ui.warn "Your Gemfile doesn't have any sources. You can add one with a line like 'source :rubygems'"
  end
  raise e
end

#lockObject



281
282
283
# File 'lib/bundler/cli.rb', line 281

def lock
  Bundler.ui.warn "Lock is deprecated. Your bundle is now locked whenever you run `bundle install`."
end

#open(name) ⇒ Object



420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/bundler/cli.rb', line 420

def open(name)
  editor = [ENV['BUNDLER_EDITOR'], ENV['VISUAL'], ENV['EDITOR']].find{|e| !e.nil? && !e.empty? }
  if editor
    gem_path = locate_gem(name)
    Dir.chdir(gem_path) do
      command = "#{editor} #{gem_path}"
      success = system(command)
      Bundler.ui.info "Could not run '#{command}'" unless success
    end
  else
    Bundler.ui.info("To open a bundled gem, set $EDITOR or $BUNDLER_EDITOR")
  end
end

#packageObject



330
331
332
333
334
# File 'lib/bundler/cli.rb', line 330

def package
  install
  # TODO: move cache contents here now that all bundles are locked
  Bundler.load.cache
end

#show(gem_name = nil) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/bundler/cli.rb', line 295

def show(gem_name = nil)
  Bundler.load.lock

  if gem_name
    Bundler.ui.info locate_gem(gem_name)
  else
    Bundler.ui.info "Gems included by the bundle:"
    Bundler.load.specs.sort_by { |s| s.name }.each do |s|
      Bundler.ui.info "  * #{s.name} (#{s.version}#{s.git_version})"
    end
  end
end

#unlockObject



286
287
288
# File 'lib/bundler/cli.rb', line 286

def unlock
  Bundler.ui.warn "Unlock is deprecated. To update to newer gem versions, use `bundle update`."
end

#update(*gems) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/bundler/cli.rb', line 263

def update(*gems)
  sources = Array(options[:source])

  if gems.empty? && sources.empty?
    # We're doing a full update
    Bundler.definition(true)
  else
    Bundler.definition(:gems => gems, :sources => sources)
  end

  opts = {"update" => true, "local" => options[:local]}
  Installer.install Bundler.root, Bundler.definition, opts
  Bundler.load.cache if Bundler.root.join("vendor/cache").exist?
  Bundler.ui.confirm "Your bundle is updated! " +
    "Use `bundle show [gemname]` to see where a bundled gem is installed."
end

#versionObject



444
445
446
# File 'lib/bundler/cli.rb', line 444

def version
  Bundler.ui.info "Bundler version #{Bundler::VERSION}"
end

#vizObject



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/bundler/cli.rb', line 458

def viz
  output_file = File.expand_path(options[:file])
  graph = Graph.new( Bundler.load )

  begin
    graph.viz(output_file, options[:version], options[:requirements])
    Bundler.ui.info output_file
  rescue LoadError => e
    Bundler.ui.error e.inspect
    Bundler.ui.warn "Make sure you have the graphviz ruby gem. You can install it with:"
    Bundler.ui.warn "`gem install ruby-graphviz`"
  rescue StandardError => e
    if e.message =~ /GraphViz not installed or dot not in PATH/
      Bundler.ui.error e.message
      Bundler.ui.warn "The ruby graphviz gem requires GraphViz to be installed"
    else
      raise
    end
  end
end