require 'cocoapods' require 'fileutils' module Pod class Command class Tag < Command self.summary = 'tag相关操作' self.description = <<-DESC tag创建、删除 DESC def self.options [ ['--beta', 'beta tag'], ['--create', 'create tag'], ['--delete', 'delete tag and remove pod repo source'], ] end def initialize(argv) @beta = argv.flag?('beta', false) @create = argv.flag?('create', false) @delete = argv.flag?('delete', false) @tag_version = argv.shift_argument # @tag_version = argv.option('tag_version') || nil # @tag_version = argv.arguments! unless argv.arguments.empty? super end def build_tag UI.puts "🍉 开始创建tag : #{get_tag}" if TagUtil.exist_tag(get_tag) TagUtil.git_delete_tag(get_tag) end `git tag #{get_tag}` `git push origin #{get_tag}` latest_tag = %x(git describe --abbrev=0 --tags 2>/dev/null) UI.puts("--------- latest tag ----------") UI.puts(latest_tag) unless get_tag.eql?(latest_tag.to_s.strip) TagUtil.git_delete_tag(get_tag) UI.warn("⚠️ commit没有改变,请先提交内容") return end module_dir = Dir.pwd Dir['*.podspec'].each do |rip| podspec_name = rip.to_s if podspec_name.empty? UI.warn("#{module_dir}目录下,不包含.podspec文件") return end podspec_json_name = "#{podspec_name}.json" pod_name = podspec_name.gsub(".podspec", "") `pod ipc spec #{pod_name}.podspec >>#{podspec_json_name}` podspec_json_dir = File.join(Dir.pwd, podspec_json_name) UI.puts("HDSourceSpecs目录: #{get_source_specs_dir}") Dir.chdir(get_source_specs_dir) `git pull` dest_dir = File.join(get_source_specs_dir, pod_name, get_tag) UI.section("创建目录: #{dest_dir}") do FileUtils.mkdir_p(dest_dir) unless File.directory?(dest_dir) end UI.section("move #{podspec_json_dir} to #{dest_dir}") do FileUtils.mv(podspec_json_dir, dest_dir) end UI.section("✅ #{podspec_json_name} 上传成功") do TagUtil.upload_origin_sources("添加 #{pod_name}/#{get_tag}") end Dir.chdir(module_dir) end unless File.directory?(cocoapods_repos_dir) remove_git_source_repo end unless @beta beta_tag = "#{@tag_version}-beta" if TagUtil.exist_tag(beta_tag) UI.section("正式tag #{@tag_version} 创建成功,删除#{@tag_version}-beta") do delete_tag(beta_tag) end end end end # 创建tag def create_tag unless TagUtil.check_master_branch UI.warn("❌ 当前分支仅支持打 beta tag") unless @beta build_tag if @beta return end unless TagUtil.check_branch_include_tag(@tag_version) UI.warn("❌ 分支后缀版本号,必须跟tag保持一致") return end build_tag end # 获取 HDSourceSpecs 路径 def get_source_specs_dir if File.directory?(cocoapods_repos_dir) cocoapods_repos_dir else module_parent_dir = File.expand_path("..", Dir.pwd) # module目录的上一级 source_repo_path = File.join(module_parent_dir, "HDSourceSpecs") if File.exist?(source_repo_path) source_repo_path else Dir.chdir(module_parent_dir) `git clone http://gitlab.dushuclub.io/cocoapods/HDSourceSpecs.git` source_repo_path end end end # 移除tag def delete_tag(tag) UI.puts "🍉 开始删除tag: #{tag}" if TagUtil.exist_tag(tag) TagUtil.git_delete_tag(tag) remove_pod_repo_source(tag) else UI.warn "❌ 不存在tag: #{tag}" end end # 获取tag名称 def get_tag @beta ? "#{@tag_version}-beta" : @tag_version end # 移除远程仓库podspec def remove_pod_repo_source(tag_name) Dir['*.podspec'].each do |rip| pod_name = rip.to_s.gsub(".podspec", "") UI.puts "pod_name: #{pod_name}" source_specs_dir = get_source_specs_dir pod_tag_path = File.join(source_specs_dir, pod_name, tag_name) unless File.exist?(pod_tag_path) UI.warn "❌ 本地HDSourceSpecs不包含#{pod_name}的#{tag_name}标签, 请先执行 pod repo update" remove_git_source_repo break end FileUtils.remove_dir(pod_tag_path) Dir.chdir(source_specs_dir) UI.section("✅ #{pod_name} #{tag_name} 标签删除成功 !!!") do TagUtil.upload_origin_sources("删除 #{pod_name}/#{tag_name}") end end end def cocoapods_repos_dir File.join(Dir.home, ".cocoapods/repos/HDSourceSpecs") end def remove_git_source_repo source_repo_path = File.join(File.dirname(Dir.pwd), "HDSourceSpecs") FileUtils.rm_r(source_repo_path) end def run if @delete delete_tag(get_tag) else create_tag end end end end end