Class: CocoapodsTools::Tag

Inherits:
Spec
  • Object
show all
Defined in:
lib/cocoapods-tools/command/spec/tag.rb,
lib/cocoapods-tools/command/spec/tag_sigle.rb

Overview

This command uses an argument for the extra parameter, instead of subcommands for each of the flavor.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Tag

Returns a new instance of Tag.



25
26
27
28
# File 'lib/cocoapods-tools/command/spec/tag.rb', line 25

def initialize(argv)
  @arguments = argv.arguments!
  super
end

Class Method Details

.optionsObject



21
22
23
# File 'lib/cocoapods-tools/command/spec/tag.rb', line 21

def self.options
  []
end

Instance Method Details

#runObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/cocoapods-tools/command/spec/tag.rb', line 36

def run
  super

  # 工作目录
  working_dir = Pathname.pwd
  # pod 私有源库名
  pod_spec_name = 'libspec'
  # pod 私有源路径
  pod_spec_path = Pathname.pwd.parent + pod_spec_name
  # pod 私有源仓库
  pod_repo_url = 'ssh://git@htsz.hellotalk.net:7999/~panda/libspec.git'

  # 上传私有索引库
  # 新 tag
  new_tag_name = @arguments[0].to_s
  # 版本号
  last_tag_name = ''

  # 初始化组件源码 git
  pod_source_git = Git.open(working_dir)

  # 取最近的 tag 号
  max_tag_no = -1
  pod_source_git.tags.each do |tag|
    tag_name = tag.name
    tag_no = (tag_name.gsub '.', '').to_i
    if max_tag_no < tag_no
      max_tag_no = tag_no
      last_tag_name = tag_name
    end
  end

  # 上次提交 sha
  last_commit = pod_source_git.log(1).first
  # 最近 tag 对应的提交 sha
  last_tag = pod_source_git.tag(last_tag_name)
  # 读取两个 sha 间的文件变更
  changes = `git log --name-only --pretty=oneline --full-index #{last_commit}...#{last_tag} | grep -vE '^[0-9a-f]{40} ' | sort | uniq`
  puts Color.color_text "===== 与 tag:#{last_tag_name} 对比,所有文件变更如下 ======", Color.green
  puts changes
  puts Color.color_text '==================变更文件打印结束=================', Color.green

  specs = []
  Dir.entries(working_dir).each do |sub|
    specs.push(sub) if File.directory?(sub) && !sub.include?('.') && changes.include?("#{sub}/#{sub}")
  end

  puts Color.color_text "开始更新组件列表:#{specs}", Color.red

  # 更新组件源码版本号
  specs.each do |spec|
    # 读取 podspec 文件
    podspec_path = working_dir + spec + "#{spec}.podspec"
    # 修改当前podspec文件的版本
    File.open(podspec_path, 'r+') do |t|
      File.open(podspec_path) do |f|
        f.each_line do |line|
          # 查找.version
          # s.version      = "0.0.2"
          # 需要注意的是,版本号可以是'',也可以是""
          write_line = line
          version_desc = /.*\.version\s*=.*/.match line
          unless version_desc.nil?
            version_comes = version_desc.to_s.split('=')
            puts Color.color_text('New Tag Name = ', Color.white) + Color.color_text(new_tag_name.to_s, Color.green)
            write_line = "#{version_comes.first}= '#{new_tag_name}'\n"
          end
          t.write write_line
        end
      end
    end
    puts Color.color_text('当前版本号 = ', Color.white) + Color.color_text(last_tag_name, Color.green)
  end

  # 更新组件版本号私有库
  unless File.exists? pod_spec_path
    puts Color.color_text("Init Spec Repo... ☕️! Current tag = #{last_tag_name}", Color.green)
    Git.clone(pod_repo_url, podspec_path, tag: last_tag_name)
  end

  # 更新组件版本号私有库
  Dir.chdir(pod_spec_path) do

    pod_spec_git = Git.open(pod_spec_path)
    # ruby open 会重置代码,下方操作没必要
    # pod_spec_git.checkout('master', { f: true })
    # pod_spec_git.pull

    specs.each do |spec|
      pod_repo_spec_sorce = working_dir + spec + "#{spec}.podspec"
      pod_repo_path = pod_spec_path + spec
      new_tag_path = pod_repo_path + new_tag_name
      FileUtils.mkdir_p(pod_repo_path, mode: 0o755) unless pod_repo_path.exist?
      Dir.chdir(pod_repo_path) do
        if new_tag_path.exist?
          Color.die_log("[!] #{spec}组件#{new_tag_name}版本已存在,请确认后继续执行。")
          # 发布时打开
          # return
        else
          FileUtils.mkdir_p(new_tag_path, mode: 0o755)
        end

        pod_repo_spec_dest = new_tag_path + "#{spec}.podspec"
        if pod_repo_spec_dest.exist?
          Color.die_log("[!] #{spec}组件#{new_tag_name}版本 podspec 已存在,请确认后继续执行。")
          # 发布时打开
          # return
        else
          FileUtils.copy_file(pod_repo_spec_sorce, pod_repo_spec_dest)
        end
      end
    end

    # git 提交到私有源
    puts Color.color_text("Start push pod repo to remote repo '#{pod_spec_name}'", Color.white)
    pod_spec_git.add
    pod_spec_git.commit_all("组件库tag:#{new_tag_name}")
    pod_spec_git.push
    puts Color.color_text("Update success ☕️! Current version = #{new_tag_name}", Color.green)
  end

  # 提交组件代码到远程仓库
  puts Color.color_text('Start upload source code to remote', Color.white)
  pod_source_git.add
  pod_source_git.commit("组件库tag:#{new_tag_name}")
  pod_source_git.push
  pod_source_git.add_tag(new_tag_name)
  puts Color.color_text("Update success ☕️! Current version = #{new_tag_name}", Color.green)

end

#validate!Object



30
31
32
33
34
# File 'lib/cocoapods-tools/command/spec/tag.rb', line 30

def validate!
  super
  # puts Color.color_text('validate spec', Color.green)
  help! '输入参数过多' if @arguments.count > 1
end