From 0753fc47bcf12e69ce294657db21f94c64967a84 Mon Sep 17 00:00:00 2001
From: david_williams <david_williams>
Date: Tue, 10 Mar 2009 07:42:18 +0000
Subject: [PATCH] add map generator

---
 .../org.eclipse.wtp.releng.tools/plugin.xml   |   5 +
 .../wtp/releng/tools/MapFileGenerator.java    | 212 ++++++++++++++++++
 2 files changed, 217 insertions(+)
 create mode 100644 plugins/org.eclipse.wtp.releng.tools/src/org/eclipse/wtp/releng/tools/MapFileGenerator.java

diff --git a/plugins/org.eclipse.wtp.releng.tools/plugin.xml b/plugins/org.eclipse.wtp.releng.tools/plugin.xml
index a1c464088..d0304fd12 100644
--- a/plugins/org.eclipse.wtp.releng.tools/plugin.xml
+++ b/plugins/org.eclipse.wtp.releng.tools/plugin.xml
@@ -30,5 +30,10 @@
             name="updatePackProperties"
             class="org.eclipse.wtp.releng.tools.UpdatePackPropertiesFile">
         </antTask>
+        <antTask
+            library="wtpRelengTools.jar"
+            name="generateMapFiles"
+            class="org.eclipse.wtp.releng.tools.MapFileGenerator">
+        </antTask>        
     </extension>
 </plugin>
\ No newline at end of file
diff --git a/plugins/org.eclipse.wtp.releng.tools/src/org/eclipse/wtp/releng/tools/MapFileGenerator.java b/plugins/org.eclipse.wtp.releng.tools/src/org/eclipse/wtp/releng/tools/MapFileGenerator.java
new file mode 100644
index 000000000..527eeb455
--- /dev/null
+++ b/plugins/org.eclipse.wtp.releng.tools/src/org/eclipse/wtp/releng/tools/MapFileGenerator.java
@@ -0,0 +1,212 @@
+/*******************************************************************************
+ * Copyright (c) 2007, 2009 IBM Corporation and others. All rights reserved.
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ * 
+ * Contributors: IBM Corporation - initial API and implementation
+ * 
+ * This Class was copied from similar package in Orbit, org.eclipse.orbit.releng.tools
+ * Thanks to DJ and others. 
+ *******************************************************************************/
+package org.eclipse.wtp.releng.tools;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.text.DateFormat;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Task;
+
+/**
+ * <pre>
+ * <code>
+ *         
+ *    &lt;!-- This declares the task to ant. It needs to be tweaked to point to the jar containing the task --&gt;
+ *    &lt;taskdef
+ *    classname=&quot;org.eclipse.orbit.internal.releng.tools.MapFileGenerator&quot;
+ *    name=&quot;eclipse.mapGenerator&quot;&gt;
+ *    &lt;classpath&gt;
+ *    &lt;pathelement path=&quot;${classpath}&quot; /&gt;
+ *    &lt;pathelement
+ *    location=&quot;${builder.home}/scripts/tools/mapGenerator.jar&quot; /&gt;
+ *    &lt;/classpath&gt;
+ *    &lt;/taskdef&gt;
+ *   
+ *   
+ *   
+ *   
+ *    &lt;target name=&quot;default&quot;&gt;
+ *    &lt;eclipse.mapGenerator
+ *    root=&quot;${topBuildDirectory}/${buildLabel}/bundles&quot;
+ *    addressPrefix=&quot;http://download.eclipse.org/tools/orbit/committers/drops/${buildLabel}/bundles/&quot;
+ *    inputFile=&quot;${topBuildDirectory}/finalPluginsVersions.properties&quot;
+ *    outputFile=&quot;${topBuildDirectory}/${buildLabel}/orbitBundles.map&quot; /&gt;
+ *    &lt;/target&gt;
+ * </code>
+ </pre>
+ */
+public class MapFileGenerator extends Task {
+	private static final String EOL = System.getProperty("line.separator");
+	// Longterm we'll want to use 'bundle', but due to 
+	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=174331
+	// we'll use 'plugin' for now.
+	private static final String BUNDLE = "bundle";
+	private static final String PLUGIN = "plugin";
+	private String BUNDLE_LINE_PREFIX = BUNDLE;
+	private boolean usePlugin = true;
+
+	private DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
+
+	private File root;
+	private String username;
+	private String pwd;
+	private String addressPrefix;
+	private String inputFile;
+	private String outputFile;
+	private String p2MapFile;
+	private String p2Repository;
+
+	static class BundleInfo implements Comparable {
+		String id;
+		String version;
+		String fullVersion;
+
+		public int compareTo(Object o) {
+			BundleInfo other = (BundleInfo) o;
+			return (id + ',' + version).compareTo(other.id + ',' + version);
+		}
+	}
+
+	public void execute() throws BuildException {
+		if (usePlugin) {
+			BUNDLE_LINE_PREFIX = PLUGIN;
+		}
+		try {
+			List list = getBundleDetails();
+			writeList(list, outputFile, false);
+			// only write out a p2 map file if the required attributes are present
+			if (p2MapFile != null && p2Repository != null)
+				writeList(list, p2MapFile, true);
+
+		} catch (IOException e) {
+			new BuildException(e);
+		}
+	}
+
+	// plugin@my.bundle.id,1.0.0=p2IU,id=my.bundle.id,version=1.0.0.v20081201,repository=http:/example.com/repo 
+	private String generateP2Fetch(BundleInfo info) {
+		return BUNDLE_LINE_PREFIX + '@' + info.id + ',' + info.version + "=p2IU,id=" + info.id + ",version=" + info.fullVersion + ",repository=" + p2Repository + EOL;
+	}
+
+	// plugin@my.bundle.id,1.0.0=GET,http://eclipse.org/downloads/orbit/my.bundle.id_1.0.0.v20081201.zip,unpack=true|false
+	private String generateCVSFetch(BundleInfo info) {
+		String fileName = info.id + "_" + info.fullVersion;
+		boolean isJar = isJar(fileName);
+		return new String(BUNDLE_LINE_PREFIX + "@" + info.id + ',' + info.version + "=GET," + addressPrefix + fileName + (isJar ? ".jar" : ".zip,unpack=true") + (username == null ? "" : ',' + username) + (pwd == null ? "" : ',' + pwd) + EOL);
+	}
+
+	private List getBundleDetails() throws FileNotFoundException, IOException {
+		List bundleDetailsList = new ArrayList();
+		BufferedReader reader = null;
+		try {
+			reader = new BufferedReader(new FileReader(inputFile));
+			// This expression matches an entry in a
+			// finalPluginsVersions.properties file and split it into 3
+			// segments (id, version, full version)
+			Pattern replacementExpression = Pattern.compile("^([a-zA-Z0-9\\-._]*)_([0-9]*\\.[0-9]*\\.[0-9]*)?=(.*)$");
+			Matcher m = null;
+			String current = null;
+			while ((current = reader.readLine()) != null) {
+				m = replacementExpression.matcher(current);
+				if (!m.matches())
+					continue;
+				// the properties file contains entries with and without a version and
+				// we want to skip the non-version ones
+				if (m.group(2) == null)
+					continue;
+
+				BundleInfo info = new BundleInfo();
+				info.id = m.group(1);
+				info.version = m.group(2);
+				info.fullVersion = m.group(3);
+
+				bundleDetailsList.add(info);
+			}
+		} finally {
+			if (reader != null)
+				reader.close();
+		}
+		return bundleDetailsList;
+	}
+
+	private void writeList(List bundleDetailsList, String output, boolean isP2) throws IOException {
+		BufferedWriter writer = null;
+		try {
+			writer = new BufferedWriter(new FileWriter(output));
+			writer.write("!*** This file was created on " + dateFormat.format(new Date()));
+			Collections.sort(bundleDetailsList);
+			Iterator iterator = bundleDetailsList.iterator();
+			while (iterator.hasNext()) {
+				BundleInfo info = (BundleInfo) iterator.next();
+				// double space for readability
+				writer.write(EOL);
+				writer.write(isP2 ? generateP2Fetch(info) : generateCVSFetch(info));
+			}
+		} finally {
+			if (writer != null)
+				writer.close();
+		}
+	}
+
+	private boolean isJar(String path) {
+		File checked = new File(root, path + ".jar");
+		if (checked.exists())
+			return true;
+		return false;
+	}
+
+	public void setRoot(File root) {
+		this.root = root;
+	}
+
+	public void setUsername(String username) {
+		this.username = username;
+	}
+
+	public void setPassword(String pwd) {
+		this.pwd = pwd;
+	}
+
+	public void setAddressPrefix(String addressPrefix) {
+		this.addressPrefix = addressPrefix;
+	}
+
+	public void setInputFile(String inputFile) {
+		this.inputFile = inputFile;
+	}
+
+	public void setOutputFile(String outputFile) {
+		this.outputFile = outputFile;
+	}
+
+	public void setP2MapFile(String value) {
+		this.p2MapFile = value;
+	}
+	
+	public void setP2Repository(String value) {
+		this.p2Repository = value;
+	}
+}
-- 
GitLab