All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nebula.test.functional.internal.classpath.ClasspathAddingInitScriptBuilder.groovy Maven / Gradle / Ivy

Go to download

Test harness for Gradle plugins. Hopefully retiring in favor of Gradle TestKit

There is a newer version: 10.6.1
Show newest version
package nebula.test.functional.internal.classpath

import com.google.common.base.Function
import com.google.common.base.Predicate
import com.google.common.collect.FluentIterable
import groovy.transform.CompileStatic
import org.gradle.internal.ErroringAction
import org.gradle.internal.IoActions
import org.gradle.internal.classloader.ClasspathUtil
import org.gradle.internal.classpath.ClassPath
import org.gradle.util.TextUtil

@CompileStatic
class ClasspathAddingInitScriptBuilder {
    private ClasspathAddingInitScriptBuilder() {
    }

    public static void build(File initScriptFile, final ClassLoader classLoader, Predicate classpathFilter) {
        build(initScriptFile, getClasspathAsFiles(classLoader, classpathFilter));
    }

    public static void build(File initScriptFile, final List classpath) {
        IoActions.writeTextFile(initScriptFile, new ErroringAction() {
            @Override
            protected void doExecute(Writer writer) throws Exception {
                writer.write("allprojects {\n")
                writer.write("  buildscript {\n")
                writer.write("    dependencies {\n")
                writeClasspath(writer, classpath)
                writer.write("    }\n")
                writer.write("  }\n")
                writer.write("}\n")
                writer.write("initscript {\n")
                writer.write("  dependencies {\n")
                writeClasspath(writer, classpath)
                writer.write("  }\n")
                writer.write("}\n")
            }
        })
    }

    public static writeClasspath(Writer writer, List classpath) {
        for (File file : classpath) {
            // Commons-lang 2.4 does not escape forward slashes correctly, https://issues.apache.org/jira/browse/LANG-421
            writer.write(String.format("      classpath files('%s')\n", TextUtil.escapeString(file.getAbsolutePath())));
        }
    }

    public static List getClasspathAsFiles(ClassLoader classLoader, Predicate classpathFilter) {
        List classpathUrls = getClasspathUrls(classLoader)
        return FluentIterable.from(classpathUrls).filter(classpathFilter).transform(new Function() {
            @Override
            File apply(URL url) {
                return new File(url.toURI());
            }
        }).toList()
    }

    private static List getClasspathUrls(ClassLoader classLoader) {
        Object cp = ClasspathUtil.getClasspath(classLoader)
        if (cp instanceof List) {
            return (List) cp
        }
        if (cp instanceof ClassPath) { // introduced by gradle/gradle@0ab8bc2
            return ((ClassPath) cp).asURLs
        }
        throw new IllegalStateException("Unable to extract classpath urls from type ${cp.class.canonicalName}")
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy