@Override
  public Path getCommandPath(Path command) {
    if (command.isAbsolute()) {
      return command;
    }

    if (Platform.getOS().equals(Platform.OS_WIN32)) {
      if (!command.toString().endsWith(".exe")) { // $NON-NLS-1$
        command = Paths.get(command.toString() + ".exe"); // $NON-NLS-1$
      }
    }

    if (path != null) {
      for (Path p : path) {
        Path c = p.resolve(command);
        if (Files.isExecutable(c)) {
          return c;
        }
      }
    }

    // Look for it in the path environment var
    IEnvironmentVariable myPath = getVariable("PATH"); // $NON-NLS-1$
    String path = myPath != null ? myPath.getValue() : System.getenv("PATH"); // $NON-NLS-1$
    for (String entry : path.split(File.pathSeparator)) {
      Path entryPath = Paths.get(entry);
      Path cmdPath = entryPath.resolve(command);
      if (Files.isExecutable(cmdPath)) {
        return cmdPath;
      }
    }

    return null;
  }
 @Override
 public void handleEvent(CProjectDescriptionEvent event) {
   // we are only interested in about to apply
   if (event.getEventType() != CProjectDescriptionEvent.ABOUT_TO_APPLY) return;
   ICProjectDescription projDesc = event.getNewCProjectDescription();
   IEnvironmentVariableManager envManager = CCorePlugin.getDefault().getBuildEnvironmentManager();
   IContributedEnvironment contribEnv = envManager.getContributedEnvironment();
   if (projDesc.getActiveConfiguration() != null) {
     IEnvironmentVariable var =
         contribEnv.getVariable(
             ArduinoConst.ENV_KEY_JANTJE_PLATFORM_FILE, projDesc.getActiveConfiguration());
     if (var != null) {
       IPath platformPath = new Path(var.getValue());
       ArduinoHelpers.setProjectPathVariables(
           projDesc.getProject(), platformPath.removeLastSegments(1));
       ArduinoHelpers.setTheEnvironmentVariables(
           projDesc.getProject(), projDesc.getActiveConfiguration(), false);
       try {
         ArduinoHelpers.addArduinoCodeToProject(
             projDesc.getProject(), projDesc.getActiveConfiguration());
       } catch (CoreException e1) {
         Common.log(
             new Status(
                 IStatus.ERROR, ArduinoConst.CORE_PLUGIN_ID, "Error adding the arduino code", e1));
       }
       ArduinoLibraries.reAttachLibrariesToProject(projDesc.getActiveConfiguration());
     }
   }
 }
Exemple #3
0
  /**
   * Gets the CDT environment from the CDT project's configuration referenced by the launch
   *
   * @since 3.0
   */
  public static String[] getLaunchEnvironment(ILaunchConfiguration config) throws CoreException {
    // Get the project
    String projectName =
        config.getAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String) null);
    if (projectName == null) {
      return null;
    }
    projectName = projectName.trim();
    if (projectName.length() == 0) {
      return null;
    }
    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
    if (project == null || !project.isAccessible()) return null;

    ICProjectDescription projDesc = CoreModel.getDefault().getProjectDescription(project, false);

    // Not a CDT project?
    if (projDesc == null) return null;

    String buildConfigID =
        config.getAttribute(
            ICDTLaunchConfigurationConstants.ATTR_PROJECT_BUILD_CONFIG_ID, ""); // $NON-NLS-1$
    ICConfigurationDescription cfg = null;
    if (buildConfigID.length() != 0) cfg = projDesc.getConfigurationById(buildConfigID);

    // if configuration is null fall-back to active
    if (cfg == null) cfg = projDesc.getActiveConfiguration();

    // Environment variables and inherited vars
    HashMap<String, String> envMap = new HashMap<String, String>();
    IEnvironmentVariable[] vars =
        CCorePlugin.getDefault().getBuildEnvironmentManager().getVariables(cfg, true);
    for (IEnvironmentVariable var : vars) envMap.put(var.getName(), var.getValue());

    // Add variables from build info
    ICdtVariable[] build_vars = CCorePlugin.getDefault().getCdtVariableManager().getVariables(cfg);
    for (ICdtVariable var : build_vars) {
      try {
        envMap.put(var.getName(), var.getStringValue());
      } catch (CdtVariableException e) {
        // Some Eclipse dynamic variables can't be resolved dynamically... we don't care.
      }
    }

    // Turn it into an envp format
    List<String> strings = new ArrayList<String>(envMap.size());
    for (Entry<String, String> entry : envMap.entrySet()) {
      StringBuffer buffer = new StringBuffer(entry.getKey());
      buffer.append('=').append(entry.getValue());
      strings.add(buffer.toString());
    }

    return strings.toArray(new String[strings.size()]);
  }
 public IEnvironmentVariable[] getVariables(ICConfigurationDescription des) {
   EnvVarCollector cr = EnvironmentVariableManager.getVariables(getContextInfo(des), true);
   if (cr != null) {
     EnvVarDescriptor collected[] = cr.toArray(true);
     List<IEnvironmentVariable> vars = new ArrayList<IEnvironmentVariable>(collected.length);
     IEnvironmentVariable var;
     IEnvironmentContextInfo info = new DefaultEnvironmentContextInfo(des); // getContextInfo(des);
     for (int i = 0; i < collected.length; i++) {
       var = collected[i];
       var = EnvironmentVariableManager.getVariable(var.getName(), info, true);
       if (var != null) vars.add(var);
     }
     return vars.toArray(new EnvVarDescriptor[vars.size()]);
   }
   return new EnvVarDescriptor[0];
 }
 private IEnvironmentVariable[] combineVariables(
     IEnvironmentVariable[] oldVariables, IEnvironmentVariable[] newVariables) {
   Map<String, IEnvironmentVariable> vars =
       new HashMap<String, IEnvironmentVariable>(oldVariables.length + newVariables.length);
   for (IEnvironmentVariable variable : oldVariables) vars.put(variable.getName(), variable);
   for (IEnvironmentVariable variable : newVariables) {
     if (!vars.containsKey(variable.getName())) vars.put(variable.getName(), variable);
     else
       vars.put(
           variable.getName(),
           EnvVarOperationProcessor.performOperation(vars.get(variable.getName()), variable));
   }
   return vars.values().toArray(new IEnvironmentVariable[vars.size()]);
 }
 public IEnvironmentVariable addVariable(
     IEnvironmentVariable var, ICConfigurationDescription des) {
   return addVariable(var.getName(), var.getValue(), var.getOperation(), var.getDelimiter(), des);
 }