Exemple #1
0
 protected void refreshOutputFolders(
     IBuildContext ctx,
     Map<String, OutputConfiguration> outputConfigurations,
     IProgressMonitor monitor)
     throws CoreException {
   SubMonitor subMonitor = SubMonitor.convert(monitor, outputConfigurations.size());
   for (OutputConfiguration config : outputConfigurations.values()) {
     SubMonitor child = subMonitor.newChild(1);
     final IProject project = ctx.getBuiltProject();
     IFolder folder = project.getFolder(config.getOutputDirectory());
     folder.refreshLocal(IResource.DEPTH_INFINITE, child);
   }
 }
Exemple #2
0
 protected void cleanOutput(
     IBuildContext ctx, OutputConfiguration config, IProgressMonitor monitor)
     throws CoreException {
   final IProject project = ctx.getBuiltProject();
   IFolder folder = project.getFolder(config.getOutputDirectory());
   if (!folder.exists()) return;
   if (config.isCanClearOutputDirectory()) {
     for (IResource resource : folder.members()) resource.delete(IResource.KEEP_HISTORY, monitor);
   } else {
     if (config.isCleanUpDerivedResources()) {
       List<IFile> resources = derivedResourceMarkers.findDerivedResources(folder, null);
       for (IFile iFile : resources) {
         iFile.delete(IResource.KEEP_HISTORY, monitor);
       }
     }
   }
 }
 /** @since 2.7 */
 protected String getCurrentSourceFolder(IBuildContext context, Delta delta) {
   Iterable<org.eclipse.xtext.util.Pair<IStorage, IProject>> storages =
       storage2UriMapper.getStorages(delta.getUri());
   for (org.eclipse.xtext.util.Pair<IStorage, IProject> pair : storages) {
     final IResource resource = (IResource) pair.getFirst();
     IProject project = pair.getSecond();
     for (OutputConfiguration output : getOutputConfigurations(context).values()) {
       for (SourceMapping sourceMapping : output.getSourceMappings()) {
         IContainer folder = ResourceUtil.getContainer(project, sourceMapping.getSourceFolder());
         if (folder.contains(resource)) {
           return sourceMapping.getSourceFolder();
         }
       }
     }
   }
   return null;
 }
 /** @since 2.6 */
 protected Set<IContainer> getOutputs(IProject project, OutputConfiguration outputConfiguration) {
   Set<IContainer> outputs = Sets.newLinkedHashSet();
   for (String outputPath : outputConfiguration.getOutputDirectories()) {
     IContainer output = getContainer(project, outputPath);
     outputs.add(output);
   }
   return outputs;
 }
 /** @since 2.7 */
 protected Set<IFile> getDerivedResources(
     Delta delta,
     final Map<String, OutputConfiguration> outputConfigurations,
     Map<OutputConfiguration, Iterable<IMarker>> generatorMarkers) {
   String uri = delta.getUri().toString();
   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());
       }
     }
   }
   return derivedResources;
 }
 /** @since 2.4 */
 protected Map<OutputConfiguration, Iterable<IMarker>> getGeneratorMarkers(
     IProject builtProject, Collection<OutputConfiguration> outputConfigurations)
     throws CoreException {
   Map<OutputConfiguration, Iterable<IMarker>> generatorMarkers = newHashMap();
   for (OutputConfiguration config : outputConfigurations) {
     if (config.isCleanUpDerivedResources()) {
       List<IMarker> markers = Lists.newArrayList();
       for (IContainer container : getOutputs(builtProject, config)) {
         Iterables.addAll(
             markers,
             derivedResourceMarkers.findDerivedResourceMarkers(
                 container, generatorIdProvider.getGeneratorIdentifier()));
       }
       generatorMarkers.put(config, markers);
     }
   }
   return generatorMarkers;
 }
 /** @since 2.5 */
 protected void cleanOutput(
     IBuildContext ctx,
     OutputConfiguration config,
     EclipseResourceFileSystemAccess2 access,
     IProgressMonitor monitor)
     throws CoreException {
   final IProject project = ctx.getBuiltProject();
   for (IContainer container : getOutputs(project, config)) {
     if (!container.exists()) {
       return;
     }
     if (config.isCanClearOutputDirectory()) {
       for (IResource resource : container.members()) {
         if (!config.isKeepLocalHistory()) {
           resource.delete(IResource.NONE, monitor);
         } else if (access == null) {
           resource.delete(IResource.KEEP_HISTORY, monitor);
         } else {
           delete(resource, config, access, monitor);
         }
       }
     } else if (config.isCleanUpDerivedResources()) {
       List<IFile> resources = derivedResourceMarkers.findDerivedResources(container, null);
       for (IFile iFile : resources) {
         if (monitor.isCanceled()) {
           throw new OperationCanceledException();
         }
         if (access != null) {
           access.deleteFile(iFile, config.getName(), monitor);
         } else {
           iFile.delete(
               config.isKeepLocalHistory() ? IResource.KEEP_HISTORY : IResource.NONE, monitor);
         }
       }
     }
   }
 }
 private void delete(
     IResource resource,
     OutputConfiguration config,
     EclipseResourceFileSystemAccess2 access,
     IProgressMonitor monitor)
     throws CoreException {
   if (monitor.isCanceled()) {
     throw new OperationCanceledException();
   }
   if (resource instanceof IContainer) {
     IContainer container = (IContainer) resource;
     for (IResource child : container.members()) {
       delete(child, config, access, monitor);
     }
     container.delete(IResource.KEEP_HISTORY, monitor);
   } else if (resource instanceof IFile) {
     IFile file = (IFile) resource;
     access.deleteFile(file, config.getName(), monitor);
   } else {
     resource.delete(IResource.KEEP_HISTORY, monitor);
   }
 }
Exemple #9
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();
        }
      }
    }
  }
  /** @return a set of {@link OutputConfiguration} available for the generator */
  public Set<OutputConfiguration> getOutputConfigurations() {
    OutputConfiguration defaultOutput = new OutputConfiguration(IFileSystemAccess.DEFAULT_OUTPUT);
    defaultOutput.setDescription("Output Folder");
    defaultOutput.setOutputDirectory("./src-gen");
    defaultOutput.setOverrideExistingResources(true);
    defaultOutput.setCreateOutputDirectory(true);
    defaultOutput.setCleanUpDerivedResources(true);
    defaultOutput.setSetDerivedProperty(true);

    OutputConfiguration onceOutput = new OutputConfiguration(SRC_OUTPUT);
    onceOutput.setDescription("Output Folder (once)");
    onceOutput.setOutputDirectory("./src");
    onceOutput.setOverrideExistingResources(true);
    onceOutput.setCreateOutputDirectory(true);
    onceOutput.setCleanUpDerivedResources(true);
    onceOutput.setSetDerivedProperty(true);
    return newHashSet(defaultOutput, onceOutput);
  }