|
| 1 | +package io.sloeber.core.eclipseIntegrations; |
| 2 | + |
| 3 | +import static io.sloeber.core.common.Const.*; |
| 4 | + |
| 5 | +import org.eclipse.cdt.core.CCorePlugin; |
| 6 | +import org.eclipse.cdt.core.envvar.IEnvironmentVariableManager; |
| 7 | +import org.eclipse.cdt.core.settings.model.ICConfigurationDescription; |
| 8 | +import org.eclipse.cdt.core.settings.model.ICProjectDescription; |
| 9 | +import org.eclipse.core.resources.IProject; |
| 10 | +import org.eclipse.core.resources.IWorkspaceRoot; |
| 11 | +import org.eclipse.core.resources.ResourcesPlugin; |
| 12 | +import org.eclipse.core.runtime.CoreException; |
| 13 | +import org.eclipse.core.runtime.IStatus; |
| 14 | +import org.eclipse.core.runtime.Path; |
| 15 | +import org.eclipse.core.runtime.Status; |
| 16 | +import org.eclipse.core.variables.IDynamicVariable; |
| 17 | +import org.eclipse.core.variables.IDynamicVariableResolver; |
| 18 | +import org.eclipse.core.variables.IStringVariableManager; |
| 19 | +import org.eclipse.core.variables.VariablesPlugin; |
| 20 | + |
| 21 | +import io.sloeber.core.Messages; |
| 22 | + |
| 23 | +public class CDT_EnvironmentVariableResolver implements IDynamicVariableResolver { |
| 24 | + |
| 25 | + @Override |
| 26 | + public String resolveValue(IDynamicVariable variable, String varName) throws CoreException { |
| 27 | + try { |
| 28 | + ICConfigurationDescription confDesc = getConfigurationDescription(); |
| 29 | + return getBuildEnvironmentVariable(confDesc, varName); |
| 30 | + |
| 31 | + } catch (@SuppressWarnings("unused") Exception dontCare) { |
| 32 | + Status iStatus = new Status(IStatus.ERROR, PLUGIN_ID, Messages.projectNotFoundInGUI); |
| 33 | + throw new CoreException(iStatus); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Find the active configuration of the project selected in the project manager |
| 39 | + * |
| 40 | + * @return The configuration description or null if not found |
| 41 | + */ |
| 42 | + private static ICConfigurationDescription getConfigurationDescription() { |
| 43 | + IProject project = getGUISelectedProject(); |
| 44 | + |
| 45 | + if (project != null && project.exists() && project.isOpen()) { |
| 46 | + CCorePlugin cCorePlugin = CCorePlugin.getDefault(); |
| 47 | + ICProjectDescription prjCDesc = cCorePlugin.getProjectDescription(project); |
| 48 | + return prjCDesc.getActiveConfiguration(); |
| 49 | + } |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + static private String getBuildEnvironmentVariable(ICConfigurationDescription configurationDescription, |
| 54 | + String envName) { |
| 55 | + try { |
| 56 | + IEnvironmentVariableManager envManager = CCorePlugin.getDefault().getBuildEnvironmentManager(); |
| 57 | + return envManager.getVariable(envName, configurationDescription, true).getValue(); |
| 58 | + } catch (@SuppressWarnings("unused") Exception e) {// ignore all errors and return the default value |
| 59 | + } |
| 60 | + return new String(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Returns the project selected GUI. Uses the ${selected_resource_path} variable |
| 65 | + * to determine the selected resource. This variable is provided by the debug.ui |
| 66 | + * plug-in. Selected resource resolution is only available when the debug.ui |
| 67 | + * plug-in is present. |
| 68 | + * |
| 69 | + * @return project related to selected resource in the gui if no project is |
| 70 | + * found null is returned |
| 71 | + * |
| 72 | + */ |
| 73 | + private static IProject getGUISelectedProject() { |
| 74 | + try { |
| 75 | + IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager(); |
| 76 | + IWorkspaceRoot workSpaceRoot = ResourcesPlugin.getWorkspace().getRoot(); |
| 77 | + String pathString; |
| 78 | + |
| 79 | + pathString = manager.performStringSubstitution("${selected_resource_path}"); //$NON-NLS-1$ |
| 80 | + |
| 81 | + return workSpaceRoot.findMember(new Path(pathString)).getProject(); |
| 82 | + } catch (@SuppressWarnings("unused") CoreException e) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments