Class: Pod::XBuilder

Inherits:
Object
  • Object
show all
Includes:
Config::Mixin, PodUtil, XcodeProjHelper, XcodeXBuilder
Defined in:
lib/cocoapods-framework/xbuilder.rb,
lib/cocoapods-framework/xbuilder/xcode_xbuild.rb,
lib/cocoapods-framework/xbuilder/xcodeproj_helper.rb

Defined Under Namespace

Modules: XcodeProjHelper, XcodeXBuilder

Instance Method Summary collapse

Methods included from PodUtil

#build_static_sandbox, #generic_new_podspec_hash, #installation_root, #podfile_from_spec, #spec_with_name, #spec_with_path

Methods included from XcodeProjHelper

#modify_xcode_project_sdk_to_simullator, #to_native_platform

Methods included from XcodeXBuilder

#xcode_xbuild

Constructor Details

#initialize(installer, source_dir, sandbox_root, spec, configuration) ⇒ XBuilder

Returns a new instance of XBuilder.



9
10
11
12
13
14
15
16
17
18
# File 'lib/cocoapods-framework/xbuilder.rb', line 9

def initialize(installer, source_dir, sandbox_root, spec, configuration)
# def initialize(platform, installer, source_dir, sandbox_root, spec, config)
    # @platform = platform
  @installer = installer
  @source_dir = source_dir
  @sandbox_root = sandbox_root
  @spec = spec
  @configuration = configuration
  @outputs = {}
end

Instance Method Details

#buildObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cocoapods-framework/xbuilder.rb', line 20

def build
  UI.puts("Building framework #{@spec} with configuration #{@configuration}")
  UI.puts "Work dir is :#{@sandbox_root}"
  defines = "GCC_PREPROCESSOR_DEFINITIONS='$(inherited) PodsDummy_Pods_#{@spec.name}=PodsDummy_PodPackage_#{@spec.name}'"

  if @configuration == 'Debug'
    defines << 'GCC_GENERATE_DEBUGGING_SYMBOLS=YES ONLY_ACTIVE_ARCH=NO'
  else
    defines << "GCC_GENERATE_DEBUGGING_SYMBOLS=NO"
  end

  build_all_device defines

  collect_xc_frameworks

  collect_bundles
end

#build_all_device(defines) ⇒ Object



76
77
78
79
80
# File 'lib/cocoapods-framework/xbuilder.rb', line 76

def build_all_device defines
  # build general first because simulator will exchange SDKROOT to simulat sdk
  build_general_device defines
  build_simulator_device defines
end

#build_general_device(defines) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/cocoapods-framework/xbuilder.rb', line 82

def build_general_device defines
  UI.puts("--- Building framework #{@spec} with general device")
  xcode_xbuild(
    defines,
    @configuration,
    @sandbox_root
  )
end

#build_simulator_device(defines) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/cocoapods-framework/xbuilder.rb', line 91

def build_simulator_device defines
  UI.puts("--- Building framework #{@spec} with simulator device")
  modify_xcode_project_sdk_to_simullator "#{@sandbox_root}/Pods.xcodeproj"
  xcode_xbuild(
    defines,
    @configuration,
    @sandbox_root
  )
end

#collect_bundlesObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cocoapods-framework/xbuilder.rb', line 44

def collect_bundles
  ["iphoneos","macOS","appletvos","watchos"].each do |plat|
    export_dir = "#{@sandbox_root}/export/*-#{plat}/**/#{@spec.name}.bundle/**"
    Pathname.glob(export_dir).each do |bundle|
      if bundle.to_s.include? "#{@spec.name}.bundle/Info.plist"
        return
      end
      @outputs[:bundle] = "#{@sandbox_root}/bundle"
      native_platform = to_native_platform plat
      path = Pathname.new "#{@sandbox_root}/bundle/#{native_platform}"
      if not path.exist?
        path.mkpath
      end
      FileUtils.cp_r(Dir["#{bundle}"],"#{path}")
    end
  end
end

#collect_xc_frameworksObject



38
39
40
41
42
# File 'lib/cocoapods-framework/xbuilder.rb', line 38

def collect_xc_frameworks
  export_dir = "#{@sandbox_root}/export/**/#{@spec.name}.framework"
  frameworks = Pathname.glob(export_dir)
  @outputs[:xcframework] = create_xc_framework_by_frameworks frameworks
end

#create_xc_framework_by_frameworks(frameworks) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cocoapods-framework/xbuilder.rb', line 62

def create_xc_framework_by_frameworks frameworks
  command = 'xcodebuild -create-xcframework '
  frameworks.each do |framework|
    command << "-framework #{framework} "
  end
  command << "-output #{@sandbox_root}/#{@spec.name}.xcframework 2>&1"
  output = `#{command}`.lines.to_a
  if $?.exitstatus != 0
    Pod::ErrorUtil.error_report command,output
    Process.exit -1
  end
  "#{@sandbox_root}/#{@spec.name}.xcframework"
end

#find_bundles(target_dir) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/cocoapods-framework/xbuilder.rb', line 127

def find_bundles target_dir
  bundle_root = "#{target_dir}/bundle/"
  pattern = "#{bundle_root}*"
  result = {}
  Pathname.glob(pattern).each do |bundle|
    bundle_relative_path = bundle.to_s.gsub(bundle_root, "")
    plat = bundle_relative_path
    result[plat] = {
      "#{@spec.name}" => "bundle/" + bundle_relative_path + "/*"
    }
  end
  result
end

#outputs(target_dir) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cocoapods-framework/xbuilder.rb', line 101

def outputs target_dir
  if not File.exist? target_dir
    Pathname.new(target_dir).mkdir
  end
  outputs_xcframework target_dir
  outputs_bundle target_dir
  new_spec_hash = generic_new_podspec_hash @spec
  new_spec_hash[:vendored_frameworks] = "#{@spec.name}.xcframework"
  find_bundles(target_dir).each do |plat, value| 
    if new_spec_hash[plat]
      new_spec_hash[plat]["resource_bundles"] = value
    else
      new_spec_hash[plat] = {
        "resource_bundles" => value
      }
    end
  end
  require 'json'
  spec_json = JSON.pretty_generate(new_spec_hash) << "\n"
  File.open("#{target_dir}/#{@spec.name}.podspec.json",'wb+') do |f|
    f.write(spec_json)
  end
  UI.puts "result export at :#{target_dir}"
  target_dir
end

#outputs_bundle(target_dir) ⇒ Object



150
151
152
153
154
# File 'lib/cocoapods-framework/xbuilder.rb', line 150

def outputs_bundle target_dir
  if @outputs[:bundle]
    FileUtils.cp_r(Dir[@outputs[:bundle]],target_dir)
  end
end

#outputs_xcframework(target_dir) ⇒ Object



141
142
143
144
145
146
147
148
# File 'lib/cocoapods-framework/xbuilder.rb', line 141

def outputs_xcframework target_dir
  command = "cp -rp #{@outputs[:xcframework]} #{target_dir} 2>&1"
  output = `#{command}`.lines.to_a
  if $?.exitstatus != 0
    Pod::ErrorUtil.error_report command,output
    Process.exit -1
  end
end