Class: Lhj::Command::SyncPod

Inherits:
Lhj::Command show all
Defined in:
lib/lhj/command/sync_pod_repo.rb

Overview

sync code to pod

Instance Method Summary collapse

Methods inherited from Lhj::Command

#auto_spin, #run, #stop

Constructor Details

#initialize(argv) ⇒ SyncPod

Returns a new instance of SyncPod.



10
11
12
13
# File 'lib/lhj/command/sync_pod_repo.rb', line 10

def initialize(argv)
  @cli = HighLine.new
  super
end

Instance Method Details

#add_tag(tag) ⇒ Object



103
104
105
106
107
108
# File 'lib/lhj/command/sync_pod_repo.rb', line 103

def add_tag(tag)
  cmd = ['git tag']
  cmd << '--force'
  cmd << tag
  Actions.sh(cmd.join(' '))
end

#begin_titleObject



15
16
17
# File 'lib/lhj/command/sync_pod_repo.rb', line 15

def begin_title
  '读取映射文件~/.lhj/pod_config.yml'
end

#find_src_root_dir(src) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/lhj/command/sync_pod_repo.rb', line 76

def find_src_root_dir(src)
  ps = []
  src.scan(%r{(/[^/]*)}).flatten.each do |path|
    path =~ /Code/ ? break : ps << path
  end
  File.join(ps)
end

#handleObject



19
20
21
# File 'lib/lhj/command/sync_pod_repo.rb', line 19

def handle
  sync
end

#push_tagObject



110
111
112
113
# File 'lib/lhj/command/sync_pod_repo.rb', line 110

def push_tag
  cmd = ['git push --tags']
  Actions.sh(cmd.join(' '))
end

#syncObject



23
24
25
26
27
28
29
30
31
32
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
# File 'lib/lhj/command/sync_pod_repo.rb', line 23

def sync
  config_file = File.join(Lhj::Config.instance.home_dir, 'pod_config.yml')
  arr = YAML.load_file(config_file)
  arr.each_index { |i| puts "#{i}.#{arr[i]['pod']}".yellow }
  idx = @cli.ask('请选择哪一个库同步: '.green).strip.to_i
  pod_name = arr[idx]['pod']
  src = arr[idx]['main_path']
  dest = arr[idx]['pod_path']
  FileUtils.cp_r(src, dest, remove_destination: true)
  puts '1.从主工程复制代码到pod库成功'.green

  ma = nil
  Dir.chdir(dest) do
    Dir.glob('*.podspec').each do |p|
      update_podspec_version(p)
      version_line = IO.readlines(p).find{ |line| (/\.version/ =~ line) && (version_regex =~ line) }
      ma = version_line.match(version_regex)
    end
    puts '2.更新版本号成功'.green

    Actions.sh('git add .')
    puts '3.git add成功'.green

    Actions.sh("git commit -m '使用工具同步主工程代码'")
    puts '4.git 提交成功'.green

    Actions.sh('git push')
    puts '5.git 推送成功'.green

    add_tag(ma[0]) if ma
    puts "6.设置tag成功! tag号:#{ma[0]}".green

    push_tag
    puts '7.推送tag成功'.green

    update_pod_repo
    puts "8.#{pod_name}推送pod repo成功".green

  end

  # find src root dir
  src_root_dir = find_src_root_dir(src)
  Dir.chdir(src_root_dir) do
    if ma
      pod_version = ma[0]
      update_all_pod_dependency(pod_name, pod_version)
      puts '9.更新主工程引用pod版本号成功'.green
    end
  end

  puts '10.手动执行`pod update --verbose --no-repo-update`更新pod'.green
end

#update_all_pod_dependency(pod_name, pod_version) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/lhj/command/sync_pod_repo.rb', line 128

def update_all_pod_dependency(pod_name, pod_version)
  Dir.glob(%w[./**/*.podspec ./**/Podfile]).each do |p|
    next if /Example/ =~ p || /temp/ =~ p

    update_main_version(p, pod_name, pod_version)
  end
end

#update_main_version(file, pod_name, pod_version) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/lhj/command/sync_pod_repo.rb', line 136

def update_main_version(file, pod_name, pod_version)
  cont = ''
  File.readlines(file).each do |l|
    if (/#{pod_name}/ =~ l) && (/\d+\.\d+\.\d+/ =~ l)
      path = File.absolute_path(file)
      puts "更新文件:#{path}".magenta
      l.scan(/\d+\.\d+\.\d+/).each do |key|
        cont += l.gsub(key, pod_version)
      end
    else
      cont += l.dup
    end
  end
  File.write(file, cont)
end

#update_pod_repoObject



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/lhj/command/sync_pod_repo.rb', line 115

def update_pod_repo
  # pod repo push miguatech-aomi_ios-mlspecs *.podspec --sources=miguatech-aomi_ios-mlspecs,aliyun,trunk --allow-warnings --use-libraries --verbose
  cmd = ['pod repo push']
  pod_repo_name = 'miguatech-aomi_ios-mlspecs'
  cmd << pod_repo_name
  cmd << '*.podspec'
  cmd << "--sources=#{pod_repo_name},aliyun,trunk"
  cmd << '--allow-warnings'
  cmd << '--use-libraries'
  cmd << '--verbose'
  Actions.sh(cmd.join(' '), log: true, error_callback: nil)
end

#update_podspec_version(path) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/lhj/command/sync_pod_repo.rb', line 88

def update_podspec_version(path)
  str = ''
  File.readlines(path).each do |l|
    if (/\.version/ =~ l) && (version_regex =~ l)
      last_version = l.scan(version_regex).flatten.first
      next_version = last_version.to_i + 1
      next_version_str = next_version.to_s
      str += l.gsub(/(\d+\.\d+\.)(\d+)/, '\1' + next_version_str)
    else
      str += l.dup
    end
  end
  File.write(path, str)
end

#version_regexObject



84
85
86
# File 'lib/lhj/command/sync_pod_repo.rb', line 84

def version_regex
  /\d+\.\d+\.(\d+)/
end