config-importer.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. require_relative 'common'
  2. options = get_options(__FILE__)
  3. config_path = options['config_path']
  4. ini_path = options['generate_ini_path']
  5. text_path = options['generate_text_path']
  6. code_path = options['generate_code_path']
  7. is_generate_field_info = options['source_text_language']
  8. language = options['generate_field_info']
  9. recreate_dir ini_path
  10. recreate_dir text_path
  11. recreate_dir code_path
  12. require 'csv'
  13. # 各类型定义
  14. TypeInfo = Struct.new(:data_type, :data_type_extra, :ref_info, :default_value)
  15. FieldInfo = Struct.new(:column_id, :identifier, :comment, :type_info)
  16. Table = Struct.new(:csv, :fields, :name2id, :id_field)
  17. Ini = Struct.new(:io, :tables)
  18. inis = {}
  19. # 读取配置表子目录,以形成配置数据目录
  20. Dir["#{config_path}/*"].each do |path|
  21. ini_name = File::basename(path)
  22. ini = (inis[ini_name] ||= Ini.new(File.open("#{ini_path}/#{ini_name}.ini", 'wb'), {}))
  23. next unless File::directory?(path)
  24. # 每个配置表子目录将成为一个 ini 文件
  25. # 内含全部子 .csv 表格的导入数据
  26. Dir["#{path}/*.csv"].each do |file|
  27. table_name = File::basename(file, '.csv')
  28. table = (ini.tables[convert_case(table_name)] = Table.new(csv = CSV.open(file), [], {}, nil))
  29. identifiers = csv.readline
  30. comments = csv.readline
  31. type_infos = csv.readline
  32. field_id = nil
  33. field_id_name = nil
  34. # 处理表头列
  35. identifiers.each_with_index do |identifier, index|
  36. next if identifier.nil? || identifier.empty?
  37. type_info = TypeInfo.new(nil, nil, nil, nil)
  38. type_infos[index].gsub(/\s+/, '').scan(/[(:|,|@|=)][a-zA-Z_]+/).each do |info|
  39. type_info.data_type = info[1..info.size] if info.start_with?(':')
  40. type_info.data_type_extra = info[1..info.size] if info.start_with?(',')
  41. type_info.ref_info = info[1..info.size] if info.start_with?('@')
  42. type_info.default_value = info[1..info.size] if info.start_with?('=')
  43. end
  44. field = FieldInfo.new(index, identifier, comments[index], type_info)
  45. # 处理特殊类型的字段
  46. case type_info.data_type
  47. when 'id'
  48. field_id = field
  49. next
  50. when 'id_name'
  51. field_id_name = field
  52. next
  53. end
  54. # 参与导入的条目
  55. table.fields << field
  56. end
  57. # 验证 Id 列
  58. next if field_id.nil?
  59. table.id_field = field_id
  60. # 处理条目形成引用表
  61. while !csv.eof?
  62. line = csv.readline
  63. next if field_id_name.nil?
  64. table.name2id[line[field_id_name.column_id]] = line[field_id.column_id].upcase
  65. end
  66. # 回滚 csv 状态至表头读取后
  67. csv.rewind
  68. 3.times { csv.readline }
  69. end
  70. end
  71. # 导入实际数据条目
  72. inis.each_pair do |ini_name, ini_data|
  73. register_table = {}
  74. buffer = StringIO.new
  75. ini_data.tables.each_pair do |table_name, table|
  76. register = (register_table[table_name] = [])
  77. while !table.csv.eof?
  78. line = table.csv.readline
  79. id = line[table.id_field.column_id].upcase
  80. buffer.puts "[#{id}]"
  81. table.fields.each do |field|
  82. value = line[field.column_id]
  83. next if value.nil? || value.empty?
  84. buffer.puts "#{field.identifier}=#{value}"
  85. end
  86. buffer.puts
  87. register << id
  88. end
  89. end
  90. File.open("#{ini_path}/#{ini_name}.ini", 'wb') do |ini|
  91. ini.puts '[REGISTER]'
  92. register_table.each_pair do |table_name, register|
  93. ini.puts "#{table_name}=#{register.join(',')}"
  94. end
  95. ini.puts
  96. ini.puts buffer.string
  97. end
  98. end