Commit ec45f295 authored by Keith Smiley's avatar Keith Smiley

Added pod spec which and edit, edited cat

parent aebd8c84
...@@ -115,65 +115,236 @@ module Pod ...@@ -115,65 +115,236 @@ module Pod
#-----------------------------------------------------------------------# #-----------------------------------------------------------------------#
class Which < Spec
self.summary = 'Prints the path of the given spec.'
self.description = <<-DESC
Prints the path of 'NAME.podspec'
DESC
self.arguments = '[ NAME ]'
def self.options
[["--show-all", "Print all versions of the given podspec"]].concat(super)
end
def initialize(argv)
@show_all = argv.flag?('show-all')
@spec = argv.shift_argument
@spec = @spec.gsub('.podspec', '')
super
end
def validate!
super
help! "A podspec name is required." unless @spec
end
def run
UI.puts get_path_of_spec(@spec, @show_all)
end
end
#-----------------------------------------------------------------------#
class Cat < Spec class Cat < Spec
self.summary = 'Prints a spec file' self.summary = 'Prints a spec file.'
self.description = <<-DESC self.description = <<-DESC
Prints `NAME.podspec` to standard output. Prints 'NAME.podspec' to standard output.
DESC DESC
self.arguments = '[ NAME.podspec ]' self.arguments = '[ NAME ]'
def self.options
[["--show-all", "Pick from all versions of the given podspec"]].concat(super)
end
def initialize(argv) def initialize(argv)
@name = argv.shift_argument @show_all = argv.flag?('show-all')
@spec = argv.shift_argument
@spec = @spec.gsub('.podspec', '')
super super
end end
def validate! def validate!
super super
help! "A pod name is required." unless @name help! "A podspec name is required." unless @spec
end end
def run def run
found_sets = SourcesManager.search_by_name(@name) filepath = if @show_all
raise Informative, "Unable to find a spec named `#{@name}'." if found_sets.count == 0 specs = get_path_of_spec(@spec, @show_all).split(/\n/)
unless found_sets.count == 1 index = choose_from_array(specs, "Which spec would you like to print [1-#{ specs.count }]? ")
names = found_sets.map(&:name) * ', ' specs[index]
raise Informative, "More that one fitting spec found:\n #{names}" else
get_path_of_spec(@spec)
end end
set = found_sets.first UI.puts File.open(filepath).read
spec = best_spec_from_set(set) end
file_name = spec.defined_in_file
UI.puts File.open(file_name).read
end end
#--------------------------------------# #-----------------------------------------------------------------------#
class Edit < Spec
self.summary = 'Edit a spec file.'
self.description = <<-DESC
Opens 'NAME.podspec' to be edited.
DESC
self.arguments = '[ NAME ]'
def self.options
[["--show-all", "Pick which spec to edit from all avaliable versions of the given podspec"]].concat(super)
end
def initialize(argv)
@show_all = argv.flag?('show-all')
@spec = argv.shift_argument
@spec = @spec.gsub('.podspec', '')
super
end
# @return [Specification] the highest know specification of the given def validate!
super
help! "A podspec name is required." unless @spec
end
def run
filepath = if @show_all
specs = get_path_of_spec(@spec, @show_all).split(/\n/)
index = choose_from_array(specs, "Which spec would you like to edit [1-#{ specs.count }]? ")
specs[index]
else
get_path_of_spec(@spec)
end
exec_editor(filepath.to_s) if File.exists? filepath
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 Textmate
return 'mate' if which 'mate'
# Find # BBEdit / TextWrangler
return 'edit' if which 'edit'
# Find Sublime Text 2
return 'subl' if which 'subl'
# Default to vim
return 'vim' if which 'vim'
raise Informative, "[!] Failed to open editor. Set your EDITOR environment variable.".red
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
#-----------------------------------------------------------------------#
# TODO some of the following methods can probably move to one of the subclasses.
private
# @return [Fixnum] the index of the chosen array item
#
def choose_from_array(array, message)
array.each_with_index do |item, index|
UI.puts "#{ index + 1 }: #{ item }"
end
print message
index = STDIN.gets.chomp.to_i - 1
if index < 0 || index > array.count
raise Informative, "#{ index + 1 } is invalid [1-#{ array.count }]"
else
index
end
end
# @return [Pathname] the absolute path or paths of the given podspec
#
def get_path_of_spec(spec, show_all = false)
pods = SourcesManager.search_by_name(spec)
unless pods.count == 1
names = pods.collect(&:name) * ', '
raise Informative, "More than one spec found for '#{ spec }':\n#{ names }"
end
unless show_all
best_spec, spec_source = spec_and_source_from_set(pods.first)
return pathname_from_spec(best_spec, spec_source)
end
return all_paths_from_set(pods.first)
end
# @return [Pathname] the absolute path of the given spec and source
#
def pathname_from_spec(spec, source)
Pathname.new("~/.cocoapods/#{ source }/#{ spec.name }/#{ spec.version }/#{ spec.name }.podspec").expand_path
end
# @return [String] of spec paths one on each line
#
def all_paths_from_set(set)
paths = ""
sources = set.sources
sources.each do |source|
versions = source.versions(set.name)
versions.each do |version|
spec = source.specification(set.name, version)
paths += "#{ pathname_from_spec(spec, source) }\n"
end
end
paths
end
# @return [Specification, Source] the highest known specification with it's source of the given
# set. # set.
# #
def best_spec_from_set(set) def spec_and_source_from_set(set)
sources = set.sources sources = set.sources
best_source = sources.first best_source = sources.first
best_version = best_source.versions(set.name).first best_version = best_source.versions(set.name).first
sources.each do |source| sources.each do |source|
if source.versions(set.name).first > best_version version = source.versions(set.name).first
if version > best_version
best_source = source best_source = source
best_version = version best_version = version
end end
end end
best_spec = best_source.specification(set.name, best_version) return best_source.specification(set.name, best_version), best_source
end
end end
#-----------------------------------------------------------------------#
# TODO some of the following methods can probably move to one of the subclasses.
private
def podspecs_to_lint def podspecs_to_lint
@podspecs_to_lint ||= begin @podspecs_to_lint ||= begin
...@@ -288,7 +459,7 @@ Pod::Spec.new do |s| ...@@ -288,7 +459,7 @@ Pod::Spec.new do |s|
s.homepage = "#{data[:homepage]}" s.homepage = "#{data[:homepage]}"
# Specify the license type. CocoaPods detects automatically the license file if it is named # Specify the license type. CocoaPods detects automatically the license file if it is named
# `LICEN{C,S}E*.*', however if the name is different, specify it. # 'LICENCE*.*' or 'LICENSE*.*', however if the name is different, specify it.
s.license = 'MIT (example)' s.license = 'MIT (example)'
# s.license = { :type => 'MIT (example)', :file => 'FILE_LICENSE' } # s.license = { :type => 'MIT (example)', :file => 'FILE_LICENSE' }
# #
......
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