1 11 package org.eclipse.pde.internal.build.tasks; 12 13 import java.io.*; 14 import java.util.*; 15 import org.apache.tools.ant.BuildException; 16 import org.apache.tools.ant.Task; 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.osgi.util.NLS; 19 import org.eclipse.pde.internal.build.*; 20 import org.eclipse.update.core.Feature; 21 import org.eclipse.update.core.IPluginEntry; 22 import org.eclipse.update.internal.core.FeatureExecutableFactory; 23 24 28 public class BuildManifestTask extends Task implements IPDEBuildConstants, IXMLConstants { 29 private String buildId; 30 protected String buildName; 31 private String buildQualifier; 32 private String buildType; 33 protected boolean children = true; 34 protected String destination; 35 protected Properties directory; 36 protected String directoryLocation; 37 protected String [] elements; 38 protected String installLocation; 39 40 43 public void execute() throws BuildException { 44 try { 45 if (this.elements == null) { 46 String message = TaskMessages.error_missingElement; 47 throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_ELEMENT_MISSING, message, null)); 48 } 49 readDirectory(); 50 PrintWriter output = new PrintWriter(new BufferedOutputStream(new FileOutputStream(destination))); 51 try { 52 List entries = new ArrayList(20); 53 for (int i = 0; i < elements.length; i++) 54 collectEntries(entries, elements[i]); 55 generatePrologue(output); 56 generateEntries(output, entries); 57 } finally { 58 output.close(); 59 } 60 } catch (Exception e) { 61 throw new BuildException(e); 62 } 63 } 64 65 69 protected void generatePrologue(PrintWriter output) { 70 output.print("# Build Manifest for "); output.println(buildName); 72 output.println(); 73 output.println("# The format of this file is:"); output.println("# <type>@<element>=<CVS tag>"); output.println(); 76 String id = getBuildId(); 77 if (id != null) { 78 output.print(PROPERTY_BUILD_ID + "="); output.println(id); 80 } 81 String type = getBuildType(); 82 if (type != null) { 83 output.print(PROPERTY_BUILD_TYPE + "="); output.println(type); 85 } 86 String qualifier = getBuildQualifier(); 87 if (qualifier != null) { 88 output.print(PROPERTY_BUILD_QUALIFIER + "="); output.println(qualifier); 90 } 91 output.println(); 92 } 93 94 98 protected String getBuildId() { 99 if (buildId == null) 100 buildId = getProject().getProperty(PROPERTY_BUILD_ID); 101 return buildId; 102 } 103 104 108 protected String getBuildQualifier() { 109 if (buildQualifier == null) 110 buildQualifier = getProject().getProperty(PROPERTY_BUILD_QUALIFIER); 111 return buildQualifier; 112 } 113 114 118 protected String getBuildType() { 119 if (buildType == null) 120 buildType = getProject().getProperty(PROPERTY_BUILD_TYPE); 121 return buildType; 122 } 123 124 129 protected void generateEntries(PrintWriter output, List entries) { 130 Collections.sort(entries); 131 for (Iterator iterator = entries.iterator(); iterator.hasNext();) { 132 String entry = (String ) iterator.next(); 133 output.println(entry); 134 } 135 } 136 137 140 protected void collectEntries(List entries, String entry) throws CoreException { 141 String cvsInfo = directory.getProperty(entry); 142 if (cvsInfo == null) { 143 String message = NLS.bind(TaskMessages.error_missingDirectoryEntry, entry); 144 throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_ENTRY_MISSING, message, null)); 145 } 146 147 int index = entry.indexOf('@'); 148 String type = entry.substring(0, index); 149 String element = entry.substring(index + 1); 150 if (type.equals("plugin") || type.equals("fragment")) { String [] cvsFields = Utils.getArrayFromString(cvsInfo); 152 String tag = cvsFields[0]; 153 StringBuffer sb = new StringBuffer (); 154 sb.append(entry); 155 sb.append("="); sb.append(tag); 157 entries.add(sb.toString()); 158 } else if (children && type.equals("feature")) { Feature feature = readFeature(element); 160 collectChildrenEntries(entries, feature); 161 } 162 } 163 164 170 protected void collectChildrenEntries(List entries, Feature feature) throws CoreException { 171 IPluginEntry[] pluginEntries = feature.getPluginEntries(); 172 for (int i = 0; i < pluginEntries.length; i++) { 173 String elementId = pluginEntries[i].getVersionedIdentifier().getIdentifier(); 174 if (pluginEntries[i].isFragment()) 175 collectEntries(entries, "fragment@" + elementId); else 177 collectEntries(entries, "plugin@" + elementId); } 179 } 180 181 187 protected Feature readFeature(String element) throws CoreException { 188 IPath root = new Path(installLocation); 189 root = root.append(DEFAULT_FEATURE_LOCATION); 190 root = root.append(element); 191 try { 192 FeatureExecutableFactory factory = new FeatureExecutableFactory(); 193 return (Feature) factory.createFeature(root.toFile().toURL(), null, null); 194 } catch (Exception e) { 195 String message = NLS.bind(TaskMessages.error_creatingFeature, element); 196 throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_FEATURE_MISSING, message, e)); 197 } 198 } 199 200 203 public void setInstall(String installLocation) { 204 this.installLocation = installLocation; 205 } 206 207 210 protected void readDirectory() throws CoreException { 211 try { 212 directory = new Properties(); 213 File file = new File(directoryLocation); 214 InputStream is = new BufferedInputStream(new FileInputStream(file)); 215 try { 216 directory.load(is); 217 } finally { 218 is.close(); 219 } 220 } catch (IOException e) { 221 String message = NLS.bind(TaskMessages.error_readingDirectory, directoryLocation); 222 throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_READ_DIRECTORY, message, e)); 223 } 224 } 225 226 230 public void setDirectory(String directory) { 231 directoryLocation = directory; 232 } 233 234 238 public void setElements(String value) { 239 elements = Utils.getArrayFromString(value); 240 } 241 242 245 public void setDestination(String value) { 246 destination = value; 247 } 248 249 252 public void setChildren(boolean children) { 253 this.children = children; 254 } 255 256 260 public void setBuildName(String value) { 261 buildName = value; 262 } 263 264 267 public void setBuildId(String buildId) { 268 this.buildId = buildId; 269 } 270 271 274 public void setBuildQualifier(String buildQualifier) { 275 this.buildQualifier = buildQualifier; 276 } 277 278 281 public void setBuildType(String buildType) { 282 this.buildType = buildType; 283 } 284 285 } 286 | Popular Tags |