Module: EWP

Included in:
IAR::Project
Defined in:
lib/ebngen/adapter/iar/ewp.rb

Overview

end Hash

Instance Method Summary collapse

Instance Method Details

#add_sources(doc, source_hash, path_mod, proj_path) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/ebngen/adapter/iar/ewp.rb', line 216

def add_sources(doc, source_hash, path_mod, proj_path)
  groups_existing = Array.new
  files_hash = Hash.new
  source_hash.each do |src|
    rootdir = src['rootdir']
    virtual_dir = src['virtual_dir'] if src.has_key? 'virtual_dir'
    virtual_dir = src['virtual-dir'] if src.has_key? 'virtual-dir'
    if src.has_key?('path')
      path = src['path']
    else
      path = src['source']
    end
    if virtual_dir
      if ! groups_existing.include?(virtual_dir)
        groups_existing.insert(-1, virtual_dir)
        node = Nokogiri::XML::Node.new 'group', doc
        node << "<name>#{virtual_dir}</name>"
        doc.root << node
      end
      files_hash[virtual_dir] = Array.new if files_hash[virtual_dir].nil?
      files_hash[virtual_dir].insert(-1, {'path' => path, 'rootdir' => rootdir})
    else
      files_hash["_"] = Array.new if files_hash["_"].nil?
      files_hash["_"].insert(-1, {'path' => path, 'rootdir' => rootdir})
    end
  end #end source_hash
  doc.css("//group").each do |node|
    files_hash[node.text].each do |file|
      gfiles = Nokogiri::XML::Node.new('file', node)
      sfile = Nokogiri::XML::Node.new('name', gfiles)
      if file['rootdir']
        full_path = path_mod.fullpath(file['rootdir'],file['path'])
      else
        full_path = path_mod.fullpath('default_path',file['path'])
      end
      sfile.content = File.join("$PROJ_DIR$", path_mod.relpath(proj_path, full_path))
      gfiles << sfile
      node << gfiles
    end
  end
  return if files_hash["_"].nil?
  files_hash["_"].each do |file|
    gfiles = Nokogiri::XML::Node.new('file', doc)
    sfile = Nokogiri::XML::Node.new('name', gfiles)
    if file['rootdir']
      full_path = path_mod.fullpath(file['rootdir'],file['path'])
    else
      full_path = path_mod.fullpath('default_path',file['path'])
    end
    sfile.content = File.join("$PROJ_DIR$", path_mod.relpath(proj_path, full_path))
    gfiles << sfile
    doc.root << gfiles
  end
end

#add_specific(target_node, doc) ⇒ Object



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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/ebngen/adapter/iar/ewp.rb', line 105

def add_specific(target_node, doc)
  doc.each do |key, value|
    checked = false
    options = target_node.xpath(".//option")
    options.each do |option|
      if option.css('name').text == key
        value.each do |subkey, subvalue|
          if subvalue.class == String
            if option.css(subkey)[0].content.nil?
              option.css(subkey)[0].content = subvalue
            else
              create_node(option, {subkey => subvalue})
            end
          elsif subvalue.class == Array
            subvalue.each do |line|
              append_node(option, {subkey => line})
            end
          else
            puts "not supported format must be string or array"
            next
          end
        end
        #processing done
        checked = true
        break
      end
    end
    if !checked
      #not an exist option need create new node
      data_node = target_node.xpath('data')
      option_node = create_node(data_node, "option" => nil)
      create_node(option_node, {"name" => key})
      value.each do |subkey, subvalue|
        if subvalue.class == String
          create_node(option_node, {subkey => subvalue})
        elsif subvalue.class == Array
          subvalue.each do |line|
            append_node(option_node, {subkey => line})
          end
        else
          puts "not supported format must be string or array"
          next
        end
      end
    end
    if !checked
      puts "can not find match for #{key}"
    end
  end
end

#append_node(doc, hash_value) ⇒ Object



101
102
103
# File 'lib/ebngen/adapter/iar/ewp.rb', line 101

def append_node(doc, hash_value)
  hash_value.to_xml(doc)
end

#create_node(doc, hash_value) ⇒ Object



97
98
99
# File 'lib/ebngen/adapter/iar/ewp.rb', line 97

def create_node(doc, hash_value)
  hash_value.to_xml!(doc)
end

#load_node(doc, xpath) ⇒ Object



33
34
35
# File 'lib/ebngen/adapter/iar/ewp.rb', line 33

def load_node(doc, xpath)
	return doc.xpath(xpath)
end

#new_target(target, doc, name = 'debug') ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ebngen/adapter/iar/ewp.rb', line 37

def new_target(target, doc, name = 'debug')
	nset = load_node(doc, "/project/configuration")
	#use existing one
	nset.each do |element|
		if element.css("/name").text.downcase == target.downcase
      puts "find existing #{element.css("/name").text.downcase}"
      return element
		end
	end
	#create new one
	nset.each do |element|
		#use the first available configuration
		t = element.dup
		t.at_css('/name').content = target
			#doc.xpath("/project") << t
		element.add_previous_sibling(t)
		return t
	end
nil
end

#remove_sources(doc) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ebngen/adapter/iar/ewp.rb', line 74

def remove_sources(doc)
  puts "remove source"
  groups = load_node(doc, "//group")
  groups.each do |ele|
    ele.remove
  end
  files = load_node(doc, "//file")
  files.each do |ele|
    ele.remove
  end   
end

#remove_targets(doc, targets_in) ⇒ Object

remove_targets remove unused targets Params:

  • doc: the xml node project file

  • targets_in: used target array



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ebngen/adapter/iar/ewp.rb', line 62

def remove_targets(doc, targets_in)
	#remove the target that not in the targets_in
	nset = load_node(doc, "//project/configuration")
	targets_in.collect{|x| x.downcase}
  nset.each do |element|
	  target = element.xpath("name").text.downcase
	  if !targets_in.include?(target)
	  	element.remove
	  end	
 end
end

#remove_unused(doc, xpath, **names) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/ebngen/adapter/iar/ewp.rb', line 86

def remove_unused(doc, xpath, **names)
	nset = load_node(doc, xpath)
  nset.each do |element|
	  names.each do |key, value|
	  if element.xpath(key).text.downcase == value.downcase
		  element.remove
	  end
  end
 end
end

#save(xml, path) ⇒ Object



208
209
210
211
212
213
214
# File 'lib/ebngen/adapter/iar/ewp.rb', line 208

def save(xml, path)
  Core.assert(path.is_a?(String)) do
      "param is not a string #{path.class.name}"
  end
  FileUtils.mkdir_p File.dirname(path) if ! File.exist?(File.dirname(path))
  File.write(path, xml.to_xml)
end

#set_specific(target_node, doc) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/ebngen/adapter/iar/ewp.rb', line 156

def set_specific(target_node, doc)
	doc.each do |key, value|
    checked = false
		options = target_node.xpath(".//option")
		options.each do |option|
			if option.css('name').text == key
				value.each do |subkey, subvalue|
          if subvalue.class == String
            if option.css(subkey)[0].content.nil?
              option.css(subkey)[0].content = subvalue
            else
              create_node(option, {subkey => subvalue})
            end
          elsif subvalue.class == Array
            subvalue.each do |line|
              append_node(node, {subkey => line})
            end
          else
            puts "not supported format must be string or array"
            next
          end
				end
        #processing done
        checked = true
        break
			end
		end
    if !checked
      #not an exist option need create new node
      #not an exist option need create new node
      data_node = target_node.xpath('data')
      option_node = create_node(data_node, "option" => nil)
      create_node(option_node, {"name" => key})
      value.each do |subkey, subvalue|
        if subvalue.class == String
          create_node(option_node, {subkey => subvalue})
        elsif subvalue.class == Array
          subvalue.each do |line|
            append_node(option_node, {subkey => line})
          end
        else
          puts "not supported format must be string or array"
          next
        end
      end
    end
    if !checked
      puts "can not find match for #{key}"
    end
	end
end