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 all 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 @@ -25,28 +25,24 @@
import com.palantir.gradle.dist.RecommendedProductDependenciesExtension;
import com.palantir.gradle.dist.RecommendedProductDependenciesPlugin;
import com.palantir.logsafe.exceptions.SafeRuntimeException;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
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 +52,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 +66,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 +101,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 +112,8 @@ 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<RegularFile> conjureIrFile =
extractConjureIr.flatMap(task -> task.getConjureIr().file(project.getName() + ".conjure.json"));

project.getExtensions()
.getByType(RecommendedProductDependenciesExtension.class)
Expand Down Expand Up @@ -156,10 +151,10 @@ private static String sanitizePackageName(String group) {
return group.replaceAll("-", "");
}

private static Set<ProductDependency> extractProductDependencies(File irFile) {
private static Set<ProductDependency> extractProductDependencies(RegularFile irFile) {
try {
MinimalConjureDefinition conjureDefinition =
OBJECT_MAPPER.readValue(irFile, MinimalConjureDefinition.class);
OBJECT_MAPPER.readValue(irFile.getAsFile(), MinimalConjureDefinition.class);
return conjureDefinition
.extensions()
.map(MinimalConjureDefinition.Extensions::productDependencies)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* (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.DirectoryProperty;
import org.gradle.api.provider.Property;
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());
getConjureIr().set(getProject().getLayout().getBuildDirectory().dir("conjure-ir"));
getConjureIr().disallowChanges();
into(getConjureIr());
}

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

@OutputDirectory
public abstract DirectoryProperty getConjureIr();
}