Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 998a0341713e5f6421950e387e26bc18ae5769df (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
package org.eclipse.emf.parsley.tests.pde.utils;
/*******************************************************************************
 * Copyright (c) 2013 RCP Vision (http://www.rcp-vision.com) 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:
 * Lorenzo Bettini - Initial contribution and API
 *******************************************************************************/


import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.osgi.internal.framework.EquinoxBundle;
import org.eclipse.osgi.storage.BundleInfo.Generation;
import org.eclipse.pde.core.target.ITargetDefinition;
import org.eclipse.pde.core.target.ITargetLocation;
import org.eclipse.pde.core.target.ITargetPlatformService;
import org.eclipse.pde.core.target.LoadTargetDefinitionJob;
import org.eclipse.pde.internal.core.target.TargetPlatformService;
import org.osgi.framework.Bundle;

/**
 * Implements workaround suggested here:
 * https://bugs.eclipse.org/bugs/show_bug.cgi?id=343156
 * This is required when running SwtBot tests in Tycho
 * that requires the PDE, for example, for testing that the
 * imported projects compile fine, or if they use the DSL, which
 * requires PDE projects dependencies.
 * 
 * @author Lorenzo Bettini - some adaptations
 */
@SuppressWarnings("restriction")
public class PDETargetPlatformUtils {
	
	private static boolean targetPlatformAlreadySet = false;

	/**
	 * Sets a target platform in the test platform to get workspace builds OK
	 * with PDE.
	 * 
	 * @throws Exception
	 */
	public static void setTargetPlatform() throws Exception {
		if (System.getProperty("buildingWithTycho") != null) {
			if (targetPlatformAlreadySet) {
				System.out.println("Target platform already set");
				return;
			}
			targetPlatformAlreadySet = true;
			System.out.println("Generating a target platform");
		} else {
			System.out.println("Using the Workbench's target platform");
			return;
		}
		
		ITargetPlatformService tpService = TargetPlatformService.getDefault();
		ITargetDefinition targetDef = tpService.newTarget();
		targetDef.setName("Tycho platform");
		Bundle[] bundles = Platform.getBundle("org.eclipse.core.runtime").getBundleContext().getBundles();
		List<ITargetLocation> bundleContainers = new ArrayList<ITargetLocation>();
		Set<File> dirs = new HashSet<File>();
		System.out.println("Bundles for the target platform:");
		for (Bundle bundle : bundles) {
			System.out.print(bundle);
//			AbstractBundle bundleImpl = (AbstractBundle) bundle;
//			BaseData bundleData = (BaseData) bundleImpl.getBundleData();
			EquinoxBundle bundleImpl = (EquinoxBundle) bundle;
			Generation generation = (Generation) bundleImpl.getModule().getCurrentRevision().getRevisionInfo();
			File file = generation.getBundleFile().getBaseFile();
			File folder = file.getParentFile();
			if (!dirs.contains(folder)) {
				dirs.add(folder);
				bundleContainers.add(tpService.newDirectoryLocation(folder.getAbsolutePath()));
			}
		}
		System.out.println("");
		System.out.println("Bundles added the target platform.");
		targetDef.setTargetLocations(bundleContainers.toArray(new ITargetLocation[bundleContainers.size()]));
		targetDef.setArch(Platform.getOSArch());
		targetDef.setOS(Platform.getOS());
		targetDef.setWS(Platform.getWS());
		targetDef.setNL(Platform.getNL());
		// targetDef.setJREContainer()
		tpService.saveTargetDefinition(targetDef);

		System.out.print("Loading target platform... ");
		Job job = new LoadTargetDefinitionJob(targetDef);
		job.schedule();
		job.join();
		System.out.println("DONE.");
	}
}

Back to the top