001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others. All rights reserved. This
003: * program and the accompanying materials are made available under the terms of
004: * the Eclipse Public License v1.0 which accompanies this distribution, and is
005: * available at http://www.eclipse.org/legal/epl-v10.html
006: *
007: * Contributors: IBM Corporation - initial API and implementation
008: ******************************************************************************/package org.eclipse.pde.build.internal.tests;
009:
010: import java.io.*;
011: import java.net.URL;
012: import java.util.*;
013: import java.util.jar.*;
014: import java.util.jar.Attributes.Name;
015:
016: import org.apache.tools.ant.filters.ReplaceTokens;
017: import org.apache.tools.ant.util.ReaderInputStream;
018: import org.eclipse.core.resources.*;
019: import org.eclipse.core.runtime.*;
020: import org.eclipse.pde.build.tests.Activator;
021: import org.eclipse.pde.internal.build.FeatureGenerator;
022: import org.eclipse.pde.internal.build.site.BuildTimeSiteFactory;
023:
024: public class Utils {
025:
026: /**
027: * Transfer the contents of resource into the destination IFile. During the transfer, replace all
028: * instances of "@replaceTag@" with "replaceString"
029: *
030: * @param resource - input URL
031: * @param destination - IFile destination
032: * @param replacements - map of tokens and values to replaces, file should contain "@replaceTag@"
033: * @throws IOException
034: * @throws CoreException
035: */
036: static public void transferAndReplace(URL resource,
037: IFile destination, Map replacements) throws IOException,
038: CoreException {
039: Reader reader = new InputStreamReader(new BufferedInputStream(
040: resource.openStream()));
041: final ReplaceTokens replaces = new ReplaceTokens(reader);
042:
043: for (Iterator iterator = replacements.keySet().iterator(); iterator
044: .hasNext();) {
045: String replaceTag = (String) iterator.next();
046: String replaceString = (String) replacements
047: .get(replaceTag);
048: ReplaceTokens.Token token = new ReplaceTokens.Token();
049: token.setKey(replaceTag);
050: token.setValue(replaceString);
051: replaces.addConfiguredToken(token);
052: }
053:
054: ReaderInputStream inputStream = new ReaderInputStream(replaces);
055: destination.create(inputStream, true, null);
056: inputStream.close();
057: }
058:
059: static public void generateAllElements(IFolder buildFolder,
060: String element) throws CoreException, IOException {
061: if (element != null) {
062: // get the productBuild/allElements.xml and replace @ELEMENT@ with element
063: URL allElements = FileLocator.find(Platform
064: .getBundle(Activator.PLUGIN_ID), new Path(
065: "/resources/allElements.xml"), null);
066: Map replacements = new HashMap(1);
067: replacements.put("ELEMENT", element);
068: transferAndReplace(allElements, buildFolder
069: .getFile("allElements.xml"), replacements);
070: buildFolder.refreshLocal(IResource.DEPTH_INFINITE, null);
071: }
072: }
073:
074: static public void generatePluginBuildProperties(IFolder folder,
075: Properties properties) throws FileNotFoundException,
076: IOException {
077: Properties buildProperties = properties != null ? properties
078: : new Properties();
079:
080: //default contents:
081: if (!buildProperties.containsKey("source.."))
082: buildProperties.put("source..", "src/");
083: if (!buildProperties.containsKey("output.."))
084: buildProperties.put("output..", "bin/");
085: if (!buildProperties.containsKey("bin.includes"))
086: buildProperties.put("bin.includes", "META-INF/, .");
087:
088: storeBuildProperties(folder, buildProperties);
089: }
090:
091: static public void generateBundleManifest(IFolder folder,
092: String bundleId, String bundleVersion,
093: Attributes additionalAttributes) throws CoreException,
094: IOException {
095: Manifest manifest = new Manifest();
096: Attributes mainAttributes = manifest.getMainAttributes();
097: mainAttributes.put(Name.MANIFEST_VERSION, "1.0");
098: mainAttributes.put(new Name("Bundle-ManifestVersion"), "2");
099: mainAttributes.put(new Name("Bundle-Name"), "Test Bundle "
100: + bundleId);
101: mainAttributes.put(new Name("Bundle-SymbolicName"), bundleId);
102: mainAttributes.put(new Name("Bundle-Version"), bundleVersion);
103: if (additionalAttributes != null)
104: mainAttributes.putAll(additionalAttributes);
105:
106: IFile manifestFile = folder.getFile(JarFile.MANIFEST_NAME);
107: IFolder metaInf = (IFolder) manifestFile.getParent();
108: if (!metaInf.exists())
109: metaInf.create(true, true, null);
110: OutputStream outputStream = new BufferedOutputStream(
111: new FileOutputStream(manifestFile.getLocation()
112: .toFile()));
113: manifest.write(outputStream);
114: outputStream.close();
115: }
116:
117: static public void generateBundle(IFolder folder, String bundleId)
118: throws CoreException, IOException {
119: generateBundleManifest(folder, bundleId, "1.0.0", null);
120: generatePluginBuildProperties(folder, null);
121: }
122:
123: static public void storeBuildProperties(IFolder buildFolder,
124: Properties buildProperties) throws FileNotFoundException,
125: IOException {
126: File buildPropertiesFile = new File(buildFolder.getLocation()
127: .toFile(), "build.properties");
128: OutputStream outputStream = new BufferedOutputStream(
129: new FileOutputStream(buildPropertiesFile));
130: buildProperties.store(outputStream, "");
131: outputStream.close();
132: }
133:
134: static public void generateFeature(IFolder workingDirectory,
135: String id, String[] featureList, String[] pluginList)
136: throws CoreException {
137: generateFeature(workingDirectory, id, featureList, pluginList,
138: null, false, false);
139: }
140:
141: static public void generateFeature(IFolder workingDirectory,
142: String id, String[] featureList, String[] pluginList,
143: String product, boolean includeLaunchers, boolean verify)
144: throws CoreException {
145: FeatureGenerator generator = new FeatureGenerator();
146: if (verify) {
147: FeatureGenerator.setConfigInfo("*,*,*");
148: String baseLocation = Platform.getInstallLocation()
149: .getURL().getPath();
150: BuildTimeSiteFactory.setInstalledBaseSite(baseLocation);
151: File delta = findDeltaPack();
152: if (delta != null && !delta.equals(new File(baseLocation)))
153: generator.setPluginPath(new String[] { delta
154: .getAbsolutePath() });
155: }
156: generator.setIncludeLaunchers(includeLaunchers);
157: generator.setVerify(verify);
158: generator.setFeatureId(id);
159: generator.setProductFile(product);
160: generator.setFeatureList(featureList);
161: generator.setPluginList(pluginList);
162: generator.setWorkingDirectory(workingDirectory.getLocation()
163: .toOSString());
164: generator.generate();
165: }
166:
167: /**
168: * Creates a IFolder resources. Will create any folders necessary under parent
169: */
170: static public IFolder createFolder(IFolder parent, String path)
171: throws CoreException {
172: parent.refreshLocal(IResource.DEPTH_INFINITE, null);
173: IFolder folder = parent.getFolder(path);
174: if (folder.exists())
175: return folder;
176: IFolder container = (IFolder) folder.getParent();
177: if (!container.exists()) {
178: LinkedList stack = new LinkedList();
179: while (!container.equals(parent) && !container.exists()) {
180: stack.add(0, container);
181: container = (IFolder) container.getParent();
182: }
183:
184: for (Iterator iterator = stack.iterator(); iterator
185: .hasNext();) {
186: container = (IFolder) iterator.next();
187: container.create(true, true, null);
188: }
189: }
190: folder.create(true, true, null);
191: return folder;
192: }
193:
194: public static File findDeltaPack() {
195: File baseLocation = new File(Platform.getInstallLocation()
196: .getURL().getPath());
197:
198: File plugins = new File(baseLocation, "features");
199: FilenameFilter filter = new FilenameFilter() {
200: public boolean accept(File dir, String name) {
201: return name
202: .startsWith("org.eclipse.equinox.executable");
203: }
204: };
205: String[] files = plugins.list(filter);
206:
207: if (files.length > 0)
208: return baseLocation;
209:
210: File delta = new File(baseLocation.getParent(),
211: "deltapack/eclipse");
212: if (delta.exists()) {
213: files = new File(delta, "features").list(filter);
214: if (files.length > 0)
215: return delta;
216: }
217: return null;
218: }
219: }
|