Search in sources :

Example 1 with Exec

use of org.gradle.api.tasks.Exec in project gradle-conjure by palantir.

the class ConjurePlugin method setupConjureTypescriptProject.

private static void setupConjureTypescriptProject(Project project, Supplier<GeneratorOptions> options, TaskProvider<?> compileConjure, TaskProvider<?> compileIrTask, TaskProvider<GenerateConjureServiceDependenciesTask> productDependencyTask) {
    String typescriptProjectName = project.getName() + "-typescript";
    if (derivedProjectExists(project, typescriptProjectName)) {
        project.project(derivedProjectPath(project, typescriptProjectName), subproj -> {
            applyDependencyForIdeTasks(subproj, compileConjure);
            File srcDirectory = subproj.file("src");
            TaskProvider<ExtractExecutableTask> extractConjureTypeScriptTask = ExtractConjurePlugin.applyConjureTypeScript(project);
            TaskProvider<CompileConjureTypeScriptTask> compileConjureTypeScript = project.getTasks().register("compileConjureTypeScript", CompileConjureTypeScriptTask.class, task -> {
                task.setDescription("Generates TypeScript files and a package.json from your " + "Conjure definitions.");
                task.setGroup(TASK_GROUP);
                task.setSource(compileIrTask);
                task.getExecutablePath().set(extractConjureTypeScriptTask.flatMap(ExtractExecutableTask::getExecutable));
                task.getProductDependencyFile().set(productDependencyTask.flatMap(GenerateConjureServiceDependenciesTask::getOutputFile));
                task.getOutputDirectory().set(srcDirectory);
                task.setOptions(options);
                task.dependsOn(createWriteGitignoreTask(subproj, "gitignoreConjureTypeScript", subproj.getProjectDir(), "/src/\n"));
                task.dependsOn(extractConjureTypeScriptTask);
                task.dependsOn(productDependencyTask);
            });
            compileConjure.configure(t -> t.dependsOn(compileConjureTypeScript));
            registerClean(project, compileConjureTypeScript);
            String npmCommand = OsUtils.NPM_COMMAND_NAME;
            TaskProvider<Exec> installTypeScriptDependencies = project.getTasks().register("installTypeScriptDependencies", Exec.class, task -> {
                task.commandLine(npmCommand, "install", "--no-package-lock", "--no-production");
                task.workingDir(srcDirectory);
                task.dependsOn(compileConjureTypeScript);
                task.getInputs().file(new File(srcDirectory, "package.json"));
                task.getOutputs().dir(new File(srcDirectory, "node_modules"));
            });
            TaskProvider<Exec> compileTypeScript = project.getTasks().register("compileTypeScript", Exec.class, task -> {
                task.setDescription("Runs `npm tsc` to compile generated TypeScript files into JavaScript files.");
                task.setGroup(TASK_GROUP);
                task.commandLine(npmCommand, "run-script", "build");
                task.workingDir(srcDirectory);
                task.dependsOn(installTypeScriptDependencies);
                task.getOutputs().dir(srcDirectory);
            });
            TaskProvider<Exec> publishTypeScript = project.getTasks().register("publishTypeScript", Exec.class, task -> {
                task.setDescription("Runs `npm publish` to publish a TypeScript package " + "generated from your Conjure definitions.");
                task.setGroup(TASK_GROUP);
                task.commandLine(npmCommand, "publish");
                task.workingDir(srcDirectory);
                task.dependsOn(compileConjureTypeScript);
                task.dependsOn(compileTypeScript);
            });
            linkPublish(subproj, publishTypeScript);
        });
    }
}
Also used : Exec(org.gradle.api.tasks.Exec) File(java.io.File)

Example 2 with Exec

use of org.gradle.api.tasks.Exec in project OpenSearch by opensearch-project.

the class LoggedExec method genericExec.

private static <T extends BaseExecSpec> ExecResult genericExec(Function<Action<T>, ExecResult> function, Action<T> action) {
    if (LOGGER.isInfoEnabled()) {
        return function.apply(action);
    }
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {
        return function.apply(spec -> {
            spec.setStandardOutput(output);
            spec.setErrorOutput(output);
            // optimize for short-lived process
            if (spec instanceof JavaExecSpec) {
                ((JavaExecSpec) spec).setJvmArgs(shortLivedArgs());
            }
            action.execute(spec);
            try {
                output.write(("Output for " + spec.getExecutable() + ":").getBytes(StandardCharsets.UTF_8));
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        });
    } catch (Exception e) {
        try {
            if (output.size() != 0) {
                LOGGER.error("Exec output and error:");
                NEWLINE.splitAsStream(output.toString("UTF-8")).forEach(s -> LOGGER.error("| " + s));
            }
        } catch (UnsupportedEncodingException ue) {
            throw new GradleException("Failed to read exec output", ue);
        }
        throw e;
    }
}
Also used : Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WorkResult(org.gradle.api.tasks.WorkResult) Function(java.util.function.Function) Logger(org.gradle.api.logging.Logger) Inject(javax.inject.Inject) Task(org.gradle.api.Task) ExecResult(org.gradle.process.ExecResult) Exec(org.gradle.api.tasks.Exec) OutputStream(java.io.OutputStream) FileSystemOperations(org.gradle.api.file.FileSystemOperations) Action(org.gradle.api.Action) Project(org.gradle.api.Project) Files(java.nio.file.Files) ExecOperations(org.gradle.process.ExecOperations) IOException(java.io.IOException) JavaExecSpec(org.gradle.process.JavaExecSpec) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) UncheckedIOException(java.io.UncheckedIOException) Consumer(java.util.function.Consumer) List(java.util.List) GradleException(org.gradle.api.GradleException) BaseExecSpec(org.gradle.process.BaseExecSpec) Logging(org.gradle.api.logging.Logging) ExecSpec(org.gradle.process.ExecSpec) Pattern(java.util.regex.Pattern) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JavaExecSpec(org.gradle.process.JavaExecSpec) GradleException(org.gradle.api.GradleException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UncheckedIOException(java.io.UncheckedIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) GradleException(org.gradle.api.GradleException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

File (java.io.File)2 Exec (org.gradle.api.tasks.Exec)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Files (java.nio.file.Files)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 Pattern (java.util.regex.Pattern)1 Inject (javax.inject.Inject)1 Action (org.gradle.api.Action)1 GradleException (org.gradle.api.GradleException)1 Project (org.gradle.api.Project)1 Task (org.gradle.api.Task)1 FileSystemOperations (org.gradle.api.file.FileSystemOperations)1