Class: BConv::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/Converter.rb

Instance Method Summary collapse

Constructor Details

#initialize(map, configFile, debugMode) ⇒ Converter

Returns a new instance of Converter.



11
12
13
14
15
# File 'lib/Converter.rb', line 11

def initialize(map, configFile, debugMode)
  @configFile = configFile
  @map = map
  @debugMode = debugMode
end

Instance Method Details

#convertObject



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
# File 'lib/Converter.rb', line 17

def convert
  pwd = @configFile
  
  outputfilename = @map['OutputFile']
  templatefilename = Util.makeAbsolute(@map['TemplateFile'], pwd)
 
  lineIdx = 0
  tmpLineIdx = 0
  set = true
 
  @map.delete_if{|k,_| (k=="EXCLUDE_BAKE_DEPENDENCIES" || k=="EXCLUDE_BAKE_SOURCES" || k=="EXCLUDE_BAKE_INCLUDES") } #not useful anymore

  
  begin
  File.open(outputfilename, 'w') do |fout|
    File.open(templatefilename) do |fread|
      File.readlines(fread).each do |line|
        lineIdx += 1
        wroteLine = false
        
        @map.keys.each do |key|
          if line.include?(key.to_s) && @map[key].include?(".txt")
            preAndPostfix = line.scan(/(.*)\$\$\(#{key}\)(.*)/)
            preAndPostfixOpt = line.scan(/(.*)\$OPTION\(#{key}\)(.*)/)
            
            if preAndPostfix.length == 1
              prefix = preAndPostfix[0][0]
              postfix = preAndPostfix[0][1]
            elsif preAndPostfixOpt.length == 1
              prefix = preAndPostfixOpt[0][0]
              postfix = preAndPostfixOpt[0][1]
            end
            
            filename = Util.makeAbsolute(@map[key], pwd)
            raise "Error: Template file #{File.basename(filename)} is empty!" if File.zero?(filename)
            raise "Error: File #{File.basename(filename)} does not exist!" if !File.exist?(filename)
            
            File.open(filename) do |fr|
              File.readlines(fr).each do |l|
                tmpLineIdx += 1
                @map.keys.each do |k|
                  wroteLine = findAndReplace(k, l, fout, prefix, postfix)
                  break if wroteLine == true
                end                   
                
                if wroteLine == false 
                  l.strip!
                  l = prefix + l + postfix + "\n"
                  m = l.match(/\$\$\((.*)\)/)
                  opt = l.match(/\$OPTION\((.*)\)/)
                  raise "Error: Key $$(#{m[1]}) in #{File.basename(filename)}, line #{tmpLineIdx.to_s} wasn\'t replaced!" if m
                  puts "Info: Key $OPTION(#{opt[1]}) in #{File.basename(filename)}, line #{tmpLineIdx.to_s} wasn\'t replaced!" if opt  
                  fout.write(l)
                  set = false
                end
              end
            end
          elsif (@map[key][0] == "[") && (@map[key][-1] == "]")
            @map.store(key,Util.strToArray(key, @map))
            wroteLine = findAndReplace(key, line, fout, "", "")
            break
          elsif line.match(/\$\$\(#{key}\)/)
            wroteLine = findAndReplace(key, line, fout, "", "")
            break
          elsif line.match(/\$OPTION\(#{key}\)/)
            wroteLine = findAndReplace(key, line, fout, "", "")
            break
          end
        end
        
        if wroteLine == false
          if set == true
            m = line.match(/\$\$\((.*)\)/)
            opt = line.match(/\$OPTION\((.*)\)/)
            raise "Error: Key $$(#{m[1]}) in #{File.basename(templatefilename)}, line #{lineIdx.to_s} wasn\'t replaced!" if m
            puts "Info: Key $OPTION(#{opt[1]}) in #{File.basename(templatefilename)}, line #{lineIdx.to_s} wasn\'t replaced!" if opt
            fout.write(line)
          end
          set = true
        end 
      end
    end
  end
  raise "Error: Output file #{File.basename(outputfilename)} is empty!" if File.zero?(outputfilename)
  raise "Error: Template file #{File.basename(templatefilename)} is empty!" if File.zero?(templatefilename)
  rescue Exception => e
    puts e.message
    puts e.back_trace if @debugMode == true
    abort
  end
  return 0
end

#findAndReplace(key, line, fout, prefix, postfix) ⇒ Object



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
139
140
# File 'lib/Converter.rb', line 109

def findAndReplace(key, line, fout, prefix, postfix)
  wroteLine = false
  found = line.scan(/(.*)\$\$\(#{key}\)(.*)/)
  foundOpt = line.scan(/(.*)\$OPTION\(#{key}\)(.*)/)
  
  found = foundOpt if (found.length==0 && foundOpt.length!=0)
  
  if found.length == 1
    pre = found[0][0]
    post = found[0][1]
    if @map[key].kind_of?(Array)
      @map[key].each do |val|
        line = prefix + pre + val.to_s + post + postfix + "\n"
        fout.write(line)
      end
      wroteLine = true
    else
      line = prefix + pre + @map[key] + post + postfix + "\n"
      fout.write(line)
      wroteLine = true
    end
  elsif line.include?("/\$\$\(#{key}\)/") && found.length == 0
    line.gsub!(/\$\$\(#{key}\)/, @map[key].to_s)
    fout.write(line)
    wroteLine = true
  elsif line.include?("/\$OPTION\(#{key}\)/") && found.length == 0
    line.gsub!(/\$OPTION\(#{key}\)/, @map[key].to_s)
    fout.write(line)
    wroteLine = true
  end
  return wroteLine
end