Class: Pod::Command::Bin::Filter

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

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) ⇒ Filter

Returns a new instance of Filter.



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

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

Instance Method Details

#csv_file_nameObject



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

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

#fetch_keysObject



57
58
59
60
61
# File 'lib/cocoapods-lhj-bin/command/bin/local/filter.rb', line 57

def fetch_keys
  Dir.glob("#{@current_path}/**/*.{#{@file_type}}").each do |f|
    handle_file f
  end
end

#gen_csvObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cocoapods-lhj-bin/command/bin/local/filter.rb', line 45

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/filter.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
    end
  end
end

#handle_line(file, line) ⇒ Object



75
76
77
78
79
80
# File 'lib/cocoapods-lhj-bin/command/bin/local/filter.rb', line 75

def handle_line(file, line)
  line.scan(zh_ch_reg) do |str|
    str[20, str.length - 22]
    @used_keys << str[20, str.length - 22]
  end
end

#read_csvObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cocoapods-lhj-bin/command/bin/local/filter.rb', line 33

def read_csv
  path = File.join(@current_path, csv_file_name)
  Dir.glob(path).each do |p|
    CSV.foreach(p) do |row|
      key = row[0]
      if @used_keys.any? { |k| k.eql?(key) }
        @cn_keys << { key: key, cn: row[1], en: row[2], fname: row[3], dirname: row[4] }
      end
    end
  end
end

#runObject



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

def run
  fetch_keys
  read_csv
  gen_csv
end

#zh_ch_regObject



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

def zh_ch_reg
  /MLLocalizedString\([^)]+\)/
end