Beispiel #1
0
  @Override
  public void build(final IBuildContext context, IProgressMonitor monitor) throws CoreException {
    if (!isEnabled(context)) {
      return;
    }

    final List<IResourceDescription.Delta> deltas = getRelevantDeltas(context);
    if (deltas.isEmpty()) {
      return;
    }

    StoppedTask task =
        Stopwatches.forTask(
            "org.eclipse.xtext.builder.BuilderParticipant.build(IBuildContext, IProgressMonitor)");
    try {
      task.start();

      // monitor handling
      if (monitor.isCanceled()) throw new OperationCanceledException();
      SubMonitor subMonitor =
          SubMonitor.convert(monitor, context.getBuildType() == BuildType.RECOVERY ? 5 : 3);

      EclipseResourceFileSystemAccess2 access = fileSystemAccessProvider.get();
      IProject builtProject = context.getBuiltProject();
      access.setProject(builtProject);
      Map<String, OutputConfiguration> outputConfigurations = getOutputConfigurations(context);
      refreshOutputFolders(context, outputConfigurations, subMonitor.newChild(1));
      if (subMonitor.isCanceled()) {
        throw new OperationCanceledException();
      }
      access.setOutputConfigurations(outputConfigurations);
      if (context.getBuildType() == BuildType.CLEAN
          || context.getBuildType() == BuildType.RECOVERY) {
        SubMonitor cleanMonitor =
            SubMonitor.convert(subMonitor.newChild(2), outputConfigurations.size());
        for (OutputConfiguration config : outputConfigurations.values()) {
          cleanOutput(context, config, access, cleanMonitor.newChild(1));
        }
        if (context.getBuildType() == BuildType.CLEAN) return;
      }
      Map<OutputConfiguration, Iterable<IMarker>> generatorMarkers =
          getGeneratorMarkers(builtProject, outputConfigurations.values());
      if (subMonitor.isCanceled()) {
        throw new OperationCanceledException();
      }
      doBuild(
          deltas, outputConfigurations, generatorMarkers, context, access, subMonitor.newChild(2));

    } finally {
      task.stop();
    }
  }
Beispiel #2
0
  public void build(final IBuildContext context, IProgressMonitor monitor) throws CoreException {
    if (!isEnabled(context)) {
      return;
    }

    final List<IResourceDescription.Delta> deltas = getRelevantDeltas(context);
    if (deltas.isEmpty()) {
      return;
    }

    final int numberOfDeltas = deltas.size();

    // monitor handling
    if (monitor.isCanceled()) throw new OperationCanceledException();
    SubMonitor subMonitor = SubMonitor.convert(monitor, numberOfDeltas + 3);

    EclipseResourceFileSystemAccess2 access = fileSystemAccessProvider.get();
    final IProject builtProject = context.getBuiltProject();
    access.setProject(builtProject);
    final Map<String, OutputConfiguration> outputConfigurations = getOutputConfigurations(context);
    refreshOutputFolders(context, outputConfigurations, subMonitor.newChild(1));
    access.setOutputConfigurations(outputConfigurations);
    if (context.getBuildType() == BuildType.CLEAN || context.getBuildType() == BuildType.RECOVERY) {
      SubMonitor cleanMonitor =
          SubMonitor.convert(subMonitor.newChild(1), outputConfigurations.size());
      for (OutputConfiguration config : outputConfigurations.values()) {
        cleanOutput(context, config, cleanMonitor.newChild(1));
      }
      if (context.getBuildType() == BuildType.CLEAN) return;
    }

    Map<OutputConfiguration, Iterable<IMarker>> generatorMarkers = newHashMap();
    for (OutputConfiguration config : outputConfigurations.values()) {
      if (config.isCleanUpDerivedResources()) {
        final IFolder outputFolder = builtProject.getFolder(config.getOutputDirectory());
        final Iterable<IMarker> markers =
            derivedResourceMarkers.findDerivedResourceMarkers(
                outputFolder, generatorIdProvider.getGeneratorIdentifier());
        generatorMarkers.put(config, markers);
      }
    }

    for (int i = 0; i < numberOfDeltas; i++) {
      final IResourceDescription.Delta delta = deltas.get(i);

      // monitor handling
      if (subMonitor.isCanceled()) throw new OperationCanceledException();
      subMonitor.subTask(
          "Compiling " + delta.getUri().lastSegment() + " (" + i + " of " + numberOfDeltas + ")");
      access.setMonitor(subMonitor.newChild(1));

      final String uri = delta.getUri().toString();
      final Set<IFile> derivedResources = newLinkedHashSet();
      for (OutputConfiguration config : outputConfigurations.values()) {
        if (config.isCleanUpDerivedResources()) {
          Iterable<IMarker> markers = generatorMarkers.get(config);
          for (IMarker marker : markers) {
            String source = derivedResourceMarkers.getSource(marker);
            if (source != null && source.equals(uri))
              derivedResources.add((IFile) marker.getResource());
          }
        }
      }
      access.setPostProcessor(
          new EclipseResourceFileSystemAccess2.IFileCallback() {

            public boolean beforeFileDeletion(IFile file) {
              derivedResources.remove(file);
              context.needRebuild();
              return true;
            }

            public void afterFileUpdate(IFile file) {
              handleFileAccess(file);
            }

            public void afterFileCreation(IFile file) {
              handleFileAccess(file);
            }

            protected void handleFileAccess(IFile file) {
              try {
                derivedResources.remove(file);
                derivedResourceMarkers.installMarker(file, uri);
                context.needRebuild();
              } catch (CoreException e) {
                throw new RuntimeException(e);
              }
            }
          });
      if (delta.getNew() != null) {
        handleChangedContents(delta, context, access);
      }
      access.flushSourceTraces();
      SubMonitor deleteMonitor =
          SubMonitor.convert(subMonitor.newChild(1), derivedResources.size());
      for (IFile iFile : newLinkedHashSet(derivedResources)) {
        IMarker marker = derivedResourceMarkers.findDerivedResourceMarker(iFile, uri);
        if (marker != null) marker.delete();
        if (derivedResourceMarkers.findDerivedResourceMarkers(iFile).length == 0) {
          access.deleteFile(iFile, deleteMonitor);
          context.needRebuild();
        }
      }
    }
  }