ebngen 1.0.4 → 1.2.0

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