Class: Pod::Command::Init

Inherits:
Pod::Command show all
Defined in:
lib/cocoapods/command/init.rb

Instance Method Summary collapse

Methods inherited from Pod::Command

#ensure_master_spec_repo_exists!, ensure_not_root_or_allowed!, git_version, #installer_for_config, options, report_error, run, #verify_lockfile_exists!, verify_minimum_git_version!, #verify_podfile_exists!, verify_xcode_license_approved!

Methods included from Pod::Config::Mixin

#config

Constructor Details

#initialize(argv) ⇒ Init

Returns a new instance of Init.



23
24
25
26
27
28
# File 'lib/cocoapods/command/init.rb', line 23

def initialize(argv)
  @podfile_path = Pathname.pwd + 'Podfile'
  @project_path = argv.shift_argument
  @project_paths = Pathname.pwd.children.select { |pn| pn.extname == '.xcodeproj' }
  super
end

Instance Method Details

#podfile_template(project) ⇒ String (private)

Returns the text of the Podfile for the provided project.

Parameters:

  • project (Xcodeproj::Project)

    The Xcode project to generate a podfile for.

Returns:

  • (String)

    the text of the Podfile for the provided project



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cocoapods/command/init.rb', line 55

def podfile_template(project)
  podfile = ''
  podfile << "project '#{@project_path}'\n\n" if @project_path
  podfile << <<-PLATFORM.strip_heredoc
    # Uncomment the next line to define a global platform for your project
    # platform :ios, '9.0'
  PLATFORM

  # Split out the targets into app and test targets
  test_targets, app_targets = project.native_targets.sort_by { |t| t.name.downcase }.partition(&:test_target_type?)

  app_targets.each do |app_target|
    test_targets_for_app = test_targets.select do |target|
      target.name.downcase.start_with?(app_target.name.downcase)
    end
    podfile << target_module(app_target, test_targets_for_app)
  end

  podfile
end

#runObject



44
45
46
# File 'lib/cocoapods/command/init.rb', line 44

def run
  @podfile_path.open('w') { |f| f << podfile_template(@xcode_project) }
end

#target_module(host, tests) ⇒ String (private)

Returns the text for the target module.

Parameters:

  • host (PBXNativeTarget)

    the native host target for the module.

  • tests (Array<PBXNativeTarget>)

    the native test targets for the module.

Returns:

  • (String)

    the text for the target module.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cocoapods/command/init.rb', line 82

def target_module(host, tests)
  target_module = "\ntarget '#{host.name.gsub(/'/, "\\\\\'")}' do\n"

  target_module << <<-RUBY
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

   RUBY

  target_module << template_contents(config.default_podfile_path, '  ', "Pods for #{host.name}\n")

  tests.each do |test|
    target_module << "\n  target '#{test.name.gsub(/'/, "\\\\\'")}' do\n"
    unless Pod::AggregateTarget::EMBED_FRAMEWORKS_IN_HOST_TARGET_TYPES.include?(host.symbol_type) || test.symbol_type == :ui_test_bundle
      target_module << "    inherit! :search_paths\n"
    end
    target_module << template_contents(config.default_test_podfile_path, '    ', 'Pods for testing')
    target_module << "\n  end\n"
  end

  target_module << "\nend\n"
end

#template_contents(path, prefix, fallback) ⇒ String (private)

Returns the template contents for the given path.

Parameters:

  • path (Pathname)

    the path of the template to load contents from.

  • prefix (String)

    the prefix to use for each line.

  • fallback (String)

    the fallback contents to use if the path for the template does not exist.

Returns:

  • (String)

    the template contents for the given path.



113
114
115
116
117
118
119
# File 'lib/cocoapods/command/init.rb', line 113

def template_contents(path, prefix, fallback)
  if path.exist?
    path.read.chomp.lines.map { |line| "#{prefix}#{line}" }.join("\n")
  else
    "#{prefix}# #{fallback}"
  end
end

#validate!Object

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cocoapods/command/init.rb', line 30

def validate!
  super
  raise Informative, 'Existing Podfile found in directory' unless config.podfile_path_in_dir(Pathname.pwd).nil?
  if @project_path
    help! "Xcode project at #{@project_path} does not exist" unless File.exist? @project_path
    project_path = @project_path
  else
    raise Informative, 'No Xcode project found, please specify one' unless @project_paths.length > 0
    raise Informative, 'Multiple Xcode projects found, please specify one' unless @project_paths.length == 1
    project_path = @project_paths.first
  end
  @xcode_project = Xcodeproj::Project.open(project_path)
end