Skip to content

Instantly share code, notes, and snippets.

@virtuald
Created November 13, 2015 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save virtuald/77d14ad1f01ff61cd20e to your computer and use it in GitHub Desktop.
Save virtuald/77d14ad1f01ff61cd20e to your computer and use it in GitHub Desktop.
Takari incremental build context with indirect dependency support
package io.takari.incrementalbuild.spi;
import java.io.File;
import java.util.HashSet;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.maven.execution.scope.MojoExecutionScoped;
import io.takari.incrementalbuild.Resource;
import io.takari.incrementalbuild.ResourceMetadata;
import io.takari.incrementalbuild.ResourceStatus;
/**
* Provides a build context that allows detection of modified dependencies
* of an input file.
*
* Usage:
*
* for (resource: buildContext.registerInputs(..)) {
* if (!buildContext.isResourceDirty(resource))
* continue;
*
* // do something to file
*
* // for each indirect input file that is included by input:
* buildContext.associateIndirectInput(resource, indirectInputFile)
* }
*/
@Named
@MojoExecutionScoped
public class IndirectBuildContext extends DefaultBuildContext {
static final String INDIRECT_KEY = "indirect-resource";
@Inject
public IndirectBuildContext(BuildContextEnvironment configuration) {
super(configuration);
}
public boolean isResourceDirty(ResourceMetadata<File> resource) {
return resource.getStatus() != ResourceStatus.UNMODIFIED ||
areIndirectInputsModified(resource.getResource());
}
@SuppressWarnings("rawtypes")
public boolean areIndirectInputsModified(File resource) {
HashSet indirectResources = getAttribute(resource, INDIRECT_KEY, HashSet.class);
if (indirectResources != null) {
for (Object file: indirectResources) {
if (getResourceStatus(file) != ResourceStatus.UNMODIFIED)
return true;
}
}
return false;
}
/**
* Callers who use this will want to use registerInput et al instead
* of registerInputsAndProcess, as the latter will not return all resources
* that should be processed.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public void associateIndirectInput(Resource<File> resource, File indirectInput) {
File resourceFile = resource.getResource();
if (!isProcessedResource(resourceFile))
throw new IllegalStateException("Resource " + resourceFile + " hasn't been marked as processed");
// easy way to add an input
registerInput(indirectInput);
HashSet indirectResources = getResourceAttribute(state, resourceFile, INDIRECT_KEY, HashSet.class);
if (indirectResources == null) {
indirectResources = new HashSet<>();
setResourceAttribute(resourceFile, INDIRECT_KEY, indirectResources);
}
indirectResources.add(indirectInput);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment