Class: PPL::Command::Build

Inherits:
PPL::Command show all
Defined in:
lib/pod-pipeline/command/build.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PPL::Command

#argv_extension, ensure_not_root_or_allowed!, git_version, options_extension, options_extension_hash, run, verify_minimum_git_version!, verify_xcode_license_approved!

Constructor Details

#initialize(argv) ⇒ Build

Returns a new instance of Build.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pod-pipeline/command/build.rb', line 34

def initialize(argv)
    @path               = argv.arguments!
    @output             = argv.option('output', '').split(',').first
    @configuration      = argv.option('configuration', '').split(',').first
    @archs              = argv.option('arch', '').split(',')
    @combines           = argv.option('combine', '').split(',')
    @combine_headers    = argv.option('combine-headers', false)
    @combine_include    = argv.option('combine-include', '').split(',')
    @combine_exclude    = argv.option('combine-exclude', '').split(',')
    @bundle_merge       = argv.option('bundle-merge', '').split(',').first
    
    @projectPath = @path.count.zero? ? Pathname.pwd.to_s : @path.first
    @output = @output ? @output : @projectPath

    super
end

Class Method Details

.optionsObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pod-pipeline/command/build.rb', line 21

def self.options
    [
        ['--output=./', '项目构建的输出目录。(默认使用项目根目录)'],
        ['--configuration=Release', '项目构建的环境。(默认为Release)'],
        ['--arch=arm64,armv7,x86_64', '项目构建的架构。(默认为 arm64,armv7,x86_64)'],
        ['--combine=local,pod', '项目构建后合并依赖库的二进制文件,local为本地依赖库,pod为CocoaPods依赖库。(默认为 不合并)'],
        ['--combine-headers=false', '项目构建后合并依赖库的头文件。(默认为 不合并)'],
        ['--combine-include=name1,name2', '项目构建后合并依赖库的二进制文件,标注想合并的内容,执行顺序在exclude前。'],
        ['--combine-exclude=name1,name2', '项目构建后合并依赖库的二进制文件,标注不想合并的内容,执行顺序在include后。'],
        ['--bundle-merge=merge', '是否合并所有资源包,参数为合并后的资源包名。(默认为 不合并)'],
    ].concat(super)
end

Instance Method Details

#add_headersObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/pod-pipeline/command/build.rb', line 108

def add_headers
    puts "\n[合并 本地的头文件]"
    header_stands = "#{@output}/Example/Pods/Headers/Public/#{@podspec.name}/*.h"
    puts "\n合并路径:#{header_stands}"
    Dir[header_stands].each do |header_stand|
        if File.ftype(header_stand).eql? 'link'
            header_file = "#{File.dirname(header_stand)}/#{File.readlink(header_stand)}"
            if File.ftype(header_file).eql? 'file'
                header_file_basename = File.basename(header_file)
                if !(File.exist? "#{@framework_headers_path}/#{header_file_basename}")
                    puts header_file_basename
                    FileUtils.cp(header_file, @framework_headers_path)
                end
            end
        end
    end
    header_files = "#{@build_path}/**/#{@podspec.name}/#{@podspec.name}.framework/Headers/*.h"
    puts "\n合并路径:#{header_files}"
    Dir[header_files].each do |header_file|
        if File.ftype(header_file).eql? 'file'
            header_file_basename = File.basename(header_file)
            if !(File.exist? "#{@framework_headers_path}/#{header_file_basename}")
                puts header_file_basename
                FileUtils.cp(header_file, @framework_headers_path)
            end
        end
    end
end

#combine_binarys(local_dependency, pod_dependency) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/pod-pipeline/command/build.rb', line 155

def combine_binarys(local_dependency, pod_dependency)
    binary = "#{@framework_path}/#{@podspec.name}"
    #添加 构建生成的二进制文件
    inputs = []
    inputs << "#{@build_path}/*/**/lib#{@podspec.name}.a"
    inputs << "#{@build_path}/*/**/*.framework/#{@podspec.name}"
    if local_dependency
        #添加 本地依赖的二进制文件
        inputs << "#{@output}/#{@podspec.name}/Libraries/**/*.a" 
        inputs << "#{@output}/#{@podspec.name}/Frameworks/**/*.framework/*"
    end
    if pod_dependency
        #添加 Pod依赖库构建生成的二进制文件
        inputs << "#{@build_path}/*/**/lib*.a"
        inputs << "#{@build_path}/*/**/*.framework/*"
        #添加 Pod依赖库预先构建的二进制文件
        inputs << "#{@output}/Example/Pods/**/*SDK/*.framework/*"
        #添加 Pod依赖库本地依赖的二进制文件
        inputs << "#{@output}/Example/Pods/**/Libraries/**/*.a"
        inputs << "#{@output}/Example/Pods/**/Frameworks/**/*.framework/*"
    end
    
    Binary.combine(binary, inputs, @combine_include, @combine_exclude)
    Binary.thin(binary, @archs)
end

#combine_headers(local_dependency, pod_dependency) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/pod-pipeline/command/build.rb', line 137

def combine_headers(local_dependency, pod_dependency)
    headers = "#{@framework_path}/Headers"
    #添加 构建生成的二进制文件
    inputs = []
    if local_dependency
        #添加 本地依赖的二进制文件
        inputs << "#{@output}/#{@podspec.name}/Frameworks/**/*.framework"
    end
    if pod_dependency
        #添加 Pod依赖库构建生成的二进制文件
        inputs << "#{@build_path}/*/**/*.framework"
        #添加 Pod依赖库本地依赖的二进制文件
        inputs << "#{@output}/Example/Pods/**/Frameworks/**/*.framework"
    end
    
    Header.combine(headers, inputs, @combine_include, @combine_exclude)
end

#copy_bundles(local_dependency, pod_dependency) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/pod-pipeline/command/build.rb', line 181

def copy_bundles(local_dependency, pod_dependency)
    #添加 构建生成的资源包
    inputs = ["#{@build_path}/**/#{@podspec.name}/*.bundle"]
    if local_dependency
        #添加 本地依赖的资源包
        inputs << "#{@output}/#{@podspec.name}/Libraries/**/*.bundle" 
        inputs << "#{@output}/#{@podspec.name}/Frameworks/**/*.bundle"
    end
    if pod_dependency
        #添加 Pod依赖库构建生成的资源包
        inputs << "#{@build_path}/*/**/*.bundle"
        #添加 Pod依赖库预先构建的资源包
        inputs << "#{@output}/Example/Pods/**/*SDK/*.bundle"
        #添加 Pod依赖库本地依赖的资源包
        inputs << "#{@output}/Example/Pods/**/Libraries/**/*.bundle"
        inputs << "#{@output}/Example/Pods/**/Frameworks/**/*.bundle"
    end
    
    Bundle.combine(inputs, @build_path, @combine_include, @combine_exclude)
end

#merge_bundlesObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/pod-pipeline/command/build.rb', line 202

def merge_bundles
    #初始化资源文件夹
    bundle_path = "#{@build_path}/#{@bundle_merge}"
    Dir.reset(bundle_path)
    
    #合并资源文件
    Dir["#{@build_path}/*.bundle/*"].each do |asset|
        `cp -fr "#{asset}" "#{bundle_path}"`
    end

    #删除bundle
    Dir["#{@build_path}/*.bundle/"].each do |bundle|
        `rm -fr "#{bundle}"`
    end

    #将资源文件夹命名为 .bundle 格式
    `mv "#{bundle_path}" "#{bundle_path}.bundle"`
end

#reset_dirObject



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

def reset_dir
    #初始化 构建目录
    @build_path = "#{@output}/#{@podspec.name}-#{@podspec.version}"
    Dir.reset(@build_path)
    #初始化 Framework目录
    @framework_path = "#{@build_path}/#{@podspec.name}.framework"
    Dir.reset(@framework_path)
    @framework_headers_path = "#{@framework_path}/Headers"
    Dir.reset(@framework_headers_path)
end

#runObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/pod-pipeline/command/build.rb', line 51

def run
    PPL::Scanner.new(@projectPath, ["all"]).run

    @podspec = PPL::Scanner.linter.spec
    @workspace = PPL::Scanner.workspace
    puts "Pod: #{@podspec}"
    puts "Workspace: #{@workspace.path}"
    
    if !@archs
        puts "无需构建"
        return
    end

    #初始化 构建目录
    reset_dir
    
    #构建
    puts "\n[构建 #{@configuration} 环境的 #{@archs.join(", ")} 架构项目]"
    @archs.each do |arch|
        XCodebuild.build(@workspace.path, @podspec.name, arch, @configuration, @build_path)
    end

    if @combine_headers
        #合并头文件
        puts "\n[合并 #{@combines.join(", ")} 的头文件]"
        combine_headers(@combines.include?('local'), @combines.include?('pod'))
    end

    #合并本地头文件
    add_headers

    #合并二进制文件
    puts "\n[合并 #{@combines.join(", ")} 的二进制文件]"
    combine_binarys(@combines.include?('local'), @combines.include?('pod'))

    #拷贝资源包
    puts "\n[拷贝 #{@combines.join(", ")} 的资源包 到输出目录]"
    copy_bundles(@combines.include?('local'), @combines.include?('pod'))

    #合并资源包
    if @bundle_merge
        puts "\n[合并的资源包内容 到 #{@bundle_merge}.bundle]"
        merge_bundles
    end
end