Class: Gem::Commands::LocalCommand

Inherits:
Gem::Command
  • Object
show all
Defined in:
lib/rubygems/commands/local_command/version.rb,
lib/rubygems/commands/local_command.rb

Defined Under Namespace

Classes: Setting

Constant Summary collapse

VERSION =
"0.2.0"

Instance Method Summary collapse

Constructor Details

#initializeLocalCommand

Returns a new instance of LocalCommand.



13
14
15
# File 'lib/rubygems/commands/local_command.rb', line 13

def initialize
  super("local", "A configuration manager for bundler's local gem settings.")
end

Instance Method Details

#add(name = nil, location = nil, *args) ⇒ Object Also known as: new

COMMANDS



72
73
74
75
76
77
78
79
80
81
# File 'lib/rubygems/commands/local_command.rb', line 72

def add(name = nil, location = nil, *args)
  if name and location and args.empty?
    setting = Setting.new(location)
    if bundler_add name, setting
      update_configuration(name, location: setting.location, status: setting.status)
    end
  else
    arity_error __method__
  end
end

#argumentsObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rubygems/commands/local_command.rb', line 23

def arguments
  "add <gem> <path>  | Adds or overwrites a local gem configuration.\nstatus [gem]      | Displays all or a particular local gem configuration.\nremove <gem>      | Removes a local gem from `gem local` configuration.\nuse [gem, ...]    | Enables local gem(s).\nignore [gem, ...] | Disables local gem(s).\nrebuild           | Regenerates your `.gemlocal` from bundle config state.\ninstall           | Adds `.gemlocal` artifact to project `.gitignore`.\nhelp [cmd]        | Displays help information.\nversion           | Displays gem-local version.\n  ARGS\nend\n"

#descriptionObject



17
18
19
20
21
# File 'lib/rubygems/commands/local_command.rb', line 17

def description
  "The `gem local` command allows you to save, toggle, and recall per-project usage of `bundle config local.<gem>` settings.\n  DESC\nend\n"

#executeObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rubygems/commands/local_command.rb', line 41

def execute
  if cmd = options[:args].shift
    if available_cmds.include? cmd
      public_send cmd, *options[:args]
    else
      raise "`gem local #{cmd}` is not a valid command, try:\n" + arguments
    end
  else
    help
  end
end

#full_helpObject

Shows in ‘gem help local`



202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/rubygems/commands/local_command.rb', line 202

def full_help
  [
    usage, 
    nil,
    'Summary:',
    summary,
    nil,
    'Description:',
    description,
    nil,
    'Arguments:',
    arguments,
  ].join("\n")
end

#help(cmd = nil, *args) ⇒ Object



183
184
185
186
187
188
189
190
191
# File 'lib/rubygems/commands/local_command.rb', line 183

def help(cmd = nil, *args)
  if not cmd and args.empty?
    puts description + "\n" + arguments
  elsif cmd
    puts info_for(cmd)
  else
    arity_error __method__
  end    
end

#ignore(*names) ⇒ Object Also known as: off, remote, deactivate, disable



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rubygems/commands/local_command.rb', line 132

def ignore(*names)
  names = configuration.values if names.empty?
  names.each do |name|
    if setting = configuration[name]
      if bundler_remove name, setting
        update_configuration(name, status: "off")
      else
        raise "Could not deactivate gem, make sure `bundle config --delete local.#{name}` succeeds"
      end
    else
      raise "`gem local #{__method__}` could not find `#{name}` in:\n#{find_configuration}"
    end
  end
end

#install(*args) ⇒ Object Also known as: init



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/rubygems/commands/local_command.rb', line 168

def install(*args)
  if args.empty?
    File.open(find_file('.gitignore'), "a+") do |file|
      %w[.bundle .gemlocal].each do |ignorable|
        unless file.each_line.any?{ |line| line.include? ignorable }
          file.puts ignorable 
        end
      end
    end
  else
    arity_error __method__
  end    
end

#invoke_with_build_args(args, build_args) ⇒ Object

Override core dispatcher to do our own help



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rubygems/commands/local_command.rb', line 54

def invoke_with_build_args(args, build_args)
  handle_options args

  options[:build_args] = build_args

  self.ui = Gem::SilentUI.new if options[:silent]

  if options[:help] then
    puts full_help
  elsif @when_invoked then
    @when_invoked.call options
  else
    execute
  end
end

#rebuild(*args) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rubygems/commands/local_command.rb', line 151

def rebuild(*args)
  if args.empty?
    configuration = read_configuration.dup
    clear_configuration_cache
    File.open(find_configuration, "w") do |file|
      configuration.each do |name, old_setting|
        if setting = bundler_value(name, old_setting)
          file.puts configuration_for(name, setting.location, setting.status)
        end
      end
    end
    show
  else
    arity_error __method__
  end    
end

#remove(name = nil, *args) ⇒ Object Also known as: delete



102
103
104
105
106
107
108
109
# File 'lib/rubygems/commands/local_command.rb', line 102

def remove(name = nil, *args)
  if name and args.empty?
    configuration.delete(name)
    write_configuration(configuration)
  else
    arity_error __method__
  end
end

#status(name = nil, *args) ⇒ Object Also known as: config, show



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rubygems/commands/local_command.rb', line 84

def status(name = nil, *args)
  if not name and args.empty?
    configuration.each do |name, setting|
      puts show_setting_for(name, setting)
    end
  elsif name
    if setting = configuration[name]
      puts show_setting_for(name, setting)
    else
      raise "`gem local #{__method__}` could not find `#{name}` in:\n#{find_configuration}"
    end
  else
    arity_error __method__
  end
end

#usageObject



37
38
39
# File 'lib/rubygems/commands/local_command.rb', line 37

def usage
  "#{program_name} subcommand [args...]"
end

#use(*names) ⇒ Object Also known as: on, activate, enable, renable, reactivate



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rubygems/commands/local_command.rb', line 112

def use(*names)
  names = configuration.keys if names.empty?
  names.each do |name|
    if setting = configuration[name]
      if bundler_add name, setting
        update_configuration(name, status: "on")
      else
        raise "Could not activate gem, make sure `bundle config local.#{name}` #{setting.location} succeeds"
      end
    else
      raise "`gem local #{__method__}` could not find `#{name}` in:\n#{find_configuration}"
    end
  end
end

#version(*args) ⇒ Object



193
194
195
196
197
198
199
# File 'lib/rubygems/commands/local_command.rb', line 193

def version(*args)
  if args.empty?
    puts "v#{Gem::Commands::LocalCommand::VERSION}"
  else
    arity_error __method__
  end    
end