This repository has been archived by the owner on Apr 14, 2021. It is now read-only.
/
cli.rb
138 lines (115 loc) · 4.12 KB
/
cli.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
$:.unshift File.expand_path('../vendor', __FILE__)
require 'thor'
require 'bundler'
require 'rubygems/config_file'
# Work around a RubyGems bug
Gem.configuration
module Bundler
class CLI < Thor
ARGV = ::ARGV.dup
desc "init", "Generates a Gemfile into the current working directory"
def init
if File.exist?("Gemfile")
puts "Gemfile already exists at #{Dir.pwd}/Gemfile"
else
puts "Writing new Gemfile to #{Dir.pwd}/Gemfile"
FileUtils.cp(File.expand_path('../templates/Gemfile', __FILE__), 'Gemfile')
end
end
def initialize(*)
super
Bundler.ui = UI::Shell.new(shell)
Gem::DefaultUserInteraction.ui = UI::RGProxy.new(Bundler.ui)
end
desc "check", "Checks if the dependencies listed in Gemfile are satisfied by currently installed gems"
def check
env = Bundler.load
# Check top level dependencies
missing = env.dependencies.select { |d| env.index.search(d).empty? }
if missing.any?
puts "The following dependencies are missing"
missing.each do |d|
puts " * #{d}"
end
exit 1
else
env.specs
puts "The Gemfile's dependencies are satisfied"
end
end
desc "install", "Install the current environment to the system"
method_option "without", :type => :array, :banner => "Exclude gems that are part of the specified named group."
method_option "relock", :type => :boolean, :banner => "Unlock, install the gems, and relock."
method_option "disable-shared-gems", :type => :boolean, :banner => "Do not use any shared gems, such as the system gem repository."
def install(path = nil)
opts = options.dup
opts[:without] ||= []
opts[:without].map! { |g| g.to_sym }
Bundler.settings[:path] = path if path
Bundler.settings[:disable_shared_gems] = '1' if options["disable-shared-gems"]
remove_lockfiles if options[:relock]
Installer.install(Bundler.root, Bundler.definition, opts)
lock if options[:relock]
end
desc "lock", "Locks the bundle to the current set of dependencies, including all child dependencies."
def lock
if locked?
Bundler.ui.info("The bundle is already locked, relocking.")
remove_lockfiles
end
environment = Bundler.load
environment.lock
rescue GemNotFound, VersionConflict => e
Bundler.ui.error(e.message)
Bundler.ui.info "Run `bundle install` to install missing gems"
exit 128
end
desc "unlock", "Unlock the bundle. This allows gem versions to be changed"
def unlock
if locked?
remove_lockfiles
Bundler.ui.info("The bundle is now unlocked. The dependencies may be changed.")
else
Bundler.ui.info("The bundle is not currently locked.")
end
end
desc "show", "Shows all gems that are part of the bundle."
def show
environment = Bundler.load
Bundler.ui.info "Gems included by the bundle:"
environment.specs.sort_by { |s| s.name }.each do |s|
Bundler.ui.info " * #{s.name} (#{s.version})"
end
end
desc "pack", "Packs all the gems to vendor/cache"
def pack
environment = Bundler.load
environment.pack
end
desc "exec", "Run the command in context of the bundle"
def exec(*)
ARGV.delete('exec')
# Set PATH
paths = (ENV['PATH'] || "").split(File::PATH_SEPARATOR)
paths.unshift "#{Bundler.bundle_path}/bin"
ENV["PATH"] = paths.uniq.join(File::PATH_SEPARATOR)
# Set BUNDLE_GEMFILE
ENV['BUNDLE_GEMFILE'] = Bundler::SharedHelpers.default_gemfile.to_s
# Set RUBYOPT
rubyopt = [ENV["RUBYOPT"]].compact
rubyopt.unshift "-rbundler/setup"
rubyopt.unshift "-I#{File.expand_path('../..', __FILE__)}"
ENV["RUBYOPT"] = rubyopt.join(' ')
# Run
Kernel.exec *ARGV
end
private
def locked?
File.exist?("#{Bundler.root}/Gemfile.lock") || File.exist?("#{Bundler.root}/.bundle/environment.rb")
end
def remove_lockfiles
FileUtils.rm_f "#{Bundler.root}/Gemfile.lock"
FileUtils.rm_f "#{Bundler.root}/.bundle/environment.rb"
end
end
end