Class: Pod::Command::Package

Inherits:
Pod::Command show all
Defined in:
lib/pod/command/package.rb,
lib/cocoapods-packager/pod_utils.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Package

Returns a new instance of Package.



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

def initialize(argv)
  @embedded = argv.flag?('embedded')
  @force = argv.flag?('force')
  @library = argv.flag?('library')
  @mangle = argv.flag?('mangle', true)
  @name = argv.shift_argument
  @source = argv.shift_argument
  @spec_sources = argv.option('spec-sources', 'https://github.com/CocoaPods/Specs.git').split(',')

  subspecs = argv.option('subspecs')
  @subspecs = subspecs.split(',') unless subspecs.nil?

  @source_dir = Dir.pwd
  @spec = spec_with_path(@name)
  @spec = spec_with_name(@name) unless @spec
  super
end

Class Method Details

.optionsObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pod/command/package.rb', line 12

def self.options
  [
    ['--force',     'Overwrite existing files.'],
    ['--no-mangle', 'Do not mangle symbols of depedendant Pods.'],
    ['--embedded',  'Generate embedded frameworks.'],
    ['--library',   'Generate static libraries.'],
    ['--subspecs',  'Only include the given subspecs'],
    ['--spec-sources=private,https://github.com/CocoaPods/Specs.git', 'The sources to pull dependant ' \
      'pods from (defaults to https://github.com/CocoaPods/Specs.git)'],
  ]
end

Instance Method Details

#binary_only?(spec) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cocoapods-packager/pod_utils.rb', line 56

def binary_only?(spec)
  deps = spec.dependencies.map { |dep| spec_with_name(dep.name) }

  [spec, *deps].each do |specification|
    %w(vendored_frameworks vendored_libraries).each do |attrib|
      if specification.attributes_hash[attrib]
        return true
      end
    end
  end

  false
end

#build_in_sandbox(platform) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pod/command/package.rb', line 63

def build_in_sandbox(platform)
  config.sandbox_root       = 'Pods'
  config.integrate_targets  = false
  config.skip_repo_update   = true

  sandbox = install_pod(platform.name)

  begin
    perform_build(platform, sandbox)

  ensure # in case the build fails; see Builder#xcodebuild.
    Pathname.new(config.sandbox_root).rmtree
    FileUtils.rm_f('Podfile.lock')
  end
end

#build_packageObject



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/pod/command/package.rb', line 79

def build_package
  builder = SpecBuilder.new(@spec, @source, @embedded)
  newspec = builder.

  @spec.available_platforms.each do |platform|
    build_in_sandbox(platform)

    newspec += builder.spec_platform(platform)
  end

  newspec += builder.spec_close
  File.open(@spec.name + '.podspec', 'w') { |file| file.write(newspec) }
end

#create_target_directoryObject



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pod/command/package.rb', line 93

def create_target_directory
  target_dir = "#{@source_dir}/#{@spec.name}-#{@spec.version}"
  if File.exist? target_dir
    if @force
      Pathname.new(target_dir).rmtree
    else
      UI.puts "Target directory '#{target_dir}' already exists."
      return nil
    end
  end
  target_dir
end

#create_working_directoryObject



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/pod/command/package.rb', line 106

def create_working_directory
  target_dir = create_target_directory
  return if target_dir.nil?

  work_dir = Dir.tmpdir + '/cocoapods-' + Array.new(8) { rand(36).to_s(36) }.join
  Pathname.new(work_dir).mkdir
  `cp #{@path} #{work_dir}`
  Dir.chdir(work_dir)

  [target_dir, work_dir]
end

#install_pod(platform_name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cocoapods-packager/pod_utils.rb', line 6

def install_pod(platform_name)
  podfile = podfile_from_spec(
    File.basename(@path),
    @spec.name,
    platform_name,
    @spec.deployment_target(platform_name),
    @subspecs,
    @spec_sources,
  )

  sandbox = Sandbox.new(config.sandbox_root)
  installer = Installer.new(sandbox, podfile)
  installer.install!

  unless installer.nil?
    installer.pods_project.targets.each do |target|
      target.build_configurations.each do |config|
        config.build_settings['CLANG_MODULES_AUTOLINK'] = 'NO'
      end
    end
    installer.pods_project.save
  end

  sandbox
end

#perform_build(platform, sandbox) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/pod/command/package.rb', line 118

def perform_build(platform, sandbox)
  builder = Pod::Builder.new(
    @source_dir,
    config.sandbox_root,
    sandbox.public_headers.root,
    @spec,
    @embedded,
    @mangle)

  builder.build(platform, @library)

  return unless @embedded
  builder.link_embedded_resources
end

#podfile_from_spec(path, spec_name, platform_name, deployment_target, subspecs, sources) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cocoapods-packager/pod_utils.rb', line 32

def podfile_from_spec(path, spec_name, platform_name, deployment_target, subspecs, sources)
  Pod::Podfile.new do
    sources.each { |s| source s }
    platform(platform_name, deployment_target)
    if path
      if subspecs
        subspecs.each do |subspec|
          pod spec_name + '/' + subspec, :podspec => path
        end
      else
        pod spec_name, :podspec => path
      end
    else
      if subspecs
        subspecs.each do |subspec|
          pod spec_name + '/' + subspec, :path => '.'
        end
      else
        pod spec_name, :path => '.'
      end
    end
  end
end

#runObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pod/command/package.rb', line 48

def run
  if @path.nil? || @spec.nil?
    help! 'Unable to find a podspec with path or name.'
    return
  end

  target_dir, work_dir = create_working_directory
  return if target_dir.nil?
  build_package
  `mv "#{work_dir}" "#{target_dir}"`
  Dir.chdir(@source_dir)
end

#spec_with_name(name) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/cocoapods-packager/pod_utils.rb', line 70

def spec_with_name(name)
  return if name.nil?

  set = SourcesManager.search(Dependency.new(name))
  return nil if set.nil?

  set.specification.root
end

#spec_with_path(path) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cocoapods-packager/pod_utils.rb', line 79

def spec_with_path(path)
  return if path.nil? || !Pathname.new(path).exist?

  @path = path

  if Pathname.new(path).directory?
    help! path + ': is a directory.'
    return
  end

  unless ['.podspec', '.json'].include? Pathname.new(path).extname
    help! path + ': is not a podspec.'
    return
  end

  Specification.from_file(path)
end

#validate!Object



42
43
44
45
46
# File 'lib/pod/command/package.rb', line 42

def validate!
  super
  help! 'A podspec name or path is required.' unless @spec
  help! 'podspec has binary-only depedencies, mangling not possible.' if binary_only? @spec
end