Search in sources :

Example 6 with PlatformHelper

use of org.curioswitch.gradle.helpers.platform.PlatformHelper in project curiostack by curioswitch.

the class UploadCodeCovCacheTask method exec.

@TaskAction
public void exec() {
    if (!getCodeCovReportFile().exists()) {
        // UploadToCodeCovTask failed, skip uploading the cache.
        return;
    }
    ExternalExecUtil.exec(getProject(), workerExecutor, exec -> {
        final List<String> coverageFiles;
        try (var lines = Files.lines(getCodeCovReportFile().toPath())) {
            coverageFiles = lines.filter(line -> line.startsWith("# path=")).map(line -> line.substring("# path=".length())).filter(filename -> Files.exists(getProject().file(filename).toPath())).map(line -> "./" + line).collect(toImmutableList());
        } catch (IOException e) {
            throw new UncheckedIOException("Could not read coverage report dump", e);
        }
        var toolManager = DownloadedToolManager.get(getProject());
        String gsutil = new PlatformHelper().getOs() == OperatingSystem.WINDOWS ? "gsutil.cmd" : "gsutil";
        exec.executable("bash");
        exec.args("-c", "tar -cpzf - " + String.join(" ", coverageFiles) + " | " + gsutil + " cp - " + dest.get());
        toolManager.addAllToPath(exec);
    });
}
Also used : Files(java.nio.file.Files) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) PlatformHelper(org.curioswitch.gradle.helpers.platform.PlatformHelper) ExternalExecUtil(org.curioswitch.gradle.helpers.exec.ExternalExecUtil) IOException(java.io.IOException) File(java.io.File) UncheckedIOException(java.io.UncheckedIOException) Inject(javax.inject.Inject) TaskAction(org.gradle.api.tasks.TaskAction) List(java.util.List) Provider(org.gradle.api.provider.Provider) WorkerExecutor(org.gradle.workers.WorkerExecutor) DownloadedToolManager(org.curioswitch.gradle.tooldownloader.DownloadedToolManager) Property(org.gradle.api.provider.Property) InputFile(org.gradle.api.tasks.InputFile) DefaultTask(org.gradle.api.DefaultTask) OperatingSystem(org.curioswitch.gradle.helpers.platform.OperatingSystem) Input(org.gradle.api.tasks.Input) PlatformHelper(org.curioswitch.gradle.helpers.platform.PlatformHelper) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) TaskAction(org.gradle.api.tasks.TaskAction)

Example 7 with PlatformHelper

use of org.curioswitch.gradle.helpers.platform.PlatformHelper in project curiostack by curioswitch.

the class CondaPlugin method addCondaTasks.

private static void addCondaTasks(Project project, CondaExtension conda) {
    var operatingSystem = new PlatformHelper().getOs();
    project.getPlugins().withType(ToolDownloaderPlugin.class, plugin -> {
        plugin.registerToolIfAbsent(conda.getName(), tool -> {
            tool.getArtifact().set(conda.getName());
            tool.getVersion().set(conda.getVersion());
            tool.getBaseUrl().set("https://repo.continuum.io/miniconda/");
            tool.getArtifactPattern().set("[revision]-[classifier].[ext]");
            if (operatingSystem == OperatingSystem.WINDOWS) {
                tool.getPathSubDirs().addAll("", "Scripts", "Library/bin");
            } else {
                tool.getPathSubDirs().add("bin");
            }
            var osClassifiers = tool.getOsClassifiers();
            osClassifiers.getLinux().set("Linux-x86_64");
            osClassifiers.getMac().set("MacOSX-x86_64");
            osClassifiers.getWindows().set("Windows-x86_64");
            var osExtensions = tool.getOsExtensions();
            osExtensions.getLinux().set("sh");
            osExtensions.getMac().set("sh");
            osExtensions.getWindows().set("exe");
        });
        var download = DownloadToolUtil.getDownloadTask(project, conda.getName());
        download.configure(t -> t.setArchiveExtractAction(archive -> {
            archive.setExecutable(true);
            var toolDir = plugin.toolManager().getToolDir(conda.getName());
            if (operatingSystem == OperatingSystem.WINDOWS) {
                project.exec(exec -> {
                    exec.executable("cmd");
                    exec.args("/k", "start /wait " + archive.getAbsolutePath() + " /S /InstallationType=JustMe /AddToPath=0 " + "/RegisterPython=0 /NoRegistry=1 /D=" + toolDir.toAbsolutePath().toString());
                });
            } else {
                project.exec(exec -> {
                    exec.executable(archive);
                    exec.args("-f", "-b", "-p", toolDir.toAbsolutePath().toString());
                });
            }
        }));
        var condaInstallPackages = project.getTasks().register("condaInstallPackages" + TaskUtil.toTaskSuffix(conda.getName()), InstallCondaPackagesTask.class, conda, plugin.toolManager());
        condaInstallPackages.configure(t -> t.dependsOn(download));
        var condaInstallPythonPackages = project.getTasks().register("condaInstallPythonPackages" + TaskUtil.toTaskSuffix(conda.getName()), InstallPythonPackagesTask.class, conda, plugin.toolManager());
        condaInstallPythonPackages.configure(t -> t.dependsOn(condaInstallPackages));
        DownloadToolUtil.getSetupTask(project, conda.getName()).configure(t -> t.dependsOn(condaInstallPackages, condaInstallPythonPackages));
    });
}
Also used : PlatformHelper(org.curioswitch.gradle.helpers.platform.PlatformHelper) MonotonicNonNull(org.checkerframework.checker.nullness.qual.MonotonicNonNull) InstallPythonPackagesTask(org.curioswitch.gradle.conda.tasks.InstallPythonPackagesTask) ToolDownloaderPlugin(org.curioswitch.gradle.tooldownloader.ToolDownloaderPlugin) Project(org.gradle.api.Project) InstallCondaPackagesTask(org.curioswitch.gradle.conda.tasks.InstallCondaPackagesTask) PlatformHelper(org.curioswitch.gradle.helpers.platform.PlatformHelper) TaskUtil(org.curioswitch.gradle.helpers.task.TaskUtil) OperatingSystem(org.curioswitch.gradle.helpers.platform.OperatingSystem) NamedDomainObjectContainer(org.gradle.api.NamedDomainObjectContainer) DownloadToolUtil(org.curioswitch.gradle.tooldownloader.util.DownloadToolUtil) Plugin(org.gradle.api.Plugin) Preconditions.checkState(com.google.common.base.Preconditions.checkState)

Example 8 with PlatformHelper

use of org.curioswitch.gradle.helpers.platform.PlatformHelper in project curiostack by curioswitch.

the class InstallPythonPackagesTask method exec.

@TaskAction
void exec() {
    Path toolDir = toolManager.getToolDir(name);
    getProject().exec(exec -> {
        OperatingSystem operatingSystem = new PlatformHelper().getOs();
        if (operatingSystem == OperatingSystem.WINDOWS) {
            exec.executable(toolDir.resolve("Scripts").resolve("pip.exe"));
        } else {
            exec.executable(toolDir.resolve("bin").resolve("pip"));
        }
        exec.args("install");
        exec.args(packages.get());
        toolManager.addAllToPath(exec);
        CondaExecUtil.condaExec(exec, toolManager, name);
    });
}
Also used : Path(java.nio.file.Path) OperatingSystem(org.curioswitch.gradle.helpers.platform.OperatingSystem) PlatformHelper(org.curioswitch.gradle.helpers.platform.PlatformHelper) TaskAction(org.gradle.api.tasks.TaskAction)

Example 9 with PlatformHelper

use of org.curioswitch.gradle.helpers.platform.PlatformHelper in project curiostack by curioswitch.

the class CondaExecUtil method condaExec.

/**
 * Modifies the {@link ExecSpec} to run its command in a conda environment.
 */
public static void condaExec(ExecSpec exec, DownloadedToolManager toolManager, String tool) {
    var platformHelper = new PlatformHelper();
    Path condaDir = toolManager.getToolDir(tool);
    Path condaSh = condaDir.resolve(Paths.get("etc", "profile.d", "conda.sh"));
    List<String> currentCommandLine = exec.getCommandLine();
    if (platformHelper.getOs() == OperatingSystem.WINDOWS) {
        currentCommandLine = ImmutableList.<String>builderWithExpectedSize(currentCommandLine.size()).add(PathUtil.toBashString(currentCommandLine.get(0))).addAll(Iterables.skip(currentCommandLine, 1)).build();
    }
    exec.setExecutable(PathUtil.getBashExecutable());
    exec.setArgs(ImmutableList.of("-c", ". " + PathUtil.toBashString(condaSh) + " && conda activate > /dev/null && cd " + PathUtil.toBashString(exec.getWorkingDir().toPath()) + " && " + String.join(" ", currentCommandLine)));
    switch(platformHelper.getOs()) {
        case LINUX:
            exec.environment("CONAN_ENV_COMPILER", "gcc");
            exec.environment("CONAN_ENV_COMPILER_VERSION", "7");
            exec.environment("CONAN_ENV_COMPILER_LIBCXX", "libstdc++");
            break;
        case MAC_OSX:
            exec.environment("CONAN_ENV_COMPILER", "apple-clang");
            exec.environment("CONAN_ENV_COMPILER_VERSION", "10.0");
            exec.environment("CONAN_ENV_COMPILER_LIBCXX", "libc++");
            break;
        case WINDOWS:
            exec.environment("CONAN_ENV_COMPILER", "gcc");
            exec.environment("CONAN_ENV_COMPILER_VERSION", "5");
            exec.environment("CONAN_ENV_COMPILER_LIBCXX", "libstdc++");
            break;
        case UNKNOWN:
        default:
            throw new IllegalArgumentException("Unsupported OS.");
    }
    if (platformHelper.getOs() == OperatingSystem.MAC_OSX) {
        // Set known environment variables for controlling sysroot on Mac. It never hurts to define
        // too many environment variables.
        var macOsSdkPath = toolManager.getToolDir("macos-sdk").toAbsolutePath().toString();
        // CMake - CMAKE_OSX_SYSROOT
        exec.environment("SDKROOT", macOsSdkPath);
        for (var flag : SYSROOT_GCC_ENV_VARIABLES) {
            var environment = exec.getEnvironment();
            exec.environment(flag, "-isysroot" + macOsSdkPath + " -mmacosx-version-min=10.12 " + environment.getOrDefault(flag, "") + ' ' + System.getenv().getOrDefault(flag, ""));
        }
    }
}
Also used : PlatformHelper(org.curioswitch.gradle.helpers.platform.PlatformHelper) Path(java.nio.file.Path)

Example 10 with PlatformHelper

use of org.curioswitch.gradle.helpers.platform.PlatformHelper 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

PlatformHelper (org.curioswitch.gradle.helpers.platform.PlatformHelper)11 Path (java.nio.file.Path)6 OperatingSystem (org.curioswitch.gradle.helpers.platform.OperatingSystem)6 TaskAction (org.gradle.api.tasks.TaskAction)6 IOException (java.io.IOException)3 DownloadedToolManager (org.curioswitch.gradle.tooldownloader.DownloadedToolManager)3 Project (org.gradle.api.Project)3 Preconditions.checkState (com.google.common.base.Preconditions.checkState)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 File (java.io.File)2 UncheckedIOException (java.io.UncheckedIOException)2 Files (java.nio.file.Files)2 Paths (java.nio.file.Paths)2 List (java.util.List)2 ToolDependencies (org.curioswitch.gradle.plugins.curiostack.ToolDependencies)2 ToolDownloaderPlugin (org.curioswitch.gradle.tooldownloader.ToolDownloaderPlugin)2 DownloadToolUtil (org.curioswitch.gradle.tooldownloader.util.DownloadToolUtil)2 DefaultTask (org.gradle.api.DefaultTask)2 Plugin (org.gradle.api.Plugin)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1