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.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pod/command/package.rb', line 18

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

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

Class Method Details

.optionsObject



9
10
11
12
13
14
15
16
# File 'lib/pod/command/package.rb', line 9

def self.options
  [
    ['--force',     'Overwrite existing files.'],
    ['--no-mangle', 'Do not mangle symbols of depedendant Pods.'],
    ['--embedded',  'Generate embedded frameworks.'],
    ['--library',  'Generate static libraries.']
  ]
end

Instance Method Details

#build_in_sandbox(platform) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pod/command/package.rb', line 56

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

  sandbox = install_pod(platform.name)

  perform_build(platform, sandbox)

  Pathname.new(config.sandbox_root).rmtree
  FileUtils.rm_f('Podfile.lock')
end

#build_packageObject



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/pod/command/package.rb', line 69

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



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/pod/command/package.rb', line 83

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



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

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
# 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))

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

  sandbox
end

#perform_build(platform, sandbox) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/pod/command/package.rb', line 108

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) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/cocoapods-packager/pod_utils.rb', line 20

def podfile_from_spec(path, spec_name, platform_name, deployment_target)
  Pod::Podfile.new do
    platform(platform_name, deployment_target)
    if path
      pod spec_name, :podspec => path
    else
      pod spec_name, :path => '.'
    end
  end
end

#runObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pod/command/package.rb', line 41

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

#source_files_available?(spec) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cocoapods-packager/pod_utils.rb', line 31

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

  [spec, *deps].each do |specification|
    source_files = specification.attributes_hash['source_files']
    if source_files.nil?
      return false
    end
  end

  true
end

#spec_with_name(name) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/cocoapods-packager/pod_utils.rb', line 44

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



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

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

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

  Specification.from_file(path)
end

#validate!Object



32
33
34
35
36
37
38
39
# File 'lib/pod/command/package.rb', line 32

def validate!
  super
  help! 'A podspec name or path is required.' unless @spec

  if !source_files_available? @spec
    help! 'podspec has binary-only depedencies, mangling not possible.'
  end
end