require 'cocoapods-bb-PodAssistant/config/stable_source' module BB class StableEnv def initialize(verify_stable_env=true, isMatrix=false) @cache = BB::Cache.new(nil, verify_stable_env) @isMatrix = isMatrix configGitPath(@cache.cachePath) if verify_stable_env # 自动加载stable环境 loadStaleEnv if verify_stable_env_exists == false puts "[PodAssistant] ❌工程没有配置stable环境".red end end fetch_stale_git # 自动拉取远端git仓库数据 end # stable 环境,执行pod stable def loadStaleEnv 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 puts "- Error: not stable define in the podfile! you can define to podfile:".red puts "stable! 'https://git.babybus.co/babybus/ios/Specs/stable-specs.git', specs:'<业务线stbale名称-可选>', tag:'<标签-可选>', branch:'<分支-可选>'".green exit -9002 end eval(stableCommand.to_s) end # 拉取stable仓库代码 hook开发环境,执行pod install/update def system_clone_stable_git cachePath = @cache.cachePath puts "fetch stable git=>#{cachePath}" if !Dir.exist?(File.join(cachePath)) puts "git clone=>#{stable_source}" clonePath = File.dirname(cachePath) FileUtils.mkdir_p clonePath system "git clone #{stable_source} #{cachePath}" end if verify_branch_exists if stable_tag || stable_branch if stable_branch system "cd #{cachePath}; git pull --all; git checkout #{stable_branch}; git reset --hard origin/#{stable_branch}" end if stable_tag system "cd #{cachePath}; git pull --all; git checkout #{stable_tag};" end else result =`git symbolic-ref refs/remotes/origin/HEAD` protechBranch = result.split("/").last.strip || default_branch system "cd #{cachePath}; git pull --all; git checkout #{protechBranch}; git reset --hard origin/#{protechBranch}" end else puts "❌ stable配置无效分支信息source:#{stable_source} branch:#{stable_branch}".red exit end end def fetch_stale_git system_clone_stable_git end # ========podfile 约定函数规则,切勿修改 #help load podfile option def stable!(source, options = {}) @stable_source = source if options.has_key?(:specs) @businessSpec = options[:specs] end @stable_tag = options[:tag] if options.has_key?(:tag) @stable_branch = options[:branch] if options.has_key?(:branch) puts "###stable env => [stable公共源]:#{source} [业务线源名称]:#{@businessSpec} branch:#{@stable_branch} tag:#{@stable_tag}".green end def stable_source if @stable_source.nil? @stable_source = StableSource.stable_default_source end return @stable_source end def default_branch return "main" end def stable_branch if @stable_branch.nil? return default_branch end return @stable_branch end def stable_tag return @stable_tag end def business_stable if @businessSpec.nil? && @isMatrix return default_matrix_business_stable end return @businessSpec end def default_matrix_business_stable return "global_stable_specs" end # 验证stable环境是否存在 def verify_stable_env_exists return @stable_source.empty? ? false : true end # tag是否存在 def verify_tag_exists tag = stable_tag if tag cachePath = @cache.cachePath # result = system "cd #{cachePath}; git tag" result = `cd #{cachePath}; git tag` puts "verify_tag_exists:#{result}".red return result.split("\n").include?(tag) end return false end # 分支是否存在 def verify_branch_exists branch = stable_branch if branch cachePath = @cache.cachePath # result = system "cd #{cachePath}; git tag" branchs = `cd #{cachePath}; git branch -a` if branchs branchs = branchs.split("\n") return branchs.include?(branch) || branchs.include?(' remotes/origin/' + branch) || branchs.include?('remotes/origin/' + branch) end puts "verify_branch_exists:#{branchs}".red end return false end def cache_path return @cache.cachePath end end end