Class: BundleGenerater

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-aqara-localzedLoader/ios_bundle_generate.rb

Overview

require ‘colored2’

Constant Summary collapse

INFO_PLIST_MAP =

INFO_PLIST_ARRAY = [NSAppleMusicUsageDescription“,”NSLocalNetworkUsageDescription“]

{:common_app_name=>["CFBundleDisplayName"],other_perm_camera_permission_description:["NSCameraUsageDescription"],other_perm_location_permission_description:["NSLocationAlwaysAndWhenInUseUsageDescription","NSLocationWhenInUseUsageDescription","NSLocationAlwaysUsageDescription"],other_perm_bluetooth_permission_description:["NSBluetoothPeripheralUsageDescription","NSBluetoothAlwaysUsageDescription"],access_content_permssion_storage:["NSPhotoLibraryUsageDescription"],other_perm_mic_permission_description:["NSMicrophoneUsageDescription"],other_set_permissions_homedata_desc:["NSHomeKitUsageDescription"],other_set_permissions_asr_text:["NSSpeechRecognitionUsageDescription"],device_add_device:["Add_Device_Title"],automation_add:["Add_Automation_Title"],device_create_scene:["Add_Scene_Title"]}

Class Method Summary collapse

Class Method Details

.generate(project_path) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/cocoapods-aqara-localzedLoader/ios_bundle_generate.rb', line 11

def self.generate(project_path)
  # 下载excel
  puts "开始下载多语言文件...".green
  Open3.capture3("cd #{File.dirname(__FILE__)};python3 DownloadNewLanguage.py #{project_path}")
  # system "cd #{File.dirname(__FILE__)};python3 DownloadNewLanguage.py #{project_path}"
  f_path = "#{project_path}/download.xlsx"
  print(f_path)

  # 读取excel到内存
  file_til = File_util.new
  hash = file_til.read_excel f_path
  if File.exist? f_path
    FileUtils.rm_rf f_path
  end
  puts "一共有 #{hash.keys.size} 条文案".green

  # 创建文件夹
  file_til.getLangList.each do |lang|
    # puts lang
    localized_file = "./#{lang}.lproj/Localizable.strings"
    dir = File.dirname localized_file
    FileUtils.rm_rf localized_file
    FileUtils.mkdir_p dir
    new_file = File.new(localized_file, "w")
    new_file.close

  end

  #生成资源文件
  hash.each do |key, stringElement|
    num = 0
    stringElement.langHash.each do |lang, value|
      # puts "#{lang}:#{value}"
      next if lang.downcase === "selfkey" or value === nil or value === " " or value === ""
      value = self.handleValue value,stringElement
      value.scan(/%@/) do |match|
        num = match.size  if num == 0
        if num != match.size
          puts "key:#{key}中%@ 数量不一致,请检查".red
        end
      end
      str = %Q|"#{key}" = "#{value}";\n|
      localized_file = "./#{lang}.lproj/Localizable.strings"

      if File.exist?(localized_file)
        File.open(localized_file, "a") do |io|
          io.write str
      end
      end

      #如果是infoplist里的key,就写入指定的文件
      if key === nil
        puts "key值为空,#{stringElement.ios_list}"
      elsif key === "NSAppleMusicUsageDescription" || key === "NSLocalNetworkUsageDescription"
        file = "./#{lang}.lproj/InfoPlist.strings"
        File.open(file, "a") do |io|
          io.write str
        end
      elsif INFO_PLIST_MAP.has_key?(key.to_sym)
        # puts "---key:#{key}"
        keys = INFO_PLIST_MAP[key.to_sym]
        keys.each do |info_key|
          file = "./#{lang}.lproj/InfoPlist.strings"
          str = %Q|"#{info_key}" = "#{value}";\n|
          File.open(file, "a") do |io|
            io.write str
          end
        end
      end

    end
  end

  #验证导出的多语言包格式是否正确
  puts '开始校验多语言包格式'.red
  # puts "\e[31m 开始校验多语言包格式\e[0m"
  file_til.getLangList.each do |lang|
    localized_file = "./#{lang}.lproj/Localizable.strings"
    file = "./#{lang}.lproj/InfoPlist.strings"
    if File.exist?(file)
      system("plutil #{file}")
    end
    if File.exist?(localized_file)
      system("plutil #{localized_file}")
    end
  end

  #拷贝到代码仓库里
  #查找bundle路径
  bundPath = ""
  info_plist_path = "./AqaraHome/Resource"
  require 'find'
  Find.find("./") do |filePath|
    if  filePath.end_with?("LMFramework.bundle")
      bundPath = filePath
    elsif filePath.end_with?("Resource")
    end
  end
  unless bundPath != ""
    puts '没有拷贝'
    return
  end

  copy_info_plist = false
  file_til.getLangList.each do |lang|
    path = "./#{lang}.lproj/Localizable.strings"
    dest = bundPath + "/#{lang}.lproj"
    dest2 = "./AqaraHome/Resource/#{lang}.lproj"
    FileUtils.mkdir_p dest
    FileUtils.cp(path,dest2)
    FileUtils.mv("#{path}",dest,force:true)

    info_plist_file = "./#{lang}.lproj/InfoPlist.strings"
    # puts "path::::::#{info_plist_file}"
    if File.exist? info_plist_file
      copy_info_plist = true
      dest = "./AqaraHome/Resource/#{lang}.lproj"
      FileUtils.mkdir_p dest
      FileUtils.mv("#{info_plist_file}",dest,force:true)
    end
    FileUtils.rm_rf (File.dirname(path))
  end
  puts "多语言拷贝到目录:#{bundPath}"
  if copy_info_plist
    puts "InfoPlist多语言拷贝到目录:#{info_plist_path}"
  end

end

.handleValue(value = '', stringElement = nil) ⇒ Object

对多语言的value进行处理



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/cocoapods-aqara-localzedLoader/ios_bundle_generate.rb', line 140

def self.handleValue(value='',stringElement = nil)
  #替换{=}
  value = value.gsub(/{#}/,"%@")
  value = value.gsub(/""/,'"')
  value = value.gsub(/\\"/,'"')
  value = value.gsub(/""/,'"')
  value = value.gsub(/"/,'\"')
  #fix "common_time_day_with_space" = "\ day \";
  if value.end_with?"\\"
    value = value.chop
  end
  if value.end_with?"\\"
    value = value.chop
  end
  value
end