Search in sources :

Example 41 with GradleException

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

the class GitVersionControlSystem method getRemoteForUrl.

// This method is only necessary until https://bugs.eclipse.org/bugs/show_bug.cgi?id=525300 is fixed.
private static String getRemoteForUrl(Repository repository, URI url) throws URISyntaxException {
    Config config = repository.getConfig();
    Set<String> remotes = config.getSubsections("remote");
    Set<String> foundUrls = new HashSet<String>();
    String normalizedUrl = normalizeUri(url);
    for (String remote : remotes) {
        String remoteUrl = config.getString("remote", remote, "url");
        if (remoteUrl.equals(normalizedUrl)) {
            return remote;
        } else {
            foundUrls.add(remoteUrl);
        }
    }
    throw new GradleException(String.format("Could not find remote with url: %s. Found: %s", url, foundUrls));
}
Also used : Config(org.eclipse.jgit.lib.Config) GradleException(org.gradle.api.GradleException) HashSet(java.util.HashSet)

Example 42 with GradleException

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

the class DefaultScriptCompilationHandler method compileToDir.

@Override
public void compileToDir(ScriptSource source, ClassLoader classLoader, File classesDir, File metadataDir, CompileOperation<?> extractingTransformer, Class<? extends Script> scriptBaseClass, Action<? super ClassNode> verifier) {
    Timer clock = Time.startTimer();
    GFileUtils.deleteDirectory(classesDir);
    GFileUtils.mkdirs(classesDir);
    CompilerConfiguration configuration = createBaseCompilerConfiguration(scriptBaseClass);
    configuration.setTargetDirectory(classesDir);
    try {
        compileScript(source, classLoader, configuration, metadataDir, extractingTransformer, verifier);
    } catch (GradleException e) {
        GFileUtils.deleteDirectory(classesDir);
        GFileUtils.deleteDirectory(metadataDir);
        throw e;
    }
    logger.debug("Timing: Writing script to cache at {} took: {}", classesDir.getAbsolutePath(), clock.getElapsed());
}
Also used : Timer(org.gradle.internal.time.Timer) GradleException(org.gradle.api.GradleException) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration)

Example 43 with GradleException

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

the class DefaultScriptCompilationHandler method compileScript.

private void compileScript(final ScriptSource source, ClassLoader classLoader, CompilerConfiguration configuration, File metadataDir, final CompileOperation<?> extractingTransformer, final Action<? super ClassNode> customVerifier) {
    final Transformer transformer = extractingTransformer != null ? extractingTransformer.getTransformer() : null;
    logger.info("Compiling {} using {}.", source.getDisplayName(), transformer != null ? transformer.getClass().getSimpleName() : "no transformer");
    final EmptyScriptDetector emptyScriptDetector = new EmptyScriptDetector();
    final PackageStatementDetector packageDetector = new PackageStatementDetector();
    GroovyClassLoader groovyClassLoader = new GroovyClassLoader(classLoader, configuration, false) {

        @Override
        protected CompilationUnit createCompilationUnit(CompilerConfiguration compilerConfiguration, CodeSource codeSource) {
            CompilationUnit compilationUnit = new CustomCompilationUnit(source, compilerConfiguration, codeSource, customVerifier, this);
            if (transformer != null) {
                transformer.register(compilationUnit);
            }
            compilationUnit.addPhaseOperation(packageDetector, Phases.CANONICALIZATION);
            compilationUnit.addPhaseOperation(emptyScriptDetector, Phases.CANONICALIZATION);
            return compilationUnit;
        }
    };
    groovyClassLoader.setResourceLoader(NO_OP_GROOVY_RESOURCE_LOADER);
    String scriptText = source.getResource().getText();
    String scriptName = source.getClassName();
    GroovyCodeSource codeSource = new GroovyCodeSource(scriptText == null ? "" : scriptText, scriptName, "/groovy/script");
    try {
        try {
            groovyClassLoader.parseClass(codeSource, false);
        } catch (MultipleCompilationErrorsException e) {
            wrapCompilationFailure(source, e);
        } catch (CompilationFailedException e) {
            throw new GradleException(String.format("Could not compile %s.", source.getDisplayName()), e);
        }
        if (packageDetector.hasPackageStatement) {
            throw new UnsupportedOperationException(String.format("%s should not contain a package statement.", StringUtils.capitalize(source.getDisplayName())));
        }
        serializeMetadata(source, extractingTransformer, metadataDir, emptyScriptDetector.isEmptyScript(), emptyScriptDetector.getHasMethods());
    } finally {
        ClassLoaderUtils.tryClose(groovyClassLoader);
    }
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit) Transformer(org.gradle.groovy.scripts.Transformer) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) GroovyCodeSource(groovy.lang.GroovyCodeSource) CodeSource(java.security.CodeSource) GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyCodeSource(groovy.lang.GroovyCodeSource) GradleException(org.gradle.api.GradleException) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException)

Example 44 with GradleException

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

the class TarCopyAction method execute.

public WorkResult execute(final CopyActionProcessingStream stream) {
    final OutputStream outStr;
    try {
        outStr = compressor.createArchiveOutputStream(tarFile);
    } catch (Exception e) {
        throw new GradleException(String.format("Could not create TAR '%s'.", tarFile), e);
    }
    IoActions.withResource(outStr, new ErroringAction<OutputStream>() {

        @Override
        protected void doExecute(final OutputStream outStr) throws Exception {
            TarOutputStream tarOutStr;
            try {
                tarOutStr = new TarOutputStream(outStr);
            } catch (Exception e) {
                throw new GradleException(String.format("Could not create TAR '%s'.", tarFile), e);
            }
            tarOutStr.setLongFileMode(TarOutputStream.LONGFILE_GNU);
            stream.process(new StreamAction(tarOutStr));
            tarOutStr.close();
        }
    });
    return WorkResults.didWork(true);
}
Also used : TarOutputStream(org.apache.tools.tar.TarOutputStream) OutputStream(java.io.OutputStream) TarOutputStream(org.apache.tools.tar.TarOutputStream) GradleException(org.gradle.api.GradleException) CopyActionProcessingStreamAction(org.gradle.api.internal.file.CopyActionProcessingStreamAction) GradleException(org.gradle.api.GradleException)

Example 45 with GradleException

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

the class ZipFileTree method visit.

public void visit(FileVisitor visitor) {
    if (!zipFile.exists()) {
        throw new InvalidUserDataException(String.format("Cannot expand %s as it does not exist.", getDisplayName()));
    }
    if (!zipFile.isFile()) {
        throw new InvalidUserDataException(String.format("Cannot expand %s as it is not a file.", getDisplayName()));
    }
    AtomicBoolean stopFlag = new AtomicBoolean();
    try {
        ZipFile zip = new ZipFile(zipFile);
        File expandedDir = getExpandedDir();
        try {
            // The iteration order of zip.getEntries() is based on the hash of the zip entry. This isn't much use
            // to us. So, collect the entries in a map and iterate over them in alphabetical order.
            Map<String, ZipEntry> entriesByName = new TreeMap<String, ZipEntry>();
            Enumeration entries = zip.getEntries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) entries.nextElement();
                entriesByName.put(entry.getName(), entry);
            }
            Iterator<ZipEntry> sortedEntries = entriesByName.values().iterator();
            while (!stopFlag.get() && sortedEntries.hasNext()) {
                ZipEntry entry = sortedEntries.next();
                if (entry.isDirectory()) {
                    visitor.visitDir(new DetailsImpl(zipFile, expandedDir, entry, zip, stopFlag, chmod));
                } else {
                    visitor.visitFile(new DetailsImpl(zipFile, expandedDir, entry, zip, stopFlag, chmod));
                }
            }
        } finally {
            zip.close();
        }
    } catch (Exception e) {
        throw new GradleException(String.format("Could not expand %s.", getDisplayName()), e);
    }
}
Also used : Enumeration(java.util.Enumeration) ZipEntry(org.apache.tools.zip.ZipEntry) TreeMap(java.util.TreeMap) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) GradleException(org.gradle.api.GradleException) InvalidUserDataException(org.gradle.api.InvalidUserDataException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ZipFile(org.apache.tools.zip.ZipFile) InvalidUserDataException(org.gradle.api.InvalidUserDataException) GradleException(org.gradle.api.GradleException) ZipFile(org.apache.tools.zip.ZipFile) File(java.io.File)

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