Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 24439e69d5d4e10645d19bd82e401a4d7601bc70 (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
package org.eclipse.cdt.debug.core.launch;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.cdt.debug.core.CDebugCorePlugin;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.launchbar.core.AbstractLaunchConfigProvider;
import org.eclipse.launchbar.core.ILaunchDescriptor;
import org.eclipse.launchbar.core.target.ILaunchTarget;
import org.eclipse.launchbar.core.target.ILaunchTargetManager;

/**
 * Core launch configuration provider used by generic target types.
 * 
 * @since 8.3
 */
public class CoreBuildGenericLaunchConfigProvider extends AbstractLaunchConfigProvider {

	private static final String ATTR_OS = CDebugCorePlugin.PLUGIN_ID + ".target_os"; //$NON-NLS-1$
	private static final String ATTR_ARCH = CDebugCorePlugin.PLUGIN_ID + ".target_arch"; //$NON-NLS-1$
	private static final String EMPTY = ""; //$NON-NLS-1$

	private Map<IProject, Map<String, ILaunchConfiguration>> configs = new HashMap<>();

	@Override
	public boolean supports(ILaunchDescriptor descriptor, ILaunchTarget target) throws CoreException {
		return target.getTypeId().equals(GenericTargetTypeProvider.TYPE_ID);
	}

	@Override
	public ILaunchConfigurationType getLaunchConfigurationType(ILaunchDescriptor descriptor, ILaunchTarget target)
			throws CoreException {
		return DebugPlugin.getDefault().getLaunchManager()
				.getLaunchConfigurationType(CoreBuildGenericLaunchConfigDelegate.TYPE_ID);
	}

	@Override
	public ILaunchConfiguration getLaunchConfiguration(ILaunchDescriptor descriptor, ILaunchTarget target)
			throws CoreException {
		ILaunchConfiguration config = null;
		IProject project = descriptor.getAdapter(IProject.class);
		if (project != null) {
			Map<String, ILaunchConfiguration> projectConfigs = configs.get(project);
			if (projectConfigs == null) {
				projectConfigs = new HashMap<>();
				configs.put(project, projectConfigs);
			}

			String os = target.getAttribute(ILaunchTarget.ATTR_OS, EMPTY);
			String arch = target.getAttribute(ILaunchTarget.ATTR_ARCH, EMPTY);
			String targetConfig = os + '.' + arch;
			config = projectConfigs.get(targetConfig);
			if (config == null) {
				config = createLaunchConfiguration(descriptor, target);
			}
		}
		return config;
	}

	@Override
	protected void populateLaunchConfiguration(ILaunchDescriptor descriptor, ILaunchTarget target,
			ILaunchConfigurationWorkingCopy workingCopy) throws CoreException {
		super.populateLaunchConfiguration(descriptor, target, workingCopy);

		// Set the project
		IProject project = descriptor.getAdapter(IProject.class);
		workingCopy.setMappedResources(new IResource[] { project });

		// set the OS and ARCH
		String os = target.getAttribute(ILaunchTarget.ATTR_OS, EMPTY);
		workingCopy.setAttribute(ATTR_OS, os);

		String arch = target.getAttribute(ILaunchTarget.ATTR_ARCH, EMPTY);
		workingCopy.setAttribute(ATTR_ARCH, arch);
	}

	@Override
	public boolean launchConfigurationAdded(ILaunchConfiguration configuration) throws CoreException {
		// TODO make sure it's the correct type
		if (ownsLaunchConfiguration(configuration)) {
			IProject project = configuration.getMappedResources()[0].getProject();
			Map<String, ILaunchConfiguration> projectConfigs = configs.get(project);
			if (projectConfigs == null) {
				projectConfigs = new HashMap<>();
				configs.put(project, projectConfigs);
			}

			String os = configuration.getAttribute(ATTR_OS, EMPTY);
			String arch = configuration.getAttribute(ATTR_ARCH, EMPTY);
			String targetConfig = os + '.' + arch;
			projectConfigs.put(targetConfig, configuration);
			return true;
		}
		return false;
	}

	@Override
	public boolean launchConfigurationRemoved(ILaunchConfiguration configuration) throws CoreException {
		for (Entry<IProject, Map<String, ILaunchConfiguration>> projectEntry : configs.entrySet()) {
			Map<String, ILaunchConfiguration> projectConfigs = projectEntry.getValue();
			for (Entry<String, ILaunchConfiguration> entry : projectConfigs.entrySet()) {
				if (configuration.equals(entry.getValue())) {
					projectConfigs.remove(entry.getKey());
					if (projectConfigs.isEmpty()) {
						configs.remove(projectEntry.getKey());
					}
					return true;
				}
			}
		}
		return false;
	}

	@Override
	public boolean launchConfigurationChanged(ILaunchConfiguration configuration) throws CoreException {
		// nothing to do
		return false;
	}

	@Override
	public void launchDescriptorRemoved(ILaunchDescriptor descriptor) throws CoreException {
		IProject project = descriptor.getAdapter(IProject.class);
		if (project != null) {
			Map<String, ILaunchConfiguration> projectConfigs = configs.get(project);
			if (projectConfigs != null) {
				for (ILaunchConfiguration config : projectConfigs.values()) {
					config.delete();
				}
			}
		}
	}

	@Override
	public void launchTargetRemoved(ILaunchTarget target) throws CoreException {
		// Any other targets have the same OS and ARCH?
		String os = target.getAttribute(ILaunchTarget.ATTR_OS, EMPTY);
		String arch = target.getAttribute(ILaunchTarget.ATTR_ARCH, EMPTY);

		ILaunchTargetManager targetManager = CDebugCorePlugin.getService(ILaunchTargetManager.class);
		for (ILaunchTarget t : targetManager.getLaunchTargets()) {
			if (!target.equals(t) && os.equals(t.getAttribute(ILaunchTarget.ATTR_OS, EMPTY))
					&& arch.equals(t.getAttribute(ILaunchTarget.ATTR_ARCH, EMPTY))) {
				// Yup, nothing to do then
				return;
			}
		}

		for (Entry<IProject, Map<String, ILaunchConfiguration>> projectEntry : configs.entrySet()) {
			Map<String, ILaunchConfiguration> projectConfigs = projectEntry.getValue();
			ILaunchConfiguration config = projectConfigs.get(os);
			if (config != null) {
				config.delete();
			}
		}
	}

}

Back to the top