Commit 7f38d3f7 authored by Kyle Fuller's avatar Kyle Fuller

[Spec Command] Refactor into multiple files #1986

parent f9df489d
This diff is collapsed.
module Pod
class Command
class Spec < Command
class Cat < Spec
self.summary = 'Prints a spec file.'
self.description = <<-DESC
Prints the content of the podspec(s) whose name matches `QUERY` to standard output.
DESC
self.arguments = [
CLAide::Argument.new('QUERY', false),
]
def self.options
[
['--regex', 'Interpret the `QUERY` as a regular expression'],
['--show-all', 'Pick from all versions of the given podspec']
].concat(super)
end
def initialize(argv)
@use_regex = argv.flag?('regex')
@show_all = argv.flag?('show-all')
@query = argv.shift_argument
@query = @query.gsub('.podspec', '') unless @query.nil?
super
end
def validate!
super
help! 'A podspec name is required.' unless @query
validate_regex!(@query) if @use_regex
end
def run
query = @use_regex ? @query : Regexp.escape(@query)
filepath = if @show_all
specs = get_path_of_spec(query, @show_all).split(/\n/)
index = choose_from_array(specs, "Which spec would you like to print [1-#{ specs.count }]? ")
specs[index]
else
get_path_of_spec(query)
end
UI.puts File.read(filepath)
end
end
end
end
end
This diff is collapsed.
module Pod
class Command
class Spec < Command
class Edit < Spec
self.summary = 'Edit a spec file.'
self.description = <<-DESC
Opens the podspec matching `QUERY` to be edited.
DESC
self.arguments = [
CLAide::Argument.new('QUERY', false),
]
def self.options
[
['--regex', 'Interpret the `QUERY` as a regular expression'],
['--show-all', 'Pick from all versions of the given podspec']
].concat(super)
end
def initialize(argv)
@use_regex = argv.flag?('regex')
@show_all = argv.flag?('show-all')
@query = argv.shift_argument
@query = @query.gsub('.podspec', '') unless @query.nil?
super
end
def validate!
super
help! 'A podspec name is required.' unless @query
validate_regex!(@query) if @use_regex
end
def run
query = @use_regex ? @query : Regexp.escape(@query)
if @show_all
specs = get_path_of_spec(query, @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(query)
end
exec_editor(filepath.to_s) if File.exist? filepath
raise Informative, "#{ filepath } doesn't exist."
end
# Thank you homebrew
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
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
def exec_editor(*args)
return if args.to_s.empty?
safe_exec(which_editor, *args)
end
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
end
end
end
end
module Pod
class Command
class Spec < Command
class Lint < Spec
self.summary = 'Validates a spec file.'
self.description = <<-DESC
Validates `NAME.podspec`. If a `DIRECTORY` is provided, it validates
the podspec files found, including subfolders. In case
the argument is omitted, it defaults to the current working dir.
DESC
self.arguments = [
CLAide::Argument.new(%w(NAME.podspec DIRECTORY http://PATH/NAME.podspec), false, true),
]
def self.options
[['--quick', 'Lint skips checks that would require to download and build the spec'],
['--allow-warnings', 'Lint validates even if warnings are present'],
['--subspec=NAME', 'Lint validates only the given subspec'],
['--no-subspecs', 'Lint skips validation of subspecs'],
['--no-clean', 'Lint leaves the build directory intact for inspection'],
['--use-frameworks', 'Lint uses frameworks to install the spec'],
['--sources=https://github.com/artsy/Specs', 'The sources from which to pull dependant pods ' \
'(defaults to https://github.com/CocoaPods/Specs.git). '\
'Multiple sources must be comma-delimited.']].concat(super)
end
def initialize(argv)
@quick = argv.flag?('quick')
@allow_warnings = argv.flag?('allow-warnings')
@clean = argv.flag?('clean', true)
@subspecs = argv.flag?('subspecs', true)
@only_subspec = argv.option('subspec')
@use_frameworks = argv.flag?('use-frameworks')
@source_urls = argv.option('sources', 'https://github.com/CocoaPods/Specs.git').split(',')
@podspecs_paths = argv.arguments!
super
end
def run
UI.puts
invalid_count = 0
podspecs_to_lint.each do |podspec|
validator = Validator.new(podspec, @source_urls)
validator.quick = @quick
validator.no_clean = !@clean
validator.allow_warnings = @allow_warnings
validator.no_subspecs = !@subspecs || @only_subspec
validator.only_subspec = @only_subspec
validator.use_frameworks = @use_frameworks
validator.validate
invalid_count += 1 unless validator.validated?
unless @clean
UI.puts "Pods project available at `#{validator.validation_dir}/Pods/Pods.xcodeproj` for inspection."
UI.puts
end
end
count = podspecs_to_lint.count
UI.puts "Analyzed #{count} #{'podspec'.pluralize(count)}.\n\n"
if invalid_count == 0
lint_passed_message = count == 1 ? "#{podspecs_to_lint.first.basename} passed validation." : 'All the specs passed validation.'
UI.puts lint_passed_message.green << "\n\n"
else
raise Informative, count == 1 ? 'The spec did not pass validation.' : "#{invalid_count} out of #{count} specs failed validation."
end
podspecs_tmp_dir.rmtree if podspecs_tmp_dir.exist?
end
private
def podspecs_to_lint
@podspecs_to_lint ||= begin
files = []
@podspecs_paths << '.' if @podspecs_paths.empty?
@podspecs_paths.each do |path|
if path =~ /https?:\/\//
require 'open-uri'
output_path = podspecs_tmp_dir + File.basename(path)
output_path.dirname.mkpath
open(path) do |io|
output_path.open('w') { |f| f << io.read }
end
files << output_path
elsif (pathname = Pathname.new(path)).directory?
files += Pathname.glob(pathname + '**/*.podspec{.json,}')
raise Informative, 'No specs found in the current directory.' if files.empty?
else
files << (pathname = Pathname.new(path))
raise Informative, "Unable to find a spec named `#{path}'." unless pathname.exist? && path.include?('.podspec')
end
end
files
end
end
def podspecs_tmp_dir
Pathname(File.join(Pathname.new('/tmp').realpath, '/CocoaPods/Lint_podspec'))
end
end
end
end
end
module Pod
class Command
class Spec < Command
class Which < Spec
self.summary = 'Prints the path of the given spec.'
self.description = <<-DESC
Prints the path of the .podspec file(s) whose name matches `QUERY`
DESC
self.arguments = [
CLAide::Argument.new('QUERY', false),
]
def self.options
[
['--regex', 'Interpret the `QUERY` as a regular expression'],
['--show-all', 'Print all versions of the given podspec'],
].concat(super)
end
def initialize(argv)
@use_regex = argv.flag?('regex')
@show_all = argv.flag?('show-all')
@query = argv.shift_argument
@query = @query.gsub('.podspec', '') unless @query.nil?
super
end
def validate!
super
help! 'A podspec name is required.' unless @query
validate_regex!(@query) if @use_regex
end
def run
query = @use_regex ? @query : Regexp.escape(@query)
UI.puts get_path_of_spec(query, @show_all)
end
end
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment