Search in sources :

Example 1 with UnknownTaskException

use of org.gradle.api.UnknownTaskException in project crnk-framework by crnk-project.

the class TSGeneratorPlugin method setupPackageTasks.

void setupPackageTasks(Project project, Task generateTask) {
    final File buildDir = new File(project.getBuildDir(), "npm_compile");
    final File distDir = getNpmOutputDir(project);
    project.getTasks().create(PublishTypescriptStubsTask.NAME, PublishTypescriptStubsTask.class);
    TSGeneratorConfig config = project.getExtensions().getByType(TSGeneratorConfig.class);
    Copy copySources = project.getTasks().create("processTypescript", Copy.class);
    copySources.from(config.getGenDir());
    copySources.into(buildDir);
    copySources.dependsOn(generateTask);
    // copy .npmrc file from root to working directory if available
    final File npmrcFile = new File(project.getProjectDir(), ".npmrc");
    if (npmrcFile.exists()) {
        copySources.getInputs().file(npmrcFile);
        copySources.doFirst(new Action<Task>() {

            @Override
            public void execute(Task task) {
                File targetFile = new File(buildDir, ".npmrc");
                buildDir.mkdirs();
                TypescriptUtils.copyFile(npmrcFile, targetFile);
            }
        });
    }
    CompileTypescriptStubsTask compileTypescriptTask = project.getTasks().create(CompileTypescriptStubsTask.NAME, CompileTypescriptStubsTask.class);
    try {
        NpmInstallTask npmInstall = (NpmInstallTask) project.getTasks().getByName("npmInstall");
        npmInstall.setWorkingDir(buildDir);
        npmInstall.dependsOn(copySources);
        npmInstall.getInputs().files(new File(buildDir, "package.json"));
        npmInstall.getOutputs().dir(new File(buildDir, "node_modules"));
        compileTypescriptTask.dependsOn(npmInstall);
    } catch (UnknownTaskException e) {
        LOGGER.warn("task not found, ok in testing", e);
    }
    ConfigurableFileTree fileTree = project.fileTree(buildDir);
    fileTree.include("package.json");
    fileTree.include(".npmrc");
    fileTree.include("**/*.ts");
    fileTree.exclude("**/*.d.ts");
    compileTypescriptTask.getInputs().files(fileTree);
    compileTypescriptTask.setWorkingDir(buildDir);
    compileTypescriptTask.getOutputs().dir(buildDir);
    ConfigurableFileTree assembleFileTree = project.fileTree(new File(buildDir, "src"));
    assembleFileTree.include("**/*.ts");
    assembleFileTree.include("**/*.js");
    assembleFileTree.include("**/*.js.map");
    Copy assembleSources = project.getTasks().create("assembleTypescript", Copy.class);
    assembleSources.from(assembleFileTree);
    assembleSources.from(new File(buildDir, "package.json"));
    assembleSources.into(distDir);
    assembleSources.dependsOn(compileTypescriptTask);
}
Also used : UnknownTaskException(org.gradle.api.UnknownTaskException) Task(org.gradle.api.Task) NpmInstallTask(com.moowork.gradle.node.npm.NpmInstallTask) NpmInstallTask(com.moowork.gradle.node.npm.NpmInstallTask) Copy(org.gradle.api.tasks.Copy) ConfigurableFileTree(org.gradle.api.file.ConfigurableFileTree) File(java.io.File)

Example 2 with UnknownTaskException

use of org.gradle.api.UnknownTaskException in project curiostack by curioswitch.

the class CurioGenericCiPlugin method apply.

@Override
public void apply(Project project) {
    if (project.getParent() != null) {
        throw new IllegalStateException("curio-generic-ci-plugin can only be applied to the root project.");
    }
    var config = CiExtension.createAndAdd(project);
    var state = CiState.createAndAdd(project);
    if (!state.isCi()) {
        return;
    }
    var cleanWrapper = project.getTasks().register("cleanWrapper", Delete.class, t -> {
        File gradleHome = project.getGradle().getGradleHomeDir();
        t.delete(new File(gradleHome, "docs"));
        t.delete(new File(gradleHome, "media"));
        t.delete(new File(gradleHome, "samples"));
        t.delete(new File(gradleHome, "src"));
        // Zip file should always be foldername-all.zip
        var archive = new File(gradleHome.getParent(), gradleHome.getName() + "-all.zip");
        t.delete(archive);
    });
    Task continuousBuild = project.task("continuousBuild");
    if (state.isMasterBuild()) {
        continuousBuild.dependsOn(cleanWrapper);
    }
    project.allprojects(proj -> proj.getPlugins().withType(GolangPlugin.class, unused -> proj.getExtensions().getByType(GolangExtension.class).jib(jib -> jib.getAdditionalTags().addAll(state.getRevisionTags()))));
    if (state.isReleaseBuild()) {
        project.afterEvaluate(unused -> configureReleaseBuild(project, config, state));
        return;
    }
    var fetchCodeCovCache = project.getTasks().create("fetchCodeCovCache", FetchCodeCovCacheTask.class);
    var uploadCodeCovCache = project.getTasks().create("uploadCodeCovCache", UploadCodeCovCacheTask.class);
    var uploadCoverage = project.getTasks().create("uploadToCodeCov", UploadToCodeCovTask.class, t -> {
        t.dependsOn(DownloadToolUtil.getSetupTask(project, "miniconda-build"));
        if (state.isMasterBuild()) {
            t.finalizedBy(uploadCodeCovCache);
        }
    });
    // Don't need to slow down local builds with coverage.
    if (!state.isLocalBuild() && !"true".equals(project.findProperty("org.curioswitch.curiostack.ci.disableCoverage"))) {
        continuousBuild.dependsOn(uploadCoverage);
        project.allprojects(proj -> {
            proj.getPlugins().withType(JavaPlugin.class, unused -> proj.getPlugins().apply(JacocoPlugin.class));
            proj.getPlugins().withType(GolangPlugin.class, unused -> proj.getTasks().withType(GoTestTask.class).configureEach(t -> {
                t.coverage(true);
                uploadCoverage.mustRunAfter(t);
            }));
        });
        project.subprojects(proj -> proj.getPlugins().withType(JacocoPlugin.class, unused -> {
            var testReport = proj.getTasks().named("jacocoTestReport", JacocoReport.class);
            uploadCoverage.mustRunAfter(testReport);
            testReport.configure(t -> t.reports(reports -> {
                reports.getXml().setEnabled(true);
                reports.getHtml().setEnabled(true);
                reports.getCsv().setEnabled(false);
            }));
            try {
                proj.getTasks().named("build").configure(t -> t.dependsOn(testReport));
            } catch (UnknownTaskException e) {
            // Ignore.
            }
        }));
    }
    final Set<Project> affectedProjects;
    try {
        affectedProjects = computeAffectedProjects(project);
    } catch (Throwable t) {
        // Don't prevent further gradle configuration due to issues computing the git state.
        project.getLogger().warn("Couldn't compute affected targets.", t);
        return;
    }
    final Set<Project> projectsToBuild;
    if (affectedProjects.contains(project.getRootProject())) {
        // Rebuild everything when the root project is changed.
        projectsToBuild = ImmutableSet.copyOf(project.getAllprojects());
    } else {
        projectsToBuild = affectedProjects;
        // We only fetch the code cov cache on non-full builds since we don't need to propagate for
        // full ones.
        uploadCoverage.dependsOn(fetchCodeCovCache);
    }
    for (var proj : projectsToBuild) {
        proj.getPlugins().withType(GolangPlugin.class, unused -> {
            // TODO(choko): Figure out whether it's better to register plugins outside this
            // artifact here or in each plugin somehow.
            proj.getTasks().withType(JibTask.class, t -> addToMasterBuild(proj, t));
        });
        proj.getPlugins().withType(LifecycleBasePlugin.class, unused -> continuousBuild.dependsOn(proj.getTasks().named(LifecycleBasePlugin.BUILD_TASK_NAME)));
        proj.getPlugins().withType(JavaBasePlugin.class, unused -> continuousBuild.dependsOn(proj.getTasks().named(JavaBasePlugin.BUILD_DEPENDENTS_TASK_NAME)));
    }
}
Also used : JavaPlugin(org.gradle.api.plugins.JavaPlugin) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Task(org.gradle.api.Task) UploadCodeCovCacheTask(org.curioswitch.gradle.plugins.ci.tasks.UploadCodeCovCacheTask) UnknownTaskException(org.gradle.api.UnknownTaskException) JibTask(org.curioswitch.gradle.golang.tasks.JibTask) JavaBasePlugin(org.gradle.api.plugins.JavaBasePlugin) GolangPlugin(org.curioswitch.gradle.golang.GolangPlugin) TaskProvider(org.gradle.api.tasks.TaskProvider) GoTestTask(org.curioswitch.gradle.golang.tasks.GoTestTask) Map(java.util.Map) Splitter(com.google.common.base.Splitter) Path(java.nio.file.Path) JacocoReport(org.gradle.testing.jacoco.tasks.JacocoReport) Delete(org.gradle.api.tasks.Delete) ImmutableSet(com.google.common.collect.ImmutableSet) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Project(org.gradle.api.Project) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) Constants(org.eclipse.jgit.lib.Constants) Collectors(java.util.stream.Collectors) RevTree(org.eclipse.jgit.revwalk.RevTree) UncheckedIOException(java.io.UncheckedIOException) List(java.util.List) Ref(org.eclipse.jgit.lib.Ref) UploadToCodeCovTask(org.curioswitch.gradle.plugins.ci.tasks.UploadToCodeCovTask) RevCommit(org.eclipse.jgit.revwalk.RevCommit) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) GolangExtension(org.curioswitch.gradle.golang.GolangExtension) BasePlugin(org.gradle.api.plugins.BasePlugin) DownloadToolUtil(org.curioswitch.gradle.tooldownloader.util.DownloadToolUtil) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LifecycleBasePlugin(org.gradle.language.base.plugins.LifecycleBasePlugin) ImmutableList(com.google.common.collect.ImmutableList) FetchCodeCovCacheTask(org.curioswitch.gradle.plugins.ci.tasks.FetchCodeCovCacheTask) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) ExtraPropertiesExtension(org.gradle.api.plugins.ExtraPropertiesExtension) Files(java.nio.file.Files) Ascii(com.google.common.base.Ascii) JacocoPlugin(org.gradle.testing.jacoco.plugins.JacocoPlugin) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) IOException(java.io.IOException) File(java.io.File) ObjectId(org.eclipse.jgit.lib.ObjectId) Paths(java.nio.file.Paths) Git(org.eclipse.jgit.api.Git) DiffEntry(org.eclipse.jgit.diff.DiffEntry) Collections(java.util.Collections) ObjectReader(org.eclipse.jgit.lib.ObjectReader) Plugin(org.gradle.api.Plugin) Task(org.gradle.api.Task) UploadCodeCovCacheTask(org.curioswitch.gradle.plugins.ci.tasks.UploadCodeCovCacheTask) JibTask(org.curioswitch.gradle.golang.tasks.JibTask) GoTestTask(org.curioswitch.gradle.golang.tasks.GoTestTask) UploadToCodeCovTask(org.curioswitch.gradle.plugins.ci.tasks.UploadToCodeCovTask) FetchCodeCovCacheTask(org.curioswitch.gradle.plugins.ci.tasks.FetchCodeCovCacheTask) JacocoPlugin(org.gradle.testing.jacoco.plugins.JacocoPlugin) GolangPlugin(org.curioswitch.gradle.golang.GolangPlugin) JacocoReport(org.gradle.testing.jacoco.tasks.JacocoReport) UnknownTaskException(org.gradle.api.UnknownTaskException) Project(org.gradle.api.Project) GolangExtension(org.curioswitch.gradle.golang.GolangExtension) File(java.io.File)

Example 3 with UnknownTaskException

use of org.gradle.api.UnknownTaskException 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

Task (org.gradle.api.Task)3 UnknownTaskException (org.gradle.api.UnknownTaskException)3 Splitter (com.google.common.base.Splitter)2 ImmutableList (com.google.common.collect.ImmutableList)2 File (java.io.File)2 Paths (java.nio.file.Paths)2 List (java.util.List)2 GoTestTask (org.curioswitch.gradle.golang.tasks.GoTestTask)2 JibTask (org.curioswitch.gradle.golang.tasks.JibTask)2 DownloadToolUtil (org.curioswitch.gradle.tooldownloader.util.DownloadToolUtil)2 Plugin (org.gradle.api.Plugin)2 Project (org.gradle.api.Project)2 BasePlugin (org.gradle.api.plugins.BasePlugin)2 ExtraPropertiesExtension (org.gradle.api.plugins.ExtraPropertiesExtension)2 TaskProvider (org.gradle.api.tasks.TaskProvider)2 LifecycleBasePlugin (org.gradle.language.base.plugins.LifecycleBasePlugin)2 Ascii (com.google.common.base.Ascii)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1