Example是殼工程,當需求涉及到多個模塊的修改時,爲了方便提交各個Pod的代碼,寫了個ruby腳本
- 規則1:目錄結構以下
- 規則2:提交日誌以
PodSync:
開頭
- 修改下work_path的取值,腳本就能夠配置在持續集成服務器上。
- TODO1:執行狀態通知,由於各個Pod同步後commit就變了,殼工程應該
pod update
- TODO2:利用Gitlab Api 直接提交代碼,就不用clone了
├── Podfile
├── Podfile.lock
├── Pods
│ ├── ABCPods1
│ │ └── Code1.m
│ ├── ABCPods2
│ │ └── Code2.m
│ ├── ABCPods3
│ │ └── Code3.m
│ ├── Headers
│ ├── Local Podspecs
│ ├── Manifest.lock
│ ├── Pods.xcodeproj
│ └── Target Support Files
├── Example
│ ├── AppDelegate.h
│ └── AppDelegate.m
├── Example.xcodeproj
└── Example.xcworkspace
複製代碼
ABCPods都是用pod lib create 建立的,目錄結構一致 ,ABCPods下有同名ABCPods下是模塊代碼,相似:
├── ABCPods1
│ ├── ABCPods1
│ │ └── Code1.m
複製代碼
自動同步的ruby腳本以下:
require 'pathname'
require 'git'
require 'fileutils'
work_path = Pathname.new '.../path/to/Example'
url_config = { 'ABCPods1' => 'http://example.com/ios_code/ABCPods1.git',
'ABCPods2' => 'http://example.com/ios_code/ABCPods2.git',
'ABCPods3' => 'http://example.com/ios_code/ABCPods3.git' }
Git.configure do |config|
config.binary_path = '/usr/local/bin/git'
end
g = Git.open work_path
current_branch = g.current_branch
puts "current_branch:#{current_branch}"
commits = g.log(2)
first_commit = commits.first
last_commit = commits.last
puts "本次提交id: #{first_commit}"
puts first_commit.message
puts "上次提交id: #{last_commit}"
unless first_commit.message.include? 'PodSync:'
puts '提交日誌沒有 PodSync: 開頭'
exit 0
end
if first_commit.parents.size > 1
puts '這是一個合併提交,不能同步合併提交'
exit 1
end
file_list = g.diff(first_commit, commits.last).name_status.map(&:first)
pod_file_list = file_list.select do |e|
next if e.end_with? 'Manifest.lock'
next if e.start_with? 'Pods/Local Podspecs'
next if e.start_with? 'Pods/Target Support Files'
next if e.start_with? 'Pods/Headers'
e.start_with? 'Pods/'
end
puts "pod_file_list#{pod_file_list}"
pod_file_list_map = pod_file_list.map { |e| e.split('/')[1] }
pod_file_list_set = pod_file_list_map.uniq
puts pod_file_list_set
Dir.mktmpdir do |dir|
pod_file_list_set.each do |item|
begin
puts "Clone: #{url_config[item]}, dir #{item}, branch : #{current_branch}"
g = Git.clone(url_config[item], item, path: dir, branch: current_branch)
rescue StandardError => e
puts e
exit 1
end
src = work_path + 'Pods' + item + item
dest = Pathname.new(dir) + item + item
puts "#{src} copy to #{dest}"
FileUtils.rm_r(dest, force: true)
FileUtils.cp_r(src, dest.parent)
status_output = `git -C
puts status_output unless status_output.include? 'working tree clean'
`git -C
`git -C
end
end
複製代碼