config-importer.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. table.name2id[line[field_id_name.column_id]] = line[field_id.column_id].upcase
  64. end
  65. # 回滚 csv 状态至表头读取后
  66. csv.rewind
  67. 3.times { csv.readline }
  68. end
  69. end
  70. # 导入实际数据条目
  71. inis.each_pair do |ini_name, ini_data|
  72. register_table = {}
  73. buffer = StringIO.new
  74. ini_data.tables.each_pair do |table_name, table|
  75. register = (register_table[table_name] = [])
  76. while !table.csv.eof?
  77. line = table.csv.readline
  78. id = line[table.id_field.column_id].upcase
  79. buffer.puts "[#{id}]"
  80. table.fields.each do |field|
  81. value = line[field.column_id]
  82. next if value.nil? || value.empty?
  83. buffer.puts "#{field.identifier}=#{value}"
  84. end
  85. buffer.puts
  86. register << id
  87. end
  88. end
  89. File.open("#{ini_path}/#{ini_name}.ini", 'wb') do |ini|
  90. ini.puts '[REGISTER]'
  91. register_table.each_pair do |table_name, register|
  92. ini.puts "#{table_name}=#{register.join(',')}"
  93. end
  94. ini.puts
  95. ini.puts buffer.string
  96. end
  97. end