1/*******************************************************************************
2 * Copyright (c) 2005, 2006 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *     IBM Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.releng.generators;
12
13import java.io.File;
14import java.io.FileInputStream;
15import java.io.FileNotFoundException;
16import java.io.FileWriter;
17import java.io.IOException;
18import java.io.PrintWriter;
19import java.util.Enumeration;
20import java.util.Hashtable;
21import java.util.Properties;
22
23import org.apache.tools.ant.Task;
24
25public class FetchBaseTask extends Task {
26
27	private String mapFile;
28	private String outputFile;
29	private Hashtable entries;
30	/**
31	 * @param args
32	 */
33	public static void main(String[] args) {
34		// TODO Auto-generated method stub
35		FetchBaseTask task=new FetchBaseTask();
36		task.mapFile="d:/workspace/org.eclipse.releng/maps/base.map";
37		task.outputFile="d:/workspace/org.eclipse.releng/fetch.xml";
38		task.execute();
39	}
40
41	public FetchBaseTask(){
42		entries=new Hashtable();
43	}
44	public void execute(){
45		readMap();
46		printScript(fetchScript());
47	}
48
49	private void readMap (){
50		File file=new File(mapFile);
51		Properties properties=new Properties();
52		try {
53			properties.load(new FileInputStream(file));
54			Enumeration keys=properties.keys();
55			while (keys.hasMoreElements()){
56				String key=keys.nextElement().toString();
57				String script=getScript(key,properties.get(key).toString());
58				entries.put("fetch."+key,script);
59			}
60
61		} catch (FileNotFoundException e) {
62			// TODO Auto-generated catch block
63			e.printStackTrace();
64		} catch (IOException e) {
65			// TODO Auto-generated catch block
66			e.printStackTrace();
67		}
68	}
69
70	private String getScript(String key,String entry){
71		String[] keyParts=key.split("@");
72		if (keyParts.length==0)
73			return null;
74		String [] cvsinfo=entry.split(",");
75		if (cvsinfo.length<3)
76			return null;
77
78		String[] typeParts=keyParts[0].split("base.");
79		String type=typeParts[1];
80		String id=keyParts[1];
81
82		String fullName=entry.substring(entry.lastIndexOf("/")+1,entry.length());
83		if (fullName.endsWith(".jar"))
84			return fetchJarTarget(type, id, cvsinfo);
85		else
86			return fetchDirectoryTarget(type, id, fullName, cvsinfo);
87	}
88
89	public String getMapFile() {
90		return mapFile;
91	}
92
93	public void setMapFile(String mapFile) {
94		this.mapFile = mapFile;
95	}
96
97	public String getOutputFile() {
98		return outputFile;
99	}
100
101	public void setOutputFile(String outputFile) {
102		this.outputFile = outputFile;
103	}
104
105	private String fetchJarTarget(String type, String id, String[] cvsinfo){
106		return "\t<target name=\"fetch.base."+type+"@"+id+"\">\n" +
107		"\t\t<mkdir dir=\"${baseLocation}/"+type+"s\" />\n" +
108		"\t\t<property name=\"cvsroot\" value=\""+cvsinfo[1]+"\" />\n" +
109		"\t\t<property name=\"fetchTag\" value=\""+cvsinfo[0]+"\" />\n" +
110		"\t\t<cvs command=\"export -d "+type+"s\"\n" +
111		"\t\t\tcvsRoot=\"${cvsroot}\"\n" +
112		"\t\t\tpackage=\""+cvsinfo[3]+"\"\n" +
113		"\t\t\ttag=\"${fetchTag}\"\n" +
114		"\t\t\tdest=\"${baseLocation}\"\n" +
115		"\t\t\tquiet=\"true\"/>\n" +
116		"\t\t<delete includeemptydirs=\"true\">\n"+
117		"\t\t\t<fileset dir=\"${baseLocation}/"+type+"s\" includes=\"**/CVS/**\" defaultexcludes=\"no\"/>\n"+
118		"\t\t</delete>\n"+
119		"\t</target>\n";
120
121	}
122
123	private String fetchDirectoryTarget(String type, String id, String fullName,String[] cvsinfo){
124		return "\t<target name=\"fetch.base."+type+"@"+id+"\">\n" +
125		"\t\t<mkdir dir=\"${baseLocation}/"+type+"s\" />\n" +
126		"\t\t<property name=\"cvsroot\" value=\""+cvsinfo[1]+"\" />\n" +
127		"\t\t<cvs command=\"export -d "+fullName+"\"\n" +
128		"\t\t\tcvsRoot=\"${cvsroot}\"\n" +
129		"\t\t\tpackage=\""+cvsinfo[3]+"\"\n" +
130		"\t\t\ttag=\""+cvsinfo[0]+"\"\n" +
131		"\t\t\tdest=\"${baseLocation}/"+type+"s\"\n" +
132		"\t\t\tquiet=\"true\"/>\n" +
133		"\t\t<delete includeemptydirs=\"true\">\n"+
134		"\t\t\t<fileset dir=\"${baseLocation}/"+type+"s\" includes=\"**/CVS/**\" defaultexcludes=\"no\"/>\n"+
135		"\t\t</delete>\n"+
136		"\t</target>\n";
137
138	}
139
140	private String fetchScript(){
141		String script="<project default=\"all.elements\">\n" +
142			"<!--Ant script which will fetch pre-built plug-ins and features to a location where they\n" +
143			"will be consumed by the build, i.e. ${baseLocation}.  Stored in this project to capture revisions/urls of\n" +
144			"binaries.-->\n" +
145			"\t<property name=\"baseLocation\" value=\"${basedir}/baseLocation\" />\n" +
146			"\t<target name=\"all.elements\">\n";
147
148		Enumeration keys=entries.keys();
149		while (keys.hasMoreElements()){
150			script=script.concat("\t\t<antcall target=\""+keys.nextElement()+"\" />\n");
151		}
152		script=script.concat("\t</target>");
153		keys=entries.keys();
154		while (keys.hasMoreElements()){
155			script=script.concat("\n\n"+entries.get(keys.nextElement()));
156		}
157		script=script.concat("</project>");
158
159		return script;
160	}
161
162	private void printScript(String script){
163		try {
164			PrintWriter out = new PrintWriter(new FileWriter(new File(outputFile)));
165			out.print(script);
166			out.flush();
167			out.close();
168		} catch (IOException e) {
169			// TODO Auto-generated catch block
170			e.printStackTrace();
171		}
172	}
173}
174