Class: CocoapodsTools::Tag

Inherits:
Spec
  • Object
show all
Defined in:
lib/cocoapods-tools/command/spec/tag.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.



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

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

Class Method Details

.optionsObject



18
19
20
# File 'lib/cocoapods-tools/command/spec/tag.rb', line 18

def self.options
  []
end

Instance Method Details

#runObject



33
34
35
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/cocoapods-tools/command/spec/tag.rb', line 33

def run
  super

  # 搜索podspec路径
  podspec_path = ''
  find_podspec_path = Pathname.pwd

  # 有可能存在多个 podspec,当用户没有指定时,需要给用户自主选择
  pod_specs = find_podspec_path.children.select { |pn| pn.extname == '.podspec' }
  example_path = Pathname.new("#{find_podspec_path}/Example").expand_path
  project_path = example_path.children.select { |pn| pn.extname == '.xcodeproj' }.first
  project_name = File.basename(project_path, '.*')
  default_name = "#{project_name}.podspec"
  podspec_path = default_name

  # 默认为工程名同名的 spec
  # default_name = "#{project_name}.podspec"
  # spec_default = pod_specs.select { |path| path.basename.to_s == default_name }.first
  spec_default = "#{project_name}.podspec"

  if pod_specs.count > 1
    input_tag = true
    serial = 0
    puts Color.color_text(
      "Find #{pod_specs.count} podspec files, please enter the serial number selection:\n(default:#{spec_default})", Color.white)
    while input_tag
      (0...pod_specs.count).each do |i|
        puts "#{i + 1}. #{pod_specs[i]}"
      end
      serial = $stdin.gets.chomp
      input_tag = (serial.to_i > pod_specs.count || serial.to_i <= 0)
      if serial.empty?
        input_tag = false
        podspec_path = spec_default
      elsif input_tag
        puts "Input serial = #{serial}, it's invalid and you need to input 1~#{pod_specs.count}:"
      else
        input_tag = false
        podspec_path = pod_specs[serial.to_i - 1]
      end
    end
  elsif pod_specs.count == 1
    podspec_path = pod_specs.first.split.last
  else
    puts color_text("Can't find any podspec file", Color.red)
    return
  end

  if !File.exist?(podspec_path)
    puts ("找不到 podspec 文件: #{podspec_path}, 请确认当前路径")
    return
  else
    puts "Ready to deal with podspec named #{Color.color_text(podspec_path.to_s, Color.white)}"
  end

  # 在当前podspec目录下新建一个临时 need_delete_temp.podspec 文件
  podspec_dir = File.dirname podspec_path
  podspec_absolute_path = "#{find_podspec_path}/#{podspec_path}"
  temp_podspec_path = "#{podspec_dir}/need_delete_temp.podspec"
  temp_podspec_absolute_path = "#{find_podspec_path}/#{temp_podspec_path}"

  cur_version = ''
  # 读取当前podspec文件的版本
  File.open(podspec_absolute_path, 'r+') do |f|
    f.each_line do |line|
      # 查找.version
      version_desc = /.*\.version\s*=.*/.match line
      next if version_desc.nil?

      cur_version = version_desc.to_s.split('=').last.to_s.gsub("'", '')
      cur_version = cur_version.gsub(' ', '')
      break
    end
  end

  puts Color.color_text('Current version = ', Color.white) + Color.color_text(cur_version.to_s, Color.green)

  # 自定义版本号
  if @arguments.count == 1

    input_version = @arguments.first
    puts Color.color_text "不输入时默认自增。本次已输入,版本号:#{input_version}", Color.white

    # 判断输入的version是否>当前的版本号
    input_v_s = input_version.to_s.split('.')
    cur_v_s = cur_version.split('.')
    # 比较的位置,从最左边开始
    v_index = 0
    # 输入的version是否有效
    input_valid = false
    while v_index < cur_v_s.count && v_index < input_v_s.count do
      if input_v_s[v_index].to_i > cur_v_s[v_index].to_i
        # 说明用户输入的version比当前的大
        input_valid = true
        break
      elsif input_v_s[v_index].to_i == cur_v_s[v_index].to_i
        v_index += 1
      else
        break
      end
    end

    puts Color.color_text "版本号 #{input_version} 无效,默认自增", Color.natural if input_valid == false
  end

  system("touch #{temp_podspec_path}") unless File.exist? temp_podspec_absolute_path

  new_version = ''
  git_source = ''

  File.open(temp_podspec_absolute_path, 'r+') do |t|
    File.open(podspec_absolute_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('=')
          if (input_valid == true) && (@arguments.count == 1)
            new_version = input_version.to_s
          else
            version_num = version_comes.last.to_s.gsub("'", '').gsub('"', '').gsub(' ', '')
            v_s = version_num.split('.')
            # 处理版本号 0.0.1
            (0...v_s.count).each do |i|
              new_version += if i == v_s.count - 1
                               (v_s[i].to_i + 1).to_s
                             else
                               "#{v_s[i]}."
                             end
            end
          end
          puts Color.color_text('New version = ', Color.white) + Color.color_text(new_version.to_s, Color.green)
          write_line = "#{version_comes.first}= '#{new_version}'\n"
        end
        source_desc = /.*\.source\s*=.*/.match line
        unless source_desc.nil?
          source_desc = /:git.*,/.match source_desc.to_s
          source_desc = /'.*'/.match source_desc.to_s
          git_source = source_desc.to_s.gsub("'", '')
          puts "git source is #{git_source}"
        end
        t.write write_line
      end
    end
  end

  puts Color.color_text('Update version from ',
                        Color.white) + Color.color_text(cur_version.to_s,
                                                        Color.green) + Color.color_text(' to ', Color.white) + Color.color_text(new_version.to_s, Color.green)

  # 将新数据反写回到原始podspec中
  system("cp -f #{temp_podspec_path} #{podspec_path}")
  system("rm -f #{temp_podspec_path}")

  # pod_repo_name = 'third_specs'
  # pod_repo_name = ''
  pod_repo_source = 'ssh://[email protected]:7999/third/third_specs.git'
  pod_repo_source = 'ssh://[email protected]:7999/third/third_specs.git'

  # 处理正式 repo 逻辑,解析当前 repos
  # pod_repos = `pod repo list`
  #
  # # pod_repos = Array.new
  # # pod_repo = Array.new
  # curren_pod_name = ''
  # temp_pod_name = ''
  # last_line = ''
  # pod_repos.each_line do |line|
  #   continue if line == '\n'
  #
  #   line = line.gsub(/\n/, '')
  #
  #   if line.include?('- Type:')
  #     temp_pod_name = last_line
  #   elsif line.include?('- URL:')
  #     repo_url = line.split('- URL:').last.gsub(/ /, '')
  #     if repo_url == pod_repo_source
  #       pod_repo_name = temp_pod_name
  #       break
  #     end
  #   end
  #   last_line = line
  # end
  #
  # if pod_repo_name == ''
  pod_repo_name = pod_repo_source.split('/').last.split('.').first
  #   puts Color.color_text("Add pod repo named '#{pod_repo_name}' with source: #{pod_repo_source}", Color.white)
  #   system("pod repo add #{pod_repo_name} #{pod_repo_source}")
  # end

  # 提交代码到远程仓库
  puts Color.color_text('Start upload code to remote', Color.white)

  system("git commit -am 'update version to #{new_version}'")
  Color.die_log('[!] git push code error') if system('git push origin master') == false
  system("git tag #{new_version}")
  if system('git push origin --tags') == false
    Color.die_log('[!] git push tags error')
    return
  end

  # 获取当前 repo 缓存路径,建立新版本 spec 的文件夹
  home_dir ||= Pathname.new(ENV['CP_HOME_DIR'] || '~/.cocoapods').expand_path
  specs_git_syn = Pathname.new("#{home_dir}/specs_git_syn").expand_path
  FileUtils.mkdir_p(specs_git_syn) unless specs_git_syn.exist?

  pod_spec_dir = Pathname.new("#{specs_git_syn}/#{pod_repo_name}").expand_path

  Dir.chdir(specs_git_syn) do
    # 同步文件夹中,没有当前源则克隆

    system("git clone #{pod_repo_source} #{pod_spec_dir}") unless pod_spec_dir.exist?

    pod_repo_dir = Pathname.new("#{pod_spec_dir}/#{project_name}").expand_path

    # 添加最新版本文件夹
    version_dir = Pathname.new("#{pod_repo_dir}/#{new_version.to_s}").expand_path
    FileUtils.mkdir_p(version_dir, mode: 0o755)

    # 将修改版本号的 spec 文件拷贝进去
    spec_repo_path = Pathname.new("#{version_dir}/#{podspec_path}").expand_path
    system("cp -f #{podspec_absolute_path} #{spec_repo_path}")

    # 进入当前源对应的同步文件夹
    Dir.chdir(pod_spec_dir) do
      # git 提交到私有源
      puts Color.color_text("Start push pod '#{pod_spec_dir}' to remote repo '#{pod_repo_name}'", Color.white)
      system('git add .')
      system("git commit -am 'update version to #{new_version}'")
      Color.die_log('[!] git push code error') if system('git push origin master') == false
      system("git tag #{new_version}")
      if system('git push origin --tags') == false
        Color.die_log('[!] git push tags error')
        return
      end
      puts Color.color_text("Update success ☕️! Current version = #{new_version}", Color.green)

    end

  end
end

#validate!Object



27
28
29
30
31
# File 'lib/cocoapods-tools/command/spec/tag.rb', line 27

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