ebngen 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,278 +1,278 @@
1
- require 'nokogiri'
2
- require 'pathname'
3
-
4
- class Hash
5
- def to_xml(doc)
6
- return if doc.nil?
7
- self.each do |key, value|
8
- mynode = Nokogiri::XML::Node.new key, doc
9
- doc.add_child mynode
10
- value.to_xml(mynode) if value.class == Hash
11
- mynode.content = value if value.class == String or value.class == Fixnum
12
- end
13
- return doc
14
- end #end to_xml
15
- def to_xml!(doc)
16
- return if doc.nil?
17
- self.each do |key, value|
18
- if doc.css("/#{key}").count == 0
19
- mynode = Nokogiri::XML::Node.new key, doc
20
- else
21
- mynode = doc.css("/#{key}")[0]
22
- end
23
- doc.add_child mynode
24
- value.to_xml(mynode) if value.class == Hash
25
- mynode.content = value if value.class == String or value.class == Fixnum
26
- end
27
- return doc
28
- end #end to_xml!
29
- end #end Hash
30
-
31
- module UVPROJX
32
- def travesernode(node)
33
- if node.children
34
- content = node.content
35
- if @@data_provider.has_key?(content.strip)
36
- node.content = @@data_provider[content.strip]
37
- end
38
- end
39
- node.children.each do |subnode|
40
- next if subnode.nil?
41
- travesernode(subnode)
42
- end
43
-
44
- end
45
- def init_project(xml, settings)
46
- project_node = xml.at_xpath("Project")
47
- travesernode(project_node)
48
- #puts workspace_node
49
- #puts xml
50
- #mdkProvider.take(:title) # => 'The Monkey Wrench Gang'
51
- #mdkProvider.take(:author) # => 'Edward Abbey'
52
- #mdkProvider.take(:display_title) # => 'Edward Abbey - The Monkey Wrench Gang'
53
- #mdkProvider.take(:price) # => 9.99
54
- end
55
-
56
- def load_node(doc, xpath)
57
- return doc.xpath(xpath)
58
- end
59
-
60
- def new_target(target, doc, name = 'debug')
61
- nset = load_node(doc, "//Targets/Target")
62
- #use existing one
63
- nset.each do |element|
64
- if element.css("TargetName").text.downcase == target.downcase
65
- puts "find existing #{element.css("/TargetName").text.downcase}"
66
- return element
67
- end
68
- end
69
- #create new one
70
- nset.each do |element|
71
- #use the first available configuration
72
- @logger.info "add target #{target}"
73
- t = element.dup
74
- t.at_css("TargetName").content = target
75
- #doc.xpath("/project") << t
76
- element.add_previous_sibling(t)
77
- return t
78
- end
79
- nil
80
- end
81
-
82
- # remove_targets remove unused targets
83
- # Params:
84
- # - doc: the xml node project file
85
- # - targets_in: used target array (will keep)
86
- def remove_targets(doc, targets_in)
87
- #remove the target that not in the targets_in
88
- nset = load_node(doc, "//Targets/Target")
89
- targets_in.collect{|x| x.downcase}
90
- nset.each do |element|
91
- target = element.xpath("TargetName").text.downcase
92
- if !targets_in.include?(target)
93
- element.remove
94
- end
95
- end
96
- end
97
-
98
- # remove_sources remove source files
99
- # Params:
100
- # - doc: the xml node project file
101
- def remove_sources(doc)
102
- groups = load_node(doc, "//Group")
103
- groups.each do |ele|
104
- ele.remove
105
- end
106
- files = load_node(doc, "//File")
107
- files.each do |ele|
108
- ele.remove
109
- end
110
- end
111
- # remove_sources remove unused node
112
- # Params:
113
- # - doc: the xml node project file
114
- # - xpath: xpath to the node
115
- # - **names: node attribute that need to remove defined in hash
116
- def remove_unused(doc, xpath, **names)
117
- nset = load_node(doc, xpath)
118
- nset.each do |element|
119
- names.each do |key, value|
120
- if element.xpath(key).text.downcase == value.downcase
121
- element.remove
122
- end
123
- end
124
- end
125
- end
126
-
127
- # create_node convert hash to xml and add to doc
128
- # Params:
129
- # - doc: the xml node project file
130
- # - hash_value: hash that defines the nodes structure
131
- def create_node(doc, hash_value)
132
- hash_value.to_xml!(doc)
133
- end
134
-
135
- # append_node convert hash to xml and append to doc
136
- # Params:
137
- # - doc: the xml node project file
138
- # - hash_value: hash that defines the nodes structure
139
- def append_node(doc, hash_value)
140
- hash_value.to_xml(doc)
141
- end
142
-
143
- # add_specific
144
- # Params:
145
- # - doc: hash to add to target
146
- # - target_node: node to be added to
147
- # - note:
148
- # can not add none exist node for mdk xml
149
- def add_specific(target_node, doc)
150
- doc.each do |key, value|
151
- options = target_node.xpath(key)
152
- options.each do |option|
153
- if value.class == Hash
154
- value.each do |subnode|
155
- add_specific(option, subnode)
156
- end
157
- elsif value.class == String
158
- option.content += ";#{value}"
159
- elsif value.class == Array
160
- value.each do |line|
161
- option.content += ";#{line}"
162
- end
163
- else
164
- puts "not support by set_specific #{value}"
165
- end
166
- end
167
- end
168
- end
169
- # set_specific
170
- # Params:
171
- # - doc: hash to add to target
172
- # - target_node: node to be added to
173
- # - note:
174
- # can not add none exist node for mdk xml
175
- def set_specific(target_node, doc)
176
- doc.each do |key, value|
177
- options = target_node.xpath(key)
178
- options.each do |option|
179
- if value.class == Hash
180
- value.each do |subnode|
181
- add_specific(option, subnode)
182
- end
183
- elsif value.class == String
184
- option.content = value
185
- break
186
- elsif value.class == Array
187
- option.content = ""
188
- value.each do |line|
189
- option.content += ";#{line}"
190
- end
191
- break
192
- else
193
- puts "not support by set_specific #{value}"
194
- end
195
- end
196
- end
197
- end
198
-
199
- def save(xml, path)
200
- Core.assert(path.is_a?(String)) do
201
- "param is not a string #{path.class.name}"
202
- end
203
- FileUtils.mkdir_p File.dirname(path) if ! File.exist?(File.dirname(path))
204
- File.write(path, xml.to_xml)
205
- end
206
-
207
- def add_sources(doc, source_hash, path_mod, proj_path)
208
- groups_existing = Array.new
209
- files_hash = Hash.new
210
- source_hash.each do |src|
211
- rootdir = src['rootdir']
212
- virtual_dir = src['virtual_dir'] if src.has_key? 'virtual_dir'
213
- virtual_dir = src['virtual-dir'] if src.has_key? 'virtual-dir'
214
- if src.has_key?('path')
215
- path = src['path']
216
- else
217
- path = src['source']
218
- end
219
- if virtual_dir
220
- if ! groups_existing.include?(virtual_dir)
221
- groups_existing.insert(-1, virtual_dir)
222
- node = Nokogiri::XML::Node.new 'Group', doc
223
- node << "<GroupName>#{virtual_dir}</GroupName>"
224
- doc << node
225
- end
226
- files_hash[virtual_dir] = Array.new if files_hash[virtual_dir].nil?
227
- files_hash[virtual_dir].insert(-1, {'path' => path, 'rootdir' => rootdir})
228
- else
229
- #create a common src group
230
- if ! groups_existing.include?("_src")
231
- groups_existing.insert(-1, "_src")
232
- node = Nokogiri::XML::Node.new 'Group', doc
233
- node << "<GroupName>_src</GroupName>"
234
- doc << node
235
- end
236
- files_hash["_src"] = Array.new if files_hash["_src"].nil?
237
- files_hash["_src"].insert(-1, {'path' => path, 'rootdir' => rootdir})
238
- end
239
- end #end source_hash
240
- doc.css("Group").each do |node|
241
- files_hash[node.at_css("GroupName").text].each do |file|
242
- gfiles = Nokogiri::XML::Node.new('Files', node)
243
- gfile = Nokogiri::XML::Node.new('File', gfiles)
244
- sfile = Nokogiri::XML::Node.new('FileName', gfiles)
245
- spfile = Nokogiri::XML::Node.new('FilePath', gfiles)
246
- if file['rootdir']
247
- full_path = path_mod.fullpath(file['rootdir'],file['path'])
248
- else
249
- full_path = path_mod.fullpath('default_path',file['path'])
250
- end
251
- sfile.content = File.basename(file['path'])
252
- spfile.content = File.join("$PROJ_DIR$", path_mod.relpath(proj_path, full_path))
253
- gfile << sfile
254
- gfile << spfile
255
- gfiles << gfile
256
- node << gfiles
257
- end
258
- end
259
- return if files_hash["_src"].nil?
260
- files_hash["_src"].each do |file|
261
- gfiles = Nokogiri::XML::Node.new('File', doc)
262
- gfile = Nokogiri::XML::Node.new('File', gfiles)
263
- sfile = Nokogiri::XML::Node.new('FileName', gfiles)
264
- spfile = Nokogiri::XML::Node.new('FilePath', gfiles)
265
- if file['rootdir']
266
- full_path = path_mod.fullpath(file['rootdir'],file['path'])
267
- else
268
- full_path = path_mod.fullpath('default_path',file['path'])
269
- end
270
- spfile.content = File.join("$PROJ_DIR$", path_mod.relpath(proj_path, full_path))
271
- sfile.content = File.basename(file['path'])
272
- gfile << sfile
273
- gfile << spfile
274
- gfiles << gfile
275
- node << gfiles
276
- end
277
- end
1
+ require 'nokogiri'
2
+ require 'pathname'
3
+
4
+ class Hash
5
+ def to_xml(doc)
6
+ return if doc.nil?
7
+ self.each do |key, value|
8
+ mynode = Nokogiri::XML::Node.new key, doc
9
+ doc.add_child mynode
10
+ value.to_xml(mynode) if value.class == Hash
11
+ mynode.content = value if value.class == String or value.class == Fixnum
12
+ end
13
+ return doc
14
+ end #end to_xml
15
+ def to_xml!(doc)
16
+ return if doc.nil?
17
+ self.each do |key, value|
18
+ if doc.css("/#{key}").count == 0
19
+ mynode = Nokogiri::XML::Node.new key, doc
20
+ else
21
+ mynode = doc.css("/#{key}")[0]
22
+ end
23
+ doc.add_child mynode
24
+ value.to_xml(mynode) if value.class == Hash
25
+ mynode.content = value if value.class == String or value.class == Fixnum
26
+ end
27
+ return doc
28
+ end #end to_xml!
29
+ end #end Hash
30
+
31
+ module UVPROJX
32
+ def travesernode(node)
33
+ if node.children
34
+ content = node.content
35
+ if @@data_provider.has_key?(content.strip)
36
+ node.content = @@data_provider[content.strip]
37
+ end
38
+ end
39
+ node.children.each do |subnode|
40
+ next if subnode.nil?
41
+ travesernode(subnode)
42
+ end
43
+
44
+ end
45
+ def init_project(xml, settings)
46
+ project_node = xml.at_xpath("Project")
47
+ travesernode(project_node)
48
+ #puts workspace_node
49
+ #puts xml
50
+ #mdkProvider.take(:title) # => 'The Monkey Wrench Gang'
51
+ #mdkProvider.take(:author) # => 'Edward Abbey'
52
+ #mdkProvider.take(:display_title) # => 'Edward Abbey - The Monkey Wrench Gang'
53
+ #mdkProvider.take(:price) # => 9.99
54
+ end
55
+
56
+ def load_node(doc, xpath)
57
+ return doc.xpath(xpath)
58
+ end
59
+
60
+ def new_target(target, doc, name = 'debug')
61
+ nset = load_node(doc, "//Targets/Target")
62
+ #use existing one
63
+ nset.each do |element|
64
+ if element.css("TargetName").text.downcase == target.downcase
65
+ puts "find existing #{element.css("/TargetName").text.downcase}"
66
+ return element
67
+ end
68
+ end
69
+ #create new one
70
+ nset.each do |element|
71
+ #use the first available configuration
72
+ @logger.info "add target #{target}"
73
+ t = element.dup
74
+ t.at_css("TargetName").content = target
75
+ #doc.xpath("/project") << t
76
+ element.add_previous_sibling(t)
77
+ return t
78
+ end
79
+ nil
80
+ end
81
+
82
+ # remove_targets remove unused targets
83
+ # Params:
84
+ # - doc: the xml node project file
85
+ # - targets_in: used target array (will keep)
86
+ def remove_targets(doc, targets_in)
87
+ #remove the target that not in the targets_in
88
+ nset = load_node(doc, "//Targets/Target")
89
+ targets_in.collect{|x| x.downcase}
90
+ nset.each do |element|
91
+ target = element.xpath("TargetName").text.downcase
92
+ if !targets_in.include?(target)
93
+ element.remove
94
+ end
95
+ end
96
+ end
97
+
98
+ # remove_sources remove source files
99
+ # Params:
100
+ # - doc: the xml node project file
101
+ def remove_sources(doc)
102
+ groups = load_node(doc, "//Group")
103
+ groups.each do |ele|
104
+ ele.remove
105
+ end
106
+ files = load_node(doc, "//File")
107
+ files.each do |ele|
108
+ ele.remove
109
+ end
110
+ end
111
+ # remove_sources remove unused node
112
+ # Params:
113
+ # - doc: the xml node project file
114
+ # - xpath: xpath to the node
115
+ # - **names: node attribute that need to remove defined in hash
116
+ def remove_unused(doc, xpath, **names)
117
+ nset = load_node(doc, xpath)
118
+ nset.each do |element|
119
+ names.each do |key, value|
120
+ if element.xpath(key).text.downcase == value.downcase
121
+ element.remove
122
+ end
123
+ end
124
+ end
125
+ end
126
+
127
+ # create_node convert hash to xml and add to doc
128
+ # Params:
129
+ # - doc: the xml node project file
130
+ # - hash_value: hash that defines the nodes structure
131
+ def create_node(doc, hash_value)
132
+ hash_value.to_xml!(doc)
133
+ end
134
+
135
+ # append_node convert hash to xml and append to doc
136
+ # Params:
137
+ # - doc: the xml node project file
138
+ # - hash_value: hash that defines the nodes structure
139
+ def append_node(doc, hash_value)
140
+ hash_value.to_xml(doc)
141
+ end
142
+
143
+ # add_specific
144
+ # Params:
145
+ # - doc: hash to add to target
146
+ # - target_node: node to be added to
147
+ # - note:
148
+ # can not add none exist node for mdk xml
149
+ def add_specific(target_node, doc)
150
+ doc.each do |key, value|
151
+ options = target_node.xpath(key)
152
+ options.each do |option|
153
+ if value.class == Hash
154
+ value.each do |subnode|
155
+ add_specific(option, subnode)
156
+ end
157
+ elsif value.class == String
158
+ option.content += ";#{value}"
159
+ elsif value.class == Array
160
+ value.each do |line|
161
+ option.content += ";#{line}"
162
+ end
163
+ else
164
+ puts "not support by set_specific #{value}"
165
+ end
166
+ end
167
+ end
168
+ end
169
+ # set_specific
170
+ # Params:
171
+ # - doc: hash to add to target
172
+ # - target_node: node to be added to
173
+ # - note:
174
+ # can not add none exist node for mdk xml
175
+ def set_specific(target_node, doc)
176
+ doc.each do |key, value|
177
+ options = target_node.xpath(key)
178
+ options.each do |option|
179
+ if value.class == Hash
180
+ value.each do |subnode|
181
+ add_specific(option, subnode)
182
+ end
183
+ elsif value.class == String
184
+ option.content = value
185
+ break
186
+ elsif value.class == Array
187
+ option.content = ""
188
+ value.each do |line|
189
+ option.content += ";#{line}"
190
+ end
191
+ break
192
+ else
193
+ puts "not support by set_specific #{value}"
194
+ end
195
+ end
196
+ end
197
+ end
198
+
199
+ def save(xml, path)
200
+ Core.assert(path.is_a?(String)) do
201
+ "param is not a string #{path.class.name}"
202
+ end
203
+ FileUtils.mkdir_p File.dirname(path) if ! File.exist?(File.dirname(path))
204
+ File.write(path, xml.to_xml)
205
+ end
206
+
207
+ def add_sources(doc, source_hash, path_mod, proj_path)
208
+ groups_existing = Array.new
209
+ files_hash = Hash.new
210
+ source_hash.each do |src|
211
+ rootdir = src['rootdir']
212
+ virtual_dir = src['virtual_dir'] if src.has_key? 'virtual_dir'
213
+ virtual_dir = src['virtual-dir'] if src.has_key? 'virtual-dir'
214
+ if src.has_key?('path')
215
+ path = src['path']
216
+ else
217
+ path = src['source']
218
+ end
219
+ if virtual_dir
220
+ if ! groups_existing.include?(virtual_dir)
221
+ groups_existing.insert(-1, virtual_dir)
222
+ node = Nokogiri::XML::Node.new 'Group', doc
223
+ node << "<GroupName>#{virtual_dir}</GroupName>"
224
+ doc << node
225
+ end
226
+ files_hash[virtual_dir] = Array.new if files_hash[virtual_dir].nil?
227
+ files_hash[virtual_dir].insert(-1, {'path' => path, 'rootdir' => rootdir})
228
+ else
229
+ #create a common src group
230
+ if ! groups_existing.include?("_src")
231
+ groups_existing.insert(-1, "_src")
232
+ node = Nokogiri::XML::Node.new 'Group', doc
233
+ node << "<GroupName>_src</GroupName>"
234
+ doc << node
235
+ end
236
+ files_hash["_src"] = Array.new if files_hash["_src"].nil?
237
+ files_hash["_src"].insert(-1, {'path' => path, 'rootdir' => rootdir})
238
+ end
239
+ end #end source_hash
240
+ doc.css("Group").each do |node|
241
+ files_hash[node.at_css("GroupName").text].each do |file|
242
+ gfiles = Nokogiri::XML::Node.new('Files', node)
243
+ gfile = Nokogiri::XML::Node.new('File', gfiles)
244
+ sfile = Nokogiri::XML::Node.new('FileName', gfiles)
245
+ spfile = Nokogiri::XML::Node.new('FilePath', gfiles)
246
+ if file['rootdir']
247
+ full_path = path_mod.fullpath(file['rootdir'],file['path'])
248
+ else
249
+ full_path = path_mod.fullpath('default_path',file['path'])
250
+ end
251
+ sfile.content = File.basename(file['path'])
252
+ spfile.content = File.join("$PROJ_DIR$", path_mod.relpath(proj_path, full_path))
253
+ gfile << sfile
254
+ gfile << spfile
255
+ gfiles << gfile
256
+ node << gfiles
257
+ end
258
+ end
259
+ return if files_hash["_src"].nil?
260
+ files_hash["_src"].each do |file|
261
+ gfiles = Nokogiri::XML::Node.new('File', doc)
262
+ gfile = Nokogiri::XML::Node.new('File', gfiles)
263
+ sfile = Nokogiri::XML::Node.new('FileName', gfiles)
264
+ spfile = Nokogiri::XML::Node.new('FilePath', gfiles)
265
+ if file['rootdir']
266
+ full_path = path_mod.fullpath(file['rootdir'],file['path'])
267
+ else
268
+ full_path = path_mod.fullpath('default_path',file['path'])
269
+ end
270
+ spfile.content = File.join("$PROJ_DIR$", path_mod.relpath(proj_path, full_path))
271
+ sfile.content = File.basename(file['path'])
272
+ gfile << sfile
273
+ gfile << spfile
274
+ gfiles << gfile
275
+ node << gfiles
276
+ end
277
+ end
278
278
  end