Class: Lhj::Command::SyncPodVersion

Inherits:
Lhj::Command
  • Object
show all
Defined in:
lib/lhj/command/sync_pod_version.rb

Overview

sync code to pod

Instance Method Summary collapse

Methods inherited from Lhj::Command

#auto_spin, #run, #stop

Constructor Details

#initialize(argv) ⇒ SyncPodVersion

Returns a new instance of SyncPodVersion.



11
12
13
14
# File 'lib/lhj/command/sync_pod_version.rb', line 11

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

Instance Method Details

#begin_titleObject



16
17
18
# File 'lib/lhj/command/sync_pod_version.rb', line 16

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

#find_src_root_dir(src) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/lhj/command/sync_pod_version.rb', line 54

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



20
21
22
# File 'lib/lhj/command/sync_pod_version.rb', line 20

def handle
  sync
end

#syncObject



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
# File 'lib/lhj/command/sync_pod_version.rb', line 24

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']

  ma = nil
  Dir.chdir(dest) do
    Dir.glob('*.podspec').each do |p|
      version_line = IO.readlines(p).find{ |line| (/\.version/ =~ line) && (version_regex =~ line) }
      ma = version_line.match(version_regex)
    end
    puts '获取版本号成功'.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 '更新主工程引用pod版本号成功'.green
    end
  end
end

#update_all_pod_dependency(pod_name, pod_version) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/lhj/command/sync_pod_version.rb', line 66

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/lhj/command/sync_pod_version.rb', line 74

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

#version_regexObject



62
63
64
# File 'lib/lhj/command/sync_pod_version.rb', line 62

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