Commit f1f43240 authored by Olivier Halligon's avatar Olivier Halligon

Also added regex support to `pod spec which/cat/edit` for consistency.

parent f2c15270
......@@ -34,10 +34,12 @@ To install release candidates run `[sudo] gem install cocoapods --pre`
##### Enhancements
* `pod search` now uses plain-text search by default instead of a regex,
allowing to run e.g. `pod search UIView+` to search for pods containing
`UIView+` in their name. You can still use a regular expression with the
new `--regex` flag of the `pod search` command.
* `pod search`, `pod spec which`, `pod spec cat` and `pod spec edit`
now use plain text search by default instead of a regex. Especially
`pod search UIView+UI` now searches for pods containing exactly `UIView+UI`
in their name, not trying to interpret the `+` as a regular expression.
_Note: You can still use a regular expression with the new `--regex` flag that has
been added to these commands, e.g. `pod search --regex (NS|UI)Color`._
[Olivier Halligon](https://github.com/AliSoftware)
[Core#188](https://github.com/CocoaPods/Core/issues/188)
......
......@@ -152,33 +152,37 @@ module Pod
self.summary = 'Prints the path of the given spec.'
self.description = <<-DESC
Prints the path of 'NAME.podspec'
Prints the path of the .podspec file(s) whose name is matching `QUERY`
DESC
self.arguments = [
CLAide::Argument.new('NAME', false),
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')
@spec = argv.shift_argument
@spec = @spec.gsub('.podspec', '') unless @spec.nil?
@query = argv.shift_argument
@query = @query.gsub('.podspec', '') unless @query.nil?
super
end
def validate!
super
help! 'A podspec name is required.' unless @spec
help! 'A podspec name is required.' unless @query
validate_regex!(@query) if @use_regex
end
def run
UI.puts get_path_of_spec(@spec, @show_all)
query = @use_regex ? @query : Regexp.escape(@query)
UI.puts get_path_of_spec(query, @show_all)
end
end
......@@ -188,36 +192,42 @@ module Pod
self.summary = 'Prints a spec file.'
self.description = <<-DESC
Prints 'NAME.podspec' to standard output.
Prints the content of the podspec(s) whose name matches `QUERY` to standard output.
DESC
self.arguments = [
CLAide::Argument.new('NAME', false),
CLAide::Argument.new('QUERY', false),
]
def self.options
[['--show-all', 'Pick from all versions of the given podspec']].concat(super)
[
['--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')
@spec = argv.shift_argument
@spec = @spec.gsub('.podspec', '') unless @spec.nil?
@query = argv.shift_argument
@query = @query.gsub('.podspec', '') unless @query.nil?
super
end
def validate!
super
help! 'A podspec name is required.' unless @spec
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(@spec, @show_all).split(/\n/)
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(@spec)
get_path_of_spec(query)
end
UI.puts File.read(filepath)
......@@ -230,38 +240,43 @@ module Pod
self.summary = 'Edit a spec file.'
self.description = <<-DESC
Opens 'NAME.podspec' to be edited.
Opens the podspec matching `QUERY` to be edited.
DESC
self.arguments = [
CLAide::Argument.new('NAME', false),
CLAide::Argument.new('QUERY', false),
]
def self.options
[['--show-all', 'Pick which spec to edit from all available' \
'versions of the given podspec']].concat(super)
[
['--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')
@spec = argv.shift_argument
@spec = @spec.gsub('.podspec', '') unless @spec.nil?
@query = argv.shift_argument
@query = @query.gsub('.podspec', '') unless @query.nil?
super
end
def validate!
super
help! 'A podspec name is required.' unless @spec
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(@spec, @show_all).split(/\n/)
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(@spec)
filepath = get_path_of_spec(query)
end
exec_editor(filepath.to_s) if File.exist? filepath
......@@ -310,6 +325,18 @@ module Pod
private
# @param [String] query the regular expression string to validate
#
# @raise if the query is not a valid regular expression
#
def validate_regex!(query)
begin
/#{query}/
rescue RegexpError
help! 'A valid regular expression is required.'
end
end
# @return [Fixnum] the index of the chosen array item
#
def choose_from_array(array, message)
......@@ -337,7 +364,7 @@ module Pod
# @return [Pathname] the absolute path or paths of the given podspec
#
def get_path_of_spec(spec, show_all = false)
sets = SourcesManager.search_by_name(Regexp.escape(spec))
sets = SourcesManager.search_by_name(spec)
if sets.count == 1
set = sets.first
......
......@@ -226,6 +226,53 @@ module Pod
end
end
def describe_regex_support(command, raise_class = nil)
describe 'RegEx support' do
before do
@test_source = Source.new(fixture('spec-repos/test_repo'))
Source::Aggregate.any_instance.stubs(:sources).returns([@test_source])
SourcesManager.updated_search_index = nil
yield if block_given?
end
it 'raise when using an invalid regex' do
lambda { run_command('spec', command, '--regex', '+') }.should.raise CLAide::Help
end
it 'does not try to validate the query as a regex with plain-text mode' do
l = lambda { run_command('spec', command, '+') }
if raise_class
l.should.raise raise_class
else
l.should.not.raise CLAide::Help
end
end
it 'uses regex search when asked for regex mode' do
l = lambda { run_command('spec', command, '--regex', 'Ba(na)+Lib') }
if raise_class
l.should.raise raise_class
else
l.should.not.raise
end
UI.output.should.include? 'BananaLib'
UI.output.should.not.include? 'Pod+With+Plus+Signs'
UI.output.should.not.include? 'JSONKit'
end
it 'uses plain-text search when not asked for regex mode' do
l = lambda { run_command('spec', command, 'Pod+With+Plus+Signs') }
if raise_class
l.should.raise raise_class
else
l.should.not.raise
end
UI.output.should.include? 'Pod+With+Plus+Signs'
UI.output.should.not.include? 'BananaLib'
end
end
end
describe Command::Spec::Which do
it_should_check_for_existence('which')
it_should_check_for_ambiguity('which')
......@@ -235,6 +282,8 @@ module Pod
text = 'AFNetworking.podspec'
UI.output.should.include text.gsub(/\n/, '')
end
describe_regex_support('which')
end
#-------------------------------------------------------------------------#
......@@ -242,7 +291,7 @@ module Pod
describe Command::Spec::Cat do
it_should_check_for_existence('cat')
it_should_check_for_ambiguity('cat')
it 'cats the given podspec' do
lambda { command('spec', 'cat', 'AFNetworking').run }.should.not.raise
UI.output.should.include fixture('spec-repos/master/Specs/AFNetworking/2.4.1/AFNetworking.podspec.json').read
......@@ -253,6 +302,8 @@ module Pod
run_command('spec', 'cat', '--show-all', 'AFNetworking')
UI.output.should.include fixture('spec-repos/master/Specs/AFNetworking/2.4.1/AFNetworking.podspec.json').read
end
describe_regex_support('cat')
end
#-------------------------------------------------------------------------#
......@@ -268,7 +319,7 @@ module Pod
it_should_check_for_existence('edit')
it_should_check_for_ambiguity('edit')
it 'would execute the editor specified in ENV with the given podspec' do
ENV['EDITOR'] = 'podspeceditor'
lambda { command('spec', 'edit', 'AFNetworking').run }.should.raise SystemExit
......@@ -295,6 +346,8 @@ module Pod
lambda { command('spec', 'edit', 'AFNetworking').run }.should.raise Informative
File.unstub(:exists?)
end
describe_regex_support('edit', SystemExit) { ENV['EDITOR'] = 'podspeceditor' }
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