Search in sources :

Example 46 with GradleException

use of org.gradle.api.GradleException in project gradle by gradle.

the class SingleIncludePatternFileTree method doVisit.

private void doVisit(FileVisitor visitor, File file, LinkedList<String> relativePath, int segmentIndex, AtomicBoolean stopFlag) {
    if (stopFlag.get()) {
        return;
    }
    String segment = patternSegments.get(segmentIndex);
    if (segment.contains("**")) {
        PatternSet patternSet = new PatternSet();
        patternSet.include(includePattern);
        patternSet.exclude(excludeSpec);
        DirectoryFileTree fileTree = new DirectoryFileTree(baseDir, patternSet, fileSystem);
        fileTree.visitFrom(visitor, file, new RelativePath(file.isFile(), relativePath.toArray(new String[relativePath.size()])));
    } else if (segment.contains("*") || segment.contains("?")) {
        PatternStep step = PatternStepFactory.getStep(segment, false);
        File[] children = file.listFiles();
        if (children == null) {
            if (!file.canRead()) {
                throw new GradleException(String.format("Could not list contents of directory '%s' as it is not readable.", file));
            }
            // else, might be a link which points to nothing, or has been removed while we're visiting, or ...
            throw new GradleException(String.format("Could not list contents of '%s'.", file));
        }
        for (File child : children) {
            if (stopFlag.get()) {
                break;
            }
            String childName = child.getName();
            if (step.matches(childName)) {
                relativePath.addLast(childName);
                doVisitDirOrFile(visitor, child, relativePath, segmentIndex + 1, stopFlag);
                relativePath.removeLast();
            }
        }
    } else {
        relativePath.addLast(segment);
        doVisitDirOrFile(visitor, new File(file, segment), relativePath, segmentIndex + 1, stopFlag);
        relativePath.removeLast();
    }
}
Also used : RelativePath(org.gradle.api.file.RelativePath) PatternStep(org.gradle.api.internal.file.pattern.PatternStep) GradleException(org.gradle.api.GradleException) PatternSet(org.gradle.api.tasks.util.PatternSet) File(java.io.File)

Example 47 with GradleException

use of org.gradle.api.GradleException in project gradle by gradle.

the class DefaultTaskClassInfoStore method createTaskClassInfo.

private TaskClassInfo createTaskClassInfo(Class<? extends Task> type) {
    boolean cacheable = type.isAnnotationPresent(CacheableTask.class);
    boolean incremental = false;
    Map<String, Class<?>> processedMethods = Maps.newHashMap();
    ImmutableList.Builder<TaskActionFactory> taskActionFactoriesBuilder = ImmutableList.builder();
    for (Class current = type; current != null; current = current.getSuperclass()) {
        for (Method method : current.getDeclaredMethods()) {
            TaskActionFactory taskActionFactory = createTaskAction(type, method, processedMethods);
            if (taskActionFactory == null) {
                continue;
            }
            if (taskActionFactory instanceof IncrementalTaskActionFactory) {
                if (incremental) {
                    throw new GradleException(String.format("Cannot have multiple @TaskAction methods accepting an %s parameter.", IncrementalTaskInputs.class.getSimpleName()));
                }
                incremental = true;
            }
            taskActionFactoriesBuilder.add(taskActionFactory);
        }
    }
    return new TaskClassInfo(incremental, taskActionFactoriesBuilder.build(), cacheable);
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) GradleException(org.gradle.api.GradleException) Method(java.lang.reflect.Method)

Example 48 with GradleException

use of org.gradle.api.GradleException in project gradle by gradle.

the class TarTaskOutputPacker method pack.

private long pack(Collection<ResolvedTaskOutputFilePropertySpec> propertySpecs, Map<String, Map<String, FileContentSnapshot>> outputSnapshots, TarArchiveOutputStream tarOutput) {
    long entries = 0;
    for (ResolvedTaskOutputFilePropertySpec propertySpec : propertySpecs) {
        String propertyName = propertySpec.getPropertyName();
        Map<String, FileContentSnapshot> outputs = outputSnapshots.get(propertyName);
        try {
            entries += packProperty(propertySpec, outputs, tarOutput);
        } catch (Exception ex) {
            throw new GradleException(String.format("Could not pack property '%s': %s", propertyName, ex.getMessage()), ex);
        }
    }
    return entries;
}
Also used : FileContentSnapshot(org.gradle.api.internal.changedetection.state.FileContentSnapshot) GradleException(org.gradle.api.GradleException) ResolvedTaskOutputFilePropertySpec(org.gradle.api.internal.tasks.ResolvedTaskOutputFilePropertySpec) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) GradleException(org.gradle.api.GradleException)

Example 49 with GradleException

use of org.gradle.api.GradleException in project gradle by gradle.

the class Wrapper method writeWrapperTo.

private void writeWrapperTo(File destination) {
    InputStream gradleWrapperJar = Wrapper.class.getResourceAsStream("/gradle-wrapper.jar");
    if (gradleWrapperJar == null) {
        throw new GradleException("Cannot locate wrapper JAR resource.");
    }
    ZipInputStream zipInputStream = null;
    ZipOutputStream zipOutputStream = null;
    try {
        zipInputStream = new ZipInputStream(gradleWrapperJar);
        zipOutputStream = new ZipOutputStream(new FileOutputStream(destination));
        for (ZipEntry entry = zipInputStream.getNextEntry(); entry != null; entry = zipInputStream.getNextEntry()) {
            zipOutputStream.putNextEntry(entry);
            if (!entry.isDirectory()) {
                ByteStreams.copy(zipInputStream, zipOutputStream);
            }
            zipOutputStream.closeEntry();
        }
        addBuildReceipt(zipOutputStream);
    } catch (IOException e) {
        throw UncheckedException.throwAsUncheckedException(e);
    } finally {
        IoActions.closeQuietly(zipInputStream);
        IoActions.closeQuietly(zipOutputStream);
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipOutputStream(java.util.zip.ZipOutputStream) GradleException(org.gradle.api.GradleException) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException)

Example 50 with GradleException

use of org.gradle.api.GradleException in project gradle by gradle.

the class BuildScriptBuilder method create.

public TemplateOperation create() {
    return new TemplateOperation() {

        @Override
        public void generate() {
            File target = getTargetFile();
            try {
                PrintWriter writer = new PrintWriter(new FileWriter(target));
                try {
                    PrettyPrinter printer = prettyPrinterFor(dsl, writer);
                    printer.printFileHeader(headerLines);
                    printer.printPlugins(plugins);
                    printer.printConfigSpecs(configSpecs);
                    if (!dependencies.isEmpty()) {
                        printer.printDependencies(dependencies);
                        printer.printRepositories();
                    }
                } finally {
                    writer.close();
                }
            } catch (Exception e) {
                throw new GradleException("Could not generate file " + target + ".", e);
            }
        }
    };
}
Also used : FileWriter(java.io.FileWriter) GradleException(org.gradle.api.GradleException) File(java.io.File) GradleException(org.gradle.api.GradleException) PrintWriter(java.io.PrintWriter)

Aggregations

GradleException (org.gradle.api.GradleException)114 File (java.io.File)40 IOException (java.io.IOException)32 TaskAction (org.gradle.api.tasks.TaskAction)16 ArrayList (java.util.ArrayList)10 UncheckedIOException (org.gradle.api.UncheckedIOException)10 MtlBaseTaskAction (com.taobao.android.builder.tasks.manager.MtlBaseTaskAction)9 AndroidDependencyTree (com.taobao.android.builder.dependency.AndroidDependencyTree)8 HashSet (java.util.HashSet)8 FileOutputStream (java.io.FileOutputStream)6 Method (java.lang.reflect.Method)6 UncheckedException (org.gradle.internal.UncheckedException)6 AwbBundle (com.taobao.android.builder.dependency.model.AwbBundle)5 URL (java.net.URL)5 List (java.util.List)5 ConsoleRenderer (org.gradle.internal.logging.ConsoleRenderer)5 ProcessOutputHandler (com.android.ide.common.process.ProcessOutputHandler)4 FileInputStream (java.io.FileInputStream)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 MalformedURLException (java.net.MalformedURLException)4