Class: CBin::Build::Helper

Inherits:
Object
  • Object
show all
Includes:
Pod
Defined in:
lib/cocoapods-bb-bin/helpers/build_helper.rb

Constant Summary collapse

@@build_defines =

class var

""

Instance Method Summary collapse

Methods included from Pod

match_version?

Constructor Details

#initialize(spec, platform, framework_output, xcframework_output, xcframework_dylib_output, support_maccatalyst, spec_sources, zip, rootSpec, skip_archive = false, build_model = "Release") ⇒ Helper

Debug下还待完成



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cocoapods-bb-bin/helpers/build_helper.rb', line 18

def initialize(spec,
               platform,
               framework_output,
               xcframework_output,
               xcframework_dylib_output,
               support_maccatalyst,
               spec_sources,
               zip,
               rootSpec,
               skip_archive = false,
               build_model="Release")
  @spec = spec
  @platform = platform
  @build_model = build_model
  @rootSpec = rootSpec
  @isRootSpec = rootSpec.name == spec.name
  @skip_archive = skip_archive
  @framework_output = framework_output
  @xcframework_output = xcframework_output
  @xcframework_dylib_output = xcframework_dylib_output
  @support_maccatalyst = support_maccatalyst
  @spec_sources = spec_sources
  @zip = zip

  @framework_path

  UI.puts "build initialize...#{spec}"
end

Instance Method Details

#buildObject

build framework



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cocoapods-bb-bin/helpers/build_helper.rb', line 48

def build
  UI.section("Building static framework #{@spec}") do
    # 生成静态库支持xcframework
    has_xcframework = is_build_xcframework
    if has_xcframework == true
      UI.puts "build static xcframework"
      build_static_xcframework
      unless @skip_archive
        zip_static_xcframework
      end
    else
      UI.puts "build static framework"
      build_static_framework
      unless @skip_archive
        unless  CBin::Build::Utils.is_framework(@spec)
          UI.puts "build static library"
          build_static_library
          zip_static_library
        else
          zip_static_framework
        end
      end
    end
  end

end

#build_static_frameworkObject

build framework



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/cocoapods-bb-bin/helpers/build_helper.rb', line 107

def build_static_framework
  source_dir = Dir.pwd
  file_accessor = Sandbox::FileAccessor.new(Pathname.new('.').expand_path, @spec.consumer(@platform))
  Dir.chdir(workspace_directory) do
    builder = CBin::Framework::Builder.new(@spec, file_accessor, @platform, source_dir, @isRootSpec, @build_model )
    @@build_defines = builder.build if @isRootSpec
    begin
      @framework_path = builder.lipo_build(@@build_defines) unless @skip_archive
    rescue
      @skip_archive = true
    end
  end
end

#build_static_libraryObject



121
122
123
124
125
126
127
128
# File 'lib/cocoapods-bb-bin/helpers/build_helper.rb', line 121

def build_static_library
  source_dir = zip_dir
  file_accessor = Sandbox::FileAccessor.new(Pathname.new('.').expand_path, @spec.consumer(@platform))
  Dir.chdir(workspace_directory) do
    builder = CBin::Library::Builder.new(@spec, file_accessor, @platform, source_dir,@framework_path)
    builder.build
  end
end

#build_static_xcframeworkObject

build xcframework



98
99
100
101
102
103
104
# File 'lib/cocoapods-bb-bin/helpers/build_helper.rb', line 98

def build_static_xcframework
  source_dir = Dir.pwd
  UI.puts "xcframework source_dir=#{source_dir}"
  isGenDylib = @xcframework_dylib_output
  builder = CBin::XCFramework::XCBuilder.new(@spec, @spec_sources, @support_maccatalyst, isGenDylib)
  builder.build
end

#clean_workspaceObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/cocoapods-bb-bin/helpers/build_helper.rb', line 171

def clean_workspace
  UI.puts 'Cleaning workspace'

  FileUtils.rm_rf(gen_name)
  Dir.chdir(zip_dir) do
    # framework
    FileUtils.rm_rf(framework_name) if @zip
    FileUtils.rm_rf(library_name)
    FileUtils.rm_rf(framework_name) unless @framework_output
    FileUtils.rm_rf("#{framework_name}.zip") unless @framework_output
    # xcframework
    FileUtils.rm_rf(xcframework_name) if @zip
    FileUtils.rm_rf(xcframework_name) unless @framework_output
    FileUtils.rm_rf("#{xcframework_name}.zip") unless @framework_output
  end
end

#framework_nameObject



196
197
198
# File 'lib/cocoapods-bb-bin/helpers/build_helper.rb', line 196

def framework_name
  CBin::Config::Builder.instance.framework_name(@spec)
end

#framework_name_zipObject



200
201
202
# File 'lib/cocoapods-bb-bin/helpers/build_helper.rb', line 200

def framework_name_zip
  CBin::Config::Builder.instance.framework_name_version(@spec) + ".zip"
end

#gen_nameObject



216
217
218
# File 'lib/cocoapods-bb-bin/helpers/build_helper.rb', line 216

def gen_name
  CBin::Config::Builder.instance.gen_dir
end

#is_build_finishObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cocoapods-bb-bin/helpers/build_helper.rb', line 75

def is_build_finish
  if is_build_xcframework
    path = xcframework_name_zip
  else
    path = framework_name_zip
  end
  output_path =  File.join(zip_dir, path)
  unless File.exist?(xcframework_name)
    UI.puts "工程文件编译成功"
    return true
  end
  UI.puts "工程文件编译失败"
  return false
end

#is_build_xcframeworkObject

是否编译xcframework库



91
92
93
94
95
96
# File 'lib/cocoapods-bb-bin/helpers/build_helper.rb', line 91

def is_build_xcframework
  if @xcframework_output || @xcframework_dylib_output
    return true
  end
  return false
end

#library_nameObject



204
205
206
# File 'lib/cocoapods-bb-bin/helpers/build_helper.rb', line 204

def library_name
  CBin::Config::Builder.instance.library_name(@spec)
end

#spec_fileObject



221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/cocoapods-bb-bin/helpers/build_helper.rb', line 221

def spec_file
  @spec_file ||= begin
                   if @podspec
                     find_spec_file(@podspec)
                   else
                     if code_spec_files.empty?
                       raise Informative, '当前目录下没有找到可用源码 podspec.'
                     end

                     spec_file = code_spec_files.first
                     spec_file
                   end
                 end
end

#workspace_directoryObject



208
209
210
# File 'lib/cocoapods-bb-bin/helpers/build_helper.rb', line 208

def workspace_directory
  File.expand_path("#{gen_name}/#{@rootSpec.name}")
end

#xcframework_nameObject



188
189
190
# File 'lib/cocoapods-bb-bin/helpers/build_helper.rb', line 188

def xcframework_name
  CBin::Config::Builder.instance.xcframework_name(@spec)
end

#xcframework_name_zipObject



192
193
194
# File 'lib/cocoapods-bb-bin/helpers/build_helper.rb', line 192

def xcframework_name_zip
  CBin::Config::Builder.instance.xcframework_name_version(@spec) + ".zip"
end

#zip_dirObject



212
213
214
# File 'lib/cocoapods-bb-bin/helpers/build_helper.rb', line 212

def zip_dir
  CBin::Config::Builder.instance.zip_dir
end

#zip_static_frameworkObject



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/cocoapods-bb-bin/helpers/build_helper.rb', line 143

def zip_static_framework
  Dir.chdir(File.join(workspace_directory,@framework_path.root_path)) do
    output_name =  File.join(zip_dir, framework_name_zip)
    unless File.exist?(framework_name)
      UI.puts "没有需要压缩的 framework 文件:#{framework_name}"
      return
    end

    UI.puts "Compressing #{framework_name} into #{output_name}"
    `zip --symlinks -r #{output_name} #{framework_name}`
  end
end

#zip_static_libraryObject



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/cocoapods-bb-bin/helpers/build_helper.rb', line 156

def zip_static_library
  Dir.chdir(zip_dir) do
    output_library = "#{library_name}.zip"
    unless File.exist?(library_name)
      raise Informative, "没有需要压缩的 library 文件:#{library_name}"
    end

    UI.puts "Compressing #{library_name} into #{output_library}"

    `zip --symlinks -r #{output_library} #{library_name}`
  end

end

#zip_static_xcframeworkObject



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/cocoapods-bb-bin/helpers/build_helper.rb', line 130

def zip_static_xcframework
  Dir.chdir(zip_dir) do
    output_name =  File.join(zip_dir, xcframework_name_zip)
    unless File.exist?(xcframework_name)
      UI.puts "没有需要压缩的 xcframework 文件:#{xcframework_name}"
      return
    end

    UI.puts "Compressing #{xcframework_name} into #{output_name}"
    `zip --symlinks -r #{output_name} #{xcframework_name} && rm -rf #{xcframework_name}` # xcframework进行zip压缩 & 删除源文件
  end
end