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 # 创建tag def create_tag unless TagUtil.exist_branch UI.warn("当前分支名格式不正确,请修改为 release_x.x.x 格式") return end unless TagUtil.check_branch_include_tag(@tag_version) UI.warn("分支后缀版本号,必须跟tag保持一致") return end 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) source_specs_dir = get_source_specs_dir UI.puts("HDSourceSpecs目录: #{source_specs_dir}") Dir.chdir(source_specs_dir) `git pull` dest_dir = File.join(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 `git status -s` `git add .` `git commit -m 'add #{pod_name} #{get_tag}'` `git push origin master` end Dir.chdir(module_dir) end unless exist_cocoapods_source_dir module_parent_dir = File.expand_path("..", Dir.pwd) # ~/debug-cocoapods-hd source_repo_path = File.join(module_parent_dir, "HDSourceSpecs") FileUtils.rm_r(source_repo_path) 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 # 获取 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}" unless File.directory?(cocoapods_repos_dir) Pod::UI.puts "本地 HDSourceSpecs not found" break end full_path = File.join(cocoapods_repos_dir, pod_name, tag_name) unless File.exist?(full_path) UI.warn "❌ 本地HDSourceSpecs不包含#{pod_name}的#{tag_name}标签, 请先执行 pod repo update" break end FileUtils.remove_dir(full_path) Dir.chdir(cocoapods_repos_dir) # UI.puts "切换到:#{Dir.pwd}" UI.section("✅ #{pod_name} #{tag_name} 标签删除成功 !!!") do `git pull origin master` `git add .` `git commit -m '删除#{pod_name}/#{tag_name}'` `git push origin master` end end end def cocoapods_repos_dir File.join(Dir.home, ".cocoapods/repos/HDSourceSpecs") end def exist_cocoapods_source_dir File.directory?(cocoapods_repos_dir) end def run if @delete delete_tag(get_tag) else create_tag end end end end end