1 12 package org.eclipse.pde.internal.build; 13 14 import java.io.*; 15 import java.util.*; 16 import org.eclipse.core.runtime.*; 17 import org.eclipse.osgi.service.resolver.BundleDescription; 18 import org.eclipse.osgi.util.NLS; 19 import org.eclipse.pde.internal.build.builder.*; 20 import org.eclipse.pde.internal.build.packager.PackageScriptGenerator; 21 import org.eclipse.pde.internal.build.site.BuildTimeSiteFactory; 22 import org.eclipse.update.core.*; 23 import org.osgi.framework.Version; 24 25 public class BuildScriptGenerator extends AbstractScriptGenerator { 26 30 protected boolean generateArchive = true; 31 34 protected boolean children = true; 35 36 39 protected boolean groupConfigs = false; 40 41 44 protected String [] elements; 45 46 49 protected DevClassPathHelper devEntries; 50 51 protected boolean recursiveGeneration = true; 52 protected boolean generateBuildScript = true; 53 protected boolean includePlatformIndependent = true; 54 protected boolean signJars = false; 55 protected boolean generateJnlp = false; 56 protected boolean generateFeatureVersionSuffix = false; 57 private String product; 58 private HashMap archivesFormat; 60 61 private String archivesFormatAsString; 62 63 66 private boolean generateAssembleScript = true; 67 68 69 private boolean ignoreMissingPropertiesFile = true; 70 71 72 private boolean generateVersionsList = false; 73 74 private Properties antProperties = null; 75 private BundleDescription[] bundlesToBuild; 76 77 private static final String PROPERTY_ARCHIVESFORMAT = "archivesFormat"; 79 83 public void generate() throws CoreException { 84 85 if (archivesFormatAsString != null) { 86 realSetArchivesFormat(archivesFormatAsString); 87 archivesFormatAsString = null; 88 } 89 90 List plugins = new ArrayList(5); 91 List features = new ArrayList(5); 92 try { 93 AbstractScriptGenerator.setStaticAntProperties(antProperties); 94 95 sortElements(features, plugins); 96 pluginsForFilterRoots = plugins; 97 featuresForFilterRoots = features; 98 getSite(true); 102 generateModels(plugins); 105 generateFeatures(features); 106 flushState(); 107 } finally { 108 AbstractScriptGenerator.setStaticAntProperties(null); 109 } 110 } 111 112 115 protected void sortElements(List features, List plugins) { 116 if (elements == null) 117 return; 118 for (int i = 0; i < elements.length; i++) { 119 int index = elements[i].indexOf('@'); 120 String type = elements[i].substring(0, index); 121 String element = elements[i].substring(index + 1); 122 if (type.equals("plugin") || type.equals("fragment")) plugins.add(element); 124 else if (type.equals("feature")) features.add(element); 126 } 127 } 128 129 134 protected void generateModels(List models) throws CoreException { 135 ModelBuildScriptGenerator generator = null; 136 try { 137 for (Iterator iterator = models.iterator(); iterator.hasNext();) { 138 generator = new ModelBuildScriptGenerator(); 139 generator.setReportResolutionErrors(reportResolutionErrors); 140 generator.setIgnoreMissingPropertiesFile(ignoreMissingPropertiesFile); 141 String [] modelInfo = getNameAndVersion((String ) iterator.next()); 143 generator.setBuildSiteFactory(siteFactory); 144 generator.setModelId(modelInfo[0], modelInfo[1]); 145 146 generator.setPluginPath(pluginPath); 147 generator.setDevEntries(devEntries); 148 generator.setCompiledElements(generator.getCompiledElements()); 149 generator.setBuildingOSGi(isBuildingOSGi()); 150 generator.setSignJars(signJars); 151 generator.generate(); 152 } 153 if (bundlesToBuild != null) 154 for (int i = 0; i < bundlesToBuild.length; i++) { 155 generator = new ModelBuildScriptGenerator(); 156 generator.setReportResolutionErrors(reportResolutionErrors); 157 generator.setIgnoreMissingPropertiesFile(ignoreMissingPropertiesFile); 158 generator.setBuildSiteFactory(siteFactory); 160 generator.setModel(bundlesToBuild[i]); 161 162 generator.setPluginPath(pluginPath); 163 generator.setDevEntries(devEntries); 164 generator.setCompiledElements(generator.getCompiledElements()); 165 generator.setBuildingOSGi(isBuildingOSGi()); 166 generator.setSignJars(signJars); 167 generator.generate(); 168 } 169 } finally { 170 if (generator != null) 171 generator.getSite(false).getRegistry().cleanupOriginalState(); 172 } 173 } 174 175 private String [] getNameAndVersion(String id) { 176 int versionPosition = id.indexOf(":"); String [] result = new String [2]; 178 if (versionPosition != -1) { 179 result[1] = id.substring(versionPosition + 1); 180 result[0] = id.substring(0, versionPosition); 181 } else 182 result[0] = id; 183 return result; 184 } 185 186 protected void generateFeatures(List features) throws CoreException { 187 AssemblyInformation assemblageInformation = null; 188 assemblageInformation = new AssemblyInformation(); 189 190 FeatureBuildScriptGenerator generator = null; 191 try { 192 for (Iterator i = features.iterator(); i.hasNext();) { 193 String [] featureInfo = getNameAndVersion((String ) i.next()); 194 generator = new FeatureBuildScriptGenerator(featureInfo[0], featureInfo[1], assemblageInformation); 195 generator.setGenerateIncludedFeatures(this.recursiveGeneration); 196 generator.setAnalyseChildren(this.children); 197 generator.setSourceFeatureGeneration(false); 198 generator.setBinaryFeatureGeneration(true); 199 generator.setScriptGeneration(generateBuildScript); 200 generator.setPluginPath(pluginPath); 201 generator.setBuildSiteFactory(siteFactory); 202 generator.setDevEntries(devEntries); 203 generator.setSourceToGather(new SourceFeatureInformation()); generator.setCompiledElements(generator.getCompiledElements()); 205 generator.setBuildingOSGi(isBuildingOSGi()); 206 generator.includePlatformIndependent(includePlatformIndependent); 207 generator.setReportResolutionErrors(reportResolutionErrors); 208 generator.setIgnoreMissingPropertiesFile(ignoreMissingPropertiesFile); 209 generator.setSignJars(signJars); 210 generator.setGenerateJnlp(generateJnlp); 211 generator.setGenerateVersionSuffix(generateFeatureVersionSuffix); 212 generator.setProduct(product); 213 generator.generate(); 214 } 215 216 if (generator != null && generateAssembleScript == true) { 217 String [] featureInfo = null; 218 if (features.size() == 1) 219 featureInfo = getNameAndVersion((String ) features.get(0)); 220 else 221 featureInfo = new String [] {"all"}; 223 generateAssembleScripts(assemblageInformation, featureInfo, generator.siteFactory); 224 225 if (features.size() == 1) 226 featureInfo = getNameAndVersion((String ) features.get(0)); 227 else 228 featureInfo = new String [] {""}; 230 generatePackageScripts(assemblageInformation, featureInfo, generator.siteFactory); 231 } 232 if (generateVersionsList) 233 generateVersionsLists(assemblageInformation); 234 } finally { 235 if (generator != null) 236 generator.getSite(false).getRegistry().cleanupOriginalState(); 237 } 238 } 239 240 protected void generateVersionsLists(AssemblyInformation assemblageInformation) throws CoreException { 241 if (assemblageInformation == null) 242 return; 243 List configs = getConfigInfos(); 244 Set features = new HashSet(); 245 Set plugins = new HashSet(); 246 Properties versions = new Properties(); 247 248 for (Iterator iter = configs.iterator(); iter.hasNext();) { 251 Config config = (Config) iter.next(); 252 String configString = config.toStringReplacingAny("_", ANY_STRING); 254 Collection list = assemblageInformation.getFeatures(config); 256 versions.clear(); 257 features.addAll(list); 258 String featureFile = DEFAULT_FEATURE_VERSION_FILENAME_PREFIX + '.' + configString + PROPERTIES_FILE_SUFFIX; 259 readVersions(versions, featureFile); 260 for (Iterator i = list.iterator(); i.hasNext();) { 261 IFeature feature = (IFeature) i.next(); 262 VersionedIdentifier id = feature.getVersionedIdentifier(); 263 recordVersion(id.getIdentifier(), new Version(id.getVersion().toString()), versions); 264 } 265 saveVersions(versions, featureFile); 266 267 list = assemblageInformation.getPlugins(config); 269 versions.clear(); 270 plugins.addAll(list); 271 String pluginFile = DEFAULT_PLUGIN_VERSION_FILENAME_PREFIX + '.' + configString + PROPERTIES_FILE_SUFFIX; 272 readVersions(versions, pluginFile); 273 for (Iterator i = list.iterator(); i.hasNext();) { 274 BundleDescription bundle = (BundleDescription) i.next(); 275 recordVersion(bundle.getSymbolicName(), bundle.getVersion(), versions); 276 } 277 saveVersions(versions, pluginFile); 278 } 279 280 versions.clear(); 282 String featureFile = DEFAULT_FEATURE_VERSION_FILENAME_PREFIX + PROPERTIES_FILE_SUFFIX; 283 readVersions(versions, featureFile); 284 for (Iterator i = features.iterator(); i.hasNext();) { 285 IFeature feature = (IFeature) i.next(); 286 VersionedIdentifier id = feature.getVersionedIdentifier(); 287 recordVersion(id.getIdentifier(), new Version(id.getVersion().toString()), versions); 288 } 289 saveVersions(versions, featureFile); 290 291 versions.clear(); 293 String pluginVersion = DEFAULT_PLUGIN_VERSION_FILENAME_PREFIX + PROPERTIES_FILE_SUFFIX; 294 readVersions(versions, pluginVersion); 295 for (Iterator i = plugins.iterator(); i.hasNext();) { 296 BundleDescription bundle = (BundleDescription) i.next(); 297 recordVersion(bundle.getSymbolicName(), bundle.getVersion(), versions); 298 } 299 saveVersions(versions, pluginVersion); 300 } 301 302 protected void recordVersion(String name, Version version, Properties properties) { 303 String versionString = version.toString(); 304 if (properties.containsKey(name)) { 305 Version existing = new Version((String ) properties.get(name)); 306 if (version.compareTo(existing) >= 0) { 307 properties.put(name, versionString); 308 } 309 } else { 310 properties.put(name, versionString); 311 } 312 String suffix = '_' + String.valueOf(version.getMajor()) + '.' + String.valueOf(version.getMinor()) + '.' + String.valueOf(version.getMicro()); 313 properties.put(name + suffix, versionString); 314 } 315 316 private String getFilePath(String fileName) { 317 return workingDirectory + '/' + fileName; 318 } 319 320 protected void readVersions(Properties properties, String fileName) { 321 String location = getFilePath(fileName); 322 try { 323 InputStream is = new BufferedInputStream(new FileInputStream(location)); 324 try { 325 properties.load(is); 326 } finally { 327 is.close(); 328 } 329 } catch (IOException e) { 330 } 332 } 333 334 protected void saveVersions(Properties properties, String fileName) throws CoreException { 335 String location = getFilePath(fileName); 336 try { 337 OutputStream os = new BufferedOutputStream(new FileOutputStream(location)); 338 try { 339 properties.store(os, null); 340 } finally { 341 os.close(); 342 } 343 } catch (IOException e) { 344 String message = NLS.bind(Messages.exception_writingFile, location); 345 throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_WRITING_FILE, message, null)); 346 } 347 } 348 349 protected void generatePackageScripts(AssemblyInformation assemblageInformation, String [] featureInfo, BuildTimeSiteFactory factory) throws CoreException { 350 PackageScriptGenerator assembler = null; 351 assembler = new PackageScriptGenerator(workingDirectory, assemblageInformation, featureInfo[0]); 352 assembler.setSignJars(signJars); 353 assembler.setGenerateJnlp(generateJnlp); 354 assembler.setArchivesFormat(getArchivesFormat()); 355 assembler.setProduct(product); 356 assembler.setBuildSiteFactory(factory); 357 assembler.setGroupConfigs(groupConfigs); 358 assembler.generate(); 359 } 360 361 private void generateAssembleScripts(AssemblyInformation assemblageInformation, String [] featureInfo, BuildTimeSiteFactory factory) throws CoreException { 362 AssembleScriptGenerator assembler = new AssembleScriptGenerator(workingDirectory, assemblageInformation, featureInfo[0]); 363 assembler.setSignJars(signJars); 364 assembler.setGenerateJnlp(generateJnlp); 365 assembler.setArchivesFormat(getArchivesFormat()); 366 assembler.setProduct(product); 367 assembler.setBuildSiteFactory(factory); 368 assembler.setGroupConfigs(groupConfigs); 369 assembler.generate(); 370 } 371 372 public void setGenerateArchive(boolean generateArchive) { 373 this.generateArchive = generateArchive; 374 } 375 376 380 public void setChildren(boolean children) { 381 this.children = children; 382 } 383 384 388 public void setDevEntries(String devEntries) { 389 if (devEntries != null) 390 this.devEntries = new DevClassPathHelper(devEntries); 391 } 392 393 397 public void setElements(String [] elements) { 398 this.elements = elements; 399 } 400 401 407 public void setRecursiveGeneration(boolean recursiveGeneration) { 408 this.recursiveGeneration = recursiveGeneration; 409 } 410 411 415 public void setGenerateAssembleScript(boolean generateAssembleScript) { 416 this.generateAssembleScript = generateAssembleScript; 417 } 418 419 423 public void setGenerateVersionsList(boolean generateVersionsList) { 424 this.generateVersionsList = generateVersionsList; 425 } 426 427 430 public void setReportResolutionErrors(boolean value) { 431 this.reportResolutionErrors = value; 432 } 433 434 437 public void setIgnoreMissingPropertiesFile(boolean value) { 438 ignoreMissingPropertiesFile = value; 439 } 440 441 public void setProduct(String value) { 442 product = value; 443 } 444 445 public void setSignJars(boolean value) { 446 signJars = value; 447 } 448 449 public void setGenerateJnlp(boolean value) { 450 generateJnlp = value; 451 } 452 453 public void setGenerateFeatureVersionSuffix(boolean value) { 454 generateFeatureVersionSuffix = value; 455 } 456 457 private class ArchiveTable extends HashMap { 458 private static final long serialVersionUID = -3063402400461435816L; 459 460 public ArchiveTable(int size) { 461 super(size); 462 } 463 464 public Object get(Object arg0) { 465 Object result = super.get(arg0); 466 if (result == null) 467 result = IXMLConstants.FORMAT_ANTZIP; 468 return result; 469 } 470 } 471 472 public void setArchivesFormat(String archivesFormatAsString) { 473 this.archivesFormatAsString = archivesFormatAsString; 474 } 475 476 public void realSetArchivesFormat(String archivesFormatAsString) throws CoreException { 477 if (Utils.getPropertyFormat(PROPERTY_ARCHIVESFORMAT).equalsIgnoreCase(archivesFormatAsString)) { 478 archivesFormat = new ArchiveTable(0); 479 return; 480 } 481 482 archivesFormat = new ArchiveTable(getConfigInfos().size()); 483 String [] configs = Utils.getArrayFromStringWithBlank(archivesFormatAsString, "&"); for (int i = 0; i < configs.length; i++) { 485 String [] configElements = Utils.getArrayFromStringWithBlank(configs[i], ","); if (configElements.length != 3) { 487 IStatus error = new Status(IStatus.ERROR, IPDEBuildConstants.PI_PDEBUILD, IPDEBuildConstants.EXCEPTION_CONFIG_FORMAT, NLS.bind(Messages.error_configWrongFormat, configs[i]), null); 488 throw new CoreException(error); 489 } 490 String [] archAndFormat = Utils.getArrayFromStringWithBlank(configElements[2], "-"); if (archAndFormat.length != 2) { 492 String message = NLS.bind(Messages.invalid_archivesFormat, archivesFormatAsString); 493 IStatus status = new Status(IStatus.ERROR,IPDEBuildConstants.PI_PDEBUILD , message); 494 throw new CoreException(status); 495 } 496 497 Config aConfig = new Config(configElements[0], configElements[1], archAndFormat[0]); 498 if (getConfigInfos().contains(aConfig)) { 499 archivesFormat.put(aConfig, archAndFormat[1]); 500 } 501 } 502 } 503 504 protected HashMap getArchivesFormat() { 505 if (archivesFormat == null) { 506 try { 507 realSetArchivesFormat(Utils.getPropertyFormat(PROPERTY_ARCHIVESFORMAT)); 509 } catch (CoreException e) { 510 } 512 } 513 return archivesFormat; 514 } 515 516 public void includePlatformIndependent(boolean b) { 517 includePlatformIndependent = b; 518 } 519 520 public void setGroupConfigs(boolean value) { 521 groupConfigs = value; 522 } 523 524 public void setImmutableAntProperties(Properties properties) { 525 antProperties = properties; 526 } 527 528 public void setBundles(BundleDescription[] bundles) { 529 bundlesToBuild = bundles; 530 } 531 } 532 | Popular Tags |