Class: Pod::Command::Try
- Inherits:
-
Pod::Command
- Object
- Pod::Command
- Pod::Command::Try
- Includes:
- RepoUpdate
- Defined in:
- lib/pod/command/try.rb
Overview
The pod try command.
Constant Summary collapse
- TRY_TMP_DIR =
Pathname.new(Dir.tmpdir) + 'CocoaPods/Try'
Class Method Summary collapse
Instance Method Summary collapse
-
#cocoapods_version ⇒ Pod::Version
The version of CocoaPods currently running.
-
#git_url?(name) ⇒ Bool
Wether the given string is the name of a Pod or an URL for a Git repo.
-
#initialize(argv) ⇒ Try
constructor
A new instance of Try.
-
#install_pod(spec, sandbox) ⇒ Pathname
Installs the specification in the given directory.
-
#install_podfile(proj) ⇒ String
Performs a CocoaPods installation for the given project if Podfile is found.
-
#open_project(path) ⇒ String, void
Opens the project at the given path.
-
#perform_cocoapods_installation ⇒ void
Performs a CocoaPods installation in the working directory.
-
#pick_demo_project(dir) ⇒ String
Picks a project or workspace suitable for the demo purposes in the given directory.
-
#projects_in_dir(dir) ⇒ Array<String>
The list of the workspaces and projects in a given directory excluding The Pods project and the projects that have a sister workspace.
- #run ⇒ Object
-
#setup_spec_in_sandbox(sandbox) ⇒ Object
Puts the spec’s data in the sandbox.
-
#spec_with_name(name) ⇒ Specification
Returns the specification of the last version of the Pod with the given name.
-
#spec_with_url(url, spec_name = nil) ⇒ Specification
Returns the specification found in the given Git repository URL by downloading the repository.
-
#update_specs_repos ⇒ void
Updates the specs repo unless disabled by the config.
- #validate! ⇒ Object
Constructor Details
#initialize(argv) ⇒ Try
Returns a new instance of Try.
32 33 34 35 36 |
# File 'lib/pod/command/try.rb', line 32 def initialize(argv) @name = argv.shift_argument @podspec_name = argv.option('podspec_name') super end |
Class Method Details
.options ⇒ Object
26 27 28 29 30 |
# File 'lib/pod/command/try.rb', line 26 def self. [ ['--podspec_name=[name]', 'The name of the podspec file within the Git Repository'], ].concat(super) end |
Instance Method Details
#cocoapods_version ⇒ Pod::Version
Returns the version of CocoaPods currently running.
272 273 274 |
# File 'lib/pod/command/try.rb', line 272 def cocoapods_version Pod::Version.new(Pod::VERSION) end |
#git_url?(name) ⇒ Bool
Returns Wether the given string is the name of a Pod or an URL for a Git repo.
251 252 253 254 |
# File 'lib/pod/command/try.rb', line 251 def git_url?(name) prefixes = ['https://', 'http://'] prefixes.any? { |prefix| name.start_with?(prefix) } end |
#install_pod(spec, sandbox) ⇒ Pathname
Installs the specification in the given directory.
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/pod/command/try.rb', line 136 def install_pod(spec, sandbox) specs = { :ios => spec, :osx => spec } if cocoapods_version >= Pod::Version.new('1.8.0') dummy_podfile = Podfile.new installer = Installer::PodSourceInstaller.new(sandbox, dummy_podfile, specs, :can_cache => false) else installer = Installer::PodSourceInstaller.new(sandbox, specs, :can_cache => false) end installer.install! sandbox.root + spec.name end |
#install_podfile(proj) ⇒ String
Performs a CocoaPods installation for the given project if Podfile is found. Shells out to avoid issues with the config of the process running the try command.
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/pod/command/try.rb', line 192 def install_podfile(proj) return unless proj dirname = Pathname.new(proj).dirname podfile_path = dirname + 'Podfile' if podfile_path.exist? Dir.chdir(dirname) do perform_cocoapods_installation podfile = Pod::Podfile.from_file(podfile_path) if podfile.workspace_path File.(podfile.workspace_path) else proj.to_s.chomp(File.extname(proj.to_s)) + '.xcworkspace' end end else proj end end |
#open_project(path) ⇒ String, void
Opens the project at the given path.
234 235 236 237 |
# File 'lib/pod/command/try.rb', line 234 def open_project(path) UI.puts "Opening '#{path}'" `open "#{path}"` end |
#perform_cocoapods_installation ⇒ void
This method returns an undefined value.
Returns Performs a CocoaPods installation in the working directory.
242 243 244 245 246 |
# File 'lib/pod/command/try.rb', line 242 def perform_cocoapods_installation UI.titled_section 'Performing CocoaPods Installation' do Command::Install.invoke end end |
#pick_demo_project(dir) ⇒ String
Picks a project or workspace suitable for the demo purposes in the given directory.
To decide the project simple heuristics are used according to the name, if no project is found this method raises and ‘Informative` otherwise if more than one project is found the choice is presented to the user.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/pod/command/try.rb', line 160 def pick_demo_project(dir) dir = Pathname.new(dir) projs = projects_in_dir(dir) if projs.count == 0 raise Informative, 'Unable to find any project in the source files' \ " of the Pod: `#{dir}`" elsif projs.count == 1 projs.first elsif (workspaces = projs.grep(/(demo|example|sample).*\.xcworkspace$/i)).count == 1 workspaces.first elsif (projects = projs.grep(/demo|example|sample/i)).count == 1 projects.first else = 'Which project would you like to open' selection_array = projs.map do |p| Pathname.new(p).relative_path_from(dir).to_s end index = UI.choose_from_array(selection_array, ) projs[index] end end |
#projects_in_dir(dir) ⇒ Array<String>
Returns The list of the workspaces and projects in a given directory excluding The Pods project and the projects that have a sister workspace.
260 261 262 263 264 265 266 267 268 |
# File 'lib/pod/command/try.rb', line 260 def projects_in_dir(dir) glob_match = Dir.glob("#{dir}/**/*.xc{odeproj,workspace}") glob_match = glob_match.reject do |p| next true if p.include?('Pods.xcodeproj') next true if p.end_with?('.xcodeproj/project.xcworkspace') sister_workspace = p.chomp(File.extname(p.to_s)) + '.xcworkspace' p.end_with?('.xcodeproj') && glob_match.include?(sister_workspace) end end |
#run ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/pod/command/try.rb', line 44 def run ensure_master_spec_repo_exists! sandbox = Sandbox.new(TRY_TMP_DIR) spec = setup_spec_in_sandbox(sandbox) UI.title "Trying #{spec.name}" do pod_dir = install_pod(spec, sandbox) settings = TrySettings.settings_from_folder(pod_dir) Dir.chdir(pod_dir) { settings.run_pre_install_commands(true) } proj = settings.project_path || pick_demo_project(pod_dir) file = install_podfile(proj) if file open_project(file) else UI.puts "Unable to locate a project for #{spec.name}" end end end |
#setup_spec_in_sandbox(sandbox) ⇒ Object
Puts the spec’s data in the sandbox
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/pod/command/try.rb', line 74 def setup_spec_in_sandbox(sandbox) if git_url?(@name) spec = spec_with_url(@name, @podspec_name) sandbox.store_pre_downloaded_pod(spec.name) else update_specs_repos spec = spec_with_name(@name) end spec end |
#spec_with_name(name) ⇒ Specification
Returns the specification of the last version of the Pod with the given name.
93 94 95 96 97 98 99 100 |
# File 'lib/pod/command/try.rb', line 93 def spec_with_name(name) set = config.sources_manager.search(Dependency.new(name)) if set set.specification.root else raise Informative, "Unable to find a specification for `#{name}`" end end |
#spec_with_url(url, spec_name = nil) ⇒ Specification
Returns the specification found in the given Git repository URL by downloading the repository.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/pod/command/try.rb', line 113 def spec_with_url(url, spec_name = nil) name = url.split('/').last name = name.chomp('.git') if name.end_with?('.git') name = spec_name unless spec_name.nil? target_dir = TRY_TMP_DIR + name target_dir.rmtree if target_dir.exist? downloader = Pod::Downloader.for_target(target_dir, :git => url) downloader.download spec_file = Pathname.glob(target_dir + "#{name}.podspec{,.json}").first Pod::Specification.from_file(spec_file) end |
#update_specs_repos ⇒ void
This method returns an undefined value.
Returns Updates the specs repo unless disabled by the config.
220 221 222 223 224 225 |
# File 'lib/pod/command/try.rb', line 220 def update_specs_repos return unless repo_update?(:default => true) UI.section 'Updating spec repositories' do config.sources_manager.update end end |
#validate! ⇒ Object
38 39 40 41 42 |
# File 'lib/pod/command/try.rb', line 38 def validate! super help! 'A Pod name or URL is required.' unless @name help! 'Podspec name can only be used with a Git URL' if @podspec_name && !git_url?(@name) end |