Search in sources :

Example 11 with SimpleWorkResult

use of org.gradle.api.internal.tasks.SimpleWorkResult in project gradle by gradle.

the class RoutesCompiler method execute.

@Override
public WorkResult execute(RoutesCompileSpec spec) {
    boolean didWork = false;
    // Need to compile all secondary routes ("Foo.routes") before primary ("routes")
    ArrayList<File> primaryRoutes = Lists.newArrayList();
    ArrayList<File> secondaryRoutes = Lists.newArrayList();
    for (File source : spec.getSources()) {
        if (source.getName().equals("routes")) {
            primaryRoutes.add(source);
        } else {
            secondaryRoutes.add(source);
        }
    }
    // Compile all secondary routes files first
    for (File sourceFile : secondaryRoutes) {
        Boolean ret = compile(sourceFile, spec);
        didWork = ret || didWork;
    }
    // Compile all main routes files last
    for (File sourceFile : primaryRoutes) {
        Boolean ret = compile(sourceFile, spec);
        didWork = ret || didWork;
    }
    return new SimpleWorkResult(didWork);
}
Also used : SimpleWorkResult(org.gradle.api.internal.tasks.SimpleWorkResult) File(java.io.File)

Example 12 with SimpleWorkResult

use of org.gradle.api.internal.tasks.SimpleWorkResult in project gradle by gradle.

the class ApiGroovyCompiler method execute.

@Override
public WorkResult execute(final GroovyJavaJointCompileSpec spec) {
    GroovySystemLoaderFactory groovySystemLoaderFactory = new GroovySystemLoaderFactory();
    ClassLoader compilerClassLoader = this.getClass().getClassLoader();
    GroovySystemLoader compilerGroovyLoader = groovySystemLoaderFactory.forClassLoader(compilerClassLoader);
    CompilerConfiguration configuration = new CompilerConfiguration();
    configuration.setVerbose(spec.getGroovyCompileOptions().isVerbose());
    configuration.setSourceEncoding(spec.getGroovyCompileOptions().getEncoding());
    configuration.setTargetBytecode(spec.getTargetCompatibility());
    configuration.setTargetDirectory(spec.getDestinationDir());
    canonicalizeValues(spec.getGroovyCompileOptions().getOptimizationOptions());
    if (spec.getGroovyCompileOptions().getConfigurationScript() != null) {
        applyConfigurationScript(spec.getGroovyCompileOptions().getConfigurationScript(), configuration);
    }
    try {
        configuration.setOptimizationOptions(spec.getGroovyCompileOptions().getOptimizationOptions());
    } catch (NoSuchMethodError ignored) {
    /* method was only introduced in Groovy 1.8 */
    }
    Map<String, Object> jointCompilationOptions = new HashMap<String, Object>();
    final File stubDir = spec.getGroovyCompileOptions().getStubDir();
    stubDir.mkdirs();
    jointCompilationOptions.put("stubDir", stubDir);
    jointCompilationOptions.put("keepStubs", spec.getGroovyCompileOptions().isKeepStubs());
    configuration.setJointCompilationOptions(jointCompilationOptions);
    ClassLoader classPathLoader;
    VersionNumber version = parseGroovyVersion();
    if (version.compareTo(VersionNumber.parse("2.0")) < 0) {
        // using a transforming classloader is only required for older buggy Groovy versions
        classPathLoader = new GroovyCompileTransformingClassLoader(getExtClassLoader(), new DefaultClassPath(spec.getCompileClasspath()));
    } else {
        classPathLoader = new DefaultClassLoaderFactory().createIsolatedClassLoader(new DefaultClassPath(spec.getCompileClasspath()));
    }
    GroovyClassLoader compileClasspathClassLoader = new GroovyClassLoader(classPathLoader, null);
    GroovySystemLoader compileClasspathLoader = groovySystemLoaderFactory.forClassLoader(classPathLoader);
    FilteringClassLoader.Spec groovyCompilerClassLoaderSpec = new FilteringClassLoader.Spec();
    groovyCompilerClassLoaderSpec.allowPackage("org.codehaus.groovy");
    groovyCompilerClassLoaderSpec.allowPackage("groovy");
    // Disallow classes from Groovy Jar that reference external classes. Such classes must be loaded from astTransformClassLoader,
    // or a NoClassDefFoundError will occur. Essentially this is drawing a line between the Groovy compiler and the Groovy
    // library, albeit only for selected classes that run a high risk of being statically referenced from a transform.
    groovyCompilerClassLoaderSpec.disallowClass("groovy.util.GroovyTestCase");
    groovyCompilerClassLoaderSpec.disallowPackage("groovy.servlet");
    FilteringClassLoader groovyCompilerClassLoader = new FilteringClassLoader(GroovyClassLoader.class.getClassLoader(), groovyCompilerClassLoaderSpec);
    // AST transforms need their own class loader that shares compiler classes with the compiler itself
    final GroovyClassLoader astTransformClassLoader = new GroovyClassLoader(groovyCompilerClassLoader, null);
    // where the transform class is loaded from)
    for (File file : spec.getCompileClasspath()) {
        astTransformClassLoader.addClasspath(file.getPath());
    }
    JavaAwareCompilationUnit unit = new JavaAwareCompilationUnit(configuration, compileClasspathClassLoader) {

        @Override
        public GroovyClassLoader getTransformLoader() {
            return astTransformClassLoader;
        }
    };
    final boolean shouldProcessAnnotations = shouldProcessAnnotations(spec);
    if (shouldProcessAnnotations) {
        // If an annotation processor is detected, we need to force Java stub generation, so the we can process annotations on Groovy classes
        // We are forcing stub generation by tricking the groovy compiler into thinking there are java files to compile.
        // All java files are just passed to the compile method of the JavaCompiler and aren't processed internally by the Groovy Compiler.
        // Since we're maintaining our own list of Java files independent of what's passed by the Groovy compiler, adding a non-existent java file
        // to the sources won't cause any issues.
        unit.addSources(new File[] { new File("ForceStubGeneration.java") });
    }
    // Sort source files to work around https://issues.apache.org/jira/browse/GROOVY-7966
    File[] sortedSourceFiles = Iterables.toArray(spec.getSource(), File.class);
    Arrays.sort(sortedSourceFiles);
    unit.addSources(sortedSourceFiles);
    unit.setCompilerFactory(new JavaCompilerFactory() {

        public JavaCompiler createCompiler(final CompilerConfiguration config) {
            return new JavaCompiler() {

                public void compile(List<String> files, CompilationUnit cu) {
                    if (shouldProcessAnnotations) {
                        // In order for the Groovy stubs to have annotation processors invoked against them, they must be compiled as source.
                        // Classes compiled as a result of being on the -sourcepath do not have the annotation processor run against them
                        spec.setSource(spec.getSource().plus(new SimpleFileCollection(stubDir).getAsFileTree()));
                    } else {
                        // When annotation processing isn't required, it's better to add the Groovy stubs as part of the source path.
                        // This allows compilations to complete faster, because only the Groovy stubs that are needed by the java source are compiled.
                        FileCollection sourcepath = new SimpleFileCollection(stubDir);
                        if (spec.getCompileOptions().getSourcepath() != null) {
                            sourcepath = spec.getCompileOptions().getSourcepath().plus(sourcepath);
                        }
                        spec.getCompileOptions().setSourcepath(sourcepath);
                    }
                    spec.setSource(spec.getSource().filter(new Spec<File>() {

                        public boolean isSatisfiedBy(File file) {
                            return hasExtension(file, ".java");
                        }
                    }));
                    try {
                        javaCompiler.execute(spec);
                    } catch (CompilationFailedException e) {
                        cu.getErrorCollector().addFatalError(new SimpleMessage(e.getMessage(), cu));
                    }
                }
            };
        }
    });
    try {
        unit.compile();
    } catch (org.codehaus.groovy.control.CompilationFailedException e) {
        System.err.println(e.getMessage());
        // Explicit flush, System.err is an auto-flushing PrintWriter unless it is replaced.
        System.err.flush();
        throw new CompilationFailedException();
    } finally {
        // Remove compile and AST types from the Groovy loader
        compilerGroovyLoader.discardTypesFrom(classPathLoader);
        compilerGroovyLoader.discardTypesFrom(astTransformClassLoader);
        //Discard the compile loader
        compileClasspathLoader.shutdown();
    }
    return new SimpleWorkResult(true);
}
Also used : HashMap(java.util.HashMap) JavaAwareCompilationUnit(org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit) SimpleFileCollection(org.gradle.api.internal.file.collections.SimpleFileCollection) FileCollection(org.gradle.api.file.FileCollection) SimpleWorkResult(org.gradle.api.internal.tasks.SimpleWorkResult) GroovyClassLoader(groovy.lang.GroovyClassLoader) SimpleFileCollection(org.gradle.api.internal.file.collections.SimpleFileCollection) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) FilteringClassLoader(org.gradle.internal.classloader.FilteringClassLoader) GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovySystemLoader(org.gradle.api.internal.classloading.GroovySystemLoader) GroovySystemLoaderFactory(org.gradle.api.internal.classloading.GroovySystemLoaderFactory) DefaultClassLoaderFactory(org.gradle.internal.classloader.DefaultClassLoaderFactory) JavaAwareCompilationUnit(org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit) CompilationUnit(org.codehaus.groovy.control.CompilationUnit) SimpleMessage(org.codehaus.groovy.control.messages.SimpleMessage) JavaCompiler(org.codehaus.groovy.tools.javac.JavaCompiler) JavaCompilerFactory(org.codehaus.groovy.tools.javac.JavaCompilerFactory) VersionNumber(org.gradle.util.VersionNumber) FilteringClassLoader(org.gradle.internal.classloader.FilteringClassLoader) Spec(org.gradle.api.specs.Spec) File(java.io.File) DefaultClassPath(org.gradle.internal.classpath.DefaultClassPath)

Example 13 with SimpleWorkResult

use of org.gradle.api.internal.tasks.SimpleWorkResult in project gradle by gradle.

the class CommandLineJavaCompiler method execute.

@Override
public WorkResult execute(JavaCompileSpec spec) {
    final ForkOptions forkOptions = spec.getCompileOptions().getForkOptions();
    String executable = forkOptions.getJavaHome() != null ? Jvm.forHome(forkOptions.getJavaHome()).getJavacExecutable().getAbsolutePath() : getExecutable(forkOptions);
    LOGGER.info("Compiling with Java command line compiler '{}'.", executable);
    ExecHandle handle = createCompilerHandle(executable, spec);
    executeCompiler(handle);
    return new SimpleWorkResult(true);
}
Also used : ForkOptions(org.gradle.api.tasks.compile.ForkOptions) SimpleWorkResult(org.gradle.api.internal.tasks.SimpleWorkResult) ExecHandle(org.gradle.process.internal.ExecHandle)

Example 14 with SimpleWorkResult

use of org.gradle.api.internal.tasks.SimpleWorkResult in project gradle by gradle.

the class Deleter method delete.

public WorkResult delete(Action<? super DeleteSpec> action) {
    boolean didWork = false;
    DeleteSpecInternal deleteSpec = new DefaultDeleteSpec();
    action.execute(deleteSpec);
    Object[] paths = deleteSpec.getPaths();
    for (File file : fileResolver.resolveFiles(paths)) {
        if (!file.exists()) {
            continue;
        }
        LOGGER.debug("Deleting {}", file);
        didWork = true;
        doDeleteInternal(file, deleteSpec);
    }
    return new SimpleWorkResult(didWork);
}
Also used : SimpleWorkResult(org.gradle.api.internal.tasks.SimpleWorkResult) File(java.io.File)

Example 15 with SimpleWorkResult

use of org.gradle.api.internal.tasks.SimpleWorkResult in project gradle by gradle.

the class TwirlCompiler method execute.

@Override
public WorkResult execute(TwirlCompileSpec spec) {
    ArrayList<File> outputFiles = Lists.newArrayList();
    try {
        ClassLoader cl = getClass().getClassLoader();
        ScalaMethod compile = adapter.getCompileMethod(cl);
        Iterable<RelativeFile> sources = spec.getSources();
        for (RelativeFile sourceFile : sources) {
            Object result = compile.invoke(adapter.createCompileParameters(cl, sourceFile.getFile(), sourceFile.getBaseDir(), spec.getDestinationDir(), spec.getDefaultImports()));
            ScalaOptionInvocationWrapper<File> maybeFile = new ScalaOptionInvocationWrapper<File>(result);
            if (maybeFile.isDefined()) {
                outputFiles.add(maybeFile.get());
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Error invoking Play Twirl template compiler.", e);
    }
    return new SimpleWorkResult(!outputFiles.isEmpty());
}
Also used : RelativeFile(org.gradle.api.internal.file.RelativeFile) ScalaMethod(org.gradle.scala.internal.reflect.ScalaMethod) ScalaOptionInvocationWrapper(org.gradle.scala.internal.reflect.ScalaOptionInvocationWrapper) SimpleWorkResult(org.gradle.api.internal.tasks.SimpleWorkResult) RelativeFile(org.gradle.api.internal.file.RelativeFile) File(java.io.File)

Aggregations

SimpleWorkResult (org.gradle.api.internal.tasks.SimpleWorkResult)15 File (java.io.File)5 BuildOperationQueue (org.gradle.internal.operations.BuildOperationQueue)5 CommandLineToolInvocation (org.gradle.nativeplatform.toolchain.internal.CommandLineToolInvocation)3 GradleException (org.gradle.api.GradleException)2 CopyActionProcessingStreamAction (org.gradle.api.internal.file.CopyActionProcessingStreamAction)2 RelativeFile (org.gradle.api.internal.file.RelativeFile)2 WorkResult (org.gradle.api.tasks.WorkResult)2 GroovyClassLoader (groovy.lang.GroovyClassLoader)1 OutputStream (java.io.OutputStream)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 TarOutputStream (org.apache.tools.tar.TarOutputStream)1 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)1 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)1 SimpleMessage (org.codehaus.groovy.control.messages.SimpleMessage)1 JavaAwareCompilationUnit (org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit)1 JavaCompiler (org.codehaus.groovy.tools.javac.JavaCompiler)1 JavaCompilerFactory (org.codehaus.groovy.tools.javac.JavaCompilerFactory)1 FileCollection (org.gradle.api.file.FileCollection)1