Class: Pod::Command::Spec::Edit

Inherits:
Pod::Command::Spec show all
Defined in:
lib/cocoapods/command/spec.rb

Overview

———————————————————————–#

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Pod::Command

parse, report_error, run, verify_git_version!

Methods included from Pod::Config::Mixin

#config

Constructor Details

#initialize(argv) ⇒ Edit

Returns a new instance of Edit.



245
246
247
248
249
250
# File 'lib/cocoapods/command/spec.rb', line 245

def initialize(argv)
  @show_all = argv.flag?('show-all')
  @spec = argv.shift_argument
  @spec = @spec.gsub('.podspec', '') unless @spec.nil?
  super
end

Class Method Details

.optionsObject



240
241
242
243
# File 'lib/cocoapods/command/spec.rb', line 240

def self.options
  [['--show-all', 'Pick which spec to edit from all available' \
    'versions of the given podspec']].concat(super)
end

Instance Method Details

#exec_editor(*args) ⇒ Object



294
295
296
297
# File 'lib/cocoapods/command/spec.rb', line 294

def exec_editor(*args)
  return if args.to_s.empty?
  safe_exec(which_editor, *args)
end

#runObject

Raises:



257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/cocoapods/command/spec.rb', line 257

def run
  if @show_all
    specs = get_path_of_spec(@spec, @show_all).split(/\n/)
    message = "Which spec would you like to edit [1-#{specs.count}]? "
    index = choose_from_array(specs, message)
    filepath = specs[index]
  else
    filepath = get_path_of_spec(@spec)
  end

  exec_editor(filepath.to_s) if File.exist? filepath
  raise Informative, "#{ filepath } doesn't exist."
end

#safe_exec(cmd, *args) ⇒ Object



299
300
301
302
303
# File 'lib/cocoapods/command/spec.rb', line 299

def safe_exec(cmd, *args)
  # This buys us proper argument quoting and evaluation
  # of environment variables in the cmd parameter.
  exec('/bin/sh', '-i', '-c', cmd + ' "$@"', '--', *args)
end

#validate!Object



252
253
254
255
# File 'lib/cocoapods/command/spec.rb', line 252

def validate!
  super
  help! 'A podspec name is required.' unless @spec
end

#which(cmd) ⇒ Object

Thank you homebrew



272
273
274
275
# File 'lib/cocoapods/command/spec.rb', line 272

def which(cmd)
  dir = ENV['PATH'].split(':').find { |p| File.executable? File.join(p, cmd) }
  Pathname.new(File.join(dir, cmd)) unless dir.nil?
end

#which_editorObject

Raises:



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/cocoapods/command/spec.rb', line 277

def which_editor
  editor = ENV['EDITOR']
  # If an editor wasn't set, try to pick a sane default
  return editor unless editor.nil?

  # Find Sublime Text 2
  return 'subl' if which 'subl'
  # Find Textmate
  return 'mate' if which 'mate'
  # Find # BBEdit / TextWrangler
  return 'edit' if which 'edit'
  # Default to vim
  return 'vim' if which 'vim'

  raise Informative, "Failed to open editor. Set your 'EDITOR' environment variable."
end