org.gradle.api.internal.file.RelativeFile Java Examples

The following examples show how to use org.gradle.api.internal.file.RelativeFile. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TwirlCompiler.java    From playframework with Apache License 2.0 6 votes vote down vote up
@Override
public WorkResult execute(TwirlCompileSpec spec) {
    List<File> outputFiles = new ArrayList<>();
    ClassLoader cl = getClass().getClassLoader();
    ScalaMethod compile = getCompileMethod(cl);
    Iterable<RelativeFile> sources = spec.getSources();

    for (RelativeFile sourceFile : sources) {
        TwirlTemplateFormat format = findTemplateFormat(spec, sourceFile.getFile());
        try {
            Object result = compile.invoke(buildCompileArguments(spec, cl, sourceFile, format));
            ScalaOptionInvocationWrapper<File> maybeFile = new ScalaOptionInvocationWrapper<File>(result);
            if (maybeFile.isDefined()) {
                File outputFile = maybeFile.get();
                outputFiles.add(outputFile);
            }
        } catch (Exception e) {
            throw new RuntimeException("Error invoking Play Twirl template compiler.", e);
        }
    }

    return WorkResults.didWork(!outputFiles.isEmpty());
}
 
Example #2
Source File: CoffeeScriptCompilerWorker.java    From pushfish-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public Boolean process(SerializableCoffeeScriptCompileSpec spec) {
    Scriptable coffeeScriptScope = parse(spec.getCoffeeScriptJs(), "UTF-8", new Action<Context>() {
        public void execute(Context context) {
            context.setOptimizationLevel(-1);
        }
    });

    String encoding = spec.getOptions().getEncoding();

    CoffeeScriptCompileDestinationCalculator destinationCalculator = new CoffeeScriptCompileDestinationCalculator(spec.getDestinationDir());

    for (RelativeFile target : spec.getSource()) {
        String source = readFile(target.getFile(), encoding);
        String output = compile(coffeeScriptScope, source, target.getRelativePath().getPathString());
        writeFile(output, destinationCalculator.transform(target.getRelativePath()), encoding);
    }

    return Boolean.TRUE;
}
 
Example #3
Source File: CoffeeScriptCompilerWorker.java    From pushfish-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public Boolean process(SerializableCoffeeScriptCompileSpec spec) {
    Scriptable coffeeScriptScope = parse(spec.getCoffeeScriptJs(), "UTF-8", new Action<Context>() {
        public void execute(Context context) {
            context.setOptimizationLevel(-1);
        }
    });

    String encoding = spec.getOptions().getEncoding();

    CoffeeScriptCompileDestinationCalculator destinationCalculator = new CoffeeScriptCompileDestinationCalculator(spec.getDestinationDir());

    for (RelativeFile target : spec.getSource()) {
        String source = readFile(target.getFile(), encoding);
        String output = compile(coffeeScriptScope, source, target.getRelativePath().getPathString());
        writeFile(output, destinationCalculator.transform(target.getRelativePath()), encoding);
    }

    return Boolean.TRUE;
}
 
Example #4
Source File: CoffeeScriptCompilerWorker.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public Boolean process(SerializableCoffeeScriptCompileSpec spec) {
    Scriptable coffeeScriptScope = parse(spec.getCoffeeScriptJs(), "UTF-8", new Action<Context>() {
        public void execute(Context context) {
            context.setOptimizationLevel(-1);
        }
    });

    String encoding = spec.getOptions().getEncoding();

    CoffeeScriptCompileDestinationCalculator destinationCalculator = new CoffeeScriptCompileDestinationCalculator(spec.getDestinationDir());

    for (RelativeFile target : spec.getSource()) {
        String source = readFile(target.getFile(), encoding);
        String output = compile(coffeeScriptScope, source, target.getRelativePath().getPathString());
        writeFile(output, destinationCalculator.transform(target.getRelativePath()), encoding);
    }

    return Boolean.TRUE;
}
 
Example #5
Source File: CoffeeScriptCompilerWorker.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public Boolean process(SerializableCoffeeScriptCompileSpec spec) {
    Scriptable coffeeScriptScope = parse(spec.getCoffeeScriptJs(), "UTF-8", new Action<Context>() {
        public void execute(Context context) {
            context.setOptimizationLevel(-1);
        }
    });

    String encoding = spec.getOptions().getEncoding();

    CoffeeScriptCompileDestinationCalculator destinationCalculator = new CoffeeScriptCompileDestinationCalculator(spec.getDestinationDir());

    for (RelativeFile target : spec.getSource()) {
        String source = readFile(target.getFile(), encoding);
        String output = compile(coffeeScriptScope, source, target.getRelativePath().getPathString());
        writeFile(output, destinationCalculator.transform(target.getRelativePath()), encoding);
    }

    return Boolean.TRUE;
}
 
Example #6
Source File: JavaScriptMinify.java    From playframework with Apache License 2.0 5 votes vote down vote up
@Override
public void visitFile(final FileVisitDetails fileDetails) {
    final File outputFileDir = new File(destinationDir.get().getAsFile(), fileDetails.getRelativePath().getParent().getPathString());

    // Copy the raw form
    FileOperations fileOperations = ((ProjectInternal) getProject()).getFileOperations();
    fileOperations.copy(copySpec -> copySpec.from(fileDetails.getFile()).into(outputFileDir));

    // Capture the relative file
    relativeFiles.add(new RelativeFile(fileDetails.getFile(), fileDetails.getRelativePath()));
}
 
Example #7
Source File: TwirlCompiler.java    From playframework with Apache License 2.0 5 votes vote down vote up
private Object[] buildCompileArguments(TwirlCompileSpec spec, ClassLoader cl, RelativeFile sourceFile, TwirlTemplateFormat format) {
    try {
        return adapter.createCompileParameters(cl, sourceFile.getFile(), sourceFile.getBaseDir(), spec.getDestinationDir(), spec.getDefaultImports(), format, spec.getAdditionalImports(), spec.getConstructorAnnotations());
    } catch (Exception e) {
        throw new RuntimeException("Error invoking Play Twirl template compiler.", e);
    }
}
 
Example #8
Source File: DefaultTwirlCompileSpec.java    From playframework with Apache License 2.0 5 votes vote down vote up
public DefaultTwirlCompileSpec(Iterable<RelativeFile> sources, File destinationDir, TwirlImports defaultImports, Collection<TwirlTemplateFormat> userTemplateFormats, List<String> additionalImports, List<String> constructorAnnotations) {
    this.sources = sources;
    this.destinationDir = destinationDir;
    this.defaultImports = defaultImports;
    this.userTemplateFormats = userTemplateFormats;
    this.additionalImports = additionalImports;
    this.constructorAnnotations = constructorAnnotations;
}
 
Example #9
Source File: SerializableCoffeeScriptCompileSpec.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public SerializableCoffeeScriptCompileSpec(File coffeeScriptJs, File destinationDir, FileCollection source, CoffeeScriptCompileOptions options) {
    this.coffeeScriptJs = coffeeScriptJs;
    this.destinationDir = destinationDir;
    this.source = new LinkedList<RelativeFile>();
    this.options = options;

    toRelativeFiles(source, this.source);
}
 
Example #10
Source File: SerializableCoffeeScriptCompileSpec.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void toRelativeFiles(final FileCollection source, final List<RelativeFile> targets) {
    FileTree fileTree = source.getAsFileTree();

    fileTree.visit(new FileVisitor() {
        public void visitDir(FileVisitDetails dirDetails) {}

        public void visitFile(FileVisitDetails fileDetails) {
            targets.add(new RelativeFile(fileDetails.getFile(), fileDetails.getRelativePath()));
        }
    });
}
 
Example #11
Source File: GoogleClosureCompiler.java    From playframework with Apache License 2.0 5 votes vote down vote up
@Override
public WorkResult execute(JavaScriptCompileSpec spec) {
    JavaScriptCompileDestinationCalculator destinationCalculator = new JavaScriptCompileDestinationCalculator(spec.getDestinationDir());
    List<String> allErrors = new ArrayList<>();

    for (RelativeFile sourceFile : spec.getSources()) {
        allErrors.addAll(compile(sourceFile, spec, destinationCalculator));
    }

    if (allErrors.isEmpty()) {
        return WorkResults.didWork(true);
    } else {
        throw new SourceTransformationException(String.format("Minification failed with the following errors:\n\t%s", String.join( "\n\t", allErrors)), null);
    }
}
 
Example #12
Source File: SerializableCoffeeScriptCompileSpec.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public SerializableCoffeeScriptCompileSpec(File coffeeScriptJs, File destinationDir, FileCollection source, CoffeeScriptCompileOptions options) {
    this.coffeeScriptJs = coffeeScriptJs;
    this.destinationDir = destinationDir;
    this.source = new LinkedList<RelativeFile>();
    this.options = options;

    toRelativeFiles(source, this.source);
}
 
Example #13
Source File: SerializableCoffeeScriptCompileSpec.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void toRelativeFiles(final FileCollection source, final List<RelativeFile> targets) {
    FileTree fileTree = source.getAsFileTree();

    fileTree.visit(new FileVisitor() {
        public void visitDir(FileVisitDetails dirDetails) {}

        public void visitFile(FileVisitDetails fileDetails) {
            targets.add(new RelativeFile(fileDetails.getFile(), fileDetails.getRelativePath()));
        }
    });
}
 
Example #14
Source File: SerializableCoffeeScriptCompileSpec.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void toRelativeFiles(final FileCollection source, final List<RelativeFile> targets) {
    FileTree fileTree = source.getAsFileTree();

    fileTree.visit(new FileVisitor() {
        public void visitDir(FileVisitDetails dirDetails) {}

        public void visitFile(FileVisitDetails fileDetails) {
            targets.add(new RelativeFile(fileDetails.getFile(), fileDetails.getRelativePath()));
        }
    });
}
 
Example #15
Source File: SerializableCoffeeScriptCompileSpec.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public SerializableCoffeeScriptCompileSpec(File coffeeScriptJs, File destinationDir, FileCollection source, CoffeeScriptCompileOptions options) {
    this.coffeeScriptJs = coffeeScriptJs;
    this.destinationDir = destinationDir;
    this.source = new LinkedList<RelativeFile>();
    this.options = options;

    toRelativeFiles(source, this.source);
}
 
Example #16
Source File: SerializableCoffeeScriptCompileSpec.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void toRelativeFiles(final FileCollection source, final List<RelativeFile> targets) {
    FileTree fileTree = source.getAsFileTree();

    fileTree.visit(new FileVisitor() {
        public void visitDir(FileVisitDetails dirDetails) {}

        public void visitFile(FileVisitDetails fileDetails) {
            targets.add(new RelativeFile(fileDetails.getFile(), fileDetails.getRelativePath()));
        }
    });
}
 
Example #17
Source File: SerializableCoffeeScriptCompileSpec.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public SerializableCoffeeScriptCompileSpec(File coffeeScriptJs, File destinationDir, FileCollection source, CoffeeScriptCompileOptions options) {
    this.coffeeScriptJs = coffeeScriptJs;
    this.destinationDir = destinationDir;
    this.source = new LinkedList<RelativeFile>();
    this.options = options;

    toRelativeFiles(source, this.source);
}
 
Example #18
Source File: SerializableCoffeeScriptCompileSpec.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public List<RelativeFile> getSource() {
    return source;
}
 
Example #19
Source File: SerializableCoffeeScriptCompileSpec.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public List<RelativeFile> getSource() {
    return source;
}
 
Example #20
Source File: DefaultTwirlCompileSpec.java    From playframework with Apache License 2.0 4 votes vote down vote up
@Override
public Iterable<RelativeFile> getSources() {
    return sources;
}
 
Example #21
Source File: DefaultJavaScriptCompileSpec.java    From playframework with Apache License 2.0 4 votes vote down vote up
@Override
public Iterable<RelativeFile> getSources() {
    return sources;
}
 
Example #22
Source File: DefaultJavaScriptCompileSpec.java    From playframework with Apache License 2.0 4 votes vote down vote up
public DefaultJavaScriptCompileSpec(Iterable<RelativeFile> sources, File destinationDir) {
    this.sources = sources;
    this.destinationDir = destinationDir;
}
 
Example #23
Source File: GoogleClosureCompiler.java    From playframework with Apache License 2.0 4 votes vote down vote up
List<String> compile(RelativeFile javascriptFile, JavaScriptCompileSpec spec, JavaScriptCompileDestinationCalculator destinationCalculator) {
    List<String> errors = new ArrayList<>();

    loadCompilerClasses(getClass().getClassLoader());

    // Create a SourceFile object to represent an "empty" extern
    JavaMethod<?, Object> fromCodeJavaMethod = JavaReflectionUtil.staticMethod(sourceFileClass, Object.class, "fromCode", String.class, String.class);
    Object extern = fromCodeJavaMethod.invokeStatic("/dev/null", "");

    // Create a SourceFile object to represent the javascript file to compile
    JavaMethod<?, Object> fromFileJavaMethod = JavaReflectionUtil.staticMethod(sourceFileClass, Object.class, "fromFile", File.class);
    Object sourceFile = fromFileJavaMethod.invokeStatic(javascriptFile.getFile());

    // Construct a new CompilerOptions class
    Object compilerOptions = DirectInstantiator.INSTANCE.newInstance(compilerOptionsClass);

    // Get the CompilationLevel.SIMPLE_OPTIMIZATIONS class and set it on the CompilerOptions class
    @SuppressWarnings({ "rawtypes", "unchecked" }) Enum simpleLevel = Enum.valueOf(compilationLevelClass, "SIMPLE_OPTIMIZATIONS");
    @SuppressWarnings("rawtypes") JavaMethod<Enum, Void> setOptionsForCompilationLevelMethod = JavaReflectionUtil.method(compilationLevelClass, Void.class, "setOptionsForCompilationLevel", compilerOptionsClass);
    setOptionsForCompilationLevelMethod.invoke(simpleLevel, compilerOptions);

    // Construct a new Compiler class
    Object compiler = DirectInstantiator.INSTANCE.newInstance(compilerClass, getDummyPrintStream());

    // Compile the javascript file with the options we've created
    JavaMethod<Object, Object> compileMethod = JavaReflectionUtil.method(compilerClass, Object.class, "compile", sourceFileClass, sourceFileClass, compilerOptionsClass);
    Object result = compileMethod.invoke(compiler, extern, sourceFile, compilerOptions);

    // Get any errors from the compiler result
    PropertyAccessor<Object, Object[]> jsErrorsField = JavaReflectionUtil.readableField(result, Object[].class, "errors");
    Object[] jsErrors = jsErrorsField.getValue(result);

    if (jsErrors.length == 0) {
        // If no errors, get the compiled source and write it to the destination file
        JavaMethod<Object, String> toSourceMethod = JavaReflectionUtil.method(compilerClass, String.class, "toSource");
        String compiledSource = toSourceMethod.invoke(compiler);
        try {
            Files.write(destinationCalculator.transform(javascriptFile).toPath(), compiledSource.getBytes());
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    } else {
        for (Object error : jsErrors) {
            errors.add(error.toString());
        }
    }

    return errors;
}
 
Example #24
Source File: JavaScriptCompileDestinationCalculator.java    From playframework with Apache License 2.0 4 votes vote down vote up
@Override
public File transform(RelativeFile file) {
    final File outputFileDir = new File(destinationDir, file.getRelativePath().getParent().getPathString());
    return new File(outputFileDir, getMinifiedFileName(file.getFile().getName()));
}
 
Example #25
Source File: TwirlCompile.java    From playframework with Apache License 2.0 4 votes vote down vote up
@Override
public void visitFile(FileVisitDetails fileDetails) {
    relativeFiles.add(new RelativeFile(fileDetails.getFile(), fileDetails.getRelativePath()));
}
 
Example #26
Source File: SerializableCoffeeScriptCompileSpec.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public List<RelativeFile> getSource() {
    return source;
}
 
Example #27
Source File: SerializableCoffeeScriptCompileSpec.java    From pushfish-android with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public List<RelativeFile> getSource() {
    return source;
}
 
Example #28
Source File: TwirlCompileSpec.java    From playframework with Apache License 2.0 votes vote down vote up
Iterable<RelativeFile> getSources(); 
Example #29
Source File: JavaScriptCompileSpec.java    From playframework with Apache License 2.0 votes vote down vote up
Iterable<RelativeFile> getSources();