Class: CocoapodsTools::Merge

Inherits:
GT
  • Object
show all
Defined in:
lib/cocoapods-tools/command/git/merge.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) ⇒ Merge

Returns a new instance of Merge.



24
25
26
27
# File 'lib/cocoapods-tools/command/git/merge.rb', line 24

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

Class Method Details

.optionsObject



20
21
22
# File 'lib/cocoapods-tools/command/git/merge.rb', line 20

def self.options
  []
end

Instance Method Details

#runObject



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
# File 'lib/cocoapods-tools/command/git/merge.rb', line 35

def run
  super

  return 
  # 1. 初始化
  # current_branch:当前分支(主工程)。验证是否时最大分支,不是则报错提醒
  # develop_branch:版本开发主分支
  # need_merges: 需要手动合并的分支
  # success_merges: 合并成功的分支
  # pod_file: 读取 Podfile 文件
  # change_components: 解析当前分支改动的组件,以当前分支名匹配
  #
  # 2. 遍历所有组件,进行合并分支操作
  # component_dir: 修改工作目录,进入组件文件夹
  # git reset:根据主工程当前分支,拉取组件远端仓库最新代码
  # set-upstream-to:关联当前分支与 develop 分支
  # git merge:检查当前分支与 develop 分支有无变动
  #   有变动:加入 need_merges
  #   无变动:合并操作

  # 3. 输出执行结果
  # ["need_merges": [], "success_merges": []]

  # current_branch:当前分支(主工程)。验证是否时最大分支,不是则报错提醒
  working_dir = Pathname.pwd
  g = Git.open(working_dir)
  current_branch = g.current_branch
  puts Color.color_text "当前分支: #{current_branch}", Color.red

  # 主业务线分支
  business_branches = %w[/keep/ /voiceroom/ /live/ /vip/]
  # 判断当前分支是否为业务线分支
  is_business_branch = false
  business_name = ''
  business_version = ''
  business_branches.each do |business|
    next unless current_branch.include?(business)

    business_version = current_branch.split(business).last
    business_branch = (business_version.gsub '.', '').to_i
    unless business_branch.is_a?(Numeric)
      puts Color.color_text '非业务线版本分支,停止合并操作', Color.red
      return
    end

    is_business_branch = true
    business_name = business

    remote_branches = g.branches.remote.select { |bran| bran.name.include? business }
    remote_branches.each do |bran|
      version = (bran.name.split(business).last.gsub! '.', '').to_i
      if version.is_a?(Numeric) && version > business_branch
        puts Color.color_text '非业务线最新版本分支,停止合并操作', Color.red
        return
      end
    end

    break
  end

  unless is_business_branch
    puts Color.color_text '非业务线分支,停止合并操作', Color.red
    return
  end

  puts Color.color_text "#{business_name.gsub! '/', ''} 业务线最新分支为:#{business_version}", Color.green

  business_version.join('.') unless business_version.include? '.'
  # 版本开发主分支
  develop_branch = "develop/#{business_version}"
  # 需要手动合并的分支
  need_merges = []
  # 合并成功的分支
  success_merges = []
  # change_components: 解析当前分支改动的组件,以当前分支名匹配
  change_components = []
  # 读取 Podfile 文件
  File.open('Demo/Podfile').each do |line|
    current_branch_without_dot = current_branch.gsub '.', ''
    unless !(line.include? '#') && ((line.include? current_branch) || (line.include? current_branch_without_dot))
      next
    end

    params = line.split ','
    name = (params.first.split '\'').last
    url = (params[1].split '\'').last
    branch = (params[2].strip.split '\'').last
    change_components.append(Component.new(name, url, branch))
  end

  # 测试用例
  component = Component.new('Test2', 'ssh://git@git', '466')

  # 对所有变动分支进行合并
  # change_components.each do |component|
  [component].each do |component|

    # 组件总文件夹
    component_parent_path = '../iOSLib'
    FileUtils.makedirs(component_parent_path) unless File.exists? component_parent_path
    Dir.chdir(component_parent_path) do
      unless File.exists? component.component_name
        Git.clone(component.component_url, component.component_name, branch: component.component_branch)
      end

      Dir.chdir(component.component_name) do
        component_git = Git.open('./')
        feature_branch_name = component.component_branch
        component_git.checkout(feature_branch_name)
        remote_develop_branch = component_git.branches["origin/#{develop_branch}"]
        component_git.branch(develop_branch).checkout

        # 每个组件都合并业务线分支和主分支
        ['origin/master', "origin/#{feature_branch_name}"].each do |branch|
          puts Color.color_text "=======开始合并 #{component.component_name} 组件 #{branch} 分支=======", Color.green
          merge_result = system("git merge #{branch} -m '合并组件分支'")

          if remote_develop_branch.nil? # 开发分支当前版本不存在
            system("git push --set-upstream origin #{develop_branch}")
            puts Color.color_text "#{component.component_name} 组件 #{branch} 分支合并成功!", Color.green
            success_merges.append("#{component.component_name}-#{branch}")
          elsif merge_result # 合并成功
            system("git push origin #{develop_branch}")
            puts Color.color_text "#{component.component_name} 组件 #{branch} 分支合并成功!", Color.green
            success_merges.append("#{component.component_name}-#{branch}")
          else # 存在冲突
            abort_result = system('git merge --abort')
            if abort_result
              puts Color.color_text "#{component.component_name} 组件 #{branch} 分支存在冲突,请手动合并!", Color.red
              need_merges.append("#{component.component_name}-#{branch}")
            else
              puts Color.color_text "#{component.component_name} 组件 #{branch} 无变更,不需要合并!", Color.green
            end
          end
        end
      end
    end

    puts '==============合并结果=============='
    puts Color.color_text "合并成功:#{success_merges}", Color.green
    puts Color.color_text "需要手动合并:#{need_merges}", Color.red
    puts '==================================='
  end
end

#validate!Object



29
30
31
32
33
# File 'lib/cocoapods-tools/command/git/merge.rb', line 29

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