Search in sources :

Example 1 with UpdateNodeResolutions

use of org.curioswitch.gradle.plugins.nodejs.tasks.UpdateNodeResolutions in project curiostack by curioswitch.

the class NodeSetupPlugin method apply.

@Override
public void apply(Project project) {
    checkState(project.getParent() == null, "node-setup-plugin can only be applied to the root project.");
    NodeSetupExtension.create(project);
    project.getPlugins().apply(CondaBuildEnvPlugin.class);
    project.getPlugins().apply(BasePlugin.class);
    project.getTasks().withType(Delete.class).named("clean").configure(t -> t.delete("node_modules"));
    var toolManager = DownloadedToolManager.get(project);
    project.getPlugins().withType(ToolDownloaderPlugin.class, plugin -> plugin.registerToolIfAbsent("node", tool -> {
        var version = ToolDependencies.getNodeVersion(project);
        tool.getVersion().set(version);
        tool.getBaseUrl().set("https://nodejs.org/dist/");
        tool.getArtifactPattern().set("v[revision]/[artifact]-v[revision]-[classifier].[ext]");
        var classifiers = tool.getOsClassifiers();
        classifiers.getLinux().set("linux-x64");
        classifiers.getMac().set("darwin-x64");
        classifiers.getWindows().set("win-x64");
        var operatingSystem = new PlatformHelper().getOs();
        String nodePathSubDir = "node-v" + version + "-" + classifiers.getValue(operatingSystem);
        Path prefixDir = toolManager.getToolDir("node").resolve(nodePathSubDir);
        if (operatingSystem != OperatingSystem.WINDOWS) {
            nodePathSubDir += "/bin";
        }
        tool.getPathSubDirs().add(nodePathSubDir);
        tool.getAdditionalCachedDirs().add("yarn-cache");
        var downloadYarn = project.getRootProject().getTasks().register("toolsDownloadYarn", NodeTask.class, t -> {
            var yarnVersion = ToolDependencies.getYarnVersion(project);
            t.setCommand("npm");
            t.args("install", "--global", "--prefix", PathUtil.toBashString(prefixDir), "--no-save", "yarn@" + yarnVersion);
            t.dependsOn(DownloadToolUtil.getDownloadTask(project, "node"), DownloadToolUtil.getSetupTask(project, "miniconda-build"));
            t.execOverride(exec -> exec.workingDir(toolManager.getBinDir("node")));
            t.onlyIf(unused -> {
                File packageJson = toolManager.getBinDir("node").resolve(operatingSystem != OperatingSystem.WINDOWS ? "../lib" : "").resolve(Paths.get("node_modules", "yarn", "package.json")).toFile();
                if (!packageJson.exists()) {
                    return true;
                }
                try {
                    if (!OBJECT_MAPPER.readTree(packageJson).get("version").asText().equals(yarnVersion)) {
                        return true;
                    }
                } catch (IOException e) {
                    throw new UncheckedIOException("Could not read package.json", e);
                }
                return false;
            });
        });
        var setupNode = DownloadToolUtil.getSetupTask(project, "node");
        setupNode.configure(t -> t.dependsOn(downloadYarn));
    }));
    var setupNode = DownloadToolUtil.getSetupTask(project, "node");
    project.allprojects(p -> p.getTasks().withType(NodeTask.class).configureEach(t -> {
        if (t.getPath().equals(":toolsDownloadYarn")) {
            return;
        }
        t.dependsOn(setupNode);
        t.execOverride(exec -> exec.environment("YARN_CACHE_FOLDER", DownloadedToolManager.get(project).getCuriostackDir().resolve("yarn-cache").toString()));
    }));
    var updateNodeResolutions = project.getTasks().register(UpdateNodeResolutions.NAME, UpdateNodeResolutions.class, false);
    var checkNodeResolutions = project.getTasks().register(UpdateNodeResolutions.CHECK_NAME, UpdateNodeResolutions.class, true);
    var yarnWarning = project.getTasks().register("yarnWarning", task -> task.doFirst(unused -> project.getLogger().warn("yarn task failed. If you have updated a dependency and the " + "error says 'Your lockfile needs to be updated.', run \n\n" + "./gradlew yarnUpdate")));
    var yarn = project.getTasks().register("yarn", NodeTask.class, t -> {
        t.dependsOn(setupNode);
        t.args("--frozen-lockfile");
        var packageJsonFile = project.file("package.json");
        if (packageJsonFile.exists()) {
            final JsonNode packageJson;
            try {
                packageJson = OBJECT_MAPPER.readTree(packageJsonFile);
            } catch (IOException e) {
                throw new UncheckedIOException("Could not read package.json", e);
            }
            if (packageJson.has("workspaces")) {
                for (var workspaceNode : packageJson.get("workspaces")) {
                    String workspacePath = workspaceNode.asText();
                    // This is usually used for generating protos.
                    if (!workspacePath.endsWith("/build/web")) {
                        continue;
                    }
                    String projectPath = workspacePath.substring(0, workspacePath.length() - "/build/web".length());
                    Project workspace = project.findProject(':' + projectPath.replace('/', ':'));
                    if (workspace != null) {
                        t.dependsOn(workspace.getPath() + ":build");
                    }
                }
            }
        }
        yarnWarning.get().onlyIf(unused -> t.getState().getFailure() != null);
        t.finalizedBy(yarnWarning, checkNodeResolutions);
    });
    checkNodeResolutions.configure(t -> t.dependsOn(yarn));
    project.getTasks().register("yarnUpdate", NodeTask.class, t -> t.dependsOn(setupNode));
}
Also used : NodeTask(org.curioswitch.gradle.plugins.nodejs.tasks.NodeTask) PathUtil(org.curioswitch.gradle.helpers.platform.PathUtil) Delete(org.gradle.api.tasks.Delete) Project(org.gradle.api.Project) PlatformHelper(org.curioswitch.gradle.helpers.platform.PlatformHelper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ToolDependencies(org.curioswitch.gradle.plugins.curiostack.ToolDependencies) IOException(java.io.IOException) BasePlugin(org.gradle.api.plugins.BasePlugin) DownloadToolUtil(org.curioswitch.gradle.tooldownloader.util.DownloadToolUtil) UpdateNodeResolutions(org.curioswitch.gradle.plugins.nodejs.tasks.UpdateNodeResolutions) File(java.io.File) Preconditions.checkState(com.google.common.base.Preconditions.checkState) UncheckedIOException(java.io.UncheckedIOException) CondaBuildEnvPlugin(org.curioswitch.gradle.conda.CondaBuildEnvPlugin) ToolDownloaderPlugin(org.curioswitch.gradle.tooldownloader.ToolDownloaderPlugin) Paths(java.nio.file.Paths) DownloadedToolManager(org.curioswitch.gradle.tooldownloader.DownloadedToolManager) JsonNode(com.fasterxml.jackson.databind.JsonNode) OperatingSystem(org.curioswitch.gradle.helpers.platform.OperatingSystem) Path(java.nio.file.Path) Plugin(org.gradle.api.Plugin) PlatformHelper(org.curioswitch.gradle.helpers.platform.PlatformHelper) Path(java.nio.file.Path) Project(org.gradle.api.Project) UncheckedIOException(java.io.UncheckedIOException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) NodeTask(org.curioswitch.gradle.plugins.nodejs.tasks.NodeTask) File(java.io.File)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 File (java.io.File)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 CondaBuildEnvPlugin (org.curioswitch.gradle.conda.CondaBuildEnvPlugin)1 OperatingSystem (org.curioswitch.gradle.helpers.platform.OperatingSystem)1 PathUtil (org.curioswitch.gradle.helpers.platform.PathUtil)1 PlatformHelper (org.curioswitch.gradle.helpers.platform.PlatformHelper)1 ToolDependencies (org.curioswitch.gradle.plugins.curiostack.ToolDependencies)1 NodeTask (org.curioswitch.gradle.plugins.nodejs.tasks.NodeTask)1 UpdateNodeResolutions (org.curioswitch.gradle.plugins.nodejs.tasks.UpdateNodeResolutions)1 DownloadedToolManager (org.curioswitch.gradle.tooldownloader.DownloadedToolManager)1 ToolDownloaderPlugin (org.curioswitch.gradle.tooldownloader.ToolDownloaderPlugin)1 DownloadToolUtil (org.curioswitch.gradle.tooldownloader.util.DownloadToolUtil)1 Plugin (org.gradle.api.Plugin)1 Project (org.gradle.api.Project)1