Ejemplo n.º 1
0
  /**
   * Populate properties with everything required for the SonarLint analysis in issues mode.
   *
   * @param monitor
   * @param properties
   * @return
   */
  public Properties configureAnalysis(
      final IProgressMonitor monitor, List<SonarLintProperty> extraProps) {
    Properties properties = new Properties();
    IProject project = request.getProject();
    final File baseDir = project.getLocation().toFile();
    IPath projectSpecificWorkDir = project.getWorkingLocation(SonarLintCorePlugin.PLUGIN_ID);

    // Preview mode by default
    properties.setProperty(
        SonarLintProperties.ANALYSIS_MODE, SonarLintProperties.ANALYSIS_MODE_ISSUES);

    // Configuration by configurators (common and language specific)
    configure(project, this.request.getOnlyOnFiles(), properties, monitor);

    // Append workspace and project properties
    for (SonarLintProperty sonarProperty : extraProps) {
      properties.put(sonarProperty.getName(), sonarProperty.getValue());
    }
    if (this.request.getOnlyOnFiles() != null) {
      Collection<String> paths = new ArrayList<>(this.request.getOnlyOnFiles().size());
      for (IFile file : this.request.getOnlyOnFiles()) {
        MarkerUtils.deleteIssuesMarkers(file);
        paths.add(file.getProjectRelativePath().toString());
      }
      ProjectConfigurator.setPropertyList(properties, "sonar.tests", paths);
      ProjectConfigurator.setPropertyList(properties, "sonar.sources", paths);
    } else {
      MarkerUtils.deleteIssuesMarkers(project);
    }

    properties.setProperty(SonarLintProperties.PROJECT_BASEDIR, baseDir.toString());
    properties.setProperty(SonarLintProperties.WORK_DIR, projectSpecificWorkDir.toString());

    return properties;
  }
Ejemplo n.º 2
0
  @Override
  protected IStatus run(SonarRunnerFacade runner, final IProgressMonitor monitor) {

    // Configure
    Properties properties = configureAnalysis(monitor, extraProps);

    // Analyze
    Collection<File> tmpToDelete = new ArrayList<>();
    try {
      if (request.getOnlyOnFiles() != null) {
        IProject project = request.getProject();
        final File baseDir = project.getLocation().toFile();
        handleLinkedFiles(tmpToDelete, baseDir);
      }
      run(request.getProject(), properties, runner, monitor);
      analysisCompleted(properties, monitor);
    } catch (Exception e) {
      SonarLintCorePlugin.getDefault()
          .error("Error during execution of SonarLint analysis" + System.lineSeparator(), e);
      return new Status(
          Status.WARNING,
          SonarLintCorePlugin.PLUGIN_ID,
          "Error when executing SonarLint analysis",
          e);
    } finally {
      for (File f : tmpToDelete) {
        try {
          f.delete();
        } catch (Exception e) {
          SonarLintCorePlugin.getDefault()
              .error("Unable to delete temporary file" + System.lineSeparator(), e);
        }
      }
    }
    if (monitor.isCanceled()) {
      return Status.CANCEL_STATUS;
    }

    return Status.OK_STATUS;
  }
Ejemplo n.º 3
0
 private static String jobTitle(AnalyzeProjectRequest request) {
   if (request.getOnlyOnFiles() == null) {
     return "SonarLint analysis of project " + request.getProject().getName();
   }
   if (request.getOnlyOnFiles().size() == 1) {
     return "SonarLint analysis of file "
         + request.getOnlyOnFiles().iterator().next().getProjectRelativePath().toString()
         + " (Project "
         + request.getProject().getName()
         + ")";
   }
   return "SonarLint analysis of project "
       + request.getProject().getName()
       + " ("
       + request.getOnlyOnFiles().size()
       + " files)";
 }
Ejemplo n.º 4
0
 private void handleLinkedFiles(Collection<File> tmpToDelete, final File baseDir) {
   // Handle linked files
   for (IFile file : request.getOnlyOnFiles()) {
     if (file.isLinked()) {
       File tmp = new File(baseDir, file.getProjectRelativePath().toString());
       SonarLintCorePlugin.getDefault()
           .debug(
               file.getName()
                   + " is a linked resource. Will create a temporary copy"
                   + System.lineSeparator());
       try {
         Files.copy(file.getContents(), tmp.toPath());
         tmpToDelete.add(tmp);
       } catch (IOException | CoreException e) {
         SonarLintCorePlugin.getDefault()
             .error(
                 "Unable to create temporary copy for linked resource" + System.lineSeparator(),
                 e);
       }
     }
   }
 }
Ejemplo n.º 5
0
 public AnalyzeProjectJob(AnalyzeProjectRequest request) {
   super(jobTitle(request), SonarLintProject.getInstance(request.getProject()));
   this.request = request;
   this.extraProps = PreferencesUtils.getExtraPropertiesForLocalAnalysis(request.getProject());
 }