require 'cocoapods/generate' require 'cocoapods/command/gen' require 'swordfish/command/swordfish' require 'swordfish/config/config_builder' require 'swordfish/helpers/build_helper' module Pod class Command class Swordfish < Command class Archive < Swordfish @@missing_binary_specs = [] self.summary = '将组件归档为静态库 .a.' self.description = <<-DESC 将组件归档为静态库 framework,仅支持 iOS 平台 此静态 framework 不包含依赖组件的 symbol DESC def self.options [ ['--all-make', '对该组件的依赖库,全部制作为二进制组件'], ['--sources', '私有源地址,多个用分号区分'], ['--framework-output', '输出framework文件'], ['--configuration', 'Build the specified configuration (e.g. Debug). Defaults to Release'], ['--env', "该组件上传的环境 %w[dev debug_iphoneos release_iphoneos]"] ].concat(Pod::Command::Gen.options).concat(super).uniq end self.arguments = [ CLAide::Argument.new('NAME.podspec', false) ] def initialize(argv) @env = argv.option('env') || 'dev' Ocean.config.set_configuration_env(@env) UI.warn "====== cocoapods-swordfish #{Ocean::VERSION} 版本 ======== " UI.warn "====== #{@env} 环境 ======== " @code_dependencies = argv.flag?('code-dependencies') @framework_output = argv.flag?('framework-output', false ) @all_make = argv.flag?('all-make', false ) @sources = argv.option('sources') || [] @platform = Platform.new(:ios) @config = argv.option('configuration', 'Release') super @additional_args = argv.remainder! @build_finshed = false end def run #清除之前的缓存 zip_dir = Ocean::Config::Builder.instance.zip_dir FileUtils.rm_rf(zip_dir) if File.exist?(zip_dir) @spec = Specification.from_file(spec_file) generate_project source_specs = [] source_specs.concat(build_root_spec) source_specs.concat(build_dependencies) if @all_make source_specs end def build_root_spec source_specs = [] builder = Ocean::Build::Helper.new(@spec, @platform, @framework_output, @zip, @spec, Ocean::Config::Builder.instance.white_pod_list.include?(@spec.name), @config) builder.build builder.clean_workspace if @clean && !@all_make source_specs << @spec unless Ocean::Config::Builder.instance.white_pod_list.include?(@spec.name) source_specs end def build_dependencies @build_finshed = true #如果没要求,就清空依赖库数据 source_specs = [] @@missing_binary_specs.uniq.each do |spec| next if spec.name.include?('/') next if spec.name == @spec.name #过滤白名单 next if Ocean::Config::Builder.instance.white_pod_list.include?(spec.name) # #过滤 git (没有,dependency不支持git和path) # if spec.source[:git] && spec.source[:git] # spec_git = spec.source[:git] # spec_git_res = false # Ocean::Config::Builder.instance.ignore_git_list.each do |ignore_git| # spec_git_res = spec_git.include?(ignore_git) # break if spec_git_res # end # next if spec_git_res # end UI.warn "#{spec.name}.podspec 带有 vendored_frameworks 字段,请检查是否有效!!!" if spec.attributes_hash['vendored_frameworks'] next if spec.attributes_hash['vendored_frameworks'] && @spec.name != spec.name #过滤带有vendored_frameworks的 next if spec.attributes_hash['ios.vendored_frameworks'] && @spec.name != spec.name #过滤带有vendored_frameworks的 #获取没有制作二进制版本的spec集合 source_specs << spec end fail_build_specs = [] source_specs.uniq.each do |spec| begin builder = Ocean::Build::Helper.new(spec, @platform, @framework_output, @zip, @spec, false , @config) builder.build rescue Object => exception UI.puts exception fail_build_specs << spec end end if fail_build_specs.any? fail_build_specs.uniq.each do |spec| UI.warn "【#{spec.name} | #{spec.version}】组件二进制版本编译失败 ." end end source_specs - fail_build_specs end # 解析器传过来的 def self.missing_binary_specs(missing_binary_specs) @@missing_binary_specs = missing_binary_specs unless @build_finshed end private def generate_project Podfile.execute_with_bin_plugin do Podfile.execute_with_use_binaries(!@code_dependencies) do argvs = [ "--sources=#{@sources}", "--gen-directory=#{Ocean::Config::Builder.instance.gen_dir}", '--clean', *@additional_args ] podfile= File.join(Pathname.pwd, "Podfile") if File.exist?(podfile) argvs += ['--use-podfile'] end argvs << spec_file if spec_file gen = Pod::Command::Gen.new(CLAide::ARGV.new(argvs)) gen.validate! gen.run end end end 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 end end end end