| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2008, 2017 Code 9 and others. |
| 3 |
* |
| 4 |
* This |
| 5 |
* program and the accompanying materials are made available under the terms of |
| 6 |
* the Eclipse Public License 2.0 which accompanies this distribution, and is |
| 7 |
* available at |
| 8 |
* https://www.eclipse.org/legal/epl-2.0/ |
| 9 |
* |
| 10 |
* SPDX-License-Identifier: EPL-2.0 |
| 11 |
* |
| 12 |
* Contributors: |
| 13 |
* Code 9 - initial API and implementation |
| 14 |
* IBM - ongoing development |
| 15 |
******************************************************************************/ |
| 16 |
package org.eclipse.equinox.p2.publisher.actions; |
| 17 |
|
| 18 |
import java.io.File; |
| 19 |
import java.util.*; |
| 20 |
import org.eclipse.core.runtime.*; |
| 21 |
import org.eclipse.equinox.internal.p2.core.helpers.FileUtils.IPathComputer; |
| 22 |
import org.eclipse.equinox.p2.metadata.*; |
| 23 |
import org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitDescription; |
| 24 |
import org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitFragmentDescription; |
| 25 |
import org.eclipse.equinox.p2.metadata.expression.IMatchExpression; |
| 26 |
import org.eclipse.equinox.p2.publisher.*; |
| 27 |
import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor; |
| 28 |
import org.eclipse.equinox.spi.p2.publisher.PublisherHelper; |
| 29 |
|
| 30 |
public class RootFilesAction extends AbstractPublisherAction { |
| 31 |
private String idBase; |
| 32 |
private Version version; |
| 33 |
private String flavor; |
| 34 |
private boolean createParent = true; |
| 35 |
|
| 36 |
/** |
| 37 |
* Returns the id of the top level IU published by this action for the given id and flavor. |
| 38 |
* @param id the id of the application being published |
| 39 |
* @param flavor the flavor being published |
| 40 |
* @return the if for ius published by this action |
| 41 |
*/ |
| 42 |
public static String computeIUId(String id, String flavor) { |
| 43 |
return flavor + id + ".rootfiles"; //$NON-NLS-1$ |
| 44 |
} |
| 45 |
|
| 46 |
public RootFilesAction(IPublisherInfo info, String idBase, Version version, String flavor) { |
| 47 |
this.idBase = idBase == null ? "org.eclipse" : idBase; //$NON-NLS-1$ |
| 48 |
this.version = version; |
| 49 |
this.flavor = flavor; |
| 50 |
} |
| 51 |
|
| 52 |
public void setCreateParent(boolean createParent) { |
| 53 |
this.createParent = createParent; |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
public IStatus perform(IPublisherInfo publisherInfo, IPublisherResult results, IProgressMonitor monitor) { |
| 58 |
setPublisherInfo(publisherInfo); |
| 59 |
IPublisherResult innerResult = new PublisherResult(); |
| 60 |
// we have N platforms, generate a CU for each |
| 61 |
// TODO try and find common properties across platforms |
| 62 |
String[] configSpecs = publisherInfo.getConfigurations(); |
| 63 |
for (String configSpec : configSpecs) { |
| 64 |
if (monitor.isCanceled()) |
| 65 |
return Status.CANCEL_STATUS; |
| 66 |
generateRootFileIUs(configSpec, innerResult); |
| 67 |
} |
| 68 |
// merge the IUs into the final result as non-roots and create a parent IU that captures them all |
| 69 |
results.merge(innerResult, IPublisherResult.MERGE_ALL_NON_ROOT); |
| 70 |
if (createParent) |
| 71 |
publishTopLevelRootFilesIU(innerResult.getIUs(null, IPublisherResult.ROOT), results); |
| 72 |
if (monitor.isCanceled()) |
| 73 |
return Status.CANCEL_STATUS; |
| 74 |
return Status.OK_STATUS; |
| 75 |
} |
| 76 |
|
| 77 |
private void publishTopLevelRootFilesIU(Collection<? extends IVersionedId> children, IPublisherResult result) { |
| 78 |
InstallableUnitDescription descriptor = createParentIU(children, computeIUId(idBase, flavor), version); |
| 79 |
descriptor.setSingleton(true); |
| 80 |
IInstallableUnit rootIU = MetadataFactory.createInstallableUnit(descriptor); |
| 81 |
if (rootIU == null) |
| 82 |
return; |
| 83 |
result.addIU(rootIU, IPublisherResult.ROOT); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Generates IUs and CUs for the files that make up the root files for a given |
| 88 |
* ws/os/arch combination. |
| 89 |
*/ |
| 90 |
private void generateRootFileIUs(String configSpec, IPublisherResult result) { |
| 91 |
// Create the IU for the executable |
| 92 |
InstallableUnitDescription iu = new MetadataFactory.InstallableUnitDescription(); |
| 93 |
iu.setSingleton(true); |
| 94 |
String idPrefix = idBase + ".rootfiles"; //$NON-NLS-1$ |
| 95 |
String iuId = idPrefix + '.' + createIdString(configSpec); |
| 96 |
iu.setId(iuId); |
| 97 |
iu.setVersion(version); |
| 98 |
IMatchExpression<IInstallableUnit> filter = createFilterSpec(configSpec); |
| 99 |
iu.setFilter(filter); |
| 100 |
IArtifactKey key = PublisherHelper.createBinaryArtifactKey(iuId, version); |
| 101 |
iu.setArtifacts(new IArtifactKey[] {key}); |
| 102 |
iu.setTouchpointType(PublisherHelper.TOUCHPOINT_NATIVE); |
| 103 |
IProvidedCapability launcherCapability = MetadataFactory.createProvidedCapability(flavor + idBase, idPrefix, version); |
| 104 |
iu.setCapabilities(new IProvidedCapability[] {PublisherHelper.createSelfCapability(iuId, version), launcherCapability}); |
| 105 |
result.addIU(MetadataFactory.createInstallableUnit(iu), IPublisherResult.ROOT); |
| 106 |
|
| 107 |
// Create the CU that installs/configures the executable |
| 108 |
InstallableUnitFragmentDescription cu = new InstallableUnitFragmentDescription(); |
| 109 |
String configUnitId = flavor + iuId; |
| 110 |
cu.setId(configUnitId); |
| 111 |
cu.setVersion(version); |
| 112 |
cu.setFilter(filter); |
| 113 |
cu.setHost(new IRequirement[] {MetadataFactory.createRequirement(IInstallableUnit.NAMESPACE_IU_ID, iuId, new VersionRange(version, true, version, true), null, false, false)}); |
| 114 |
cu.setProperty(InstallableUnitDescription.PROP_TYPE_FRAGMENT, Boolean.TRUE.toString()); |
| 115 |
|
| 116 |
//TODO bug 218890, would like the fragment to provide the launcher capability as well, but can't right now. |
| 117 |
cu.setCapabilities(new IProvidedCapability[] {PublisherHelper.createSelfCapability(configUnitId, version)}); |
| 118 |
|
| 119 |
cu.setTouchpointType(PublisherHelper.TOUCHPOINT_NATIVE); |
| 120 |
Map<String, String> touchpointData = new HashMap<>(); |
| 121 |
String configurationData = "unzip(source:@artifact, target:${installFolder});"; //$NON-NLS-1$ |
| 122 |
touchpointData.put("install", configurationData); //$NON-NLS-1$ |
| 123 |
String unConfigurationData = "cleanupzip(source:@artifact, target:${installFolder});"; //$NON-NLS-1$ |
| 124 |
touchpointData.put("uninstall", unConfigurationData); //$NON-NLS-1$ |
| 125 |
processTouchpointAdvice(cu, touchpointData, info, configSpec); |
| 126 |
IInstallableUnit unit = MetadataFactory.createInstallableUnit(cu); |
| 127 |
result.addIU(unit, IPublisherResult.ROOT); |
| 128 |
|
| 129 |
if ((info.getArtifactOptions() & (IPublisherInfo.A_INDEX | IPublisherInfo.A_PUBLISH)) > 0) { |
| 130 |
// Create the artifact descriptor. we have several files so no path on disk |
| 131 |
IArtifactDescriptor descriptor = PublisherHelper.createArtifactDescriptor(info, key, null); |
| 132 |
IRootFilesAdvice advice = getAdvice(configSpec); |
| 133 |
publishArtifact(descriptor, advice.getIncludedFiles(), advice.getExcludedFiles(), info, createPrefixComputer(advice.getRoot())); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
private IPathComputer createPrefixComputer(File root) { |
| 138 |
if (root == null) |
| 139 |
return createParentPrefixComputer(1); |
| 140 |
return createRootPrefixComputer(root); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Compiles the <class>IRootFilesAdvice</class> from the <code>info</code> into one <class>IRootFilesAdvice</class> |
| 145 |
* and returns the result. |
| 146 |
* @param configSpec |
| 147 |
* @return a compilation of <class>IRootfilesAdvice</class> from the <code>info</code>. |
| 148 |
*/ |
| 149 |
private IRootFilesAdvice getAdvice(String configSpec) { |
| 150 |
Collection<IRootFilesAdvice> advice = info.getAdvice(configSpec, true, null, null, IRootFilesAdvice.class); |
| 151 |
ArrayList<File> inclusions = new ArrayList<>(); |
| 152 |
ArrayList<File> exclusions = new ArrayList<>(); |
| 153 |
File root = null; |
| 154 |
for (IRootFilesAdvice entry : advice) { |
| 155 |
// TODO for now we simply get root from the first advice that has one |
| 156 |
if (root == null) |
| 157 |
root = entry.getRoot(); |
| 158 |
File[] list = entry.getIncludedFiles(); |
| 159 |
if (list != null) |
| 160 |
inclusions.addAll(Arrays.asList(list)); |
| 161 |
list = entry.getExcludedFiles(); |
| 162 |
if (list != null) |
| 163 |
exclusions.addAll(Arrays.asList(list)); |
| 164 |
} |
| 165 |
File[] includeList = inclusions.toArray(new File[inclusions.size()]); |
| 166 |
File[] excludeList = exclusions.toArray(new File[exclusions.size()]); |
| 167 |
return new RootFilesAdvice(root, includeList, excludeList, configSpec); |
| 168 |
} |
| 169 |
|
| 170 |
} |