Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ffea29b8c66ca7c11bf17f1baefad905316521f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*******************************************************************************
 * Copyright (c) 2008, 2017 Code 9 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: 
 *   Code 9 - initial API and implementation
 *   IBM - ongoing development
 *   Pascal Rapicault - Support for bundled macosx http://bugs.eclipse.org/57349
 ******************************************************************************/
package org.eclipse.equinox.p2.publisher.eclipse;

import java.io.File;
import java.util.*;
import org.eclipse.core.runtime.*;
import org.eclipse.equinox.internal.p2.core.helpers.FileUtils;
import org.eclipse.equinox.internal.p2.publisher.eclipse.BrandingIron;
import org.eclipse.equinox.internal.p2.publisher.eclipse.ExecutablesDescriptor;
import org.eclipse.equinox.p2.metadata.*;
import org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitDescription;
import org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitFragmentDescription;
import org.eclipse.equinox.p2.metadata.expression.IMatchExpression;
import org.eclipse.equinox.p2.publisher.*;
import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor;
import org.eclipse.equinox.spi.p2.publisher.PublisherHelper;
import org.eclipse.osgi.service.environment.Constants;

/**
 * Given the description of an executable, this action publishes optionally 
 * non-destructively brands the executable, publishes the resultant artifacts
 * and publishes the required IUs to identify the branded executable, configure
 * the executable and set it up as the launcher for a profile.
 * <p>
 * This action works on one platform configuration only.
 * <p>
 * This action consults the following types of advice:
 * </ul>
 * <li>{@link IBrandingAdvice}</li>
 * </ul>
 */
public class EquinoxExecutableAction extends AbstractPublisherAction {
	private static String TYPE = "executable"; //$NON-NLS-1$

	protected String configSpec;
	protected String idBase;
	protected Version version;
	protected ExecutablesDescriptor executables;
	protected String flavor;

	protected EquinoxExecutableAction() {
		//hidden
	}

	public EquinoxExecutableAction(ExecutablesDescriptor executables, String configSpec, String idBase, Version version, String flavor) {
		this.executables = executables;
		this.configSpec = configSpec;
		this.idBase = idBase == null ? "org.eclipse" : idBase; //$NON-NLS-1$
		this.version = version;
		this.flavor = flavor;
	}

	@Override
	public IStatus perform(IPublisherInfo publisherinfo, IPublisherResult result, IProgressMonitor monitor) {
		setPublisherInfo(publisherinfo);
		ExecutablesDescriptor brandedExecutables = brandExecutables(executables);
		try {
			if (publishExecutableIU(brandedExecutables, result))
				publishExecutableCU(brandedExecutables, result);
			publishExecutableSetter(brandedExecutables, result);
		} finally {
			if (brandedExecutables.isTemporary())
				FileUtils.deleteAll(brandedExecutables.getLocation());
		}
		return Status.OK_STATUS;
	}

	/**
	 * Publishes the IUs that cause the executable to be actually set as the launcher for 
	 * the profile
	 */
	private void publishExecutableSetter(ExecutablesDescriptor brandedExecutables, IPublisherResult result) {
		InstallableUnitDescription iud = new MetadataFactory.InstallableUnitDescription();
		String executableName = brandedExecutables.getExecutableName();
		String id = getExecutableId() + '.' + executableName;
		iud.setId(id);
		iud.setVersion(version);
		iud.setTouchpointType(PublisherHelper.TOUCHPOINT_OSGI);
		iud.setCapabilities(new IProvidedCapability[] {createSelfCapability(id, version)});
		iud.setFilter(createFilterSpec(configSpec));
		Map<String, String> touchpointData = new HashMap<>();
		touchpointData.put("configure", "setLauncherName(name:" + executableName + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		touchpointData.put("unconfigure", "setLauncherName()"); //$NON-NLS-1$ //$NON-NLS-2$
		iud.addTouchpointData(MetadataFactory.createTouchpointData(touchpointData));
		result.addIU(MetadataFactory.createInstallableUnit(iud), IPublisherResult.ROOT);
	}

	/**
	 * Publishes IUs and CUs for the files that make up the launcher for a given
	 * ws/os/arch combination.
	 */
	protected boolean publishExecutableIU(ExecutablesDescriptor execDescriptor, IPublisherResult result) {
		String[] config = parseConfigSpec(configSpec);
		if (execDescriptor.getFiles().length == 0 && (config.length == 0 || CONFIG_ANY.equalsIgnoreCase(config[0]))) {
			return false; //no cu required
		}
		boolean publishCU = true;

		// Create the IU for the executable
		InstallableUnitDescription iu = new MetadataFactory.InstallableUnitDescription();
		String id = getExecutableId();
		iu.setId(id);
		iu.setVersion(version);
		IMatchExpression<IInstallableUnit> filter = createFilterSpec(configSpec);
		iu.setFilter(filter);
		iu.setSingleton(true);
		iu.setTouchpointType(PublisherHelper.TOUCHPOINT_NATIVE);
		String namespace = ConfigCUsAction.getAbstractCUCapabilityNamespace(idBase, TYPE, flavor, configSpec);
		String capabilityId = ConfigCUsAction.getAbstractCUCapabilityId(idBase, TYPE, flavor, configSpec);
		IProvidedCapability executableCapability = MetadataFactory.createProvidedCapability(namespace, capabilityId, version);
		IProvidedCapability selfCapability = createSelfCapability(id, version);
		iu.setCapabilities(new IProvidedCapability[] {selfCapability, executableCapability});

		//Create the artifact descriptor.  we have several files so no path on disk
		if (execDescriptor.getFiles().length == 0) {
			publishCU = false;
		} else {
			IArtifactKey key = PublisherHelper.createBinaryArtifactKey(id, version);
			iu.setArtifacts(new IArtifactKey[] {key});
			IArtifactDescriptor descriptor = PublisherHelper.createArtifactDescriptor(info, key, null);
			publishArtifact(descriptor, execDescriptor.getFiles(), null, info, createRootPrefixComputer(execDescriptor.getLocation()));
			if (execDescriptor.isTemporary())
				FileUtils.deleteAll(execDescriptor.getLocation());
		}
		// setup a requirement between the executable and the launcher fragment that has the shared library
		if (config.length > 0 && !CONFIG_ANY.equalsIgnoreCase(config[0])) {
			String ws = config[0];
			String os = config[1];
			String arch = config[2];
			String launcherFragment = EquinoxLauncherCUAction.ORG_ECLIPSE_EQUINOX_LAUNCHER + '.' + ws + '.' + os;
			if (!(Constants.OS_MACOSX.equals(os) && !Constants.ARCH_X86_64.equals(arch)))
				launcherFragment += '.' + arch;
			iu.setRequirements(new IRequirement[] {MetadataFactory.createRequirement(IInstallableUnit.NAMESPACE_IU_ID, launcherFragment, VersionRange.emptyRange, filter, false, false)});
		}
		result.addIU(MetadataFactory.createInstallableUnit(iu), IPublisherResult.ROOT);
		return publishCU;
	}

	private String getExecutableId() {
		return createCUIdString(idBase, TYPE, "", configSpec); //$NON-NLS-1$
	}

	// Create the CU that installs (e.g., unzips) the executable
	private void publishExecutableCU(ExecutablesDescriptor execDescriptor, IPublisherResult result) {
		InstallableUnitFragmentDescription cu = createSkeletonExecutableCU(execDescriptor);
		String[] config = parseConfigSpec(configSpec);
		String os = config[1];
		Map<String, String> touchpointData = computeInstallActions(execDescriptor, os);
		cu.addTouchpointData(MetadataFactory.createTouchpointData(touchpointData));
		IInstallableUnit unit = MetadataFactory.createInstallableUnit(cu);
		result.addIU(unit, IPublisherResult.ROOT);

	}

	private InstallableUnitFragmentDescription createSkeletonExecutableCU(ExecutablesDescriptor execDescriptor) {
		InstallableUnitFragmentDescription cu = new InstallableUnitFragmentDescription();
		String id = createCUIdString(idBase, TYPE, flavor, configSpec);
		cu.setId(id);
		cu.setVersion(version);
		cu.setFilter(createFilterSpec(configSpec));
		String executableId = getExecutableId();
		cu.setHost(new IRequirement[] {MetadataFactory.createRequirement(IInstallableUnit.NAMESPACE_IU_ID, executableId, new VersionRange(version, true, version, true), null, false, false)});
		cu.setProperty(InstallableUnitDescription.PROP_TYPE_FRAGMENT, Boolean.TRUE.toString());
		//TODO bug 218890, would like the fragment to provide the launcher capability as well, but can't right now.
		cu.setCapabilities(new IProvidedCapability[] {PublisherHelper.createSelfCapability(id, version)});
		cu.setTouchpointType(PublisherHelper.TOUCHPOINT_NATIVE);
		return cu;
	}

	private Map<String, String> computeInstallActions(ExecutablesDescriptor execDescriptor, String os) {
		if (Constants.OS_MACOSX.equals(os))
			return computeMacInstallActions(execDescriptor);

		Map<String, String> touchpointData = new HashMap<>();
		String configurationData = "unzip(source:@artifact, target:${installFolder});"; //$NON-NLS-1$
		if (!Constants.OS_WIN32.equals(os)) {
			// We are on linux/unix.  by default set all of the files to be executable.
			File[] fileList = execDescriptor.getFiles();
			for (int i = 0; i < fileList.length; i++)
				configurationData += " chmod(targetDir:${installFolder}, targetFile:" + fileList[i].getName() + ", permissions:755);"; //$NON-NLS-1$ //$NON-NLS-2$
		}
		touchpointData.put("install", configurationData); //$NON-NLS-1$
		String unConfigurationData = "cleanupzip(source:@artifact, target:${installFolder});"; //$NON-NLS-1$
		touchpointData.put("uninstall", unConfigurationData); //$NON-NLS-1$
		return touchpointData;
	}

	private Map<String, String> computeMacInstallActions(ExecutablesDescriptor execDescriptor) {
		Map<String, String> touchpointData = new HashMap<>();
		String configurationData = "unzip(source:@artifact, target:${installFolder}/../);"; //$NON-NLS-1$
		String execName = execDescriptor.getExecutableName();
		configurationData += " chmod(targetDir:${installFolder}/../MacOS/, targetFile:" + execName + ", permissions:755);"; //$NON-NLS-1$ //$NON-NLS-2$
		touchpointData.put("install", configurationData); //$NON-NLS-1$
		String unConfigurationData = "cleanupzip(source:@artifact, target:${installFolder}/../);"; //$NON-NLS-1$
		touchpointData.put("uninstall", unConfigurationData); //$NON-NLS-1$
		return touchpointData;
	}

	private String guessMacAppName(String execName) {
		// magic moved here from BrandingIron.brandMac for bug 342550; PDE build requires appName == execName
		// TODO the application name for Mac really should be a parameter of the product configuration
		String appName = execName;
		if (appName.equals("eclipse")) //$NON-NLS-1$
			appName = "Eclipse"; //$NON-NLS-1$
		else if (appName.equals("launcher")) //$NON-NLS-1$
			appName = "Launcher"; //$NON-NLS-1$
		return appName;
	}

	/**
	 * Brands a copy of the given executable descriptor with the information in the 
	 * current product definition.  The files described in the descriptor are also copied
	 * to a temporary location to avoid destructive modification.
	 * 
	 * @param descriptor the executable descriptor to brand.
	 * @return the new descriptor
	 */
	protected ExecutablesDescriptor brandExecutables(ExecutablesDescriptor descriptor) {
		ExecutablesDescriptor result = new ExecutablesDescriptor(descriptor);
		result.makeTemporaryCopy();
		IBrandingAdvice advice = getBrandingAdvice();
		if (advice == null)
			partialBrandExecutables(result);
		else
			fullBrandExecutables(result, advice);
		return result;
	}

	private IBrandingAdvice getBrandingAdvice() {
		// there is expected to only be one branding advice for a given configspec so
		// just return the first one we find.
		Collection<IBrandingAdvice> advice = info.getAdvice(configSpec, true, null, null, IBrandingAdvice.class);
		for (Iterator<IBrandingAdvice> i = advice.iterator(); i.hasNext();)
			return i.next();
		return null;
	}

	protected void fullBrandExecutables(ExecutablesDescriptor descriptor, IBrandingAdvice advice) {
		BrandingIron iron = new BrandingIron();
		iron.setId(idBase);
		iron.setVersion(version);
		iron.setIcons(advice.getIcons());
		String name = advice.getExecutableName();
		if (name == null)
			name = "eclipse"; //$NON-NLS-1$
		iron.setName(name);
		iron.setApplicationName(guessMacAppName(name));
		iron.setOS(advice.getOS());
		// FIXME: use product's aboutText as description?
		// iron.setDescription(advice.getAboutText());
		try {
			iron.brand(descriptor);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected void partialBrandExecutables(ExecutablesDescriptor descriptor) {
		File[] list = descriptor.getFiles();
		for (int i = 0; i < list.length; i++)
			mungeExecutableFileName(list[i], descriptor);
		descriptor.setExecutableName("eclipse", true); //$NON-NLS-1$
	}

	// TODO This method is a temporary hack to rename the launcher.exe files
	// to eclipse.exe (or "launcher" to "eclipse"). Eventually we will either hand-craft
	// metadata/artifacts for launchers, or alter the delta pack to contain eclipse-branded
	// launchers.
	private void mungeExecutableFileName(File file, ExecutablesDescriptor descriptor) {
		if (file.getName().equals("launcher")) { //$NON-NLS-1$
			File newFile = new File(file.getParentFile(), "eclipse"); //$NON-NLS-1$
			file.renameTo(newFile);
			descriptor.replace(file, newFile);
		} else if (file.getName().equals("launcher.exe")) { //$NON-NLS-1$
			File newFile = new File(file.getParentFile(), "eclipse.exe"); //$NON-NLS-1$
			file.renameTo(newFile);
			descriptor.replace(file, newFile);
		}
	}
}

Back to the top