require 'cocoapods-bb-PodAssistant/config/source_manager' require 'cocoapods-bb-PodAssistant/config/cache_path' require 'cocoapods-bb-PodAssistant/helpers' module Pod # class StableOptions # # define business specs,defalut is stable_specs # option :business_specs, "stable_specs" # # define custom lockSpecs tag # option :tag, "" # # define custom lockSpecs branch # option :branch, "" # end class Command class Stable < Command require 'fileutils' require 'cocoapods/executable.rb' extend Executable executable :git self.summary = '拉取稳定lock配置' self.description = <<-DESC 拉取稳定lock配置插件。利用版本比对进行更新配置实现对组件版本控制管理。 DESC self.arguments = [ CLAide::Argument.new('Podfile', false) ] def self.options [ ['--init', '初始化项目yml配置,根据当前项目Podfile.lock生成'], ['--sync', '同步远端yml配置,更新项目yml配置'], ['--install', '命令同执行`pod install操作`'], ['--update', '命令同执行`pod update操作`'], ['--update-pod', '命令同执行`pod update <组件>操作`,更新yml配置组件版本信息,多个组件使用`,`或者`空格`隔开,注意⚠️ 使用`空格`需要使用双引号'], ].concat(super) end def initialize(argv) @names = argv.arguments! unless argv.arguments.empty? @help = argv.flag?('help') @init = argv.flag?('init') @sync = argv.flag?('sync') # install操作 @install = argv.flag?('install') @update = argv.flag?('update') # update组件数据支持多个使用`,`或者`空格`隔开 update_pods = argv.option('update-pod','') if !update_pods.empty? if update_pods.include?(' ') @update_pods = update_pods.split(' ') unless update_pods.nil? else update_pods.include?(',') @update_pods = update_pods.split(',') unless update_pods.nil? end end super end def validate! super banner! if @help end #help load podfile option def stable!(source, options = {}) @ll_stable_source = source if options.has_key?(:specs) @businessSpec = options[:specs] puts "当前业务使用源=>#{@businessSpec}" end @ll_stable_tag = options[:tag] if options.has_key?(:tag) @ll_stable_branch = options[:branch] if options.has_key?(:branch) @cache = BB::Cache.new() configGitPath(@cache.cachePath) end ######################################## Main ######################################## def run # begin source_manager = BB::SourceManager.new(@businessSpec) if @init puts "[PodAssistant] 开始配置生成当前项目yml文件".yellow # 生成本地yml配置 source_manager.generate_localStable puts "[PodAssistant] `pod stable --init` complete!".green elsif @install puts "[PodAssistant] 执行 $ pod install".yellow system "pod install" puts "[PodAssistant] `pod stable --install` complete!".green elsif @update puts "[PodAssistant] 执行 $ pod update".yellow system "pod update" puts "[PodAssistant] `pod stable --update` complete!".green elsif @update_pods # 更新单个组件 puts "更新pod组件#{@update_pods} count:#{@update_pods.count}".yellow source_manager.update_podmodules(@update_pods) podnames = @update_pods.join(" ") puts "[PodAssistant] 执行 $ pod update #{podnames}".yellow system "pod update #{podnames}" source_manager.update_local_stable_from_podmodules(@update_pods) podnames = @update_pods.join(",") puts "[PodAssistant] `pod stable --update-pod=#{podnames}` complete!".green elsif @sync puts "开始合并远端lock数据".yellow ll_load_stable #2、clone origin lock spec to cache dir ll_cloneStable #3、fetch newest code ll_fetch_stale_git # 数据合并 source_manager.merge_stable_data puts "[PodAssistant] `pod stable` complete!".green else puts "[PodAssistant] `pod stable` 帮助文档请查看👇".yellow banner! end # rescue => exception # puts "[pod stable] error(已捕获): #{exception}".red # end end private ######################################## API ######################################## def ll_load_stable unless File.exist?(File.join(Pathname.pwd, "Podfile")) err_msg = "- Error: #{File.join(Pathname.pwd, "Podfile")} is not exit" Pod::UI.puts "#{err_msg}".send(:red) exit -9001 end #获取podfile 内容 #1、删除所有注释行,避免干扰 #2、正则匹配,筛选出stable 方法 #3、执行stable 方法,获取配置 podfileContent = File.read(File.join(Pathname.pwd, "Podfile")) podfileContent_vaild = podfileContent.lines.reject { |line| line.strip.start_with?("#") }.join stableCommand = podfileContent_vaild.match(/^\s*stable!\s*'([^']+)'(?:,\s*(\w+):\s*'([^']+)')*/m) unless stableCommand err_msg = "- Error: not stable define in the podfile! you can define like【stable! 'https://git.babybus.co/babybus/ios/Specs/stable-specs.git', specs:'global_stable_specs'】in podfile" Pod::UI.puts "#{err_msg}".send(:red) exit -9002 end eval(stableCommand.to_s) end def ll_cloneStable cachePath = @cache.cachePath unless Dir.exist?(File.join(cachePath)) clonePath = File.dirname(cachePath) FileUtils.mkdir_p clonePath git_clone(@stable_source,clonePath) end end def ll_fetch_stale_git git_reset git_fetch git_checkout_and_pull(@stable_source, @stable_branch, @stable_tag) end end end end