KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > build > packager > PackagingConfigScriptGenerator


1 /**********************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM - Initial API and implementation
10  **********************************************************************/

11 package org.eclipse.pde.internal.build.packager;
12
13 import java.io.*;
14 import java.util.*;
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.pde.internal.build.*;
17 import org.eclipse.pde.internal.build.ant.*;
18
19 public class PackagingConfigScriptGenerator extends AssembleConfigScriptGenerator {
20     private Properties packagingProperties;
21     private String JavaDoc[] rootFiles;
22     private String JavaDoc[] rootDirs;
23     private String JavaDoc output;
24
25     public void generate() throws CoreException {
26         generatePrologue();
27         generateMainTarget();
28         generateAssembleTarget();
29         generateEpilogue();
30     }
31
32     private void generatePrologue() {
33         script.printProjectDeclaration("Package " + featureId, TARGET_MAIN, null); //$NON-NLS-1$
34
script.printProperty(PROPERTY_ARCHIVE_NAME, computeArchiveName());
35     }
36
37     private void generateMainTarget() {
38         script.printTargetDeclaration(TARGET_MAIN, null, null, null, null);
39
40         if (BundleHelper.getDefault().isDebugging()) {
41             script.printEchoTask(PROPERTY_BASEDIR + ": " + getPropertyFormat(PROPERTY_BASEDIR)); //$NON-NLS-1$
42
script.printEchoTask("tmpDir: " + getPropertyFormat("tempDirectory")); //$NON-NLS-1$//$NON-NLS-2$
43
script.printEchoTask(PROPERTY_COLLECTING_FOLDER + ": " + getPropertyFormat(PROPERTY_COLLECTING_FOLDER)); //$NON-NLS-1$
44
script.printEchoTask(PROPERTY_ARCHIVE_PREFIX + ": " + getPropertyFormat(PROPERTY_ARCHIVE_PREFIX)); //$NON-NLS-1$
45
script.printEchoTask(PROPERTY_ECLIPSE_BASE + ": " + getPropertyFormat(PROPERTY_ECLIPSE_BASE)); //$NON-NLS-1$
46
script.printEchoTask(PROPERTY_ASSEMBLY_TMP + ": " + getPropertyFormat(PROPERTY_ASSEMBLY_TMP)); //$NON-NLS-1$
47
script.printEchoTask(PROPERTY_DESTINATION_TEMP_FOLDER + ": " + getPropertyFormat(PROPERTY_DESTINATION_TEMP_FOLDER)); //$NON-NLS-1$
48
}
49         script.println("<condition property=\"" + PROPERTY_PLUGIN_ARCHIVE_PREFIX + "\" value=\"" + DEFAULT_PLUGIN_LOCATION + "\">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
50
script.println("\t<equals arg1=\"" + getPropertyFormat(PROPERTY_ARCHIVE_PREFIX) + "\" arg2=\"\" trim=\"true\"/>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
51
script.println("</condition>"); //$NON-NLS-1$
52
script.printProperty(PROPERTY_PLUGIN_ARCHIVE_PREFIX, getPropertyFormat(PROPERTY_ARCHIVE_PREFIX) + '/' + DEFAULT_PLUGIN_LOCATION);
53
54         script.println();
55         script.println("<condition property=\"" + PROPERTY_FEATURE_ARCHIVE_PREFIX + "\" value=\"" + DEFAULT_FEATURE_LOCATION + "\">"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
56
script.println("\t<equals arg1=\"" + getPropertyFormat(PROPERTY_ARCHIVE_PREFIX) + "\" arg2=\"\" trim=\"true\"/>"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
57
script.println("</condition>"); //$NON-NLS-1$
58
script.printProperty(PROPERTY_FEATURE_ARCHIVE_PREFIX, getPropertyFormat(PROPERTY_ARCHIVE_PREFIX) + '/' + DEFAULT_FEATURE_LOCATION);
59
60         Map parameters = new HashMap(1);
61         parameters.put("assembleScriptName", filename); //$NON-NLS-1$
62
//TODO Improve the name handling
63
script.printAntTask(getPropertyFormat(DEFAULT_CUSTOM_TARGETS), null, "assemble." + configInfo.toStringReplacingAny(".", ANY_STRING) + ".xml", null, null, parameters); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
64
script.printTargetEnd();
65     }
66
67     private void generateAssembleTarget() throws CoreException {
68         script.printTargetDeclaration("assemble", null, null, null, null); //$NON-NLS-1$
69
if (output.equalsIgnoreCase("tarGz")) { //$NON-NLS-1$
70
generateAntTarTarget();
71         } else if (output.equalsIgnoreCase("antZip")) { //$NON-NLS-1$
72
generateAntZipTarget();
73         } else if (output.equalsIgnoreCase("folder")) { //$NON-NLS-1$
74
generateFolderTarget();
75         } else { //By default use zip.exe
76
generateZipRootFiles();
77             generateZip();
78             List args = new ArrayList(2);
79             args.add("-R"); //$NON-NLS-1$
80
args.add("700"); //$NON-NLS-1$
81
args.add("."); //$NON-NLS-1$
82
script.printExecTask("chmod", getPropertyFormat("tempDirectory") + '/' + getPropertyFormat(PROPERTY_COLLECTING_FOLDER), args, "Linux"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
83
script.printDeleteTask(getPropertyFormat("tempDirectory") + '/' + getPropertyFormat(PROPERTY_COLLECTING_FOLDER), "**/*", null); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
84
}
85         script.printTargetEnd();
86     }
87
88     private void generateZipRootFiles() {
89         String JavaDoc fileList = packagingProperties.getProperty("root", ""); //$NON-NLS-1$ //$NON-NLS-2$
90
if (!configInfo.equals(Config.genericConfig()))
91             fileList += (fileList.length() == 0 ? "" : ",") + packagingProperties.getProperty("root." + configInfo.toString("."), ""); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
92

93         String JavaDoc[] files = Utils.getArrayFromString(fileList, ","); //$NON-NLS-1$
94
List parameters = new ArrayList(1);
95         for (int i = 0; i < files.length; i++) {
96             String JavaDoc file = files[i];
97             if (file.startsWith("file:")) { //$NON-NLS-1$
98
IPath target = new Path(file.substring(5));
99                 file = target.removeLastSegments(1).toOSString();
100             }
101             parameters.add(getPropertyFormat(PROPERTY_ARCHIVE_PREFIX) + '/' + file); //$NON-NLS-1$
102
createZipExecCommand(parameters);
103             parameters.clear();
104         }
105     }
106
107     private void generateZip() throws CoreException {
108         final int parameterSize = 15;
109         List parameters = new ArrayList(parameterSize + 1);
110         for (int i = 0; i < plugins.length; i++) {
111             parameters.add(getPropertyFormat(PROPERTY_PLUGIN_ARCHIVE_PREFIX) + '/' + plugins[i].getSymbolicName() + "_" + plugins[i].getVersion()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
112
if (i % parameterSize == 0) {
113                 createZipExecCommand(parameters);
114                 parameters.clear();
115             }
116         }
117         if (!parameters.isEmpty()) {
118             createZipExecCommand(parameters);
119             parameters.clear();
120         }
121
122         if (!parameters.isEmpty()) {
123             createZipExecCommand(parameters);
124             parameters.clear();
125         }
126
127         for (int i = 0; i < features.length; i++) {
128             parameters.add(getPropertyFormat(PROPERTY_FEATURE_ARCHIVE_PREFIX) + '/' + features[i].getVersionedIdentifier().toString()); //$NON-NLS-1$ //$NON-NLS-2$
129
if (i % parameterSize == 0) {
130                 createZipExecCommand(parameters);
131                 parameters.clear();
132             }
133         }
134         if (!parameters.isEmpty()) {
135             createZipExecCommand(parameters);
136             parameters.clear();
137         }
138     }
139
140     private void createZipExecCommand(List parameters) {
141         parameters.add(0, "-r -q " + getPropertyFormat(PROPERTY_ZIP_ARGS) + " " + getPropertyFormat(PROPERTY_ARCHIVE_NAME)); //$NON-NLS-1$ //$NON-NLS-2$
142
script.printExecTask("zip", getPropertyFormat("tempDirectory"), parameters, null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
143
}
144
145     private void generateEpilogue() {
146         script.printProjectEnd();
147         script.close();
148     }
149
150     public void setPackagingPropertiesLocation(String JavaDoc packagingPropertiesLocation) throws CoreException {
151         packagingProperties = new Properties();
152         if (packagingPropertiesLocation == null || packagingPropertiesLocation.equals("")) //$NON-NLS-1$
153
return;
154
155         InputStream propertyStream = null;
156         try {
157             propertyStream = new BufferedInputStream(new FileInputStream(packagingPropertiesLocation));
158             try {
159                 packagingProperties.load(new BufferedInputStream(propertyStream));
160             } finally {
161                 propertyStream.close();
162             }
163         } catch (FileNotFoundException e) {
164             String JavaDoc message = Policy.bind("exception.readingFile", packagingPropertiesLocation); //$NON-NLS-1$
165
throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_READING_FILE, message, e));
166         } catch (IOException e) {
167             String JavaDoc message = Policy.bind("exception.readingFile", packagingPropertiesLocation); //$NON-NLS-1$
168
throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_READING_FILE, message, e));
169         }
170     }
171
172     private boolean isFolder(Path pluginLocation) {
173         return pluginLocation.toFile().isDirectory();
174     }
175
176     private void generateAntTarTarget() {
177         int index = 0;
178         FileSet[] files = new FileSet[plugins.length + features.length + rootFiles.length + rootDirs.length];
179         if (files.length == 0)
180             return;
181
182         for (int i = 0; i < plugins.length; i++) {
183             Path pluginLocation = new Path(plugins[i].getLocation());
184             boolean isFolder = isFolder(pluginLocation);
185             files[index++] = new TarFileSet(pluginLocation.toOSString(), !isFolder, null, null, null, null, null, getPropertyFormat(PROPERTY_PLUGIN_ARCHIVE_PREFIX) + '/' + pluginLocation.lastSegment(), null);
186         }
187
188         for (int i = 0; i < features.length; i++) {
189             IPath featureLocation = new Path(features[i].getURL().getPath()); // Here we assume that all the features are local
190
featureLocation = featureLocation.removeLastSegments(1);
191             files[index++] = new TarFileSet(featureLocation.toOSString(), false, null, null, null, null, null, getPropertyFormat(PROPERTY_FEATURE_ARCHIVE_PREFIX) + '/' + featureLocation.lastSegment(), null);
192         }
193
194         if (rootFileProviders.size() == 0) {
195             FileSet[] filesCorrectSize = new FileSet[plugins.length + features.length];
196             System.arraycopy(files, 0, filesCorrectSize, 0, plugins.length + features.length);
197             script.printTarTask(getPropertyFormat(PROPERTY_ARCHIVE_FULLPATH), null, false, true, files);
198             return;
199         }
200
201         for (int i = 0; i < rootFiles.length; i++) {
202             IPath filePath = new Path(rootFiles[i]);
203             files[index++] = new TarFileSet(filePath.toOSString(), true, null, null, null, null, null, getPropertyFormat(PROPERTY_ARCHIVE_PREFIX) + '/' + filePath.lastSegment(), null);
204         }
205         for (int i = 0; i < rootDirs.length; i++) {
206             IPath dirPath = new Path(rootDirs[i]);
207             files[index++] = new TarFileSet(dirPath.toOSString(), false, null, null, null, null, null, getPropertyFormat(PROPERTY_ARCHIVE_PREFIX) + '/' + dirPath.lastSegment(), null);
208         }
209         script.printTarTask(getPropertyFormat(PROPERTY_ARCHIVE_FULLPATH), null, false, true, files);
210     }
211
212     private void generateAntZipTarget() {
213         int index = 0;
214         FileSet[] files = new FileSet[plugins.length + features.length + rootFiles.length + rootDirs.length];
215         if (files.length == 0)
216             return;
217
218         for (int i = 0; i < plugins.length; i++) {
219             Path pluginLocation = new Path(plugins[i].getLocation());
220             boolean isFolder = isFolder(pluginLocation);
221             files[index++] = new ZipFileSet(pluginLocation.toOSString(), !isFolder, null, null, null, null, null, getPropertyFormat(PROPERTY_PLUGIN_ARCHIVE_PREFIX) + '/' + pluginLocation.lastSegment(), null);
222         }
223
224         for (int i = 0; i < features.length; i++) {
225             IPath featureLocation = new Path(features[i].getURL().getPath()); // Here we assume that all the features are local
226
featureLocation = featureLocation.removeLastSegments(1);
227             files[index++] = new ZipFileSet(featureLocation.toOSString(), false, null, null, null, null, null, getPropertyFormat(PROPERTY_FEATURE_ARCHIVE_PREFIX) + '/' + featureLocation.lastSegment(), null);
228         }
229
230         if (rootFileProviders.size() == 0) {
231             FileSet[] filesCorrectSize = new FileSet[plugins.length + features.length];
232             System.arraycopy(files, 0, filesCorrectSize, 0, plugins.length + features.length);
233             script.printTarTask(getPropertyFormat(PROPERTY_ARCHIVE_FULLPATH), null, false, true, files);
234             return;
235         }
236
237         for (int i = 0; i < rootFiles.length; i++) {
238             IPath filePath = new Path(rootFiles[i]);
239             files[index++] = new ZipFileSet(filePath.toOSString(), true, null, null, null, null, null, getPropertyFormat(PROPERTY_ARCHIVE_PREFIX) + '/' + filePath.lastSegment(), null);
240         }
241         for (int i = 0; i < rootDirs.length; i++) {
242             IPath dirPath = new Path(rootDirs[i]);
243             files[index++] = new ZipFileSet(dirPath.toOSString(), false, null, null, null, null, null, getPropertyFormat(PROPERTY_ARCHIVE_PREFIX) + '/' + dirPath.lastSegment(), null);
244         }
245         script.printZipTask(getPropertyFormat(PROPERTY_ARCHIVE_FULLPATH), null, false, true, files);
246     }
247
248     private void generateFolderTarget() {
249         for (int i = 0; i < plugins.length; i++) {
250             Path pluginLocation = new Path(plugins[i].getLocation());
251             boolean isFolder = isFolder(pluginLocation);
252             if (isFolder) {
253                 script.printCopyTask(null, getPropertyFormat(PROPERTY_ASSEMBLY_TMP) + '/' + getPropertyFormat(PROPERTY_PLUGIN_ARCHIVE_PREFIX) + '/' + pluginLocation.lastSegment(), new FileSet[] {new FileSet(pluginLocation.toOSString(), null, null, null, null, null, null)}, false);
254             } else {
255                 script.printCopyTask(pluginLocation.toOSString(), getPropertyFormat(PROPERTY_ASSEMBLY_TMP) + '/' + getPropertyFormat(PROPERTY_PLUGIN_ARCHIVE_PREFIX) + '/' + pluginLocation.lastSegment(), null, false);
256             }
257         }
258
259         for (int i = 0; i < features.length; i++) {
260             IPath featureLocation = new Path(features[i].getURL().getPath()); // Here we assume that all the features are local
261
featureLocation = featureLocation.removeLastSegments(1);
262             script.printCopyTask(null, getPropertyFormat(PROPERTY_ASSEMBLY_TMP) + '/' + getPropertyFormat(PROPERTY_FEATURE_ARCHIVE_PREFIX) + '/' + featureLocation.lastSegment(), new FileSet[] {new FileSet(featureLocation.toOSString(), null, null, null, null, null, null)}, false);
263         }
264
265         for (int i = 0; i < rootFiles.length; i++) {
266             IPath filePath = new Path(rootFiles[i]);
267             script.printCopyTask(filePath.toOSString(), getPropertyFormat(PROPERTY_ASSEMBLY_TMP) + '/' + getPropertyFormat(PROPERTY_ARCHIVE_PREFIX), null, false);
268         }
269
270         for (int i = 0; i < rootDirs.length; i++) {
271             IPath dirPath = new Path(rootDirs[i]);
272             script.printCopyTask(null, getPropertyFormat(PROPERTY_ASSEMBLY_TMP) + '/' + getPropertyFormat(PROPERTY_ARCHIVE_PREFIX) + '/' + dirPath.lastSegment(), new FileSet[] {new FileSet(dirPath.toOSString(), null, null, null, null, null, null)}, false);
273         }
274     }
275
276     public void rootFiles(String JavaDoc[] rootFiles) {
277         this.rootFiles = rootFiles;
278     }
279
280     public void rootDirs(String JavaDoc[] rootDirs) {
281         this.rootDirs = rootDirs;
282     }
283
284     public void setOutput(String JavaDoc outputFormat) {
285         this.output = outputFormat;
286     }
287 }
Popular Tags