587
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
package com.nokia.helium.sysdef.ant.taskdefs;
|
|
18 |
|
|
19 |
import java.io.File;
|
|
20 |
import java.io.FileOutputStream;
|
|
21 |
import java.io.IOException;
|
|
22 |
import java.io.OutputStreamWriter;
|
|
23 |
import java.io.StringWriter;
|
|
24 |
import java.util.ArrayList;
|
|
25 |
import java.util.HashMap;
|
|
26 |
import java.util.Hashtable;
|
|
27 |
import java.util.Iterator;
|
|
28 |
import java.util.List;
|
|
29 |
import java.util.Map;
|
|
30 |
|
|
31 |
import org.apache.tools.ant.BuildException;
|
|
32 |
import org.apache.tools.ant.Project;
|
|
33 |
import org.apache.tools.ant.Task;
|
|
34 |
import org.apache.tools.ant.types.Resource;
|
|
35 |
import org.apache.tools.ant.types.ResourceCollection;
|
|
36 |
|
628
|
37 |
import com.nokia.helium.sysdef.PackageDefinition;
|
|
38 |
import com.nokia.helium.sysdef.PackageDefinitionParsingException;
|
587
|
39 |
import com.nokia.helium.sysdef.PackageMap;
|
|
40 |
import com.nokia.helium.sysdef.PackageMapParsingException;
|
|
41 |
|
|
42 |
import freemarker.cache.ClassTemplateLoader;
|
|
43 |
import freemarker.cache.FileTemplateLoader;
|
|
44 |
import freemarker.template.Configuration;
|
|
45 |
import freemarker.template.Template;
|
|
46 |
import freemarker.template.TemplateException;
|
|
47 |
|
|
48 |
/**
|
|
49 |
* Create a root System Definition file based on a set of package_definition.xml
|
|
50 |
* file. All package must also have a package_data.xml file in order to retrieve
|
|
51 |
* the root and the layer location of the package.
|
|
52 |
*
|
|
53 |
* Example:
|
|
54 |
* <pre>
|
628
|
55 |
* <hlm:createRootSysdef epocroot="E:\sdk" destFile="E:\sdk\sysdef_root.xml" >
|
587
|
56 |
* <fileset dir="E:\sdk" includes="root/**/package_definition.xml">
|
628
|
57 |
* </hlm:createRootSysdef>
|
587
|
58 |
* </pre>
|
|
59 |
*
|
|
60 |
* @ant.task name="createRootSysdef" category="Sysdef"
|
|
61 |
*/
|
|
62 |
public class CreateRootSysdefTask extends Task {
|
|
63 |
|
|
64 |
private File destFile;
|
|
65 |
private List<ResourceCollection> resourceCollections = new ArrayList<ResourceCollection>();
|
628
|
66 |
private Map<String, Map<String, List<Map<String, Object>>>> roots = new HashMap<String, Map<String, List<Map<String, Object>>>>();
|
587
|
67 |
private Map<String, List<String>> layers = new HashMap<String, List<String>>();
|
|
68 |
private File epocroot;
|
|
69 |
private boolean failOnError = true;
|
588
|
70 |
private boolean checkPackageExists;
|
587
|
71 |
private File template;
|
628
|
72 |
private String idNamespace;
|
587
|
73 |
|
|
74 |
/**
|
|
75 |
* {@inheritDoc}
|
|
76 |
*/
|
|
77 |
@SuppressWarnings("unchecked")
|
|
78 |
public void execute() {
|
|
79 |
if (getEpocroot() == null) {
|
|
80 |
throw new BuildException("epocroot attribute has not been set.");
|
|
81 |
}
|
|
82 |
if (getDestFile() == null) {
|
|
83 |
throw new BuildException("destFile attribute has not been set.");
|
|
84 |
}
|
|
85 |
if ((File.separatorChar == '/' && !getDestFile().getAbsolutePath().startsWith(getEpocroot().getAbsolutePath()))
|
|
86 |
|| (File.separatorChar == '\\'
|
|
87 |
&& !getDestFile().getAbsolutePath().toLowerCase().startsWith(getEpocroot().getAbsolutePath().toLowerCase()))) {
|
|
88 |
throw new BuildException(getDestFile().getAbsolutePath() + " must be under " + getEpocroot().getAbsolutePath());
|
|
89 |
}
|
|
90 |
for (ResourceCollection rc : resourceCollections) {
|
|
91 |
Iterator<Resource> ri = (Iterator<Resource>)rc.iterator();
|
|
92 |
while (ri.hasNext()) {
|
588
|
93 |
Resource resource = ri.next();
|
|
94 |
File pkgDefFile = new File(resource.toString());
|
|
95 |
log("Package definition file: " + pkgDefFile);
|
587
|
96 |
if (!pkgDefFile.exists() ||
|
|
97 |
!(pkgDefFile.getName().equalsIgnoreCase(
|
|
98 |
CreatePackageMappingTask.PACKAGE_DEFINITION_FILENAME))) {
|
|
99 |
throw new BuildException("Missing Package Definition file");
|
|
100 |
}
|
588
|
101 |
File pkgDir = pkgDefFile.getParentFile();
|
587
|
102 |
File pkgMapFile = new File(pkgDir, CreatePackageMappingTask.PACKAGE_MAP_FILENAME);
|
|
103 |
try {
|
|
104 |
if (pkgMapFile.exists()) {
|
588
|
105 |
log("Package map file: " + pkgMapFile);
|
|
106 |
if (!checkPackageExists) {
|
628
|
107 |
addPackage(pkgDefFile, pkgMapFile, pkgDir.getName());
|
588
|
108 |
} else {
|
|
109 |
PackageMap pkgMap = new PackageMap(pkgMapFile);
|
|
110 |
File destPkg = new File(epocroot, pkgMap.getRoot() + File.separator +
|
|
111 |
pkgMap.getLayer() + File.separator + pkgDir.getName() + File.separator +
|
|
112 |
CreatePackageMappingTask.PACKAGE_DEFINITION_FILENAME);
|
|
113 |
if (destPkg.exists()) {
|
628
|
114 |
addPackage(pkgDefFile, pkgMapFile, pkgDir.getName());
|
588
|
115 |
} else {
|
|
116 |
log("Could not find " + destPkg.getAbsolutePath() +
|
|
117 |
" so entry is not added to the root system definition.", Project.MSG_ERR);
|
|
118 |
}
|
|
119 |
}
|
587
|
120 |
} else {
|
588
|
121 |
pkgMapFile = new File(pkgDir.getParentFile().getParentFile(), CreatePackageMappingTask.PACKAGE_MAP_FILENAME);
|
|
122 |
log("Package map file: " + pkgMapFile);
|
587
|
123 |
if (pkgMapFile.exists()) {
|
588
|
124 |
if (!checkPackageExists) {
|
628
|
125 |
// slash must be use to generate correct path.
|
|
126 |
addPackage(pkgDefFile, pkgMapFile, pkgMapFile.getParentFile().getName() + "/" +
|
|
127 |
pkgDir.getParentFile().getName() + "/" + pkgDir.getName());
|
588
|
128 |
} else {
|
|
129 |
PackageMap pkgMap = new PackageMap(pkgMapFile);
|
|
130 |
File destPkg = new File(epocroot, pkgMap.getRoot() + File.separator +
|
|
131 |
pkgMap.getLayer() + File.separator +
|
|
132 |
pkgMapFile.getParentFile().getName() + File.separator +
|
|
133 |
pkgDir.getParentFile().getName() + File.separator + pkgDir.getName() +
|
|
134 |
File.separator +
|
|
135 |
CreatePackageMappingTask.PACKAGE_DEFINITION_FILENAME);
|
|
136 |
if (destPkg.exists()) {
|
628
|
137 |
addPackage(destPkg, pkgMapFile, pkgMapFile.getParentFile().getName() + "/" +
|
|
138 |
pkgDir.getParentFile().getName() + "/" + pkgDir.getName());
|
588
|
139 |
} else {
|
|
140 |
log("Could not find " + destPkg.getAbsolutePath() +
|
|
141 |
" so entry is not added to the root system definition.", Project.MSG_ERR);
|
|
142 |
}
|
|
143 |
}
|
587
|
144 |
} else {
|
628
|
145 |
log("Could not find package_map.xml file for " + pkgDefFile.toString(), Project.MSG_ERR);
|
587
|
146 |
if (shouldFailOnError()) {
|
628
|
147 |
throw new BuildException("Could not find package_map.xml file for " + pkgDefFile.toString());
|
587
|
148 |
}
|
|
149 |
}
|
|
150 |
}
|
|
151 |
} catch (PackageMapParsingException e) {
|
|
152 |
log("Invalid " + CreatePackageMappingTask.PACKAGE_MAP_FILENAME
|
|
153 |
+ " file: " + pkgMapFile.toString()
|
|
154 |
+ "(" + e.getMessage() + ")", Project.MSG_ERR);
|
|
155 |
if (shouldFailOnError()) {
|
|
156 |
throw new BuildException(e.getMessage(), e);
|
|
157 |
}
|
628
|
158 |
} catch (PackageDefinitionParsingException e) {
|
|
159 |
log("Invalid " + CreatePackageMappingTask.PACKAGE_DEFINITION_FILENAME
|
|
160 |
+ "(" + e.getMessage() + ")", Project.MSG_ERR);
|
|
161 |
if (shouldFailOnError()) {
|
|
162 |
throw new BuildException(e.getMessage(), e);
|
|
163 |
}
|
587
|
164 |
}
|
|
165 |
|
|
166 |
}
|
|
167 |
}
|
|
168 |
generateRootSysdef();
|
|
169 |
}
|
|
170 |
|
628
|
171 |
private void addPackage(File pkgDefinition, File pkgMapFile, String pkgPath) throws
|
|
172 |
PackageMapParsingException, PackageDefinitionParsingException {
|
|
173 |
// Some quick validity checking.
|
|
174 |
PackageDefinition pkg = new PackageDefinition(pkgDefinition);
|
|
175 |
if (idNamespace == null && (pkg.getIdNamespace() != null && pkg.getIdNamespace().length() > 0)) {
|
|
176 |
idNamespace = pkg.getIdNamespace();
|
|
177 |
} else if (idNamespace != null && !idNamespace.equals(pkg.getIdNamespace())) {
|
|
178 |
log("Warning: " + pkgDefinition.toString() +
|
|
179 |
" namespace doesn't match the default one. ("
|
|
180 |
+ idNamespace + " != " + pkg.getIdNamespace(), Project.MSG_WARN);
|
|
181 |
}
|
|
182 |
|
|
183 |
// Adding the package in the structure.
|
588
|
184 |
log("Adding: " + pkgMapFile, Project.MSG_DEBUG);
|
587
|
185 |
PackageMap pkgMap = new PackageMap(pkgMapFile);
|
|
186 |
if (!roots.containsKey(pkgMap.getRoot())) {
|
628
|
187 |
roots.put(pkgMap.getRoot(), new HashMap<String, List<Map<String, Object>>>());
|
587
|
188 |
}
|
|
189 |
if (!roots.get(pkgMap.getRoot()).containsKey(pkgMap.getLayer())) {
|
628
|
190 |
roots.get(pkgMap.getRoot()).put(pkgMap.getLayer(), new ArrayList<Map<String, Object>>());
|
587
|
191 |
layers.put(pkgMap.getLayer(), new ArrayList<String>());
|
|
192 |
}
|
628
|
193 |
Map<String, Object> data = new HashMap<String, Object>();
|
|
194 |
data.put("path", pkgPath);
|
|
195 |
data.put("id", pkg.getId());
|
|
196 |
data.put("namespaces", pkg.getNamespaces());
|
|
197 |
roots.get(pkgMap.getRoot()).get(pkgMap.getLayer()).add(data);
|
587
|
198 |
layers.get(pkgMap.getLayer()).add(pkgPath);
|
|
199 |
}
|
|
200 |
|
|
201 |
/**
|
|
202 |
* Defines the location of the generated INI file.
|
|
203 |
* @param destFile
|
|
204 |
* @ant.required
|
|
205 |
*/
|
|
206 |
public void setDestFile(File destFile) {
|
|
207 |
this.destFile = destFile;
|
|
208 |
}
|
|
209 |
|
|
210 |
/**
|
|
211 |
* Get the destination file.
|
|
212 |
* @return the dest file.
|
|
213 |
*/
|
|
214 |
public File getDestFile() {
|
|
215 |
return destFile;
|
|
216 |
}
|
|
217 |
|
|
218 |
/**
|
|
219 |
* Get epocroot.
|
|
220 |
* @return epocroot location
|
|
221 |
*/
|
|
222 |
public File getEpocroot() {
|
|
223 |
return epocroot;
|
|
224 |
}
|
|
225 |
|
|
226 |
/**
|
|
227 |
* Defines epocroot location
|
|
228 |
* @param epocroot
|
|
229 |
* @ant.required
|
|
230 |
*/
|
|
231 |
public void setEpocroot(File epocroot) {
|
|
232 |
this.epocroot = epocroot;
|
|
233 |
}
|
|
234 |
|
|
235 |
/**
|
|
236 |
* Defines if the task should fail in case of error.
|
|
237 |
* @param failOnError
|
|
238 |
* @ant.not-required Default true
|
|
239 |
*/
|
|
240 |
public void setFailOnError(boolean failOnError) {
|
|
241 |
this.failOnError = failOnError;
|
|
242 |
}
|
|
243 |
|
|
244 |
/**
|
|
245 |
* Shall we fail in case of issue.
|
|
246 |
* @return
|
|
247 |
*/
|
|
248 |
public boolean shouldFailOnError() {
|
|
249 |
return failOnError;
|
|
250 |
}
|
588
|
251 |
|
|
252 |
/**
|
|
253 |
* Defines if the task should check the existence of the
|
|
254 |
* target package_definition.xml under epocroot.
|
|
255 |
* @param checkPackageExists
|
|
256 |
* @ant.not-required Default false
|
|
257 |
*/
|
|
258 |
public void setCheckPackageExists(boolean checkPackageExists) {
|
|
259 |
this.checkPackageExists = checkPackageExists;
|
|
260 |
}
|
587
|
261 |
|
|
262 |
/**
|
|
263 |
* Support of nested resource collection like path or fileset.
|
|
264 |
* Those nested element should define where to find the
|
|
265 |
* packages.
|
|
266 |
* @param resourceCollection
|
|
267 |
*/
|
|
268 |
public void add(ResourceCollection resourceCollection) {
|
|
269 |
resourceCollections.add(resourceCollection);
|
|
270 |
}
|
|
271 |
|
|
272 |
/**
|
|
273 |
* Defines a custom Freemarker template to generate the root sysdef file.
|
|
274 |
* @param template
|
|
275 |
* @ant.not-required
|
|
276 |
*/
|
|
277 |
public void setTemplate(File template) {
|
|
278 |
this.template = template;
|
|
279 |
}
|
|
280 |
|
|
281 |
/**
|
|
282 |
* Get the template location.
|
|
283 |
* @return
|
|
284 |
*/
|
|
285 |
public File getTemplate() {
|
|
286 |
return template;
|
|
287 |
}
|
|
288 |
|
|
289 |
/**
|
|
290 |
* Generate the a root sysdef file based on the discovered package.
|
|
291 |
*/
|
|
292 |
protected void generateRootSysdef() {
|
|
293 |
try {
|
|
294 |
Configuration cfg = new Configuration();
|
|
295 |
Template template = null;
|
|
296 |
// Use custom template of default one?
|
|
297 |
if (getTemplate() != null) {
|
|
298 |
log("Loading template: " + this.getTemplate().getAbsolutePath());
|
|
299 |
cfg.setTemplateLoader(new FileTemplateLoader(this.getTemplate().getParentFile()));
|
|
300 |
template = cfg.getTemplate(this.getTemplate().getName());
|
|
301 |
} else {
|
|
302 |
cfg.setTemplateLoader(new ClassTemplateLoader(this.getClass(), "/com/nokia/helium/sysdef/templates"));
|
|
303 |
template = cfg.getTemplate("root_sysdef_model.xml.ftl");
|
|
304 |
}
|
|
305 |
Map<String, Object> data = new Hashtable<String, Object>();
|
|
306 |
// Content by root
|
|
307 |
data.put("roots", roots);
|
|
308 |
// Content by layer
|
|
309 |
data.put("layers", layers);
|
628
|
310 |
// id-namespace
|
|
311 |
if (idNamespace != null) {
|
|
312 |
data.put("idnamespace", idNamespace);
|
|
313 |
}
|
587
|
314 |
// Environment location
|
|
315 |
data.put("epocroot", getEpocroot().getAbsolutePath());
|
|
316 |
// Relative path from destFile to epocroot.
|
|
317 |
data.put("dest_dir_to_epocroot", getRelativeDiff());
|
|
318 |
StringWriter out = new StringWriter();
|
|
319 |
template.process(data, out);
|
|
320 |
|
|
321 |
// Writing the output.
|
|
322 |
log("Creating " + getDestFile().getAbsolutePath());
|
|
323 |
OutputStreamWriter output = new OutputStreamWriter(new FileOutputStream(getDestFile()));
|
|
324 |
output.append(out.getBuffer().toString());
|
|
325 |
output.close();
|
|
326 |
} catch (IOException e) {
|
|
327 |
log("Error while creating output file: " + e.getMessage(), Project.MSG_ERR);
|
|
328 |
if (this.shouldFailOnError()) {
|
|
329 |
throw new BuildException("Error while creating output file: " + e.getMessage(), e);
|
|
330 |
}
|
|
331 |
} catch (TemplateException e) {
|
|
332 |
log("Error while creating output file: " + e.getMessage(), Project.MSG_ERR);
|
|
333 |
if (this.shouldFailOnError()) {
|
|
334 |
throw new BuildException("Error while creating output file: " + e.getMessage(), e);
|
|
335 |
}
|
|
336 |
}
|
|
337 |
}
|
|
338 |
|
|
339 |
/**
|
|
340 |
* Get the relative path to go to epocroot from destdir.
|
|
341 |
* @return the path relative path to go to root.
|
|
342 |
*/
|
|
343 |
protected String getRelativeDiff() {
|
|
344 |
String rel = getEpocroot().toURI().relativize(getDestFile().getParentFile().toURI()).getPath();
|
628
|
345 |
if (rel.length() > 0) {
|
|
346 |
String[] relArray = rel.split("/"); // This is an URI not a File.
|
|
347 |
rel = ""; //"." + File.separatorChar;
|
|
348 |
for (@SuppressWarnings("unused") String string : relArray) {
|
|
349 |
rel += ".." + File.separatorChar;
|
|
350 |
}
|
|
351 |
} else {
|
|
352 |
rel = "." + File.separatorChar;
|
587
|
353 |
}
|
|
354 |
return rel;
|
|
355 |
}
|
|
356 |
}
|