Class: Pod::Command::Try

Inherits:
Pod::Command show all
Defined in:
lib/pod/command/try.rb

Overview

The pod try command.

Constant Summary collapse

TRY_TMP_DIR =

Returns:

  • (Pathname)
Pathname.new('/tmp/CocoaPods/Try')

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Try

Returns a new instance of Try.



15
16
17
18
# File 'lib/pod/command/try.rb', line 15

def initialize(argv)
  @name = argv.shift_argument
  super
end

Instance Method Details

#choose_from_array(array, message) ⇒ Fixnum

TODO:

This method was duplicated from the spec subcommands

Presents a choice among the elements of an array to the user.

Parameters:

  • array (Array<#to_s>)

    The list of the elements among which the user should make his choice.

  • message (String)

    The message to display to the user.

Returns:

  • (Fixnum)

    The index of the chosen array item.



193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/pod/command/try.rb', line 193

def choose_from_array(array, message)
  array.each_with_index do |item, index|
    UI.puts "#{ index + 1 }: #{ item }"
  end
  UI.puts "#{message} [1-#{array.count}]?"
  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

#install_pod(spec, dir) ⇒ Pathname

Installs the specification in the given directory.

Parameters:

  • The (Specification)

    specification of the Pod.

  • The (Pathname)

    directory of the sandbox where to install the Pod.

Returns:

  • (Pathname)

    The path where the Pod was installed



74
75
76
77
78
79
80
81
# File 'lib/pod/command/try.rb', line 74

def install_pod(spec, dir)
  sandbox = Sandbox.new(dir)
  specs = { :ios => spec, :osx => spec }
  installer = Installer::PodSourceInstaller.new(sandbox, specs)
  installer.aggressive_cache = config.aggressive_cache?
  installer.install!
  TRY_TMP_DIR + 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.

Returns:

  • (String)

    proj The path of the project.

  • (String)

    The path of the file to open, in other words the workspace of the installation or the given project.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/pod/command/try.rb', line 132

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.expand_path(podfile.workspace_path)
      else
        proj.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.

Returns:

  • (String)

    path The path of the project.

  • (void)


175
176
177
178
# File 'lib/pod/command/try.rb', line 175

def open_project(path)
  UI.puts "Opening '#{path}'"
  `open "#{path}"`
end

#perform_cocoapods_installationvoid

This method returns an undefined value.

Returns Performs a CocoaPods installation in the working directory.



209
210
211
# File 'lib/pod/command/try.rb', line 209

def perform_cocoapods_installation
  UI.puts `pod install`
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.

Parameters:

  • dir (#to_s)

    The path where to look for projects.

Returns:

  • (String)

    The path of the project.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/pod/command/try.rb', line 95

def pick_demo_project(dir)
  glob_match = Dir.glob("#{dir}/**/*.xc{odeproj,workspace}")
  glob_match = glob_match.reject do |p|
    p.include?('Pods.xcodeproj') || \
    p.end_with?('.xcodeproj/project.xcworkspace') || \
    (p.end_with?('.xcodeproj') && glob_match.include?(p.chomp(File.extname(p.to_s)) + '.xcworkspace'))
  end

  if glob_match.count == 0
    raise Informative, "Unable to find any project in the source files" \
      "of the Pod: `#{dir}`"
  elsif glob_match.count == 1
    glob_match.first
  elsif (workspaces = glob_match.grep(/(demo|example).*\.xcworkspace$/i)).count == 1
    workspaces.first
  elsif (projects = glob_match.grep(/demo|example/i)).count == 1
    projects.first
  else
    message = "Which project would you like to open"
    selection_array = glob_match.map do |p|
      Pathname.new(p).relative_path_from(dir).to_s
    end
    index = choose_from_array(selection_array, message)
    glob_match[index]
  end
end

#runObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pod/command/try.rb', line 25

def run
  spec = spec_with_name(@name)
  update_specs_repos
  UI.title "Trying #{spec.name}" do
    pod_dir = install_pod(spec, TRY_TMP_DIR)
    proj = 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

#spec_with_name(name) ⇒ Specification

Returns the specification of the last version of the Pod with the given name.

Parameters:

  • name (String)

    The name of the pod.

Returns:

  • (Specification)

    The specification.



57
58
59
60
61
62
63
64
# File 'lib/pod/command/try.rb', line 57

def spec_with_name(name)
  set = SourcesManager.search(Dependency.new(name))
  if set
    set.specification.root
  else
    raise Informative, "Unable to find a specification for `#{name}`"
  end
end

#update_specs_reposvoid

This method returns an undefined value.

Returns Updates the specs repo unless disabled by the config.



160
161
162
163
164
165
166
# File 'lib/pod/command/try.rb', line 160

def update_specs_repos
  unless config.skip_repo_update?
    UI.section 'Updating spec repositories' do
      SourcesManager.update
    end
  end
end

#validate!Object



20
21
22
23
# File 'lib/pod/command/try.rb', line 20

def validate!
  super
  help! "A Pod name is required." unless @name
end