001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM - Initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.build.packager;
011:
012: import java.io.*;
013: import java.util.*;
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.core.runtime.IStatus;
016: import org.eclipse.pde.internal.build.*;
017:
018: public class UnzipperGenerator extends AbstractScriptGenerator {
019: private static final String DATA_SEPARATOR = "|"; //$NON-NLS-1$
020: private static final String ENTRY_SEPARATOR = "%"; //$NON-NLS-1$
021: private static final byte ARCHIVE_NAME = 0;
022: private static final byte FOLDER = 1;
023: private static final byte CONFIGS = 2;
024:
025: // The name the file containing the list of zips
026: private String directoryLocation = DEFAULT_PACKAGER_DIRECTORY_FILENAME_DESCRIPTOR;
027: // The list of zips. The key is the name of the zipfile, and the first property is the place to extract it
028: private Properties zipsList;
029: // The location of the packaging.properties file
030: private String packagingPropertiesLocation;
031:
032: private String[] unzipOrder = new String[0];
033:
034: public void generate() throws CoreException {
035: prepareGeneration();
036: openScript(workingDirectory,
037: DEFAULT_UNZIPPER_FILENAME_DESCRIPTOR);
038: try {
039: generatePrologue();
040: generateUncompressionCommands();
041: generateEpilogue();
042: } finally {
043: closeScript();
044: }
045: }
046:
047: /**
048: *
049: */
050: private void prepareGeneration() {
051: if (packagingPropertiesLocation == null)
052: return;
053:
054: Properties packagingProperties = new Properties();
055: InputStream propertyStream = null;
056: try {
057: propertyStream = new BufferedInputStream(
058: new FileInputStream(packagingPropertiesLocation));
059: try {
060: packagingProperties.load(new BufferedInputStream(
061: propertyStream));
062: } finally {
063: propertyStream.close();
064: }
065: } catch (FileNotFoundException e) {
066: // String message = Policy.bind("exception.readingFile", packagingPropertiesLocation); //$NON-NLS-1$
067: //// Log.throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_READING_FILE, message, e));
068: } catch (IOException e) {
069: // String message = Policy.bind("exception.readingFile", packagingPropertiesLocation); //$NON-NLS-1$
070: // throw new CoreException(new Status(IStatus.ERROR, PI_PDEBUILD, EXCEPTION_READING_FILE, message, e));
071: }
072: unzipOrder = Utils.getArrayFromStringWithBlank(
073: packagingProperties.getProperty("unzipOrder", ""), ","); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
074: }
075:
076: private void generateEpilogue() {
077: script.printTargetEnd();
078: script.println();
079: script.printProjectEnd();
080: }
081:
082: private void generatePrologue() {
083: script.println();
084: script.printComment("Unzip script"); //$NON-NLS-1$
085: script.println();
086: script.printProjectDeclaration("Unzipper", TARGET_MAIN, "."); //$NON-NLS-1$ //$NON-NLS-2$
087: script.printTargetDeclaration(TARGET_MAIN, null, null, null,
088: null);
089: }
090:
091: private void generateUncompressionCommands() throws CoreException {
092: zipsList = readProperties(workingDirectory, directoryLocation,
093: IStatus.ERROR);
094:
095: String zipEntries = zipsList.getProperty("toUnzip", ""); //$NON-NLS-1$ //$NON-NLS-2$
096:
097: List toUnzipWithOrder = new ArrayList(unzipOrder.length);
098: String[] allZipEntries = Utils.getArrayFromString(zipEntries,
099: ENTRY_SEPARATOR);
100: for (int i = 0; i < allZipEntries.length; i++) {
101: String[] entryDetail = Utils.getArrayFromString(
102: allZipEntries[i], DATA_SEPARATOR);
103: script
104: .printComment("Uncompress " + entryDetail[ARCHIVE_NAME]); //$NON-NLS-1$
105:
106: if (!entryDetail[FOLDER].equals(".")) //$NON-NLS-1$
107: script
108: .printMkdirTask("${tempDirectory}/" + entryDetail[FOLDER]); //$NON-NLS-1$
109:
110: if (delayed(entryDetail[ARCHIVE_NAME])) {
111: toUnzipWithOrder.add(entryDetail);
112: continue;
113: }
114: generateUncompress(entryDetail);
115: script.println();
116: script.println();
117: }
118:
119: //Deal with the entries that have a specific order.
120: for (int i = 0; i < unzipOrder.length; i++) {
121: for (Iterator iter = toUnzipWithOrder.iterator(); iter
122: .hasNext();) {
123: String[] entry = (String[]) iter.next();
124: if (entry[ARCHIVE_NAME].startsWith(unzipOrder[i])) {
125: generateUncompress(entry);
126: iter.remove();
127: }
128: }
129: }
130: }
131:
132: private void generateUncompress(String[] entryDetail) {
133: if (entryDetail[ARCHIVE_NAME].endsWith(".zip")) { //$NON-NLS-1$
134: generateUnzipArchive(entryDetail);
135: generateUnzipRootFiles(entryDetail);
136: return;
137: }
138:
139: if (entryDetail[ARCHIVE_NAME].endsWith(".tar.gz") || entryDetail[ARCHIVE_NAME].endsWith(".tar")) { //$NON-NLS-1$ //$NON-NLS-2$
140: generateUntarArchice(entryDetail);
141: generateUntarRootFiles(entryDetail);
142: }
143: }
144:
145: private boolean delayed(String fileName) {
146: for (int i = 0; i < unzipOrder.length; i++) {
147: if (fileName.startsWith(unzipOrder[i]))
148: return true;
149: }
150: return false;
151: }
152:
153: private List getMatchingConfig(String[] entryDetail) {
154: List applyingConfigs = null;
155: if (entryDetail.length == 2) {
156: applyingConfigs = getConfigInfos();
157: } else {
158: String[] configs = Utils.getArrayFromString(
159: entryDetail[CONFIGS], "&");
160: applyingConfigs = new ArrayList(configs.length);
161: for (int i = 0; i < configs.length; i++) {
162: applyingConfigs.add(new Config(configs[i]));
163: }
164: }
165: return applyingConfigs;
166: }
167:
168: private void generateUnzipArchive(String[] entryDetail) {
169: List parameters = new ArrayList(1);
170: parameters.add("-o -X ${unzipArgs} "); //$NON-NLS-1$
171: parameters
172: .add(Utils.getPropertyFormat("downloadDirectory") + '/' + entryDetail[ARCHIVE_NAME]); //$NON-NLS-1$
173: script
174: .printExecTask(
175: "unzip", "${tempDirectory}/" + entryDetail[FOLDER], parameters, null); //$NON-NLS-1$//$NON-NLS-2$
176: }
177:
178: //Uncompress the root files into a platform specific folder
179: private void generateUnzipRootFiles(String[] entryDetail) {
180: //Unzip the root files in a "config specific folder" for all the configurations that matched this entry
181: for (Iterator iter = getMatchingConfig(entryDetail).iterator(); iter
182: .hasNext();) {
183: Config config = (Config) iter.next();
184: List parameters = new ArrayList(3);
185: String rootFilesFolder = "${tempDirectory}/"
186: + config.toString(".") + '/' + entryDetail[FOLDER];
187: script.printMkdirTask(rootFilesFolder);
188: parameters.add("-o -X ${unzipArgs} "); //$NON-NLS-1$
189: parameters
190: .add(Utils.getPropertyFormat("downloadDirectory") + '/' + entryDetail[ARCHIVE_NAME]); //$NON-NLS-1$
191: parameters.add("-x "
192: + (entryDetail[FOLDER].equals(".") ? "eclipse/"
193: : "")
194: + "features/*"
195: + " "
196: + (entryDetail[FOLDER].equals(".") ? "eclipse/"
197: : "") + "plugins/*");
198: script.printExecTask(
199: "unzip", rootFilesFolder, parameters, null); //$NON-NLS-1$
200: }
201: }
202:
203: private void generateUntarArchice(String[] entryDetail) {
204: List parameters = new ArrayList(2);
205: parameters
206: .add("-" + (entryDetail[ARCHIVE_NAME].endsWith(".gz") ? "z" : "") + "pxvf"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
207: parameters
208: .add(Utils.getPropertyFormat("downloadDirectory") + '/' + entryDetail[ARCHIVE_NAME]); //$NON-NLS-1$
209: script
210: .printExecTask(
211: "tar", "${tempDirectory}/" + entryDetail[FOLDER], parameters, null); //$NON-NLS-1$//$NON-NLS-2$
212: }
213:
214: private void generateUntarRootFiles(String[] entryDetail) {
215: //Unzip the root files in a "config specific folder" for all the configurations that matched this entry
216: for (Iterator iter = getMatchingConfig(entryDetail).iterator(); iter
217: .hasNext();) {
218: Config config = (Config) iter.next();
219: List parameters = new ArrayList(4);
220: String rootFilesFolder = "${tempDirectory}/" + config.toString(".") + '/' + entryDetail[FOLDER]; //$NON-NLS-1$ //$NON-NLS-2$
221: script.printMkdirTask(rootFilesFolder);
222: parameters
223: .add("-" + (entryDetail[ARCHIVE_NAME].endsWith(".gz") ? "z" : "") + "pxvf"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
224: parameters
225: .add(Utils.getPropertyFormat("downloadDirectory") + '/' + entryDetail[ARCHIVE_NAME]); //$NON-NLS-1$
226: parameters
227: .add("--exclude=" + (entryDetail[FOLDER].equals(".") ? "eclipse" : "") + "/features/*"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
228: parameters
229: .add("--exclude=" + (entryDetail[FOLDER].equals(".") ? "eclipse" : "") + "/plugins/*"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
230: script.printExecTask(
231: "tar", rootFilesFolder, parameters, null); //$NON-NLS-1$
232: }
233: }
234:
235: public void setDirectoryLocation(String filename) {
236: directoryLocation = filename;
237: }
238:
239: /**
240: * Set the property file containing information about packaging
241: * @param propertyFile the path to a property file
242: */
243: public void setPropertyFile(String propertyFile) {
244: packagingPropertiesLocation = propertyFile;
245: }
246: }
|