Skip to content

Commit

Permalink
extractIr task dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
carterkozak committed Dec 6, 2022
1 parent 7d31dd6 commit 99cd1c8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
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"))
.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 {

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);
}

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

@OutputDirectory
public abstract DirectoryProperty getConjureIr();
}

0 comments on commit 99cd1c8

Please sign in to comment.