1 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 [] rootFiles; 22 private String [] rootDirs; 23 private String 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); 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)); script.printEchoTask("tmpDir: " + getPropertyFormat("tempDirectory")); script.printEchoTask(PROPERTY_COLLECTING_FOLDER + ": " + getPropertyFormat(PROPERTY_COLLECTING_FOLDER)); script.printEchoTask(PROPERTY_ARCHIVE_PREFIX + ": " + getPropertyFormat(PROPERTY_ARCHIVE_PREFIX)); script.printEchoTask(PROPERTY_ECLIPSE_BASE + ": " + getPropertyFormat(PROPERTY_ECLIPSE_BASE)); script.printEchoTask(PROPERTY_ASSEMBLY_TMP + ": " + getPropertyFormat(PROPERTY_ASSEMBLY_TMP)); script.printEchoTask(PROPERTY_DESTINATION_TEMP_FOLDER + ": " + getPropertyFormat(PROPERTY_DESTINATION_TEMP_FOLDER)); } 49 script.println("<condition property=\"" + PROPERTY_PLUGIN_ARCHIVE_PREFIX + "\" value=\"" + DEFAULT_PLUGIN_LOCATION + "\">"); script.println("\t<equals arg1=\"" + getPropertyFormat(PROPERTY_ARCHIVE_PREFIX) + "\" arg2=\"\" trim=\"true\"/>"); script.println("</condition>"); 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 + "\">"); script.println("\t<equals arg1=\"" + getPropertyFormat(PROPERTY_ARCHIVE_PREFIX) + "\" arg2=\"\" trim=\"true\"/>"); script.println("</condition>"); 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); script.printAntTask(getPropertyFormat(DEFAULT_CUSTOM_TARGETS), null, "assemble." + configInfo.toStringReplacingAny(".", ANY_STRING) + ".xml", null, null, parameters); script.printTargetEnd(); 65 } 66 67 private void generateAssembleTarget() throws CoreException { 68 script.printTargetDeclaration("assemble", null, null, null, null); if (output.equalsIgnoreCase("tarGz")) { generateAntTarTarget(); 71 } else if (output.equalsIgnoreCase("antZip")) { generateAntZipTarget(); 73 } else if (output.equalsIgnoreCase("folder")) { generateFolderTarget(); 75 } else { generateZipRootFiles(); 77 generateZip(); 78 List args = new ArrayList(2); 79 args.add("-R"); args.add("700"); args.add("."); script.printExecTask("chmod", getPropertyFormat("tempDirectory") + '/' + getPropertyFormat(PROPERTY_COLLECTING_FOLDER), args, "Linux"); script.printDeleteTask(getPropertyFormat("tempDirectory") + '/' + getPropertyFormat(PROPERTY_COLLECTING_FOLDER), "**/*", null); } 85 script.printTargetEnd(); 86 } 87 88 private void generateZipRootFiles() { 89 String fileList = packagingProperties.getProperty("root", ""); if (!configInfo.equals(Config.genericConfig())) 91 fileList += (fileList.length() == 0 ? "" : ",") + packagingProperties.getProperty("root." + configInfo.toString("."), ""); 93 String [] files = Utils.getArrayFromString(fileList, ","); List parameters = new ArrayList(1); 95 for (int i = 0; i < files.length; i++) { 96 String file = files[i]; 97 if (file.startsWith("file:")) { IPath target = new Path(file.substring(5)); 99 file = target.removeLastSegments(1).toOSString(); 100 } 101 parameters.add(getPropertyFormat(PROPERTY_ARCHIVE_PREFIX) + '/' + file); 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()); 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()); 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)); script.printExecTask("zip", getPropertyFormat("tempDirectory"), parameters, null); } 144 145 private void generateEpilogue() { 146 script.printProjectEnd(); 147 script.close(); 148 } 149 150 public void setPackagingPropertiesLocation(String packagingPropertiesLocation) throws CoreException { 151 packagingProperties = new Properties(); 152 if (packagingPropertiesLocation == null || packagingPropertiesLocation.equals("")) 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 message = Policy.bind("exception.readingFile", packagingPropertiesLocation); throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_READING_FILE, message, e)); 166 } catch (IOException e) { 167 String message = Policy.bind("exception.readingFile", packagingPropertiesLocation); 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()); 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()); 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()); 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 [] rootFiles) { 277 this.rootFiles = rootFiles; 278 } 279 280 public void rootDirs(String [] rootDirs) { 281 this.rootDirs = rootDirs; 282 } 283 284 public void setOutput(String outputFormat) { 285 this.output = outputFormat; 286 } 287 } | Popular Tags |