Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extractIr task dependencies are configured correctly #1128

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-1128.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: extractIr task dependencies are configured correctly
links:
- https://github.com/palantir/gradle-conjure/pull/1128
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,19 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.file.RegularFile;
import org.gradle.api.plugins.JavaBasePlugin;
import org.gradle.api.plugins.JavaLibraryPlugin;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Copy;
import org.gradle.api.tasks.TaskProvider;

public final class ConjureJavaLocalCodegenPlugin implements Plugin<Project> {
private static final ObjectMapper OBJECT_MAPPER = ObjectMappers.newClientObjectMapper();
private static final String CONJURE_CONFIGURATION = "conjure";
private static final Pattern DEFINITION_NAME =
Pattern.compile("(.*)-([0-9]+\\.[0-9]+\\.[0-9]+(?:-rc[0-9]+)?(?:-[0-9]+-g[a-f0-9]+)?)(\\.conjure)?\\.json");

@Override
public void apply(Project project) {
Expand All @@ -56,11 +53,10 @@ public void apply(Project project) {
project.getExtensions().create(ConjureExtension.EXTENSION_NAME, ConjureExtension.class);

Configuration conjureIrConfiguration = project.getConfigurations().create(CONJURE_CONFIGURATION);
TaskProvider<Copy> extractConjureIr = project.getTasks().register("extractConjureIr", Copy.class, task -> {
task.rename(DEFINITION_NAME, "$1.conjure.json");
task.from(conjureIrConfiguration);
task.into(project.getLayout().getBuildDirectory().dir("conjure-ir"));
});
TaskProvider<ExtractConjureIrTask> extractConjureIr = project.getTasks()
.register("extractConjureIr", ExtractConjureIrTask.class, task -> {
task.getIrConfiguration().set(conjureIrConfiguration);
});

TaskProvider<ExtractExecutableTask> extractJavaTask = ExtractConjurePlugin.applyConjureJava(project);

Expand All @@ -71,7 +67,7 @@ private static void setupSubprojects(
Project project,
ConjureExtension extension,
TaskProvider<ExtractExecutableTask> extractJavaTask,
TaskProvider<Copy> extractConjureIr,
TaskProvider<ExtractConjureIrTask> extractConjureIr,
Configuration conjureIrConfiguration) {

// Validating that each subproject has a corresponding definition and vice versa.
Expand Down Expand Up @@ -106,7 +102,7 @@ private static void createGenerateTask(
Project project,
ConjureExtension extension,
TaskProvider<ExtractExecutableTask> extractJavaTask,
TaskProvider<Copy> extractConjureIr) {
TaskProvider<ExtractConjureIrTask> extractConjureIr) {
ConjurePlugin.ignoreFromCheckUnusedDependencies(project);
ConjurePlugin.addGeneratedToMainSourceSet(project);

Expand All @@ -117,8 +113,9 @@ private static void createGenerateTask(
TaskProvider<WriteGitignoreTask> generateGitIgnore = ConjurePlugin.createWriteGitignoreTask(
project, "gitignoreConjure", project.getProjectDir(), ConjurePlugin.JAVA_GITIGNORE_CONTENTS);

Provider<File> conjureIrFile = extractConjureIr.map(
irTask -> new File(irTask.getDestinationDir(), project.getName() + ".conjure.json"));
Provider<File> conjureIrFile = extractConjureIr
.flatMap(task -> task.getConjureIr().file(project.getName() + ".conjure.json"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tested this sets up the task dependency correctly (you can run with --dry-run)? This seems functionally the same to me as the previous code, except with some added custom types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

womp:

Querying the mapped value of map(flatmap(provider(task 'extractConjureIr', class com.palantir.gradle.conjure.ExtractConjureIrTask))) before task ':remote-api:extractConjureIr' has completed is not supported

.map(RegularFile::getAsFile);

project.getExtensions()
.getByType(RecommendedProductDependenciesExtension.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* (c) Copyright 2022 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.gradle.conjure;

import java.util.regex.Pattern;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.file.Directory;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.Sync;

public abstract class ExtractConjureIrTask extends Sync {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is definitely my preferred way of writing all Gradle tasks in plugins now, making an implementation of the standard task type with real input/output properties.


private static final Pattern DEFINITION_NAME =
Pattern.compile("(.*)-([0-9]+\\.[0-9]+\\.[0-9]+(?:-rc[0-9]+)?(?:-[0-9]+-g[a-f0-9]+)?)(\\.conjure)?\\.json");

public ExtractConjureIrTask() {
rename(DEFINITION_NAME, "$1.conjure.json");
from(getIrConfiguration());
Provider<Directory> outputDirectory =
getProject().getLayout().getBuildDirectory().dir("conjure-ir");
into(outputDirectory);
getConjureIr().set(outputDirectory);
carterkozak marked this conversation as resolved.
Show resolved Hide resolved
}

/** Configuration used to resolve IR. */
@Input
public abstract Property<Configuration> getIrConfiguration();

@OutputDirectory
public abstract DirectoryProperty getConjureIr();
}