Class: Pod::Command::Bin::Fetch

Inherits:
Pod::Command::Bin show all
Defined in:
lib/cocoapods-lhj-bin/command/bin/local/fetch.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Pod::Command::Bin

#validate!

Methods included from CBin::SpecFilesHelper

#binary_spec, #binary_spec_files, #binary_template_spec, #binary_template_spec_file, #binary_template_spec_files, #clear_binary_spec_file_if_needed, #code_spec, #code_spec_files, #create_binary_spec_file, #find_spec_file, #spec_files

Methods included from CBin::SourcesHelper

#binary_source, #code_source, #sources_manager, #sources_option, #valid_sources

Constructor Details

#initialize(argv) ⇒ Fetch

Returns a new instance of Fetch.



18
19
20
21
22
23
24
25
# File 'lib/cocoapods-lhj-bin/command/bin/local/fetch.rb', line 18

def initialize(argv)
  @current_path = argv.shift_argument || Dir.pwd
  @file_type = argv.option('file-type', 'm,h')
  @file_name = argv.option('file-name', 'gen_cn_key.csv')
  @cn_keys = []
  @key_map = {}
  super
end

Class Method Details

.optionsObject



11
12
13
14
15
16
# File 'lib/cocoapods-lhj-bin/command/bin/local/fetch.rb', line 11

def self.options
  [
    %w[--file-type 从文件扩展名中查找中文字符串,默认为m,h],
    %w[--file-name 生成csv文件名,默认为gen_cn_key.csv]
  ]
end

Instance Method Details

#csv_file_nameObject



33
34
35
36
37
# File 'lib/cocoapods-lhj-bin/command/bin/local/fetch.rb', line 33

def csv_file_name
  file_name = @file_name
  file_name = "#{@file_name}.csv" unless /.csv$/ =~ @file_name
  file_name
end

#file_string(file) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'lib/cocoapods-lhj-bin/command/bin/local/fetch.rb', line 131

def file_string(file)
  str = ''
  File.open(file, 'r+') do |f|
    f.each_line do |line|
      str += format_string(f, line)
    end
  end
  str
end

#format_string(file, line) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/cocoapods-lhj-bin/command/bin/local/fetch.rb', line 141

def format_string(file, line)
  result = line
  unless /static/ =~ line
    @key_map.each_key do |key|
      n_key = /#{key.to_s}\s/
      n_val = "#{@key_map[key]}\s"
      result = result.gsub(n_key, n_val)
    end
  end
  result
end

#framework_name(path) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cocoapods-lhj-bin/command/bin/local/fetch.rb', line 88

def framework_name(path)
  mod_name = 'Main'
  if /pods/i =~ path
    ary = path.split('/')
    index = ary.find_index { |p| p.eql?('Pods') }
    if index
      i = index + 1
      mod_name = ary[i]
    end
  end
  mod_name
end

#gen_csvObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cocoapods-lhj-bin/command/bin/local/fetch.rb', line 39

def gen_csv
  file = File.join(@current_path, csv_file_name)
  FileUtils.rm_rf(file) if File.exist?(file)
  CSV.open(file, 'wb:utf-8') do |csv|
    csv << %w[国际化key 中文 英文 所在文件 文件路径]
    @cn_keys.each do |k|
      csv << [k[:key], k[:cn], k[:en], k[:fname], k[:dirname]]
    end
  end
  UI.puts "生成csv文件完成.\n文件路径:#{File.absolute_path(file)}".green
end

#handle_file(file) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/cocoapods-lhj-bin/command/bin/local/fetch.rb', line 67

def handle_file(file)
  File.open(file, 'r') do |f|
    f.each_line do |line|
      handle_line(file, line) if zh_ch_reg =~ line && !((/DDLog/ =~ line) || (/NSLog/ =~ line))
    end
  end
end

#handle_filesObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cocoapods-lhj-bin/command/bin/local/fetch.rb', line 51

def handle_files
  Dir.glob("#{@current_path}/**/*.{#{@file_type}}").each do |f|
    dir_name = File.dirname(f)
    if /Pods/ =~ f
      mod_name = framework_name(dir_name)
      handle_file f if /^ML/ =~ mod_name
    else
      handle_file f
    end
  end
end

#handle_line(file, line) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cocoapods-lhj-bin/command/bin/local/fetch.rb', line 75

def handle_line(file, line)
  line.scan(zh_ch_reg) do |str|
    fname = File.basename(file)
    dir_name = File.dirname(file)
    mod_name = framework_name(dir_name)
    # ('a'..'z').to_a.shuffle[0..12].join
    key = "#{mod_name}.#{File.basename(file, '.*')}.#{rand(36**8).to_s(36)}"
    cn_str = str[2, str.length - 3]
    en_str = cn_str.gsub(/[\u4e00-\u9fa5]/, 'x')
    @cn_keys << { key: key, cn: cn_str, en: en_str, fname: fname, dirname: dir_name }
  end
end

#handle_static_line(file, line) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/cocoapods-lhj-bin/command/bin/local/fetch.rb', line 101

def handle_static_line(file, line)
  line.scan(zh_ch_reg) do |str|
    ma = line.match(/\*.*=/)
    key = ma[0][1, ma[0].length - 2].strip
    @key_map[key.to_sym] = str
  end
end

#handler_file(file) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/cocoapods-lhj-bin/command/bin/local/fetch.rb', line 121

def handler_file(file)
  puts "#{File.absolute_path(file)} \n"
  File.chmod(0o644, file)
  str = file_string(file)
  File.open(file, 'w+') do |f|
    f.write(str)
  end
  File.chmod(0o444, file) if file =~ /Pods/
end

#runObject



27
28
29
30
31
# File 'lib/cocoapods-lhj-bin/command/bin/local/fetch.rb', line 27

def run
  handle_files
  gen_csv
  # update_source_header
end

#update_source_headerObject



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/cocoapods-lhj-bin/command/bin/local/fetch.rb', line 109

def update_source_header
  Dir.glob("#{@current_path}/**/*.{m,h}").each do |f|
    if f =~ /Pods/
      dir_name = File.dirname(f)
      mod_name = framework_name(dir_name)
      handle_file f if /^ML/ =~ mod_name
    else
      handler_file f
    end
  end
end

#zh_ch_regObject



63
64
65
# File 'lib/cocoapods-lhj-bin/command/bin/local/fetch.rb', line 63

def zh_ch_reg
  /@"[^"]*[\u4e00-\u9fa5]+[^"]*"/
end