Search in sources :

Example 26 with TaskProvider

use of org.gradle.api.tasks.TaskProvider in project gradle by gradle.

the class GradleBuildDocumentationPlugin method applyConventions.

private void applyConventions(Project project, TaskContainer tasks, ObjectFactory objects, ProjectLayout layout, GradleDocumentationExtension extension) {
    TaskProvider<Sync> stageDocs = tasks.register("stageDocs", Sync.class, task -> {
        // release notes goes in the root of the docs
        task.from(extension.getReleaseNotes().getRenderedDocumentation());
        // DSL reference goes into dsl/
        task.from(extension.getDslReference().getRenderedDocumentation(), sub -> sub.into("dsl"));
        // Javadocs reference goes into javadoc/
        task.from(extension.getJavadocs().getRenderedDocumentation(), sub -> sub.into("javadoc"));
        // User manual goes into userguide/ (for historical reasons)
        task.from(extension.getUserManual().getRenderedDocumentation(), sub -> sub.into("userguide"));
        task.into(extension.getDocumentationRenderedRoot());
    });
    extension.getSourceRoot().convention(layout.getProjectDirectory().dir("src/docs"));
    extension.getDocumentationRenderedRoot().convention(layout.getBuildDirectory().dir("docs"));
    extension.getStagingRoot().convention(layout.getBuildDirectory().dir("working"));
    ConfigurableFileTree css = objects.fileTree();
    css.from(extension.getSourceRoot().dir("css"));
    css.include("*.css");
    extension.getCssFiles().from(css);
    extension.getRenderedDocumentation().from(stageDocs);
    Configuration runtimeClasspath = project.getConfigurations().getByName("runtimeClasspath");
    Configuration sourcesPath = project.getConfigurations().create("sourcesPath");
    sourcesPath.attributes(a -> {
        a.attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.class, Usage.JAVA_RUNTIME));
        a.attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.class, Category.DOCUMENTATION));
        a.attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType.class, "gradle-source-folders"));
    });
    sourcesPath.setCanBeConsumed(false);
    sourcesPath.setCanBeResolved(true);
    sourcesPath.setVisible(false);
    sourcesPath.extendsFrom(runtimeClasspath);
    extension.getClasspath().from(runtimeClasspath);
    extension.getDocumentedSource().from(sourcesPath.getIncoming().artifactView(v -> v.lenient(true)).getFiles().getAsFileTree().matching(f -> {
        // Filter out any non-public APIs
        f.include(PublicApi.INSTANCE.getIncludes());
        f.exclude(PublicApi.INSTANCE.getExcludes());
    }));
}
Also used : PublicApi(gradlebuild.basics.PublicApi) Action(org.gradle.api.Action) Project(org.gradle.api.Project) Category(org.gradle.api.attributes.Category) DocsType(org.gradle.api.attributes.DocsType) ConfigurableFileTree(org.gradle.api.file.ConfigurableFileTree) Configuration(org.gradle.api.artifacts.Configuration) ProviderFactory(org.gradle.api.provider.ProviderFactory) Task(org.gradle.api.Task) ProjectLayout(org.gradle.api.file.ProjectLayout) Sync(org.gradle.api.tasks.Sync) ObjectFactory(org.gradle.api.model.ObjectFactory) TaskContainer(org.gradle.api.tasks.TaskContainer) TaskProvider(org.gradle.api.tasks.TaskProvider) PathSensitivity(org.gradle.api.tasks.PathSensitivity) Exec(org.gradle.api.tasks.Exec) Collections(java.util.Collections) Plugin(org.gradle.api.Plugin) Test(org.gradle.api.tasks.testing.Test) Usage(org.gradle.api.attributes.Usage) Usage(org.gradle.api.attributes.Usage) Category(org.gradle.api.attributes.Category) Configuration(org.gradle.api.artifacts.Configuration) ConfigurableFileTree(org.gradle.api.file.ConfigurableFileTree) DocsType(org.gradle.api.attributes.DocsType) Sync(org.gradle.api.tasks.Sync)

Example 27 with TaskProvider

use of org.gradle.api.tasks.TaskProvider in project gradle by gradle.

the class GradleJavadocsPlugin method generateJavadocs.

private void generateJavadocs(Project project, ProjectLayout layout, TaskContainer tasks, GradleDocumentationExtension extension) {
    // TODO: Staging directory should be a part of the Javadocs extension
    // TODO: Pull out more of this configuration into the extension if it makes sense
    // TODO: in a typical project, this may need to be the regular javadoc task vs javadocAll
    ObjectFactory objects = project.getObjects();
    // TODO: This breaks if version is changed later
    Object version = project.getVersion();
    TaskProvider<Javadoc> javadocAll = tasks.register("javadocAll", Javadoc.class, task -> {
        task.setGroup("documentation");
        task.setDescription("Generate Javadocs for all API classes");
        task.setTitle("Gradle API " + version);
        Javadocs javadocs = extension.getJavadocs();
        // TODO: This should be part of Javadoc task
        task.getInputs().file(javadocs.getJavadocCss()).withPropertyName("stylesheetFile").withPathSensitivity(PathSensitivity.NAME_ONLY);
        StandardJavadocDocletOptions options = (StandardJavadocDocletOptions) task.getOptions();
        options.setEncoding("utf-8");
        options.setDocEncoding("utf-8");
        options.setCharSet("utf-8");
        // TODO: This would be better to model as separate options
        options.addStringOption("Xdoclint:syntax,html,reference", "-quiet");
        // TODO: This breaks the provider
        options.addStringOption("stylesheetfile", javadocs.getJavadocCss().get().getAsFile().getAbsolutePath());
        options.addStringOption("source", "8");
        // TODO: This breaks the provider
        options.links(javadocs.getJavaApi().get().toString(), javadocs.getGroovyApi().get().toString());
        task.source(extension.getDocumentedSource());
        task.setClasspath(extension.getClasspath());
        // TODO: This should be in Javadoc task
        DirectoryProperty generatedJavadocDirectory = objects.directoryProperty();
        generatedJavadocDirectory.set(layout.getBuildDirectory().dir("javadoc"));
        task.getOutputs().dir(generatedJavadocDirectory);
        task.getExtensions().getExtraProperties().set("destinationDirectory", generatedJavadocDirectory);
        // TODO: This breaks the provider
        task.setDestinationDir(generatedJavadocDirectory.get().getAsFile());
        if (BuildEnvironment.INSTANCE.getJavaVersion().isJava11Compatible()) {
            options.addBooleanOption("html4", true);
            options.addBooleanOption("-no-module-directories", true);
            FileSystemOperations fs = getFs();
            // noinspection Convert2Lambda
            task.doLast(new Action<Task>() {

                @Override
                public void execute(Task task) {
                    fs.copy(copySpec -> {
                        // Commit http://hg.openjdk.java.net/jdk/jdk/rev/89dc31d7572b broke use of JSZip (https://bugs.openjdk.java.net/browse/JDK-8214856)
                        // fixed in Java 12 by http://hg.openjdk.java.net/jdk/jdk/rev/b4982a22926b
                        // TODO: Remove this script.js workaround when we distribute Gradle using JDK 12 or higher
                        copySpec.from(extension.getSourceRoot().dir("js/javadoc"));
                        // This is a work-around for https://bugs.openjdk.java.net/browse/JDK-8211194. Can be removed once that issue is fixed on JDK"s side
                        // Since JDK 11, package-list is missing from javadoc output files and superseded by element-list file, but a lot of external tools still need it
                        // Here we generate this file manually
                        copySpec.from(generatedJavadocDirectory.file("element-list"), sub -> {
                            sub.rename(t -> "package-list");
                        });
                        copySpec.into(generatedJavadocDirectory);
                    });
                }
            });
        }
    });
    extension.javadocs(javadocs -> {
        javadocs.getJavadocCss().convention(extension.getSourceRoot().file("css/javadoc.css"));
        // TODO: destinationDirectory should be part of Javadoc
        javadocs.getRenderedDocumentation().from(javadocAll.flatMap(task -> (DirectoryProperty) task.getExtensions().getExtraProperties().get("destinationDirectory")));
    });
    CheckstyleExtension checkstyle = project.getExtensions().getByType(CheckstyleExtension.class);
    tasks.register("checkstyleApi", Checkstyle.class, task -> {
        task.source(extension.getDocumentedSource());
        // TODO: This is ugly
        task.setConfig(project.getResources().getText().fromFile(checkstyle.getConfigDirectory().file("checkstyle-api.xml")));
        task.setClasspath(layout.files());
        task.getReports().getXml().setDestination(new File(checkstyle.getReportsDir(), "checkstyle-api.xml"));
    });
}
Also used : FileSystemOperations(org.gradle.api.file.FileSystemOperations) Checkstyle(org.gradle.api.plugins.quality.Checkstyle) Action(org.gradle.api.Action) Project(org.gradle.api.Project) StandardJavadocDocletOptions(org.gradle.external.javadoc.StandardJavadocDocletOptions) File(java.io.File) Inject(javax.inject.Inject) Task(org.gradle.api.Task) ProjectLayout(org.gradle.api.file.ProjectLayout) Javadoc(org.gradle.api.tasks.javadoc.Javadoc) ObjectFactory(org.gradle.api.model.ObjectFactory) TaskContainer(org.gradle.api.tasks.TaskContainer) TaskProvider(org.gradle.api.tasks.TaskProvider) DirectoryProperty(org.gradle.api.file.DirectoryProperty) PathSensitivity(org.gradle.api.tasks.PathSensitivity) CheckstyleExtension(org.gradle.api.plugins.quality.CheckstyleExtension) BuildEnvironment(gradlebuild.basics.BuildEnvironment) Plugin(org.gradle.api.Plugin) Task(org.gradle.api.Task) Javadoc(org.gradle.api.tasks.javadoc.Javadoc) FileSystemOperations(org.gradle.api.file.FileSystemOperations) CheckstyleExtension(org.gradle.api.plugins.quality.CheckstyleExtension) StandardJavadocDocletOptions(org.gradle.external.javadoc.StandardJavadocDocletOptions) DirectoryProperty(org.gradle.api.file.DirectoryProperty) ObjectFactory(org.gradle.api.model.ObjectFactory) File(java.io.File)

Example 28 with TaskProvider

use of org.gradle.api.tasks.TaskProvider in project curiostack by curioswitch.

the class GolangPlugin method apply.

@Override
public void apply(Project project) {
    project.getRootProject().getPlugins().apply(GolangSetupPlugin.class);
    project.getPlugins().apply(BasePlugin.class);
    var golang = GolangExtension.createAndAdd(project);
    var toolManager = DownloadedToolManager.get(project);
    project.getTasks().addRule(new Rule() {

        @Override
        public String getDescription() {
            return "Pattern: \"go_<command>\": Executes a go command.";
        }

        @Override
        public void apply(String taskName) {
            if (taskName.startsWith("go_")) {
                project.getTasks().register(taskName, GoTask.class, t -> {
                    List<String> tokens = TASK_NAME_SPLITTER.splitToList(taskName);
                    t.args(tokens.subList(1, tokens.size()));
                });
            }
        }
    });
    var setupGo = DownloadToolUtil.getSetupTask(project, "go");
    setupGo.configure(t -> t.dependsOn(DownloadToolUtil.getSetupTask(project, "miniconda-build")));
    project.getTasks().withType(GoTask.class).configureEach(t -> t.dependsOn(setupGo));
    project.getRootProject().mkdir("build");
    project.getExtensions().getByType(ExtraPropertiesExtension.class).set("gopath", project.getGradle().getGradleUserHomeDir().toPath().resolve("curiostack").resolve("gopath"));
    var downloadDeps = project.getTasks().register("goDownloadDeps", GoTask.class, t -> {
        t.args("mod", "download");
        project.getRootProject().mkdir("build");
    });
    project.getTasks().register("goUpdateDeps", GoTask.class, t -> {
        t.args("get", "-u");
        // Go get often has a failed status code yet still basically worked so we let it go.
        t.execCustomizer(exec -> exec.setIgnoreExitValue(true));
    });
    var checkFormat = project.getTasks().register("goCheck", GoTask.class, t -> {
        t.command("gofmt");
        t.args("-s", "-d", ".");
        ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
        t.doLast(unused -> {
            if (stdOut.size() > 0) {
                throw new GradleException("There were formatting violations. Run :goFormat to fix.\n" + stdOut.toString());
            }
        });
        t.execCustomizer(exec -> exec.setStandardOutput(stdOut));
    });
    var check = project.getTasks().named(LifecycleBasePlugin.CHECK_TASK_NAME);
    check.configure(t -> t.dependsOn(checkFormat));
    project.getTasks().register("goFormat", GoTask.class, t -> {
        t.command("gofmt");
        t.args("-s", "-w", ".");
    });
    var goTest = project.getTasks().register("goTest", GoTestTask.class, t -> {
        t.execCustomizer(exec -> CondaExecUtil.condaExec(exec, toolManager, golang.getConda().get()));
        t.dependsOn(DownloadToolUtil.getSetupTask(project, golang.getConda().get()), downloadDeps);
    });
    var goBuildAll = project.getTasks().register("goBuildAll");
    project.getTasks().named(LifecycleBasePlugin.ASSEMBLE_TASK_NAME).configure(t -> t.dependsOn(goBuildAll));
    project.afterEvaluate(unused -> {
        TaskProvider<Task> test;
        try {
            test = project.getTasks().named("test");
        } catch (UnknownTaskException e) {
            test = project.getTasks().register("test");
            TaskProvider<Task> testCopy = test;
            check.configure(t -> t.dependsOn(testCopy));
        }
        test.configure(t -> t.dependsOn(goTest));
        List<String> goOses = golang.getGoOses().get();
        if (goOses.isEmpty()) {
            goOses = ImmutableList.of("");
        }
        List<String> goArchs = golang.getGoArchs().get();
        if (goArchs.isEmpty()) {
            goArchs = ImmutableList.of("");
        }
        var goBuildDir = project.file("build/exe").toPath();
        var exeName = golang.getExecutableName().getOrElse(project.getProjectDir().getName());
        for (String goOs : goOses) {
            for (String goArch : goArchs) {
                String outputDir = goOs.isEmpty() ? "current" : goOs;
                if (!goArch.isEmpty()) {
                    outputDir += '-' + goArch;
                }
                String outPath = goBuildDir.resolve(outputDir).resolve(exeName).toString() + (goOs.equals("windows") ? ".exe" : "");
                var goBuild = project.getTasks().register("goBuild" + toTaskSuffix(goOs) + toTaskSuffix(goArch), GoTask.class, t -> {
                    t.args("build", "-o", outPath);
                    t.execCustomizer(exec -> {
                        if (!goOs.isEmpty()) {
                            exec.environment("GOOS", goOs);
                        }
                        if (!goArch.isEmpty()) {
                            exec.environment("GOARCH", goArch);
                        }
                        CondaExecUtil.condaExec(exec, DownloadedToolManager.get(project), golang.getConda().get());
                    });
                    t.dependsOn(DownloadToolUtil.getSetupTask(project, golang.getConda().get()), downloadDeps);
                });
                project.getTasks().register("jib" + toTaskSuffix(goOs) + toTaskSuffix(goArch), JibTask.class, t -> {
                    t.dependsOn(goBuild);
                    t.setExePath(Paths.get(outPath));
                });
                goBuildAll.configure(t -> t.dependsOn(goBuild));
            }
        }
    });
}
Also used : ExtraPropertiesExtension(org.gradle.api.plugins.ExtraPropertiesExtension) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Project(org.gradle.api.Project) GoTask(org.curioswitch.gradle.golang.tasks.GoTask) BasePlugin(org.gradle.api.plugins.BasePlugin) DownloadToolUtil(org.curioswitch.gradle.tooldownloader.util.DownloadToolUtil) Rule(org.gradle.api.Rule) LifecycleBasePlugin(org.gradle.language.base.plugins.LifecycleBasePlugin) List(java.util.List) Task(org.gradle.api.Task) TaskUtil.toTaskSuffix(org.curioswitch.gradle.helpers.task.TaskUtil.toTaskSuffix) CondaExecUtil(org.curioswitch.gradle.conda.exec.CondaExecUtil) UnknownTaskException(org.gradle.api.UnknownTaskException) ImmutableList(com.google.common.collect.ImmutableList) JibTask(org.curioswitch.gradle.golang.tasks.JibTask) Paths(java.nio.file.Paths) GradleException(org.gradle.api.GradleException) TaskProvider(org.gradle.api.tasks.TaskProvider) GoTestTask(org.curioswitch.gradle.golang.tasks.GoTestTask) DownloadedToolManager(org.curioswitch.gradle.tooldownloader.DownloadedToolManager) Splitter(com.google.common.base.Splitter) Plugin(org.gradle.api.Plugin) GoTask(org.curioswitch.gradle.golang.tasks.GoTask) Task(org.gradle.api.Task) JibTask(org.curioswitch.gradle.golang.tasks.JibTask) GoTestTask(org.curioswitch.gradle.golang.tasks.GoTestTask) ExtraPropertiesExtension(org.gradle.api.plugins.ExtraPropertiesExtension) GoTask(org.curioswitch.gradle.golang.tasks.GoTask) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UnknownTaskException(org.gradle.api.UnknownTaskException) GradleException(org.gradle.api.GradleException) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Rule(org.gradle.api.Rule) TaskProvider(org.gradle.api.tasks.TaskProvider)

Aggregations

TaskProvider (org.gradle.api.tasks.TaskProvider)28 Project (org.gradle.api.Project)25 Plugin (org.gradle.api.Plugin)23 TaskContainer (org.gradle.api.tasks.TaskContainer)18 Inject (javax.inject.Inject)17 ObjectFactory (org.gradle.api.model.ObjectFactory)14 Provider (org.gradle.api.provider.Provider)12 LifecycleBasePlugin (org.gradle.language.base.plugins.LifecycleBasePlugin)12 Task (org.gradle.api.Task)11 DirectoryProperty (org.gradle.api.file.DirectoryProperty)11 File (java.io.File)10 Configuration (org.gradle.api.artifacts.Configuration)10 Callable (java.util.concurrent.Callable)9 ProjectInternal (org.gradle.api.internal.project.ProjectInternal)9 List (java.util.List)8 Action (org.gradle.api.Action)8 SourceSet (org.gradle.api.tasks.SourceSet)8 Names (org.gradle.language.nativeplatform.internal.Names)8 SourceSetContainer (org.gradle.api.tasks.SourceSetContainer)7 Set (java.util.Set)6