diff --git a/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/CheckMetricMarkdownTask.java b/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/CheckMetricMarkdownTask.java index 52bff38c..57bd7d3a 100644 --- a/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/CheckMetricMarkdownTask.java +++ b/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/CheckMetricMarkdownTask.java @@ -27,50 +27,30 @@ import java.util.Map; import org.gradle.api.DefaultTask; import org.gradle.api.GradleException; -import org.gradle.api.file.RegularFile; import org.gradle.api.file.RegularFileProperty; import org.gradle.api.provider.Property; -import org.gradle.api.provider.Provider; import org.gradle.api.tasks.CacheableTask; import org.gradle.api.tasks.Input; -import org.gradle.api.tasks.InputFile; -import org.gradle.api.tasks.Optional; +import org.gradle.api.tasks.InputFiles; import org.gradle.api.tasks.PathSensitive; import org.gradle.api.tasks.PathSensitivity; import org.gradle.api.tasks.TaskAction; @CacheableTask -public class CheckMetricMarkdownTask extends DefaultTask { - private final RegularFileProperty markdownFile = getProject().getObjects().fileProperty(); - private final RegularFileProperty manifestFile = getProject().getObjects().fileProperty(); - private final Property localCoordinates = getProject() - .getObjects() - .property(String.class) - .value(getProject() - .provider(() -> - "" + getProject().getGroup() + ':' + getProject().getName())); +public abstract class CheckMetricMarkdownTask extends DefaultTask { - @InputFile - @PathSensitive(PathSensitivity.RELATIVE) - public final RegularFileProperty getManifestFile() { - return manifestFile; - } + static final String NAME = "checkMetricsMarkdown"; - @Optional - @InputFile + @InputFiles @PathSensitive(PathSensitivity.RELATIVE) - public final Provider getMarkdownFile() { - return ProviderUtils.filterNonExistentFile(getProject(), markdownFile); - } + public abstract RegularFileProperty getManifestFile(); @Input - public final Property getLocalCoordinates() { - return localCoordinates; - } + public abstract Property getLocalCoordinates(); - final void setMarkdownFile(File value) { - markdownFile.set(value); - } + @InputFiles + @PathSensitive(PathSensitivity.RELATIVE) + public abstract RegularFileProperty getMarkdownFile(); @TaskAction public final void check() throws IOException { @@ -81,8 +61,8 @@ public final void check() throws IOException { return; } - File markdown = markdownFile.get().getAsFile(); - String upToDateContents = MarkdownRenderer.render(localCoordinates.get(), schemas); + File markdown = getMarkdownFile().get().getAsFile(); + String upToDateContents = MarkdownRenderer.render(getLocalCoordinates().get(), schemas); if (!markdown.exists()) { throw new GradleException(String.format( @@ -95,7 +75,7 @@ public final void check() throws IOException { fromDisk.equals(upToDateContents), "%s is out of date, please run `./gradlew %s` or `./gradlew --write-locks` to update it.", markdown.getName(), - MetricSchemaPlugin.GENERATE_METRICS_MARKDOWN); + GenerateMetricMarkdownTask.NAME); } } diff --git a/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/CompileMetricSchemaTask.java b/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/CompileMetricSchemaTask.java index 3dae41ef..dafe7e44 100644 --- a/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/CompileMetricSchemaTask.java +++ b/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/CompileMetricSchemaTask.java @@ -22,9 +22,12 @@ import java.io.IOException; import org.gradle.api.DefaultTask; import org.gradle.api.file.ConfigurableFileCollection; -import org.gradle.api.file.RegularFileProperty; +import org.gradle.api.file.DirectoryProperty; +import org.gradle.api.file.RegularFile; +import org.gradle.api.provider.Provider; import org.gradle.api.tasks.CacheableTask; import org.gradle.api.tasks.InputFiles; +import org.gradle.api.tasks.OutputDirectory; import org.gradle.api.tasks.OutputFile; import org.gradle.api.tasks.PathSensitive; import org.gradle.api.tasks.PathSensitivity; @@ -32,20 +35,25 @@ @CacheableTask public abstract class CompileMetricSchemaTask extends DefaultTask { + + static final String NAME = "compileMetricSchema"; + @InputFiles @PathSensitive(PathSensitivity.RELATIVE) public abstract ConfigurableFileCollection getSource(); + @OutputDirectory + public abstract DirectoryProperty getOutputDir(); + @OutputFile - public abstract RegularFileProperty getOutputFile(); + public final Provider getMetricsJsonFile() { + return getOutputDir().file(MetricSchemaPlugin.METRICS_JSON_FILE); + } @TaskAction public final void action() throws IOException { - File output = getOutputFile().getAsFile().get(); - getProject().mkdir(output.getParent()); - ObjectMappers.mapper.writeValue( - output, + getMetricsJsonFile().get().getAsFile(), getSource().getFiles().stream() .map(File::toPath) .map(MetricSchemaCompiler::compile) diff --git a/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/CreateMetricsManifestTask.java b/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/CreateMetricsManifestTask.java index 28ba2d58..6fbe4f9b 100644 --- a/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/CreateMetricsManifestTask.java +++ b/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/CreateMetricsManifestTask.java @@ -56,6 +56,8 @@ public class CreateMetricsManifestTask extends DefaultTask { private static final Logger log = Logging.getLogger(CreateMetricsManifestTask.class); + static final String NAME = "createMetricsManifest"; + private final Provider projectDependencyMetrics; private final RegularFileProperty metricsFile = getProject().getObjects().fileProperty(); private final Property configuration = @@ -129,11 +131,8 @@ private Provider otherProjectMetricSchemaTasks() { @TaskAction public final void createManifest() throws IOException { - File output = getOutputFile().getAsFile().get(); - getProject().mkdir(output.getParent()); - ObjectMappers.mapper.writeValue( - output, + getOutputFile().get().getAsFile(), ImmutableMap.builder() .putAll(getLocalMetrics()) .putAll(getDiscoveredMetrics()) @@ -174,10 +173,10 @@ private static Optional> inferProjectDependencyMetrics(Projec if (!dependencyProject.getPlugins().hasPlugin(MetricSchemaPlugin.class)) { return Optional.empty(); } - CompileMetricSchemaTask compileMetricSchemaTask = (CompileMetricSchemaTask) - dependencyProject.getTasks().getByName(MetricSchemaPlugin.COMPILE_METRIC_SCHEMA); + CompileMetricSchemaTask compileMetricSchemaTask = + (CompileMetricSchemaTask) dependencyProject.getTasks().getByName(CompileMetricSchemaTask.NAME); - File file = compileMetricSchemaTask.getOutputFile().get().getAsFile(); + File file = compileMetricSchemaTask.getMetricsJsonFile().get().getAsFile(); if (!file.isFile()) { log.debug("File {} does not exist", file); return Optional.empty(); @@ -195,7 +194,7 @@ private static Optional> getExternalMetrics(ComponentIdentifi } try (ZipFile zipFile = new ZipFile(artifact.getFile())) { - ZipEntry manifestEntry = zipFile.getEntry(MetricSchemaPlugin.METRIC_SCHEMA_RESOURCE); + ZipEntry manifestEntry = zipFile.getEntry(MetricSchemaPlugin.METRICS_JSON_FILE); if (manifestEntry == null) { log.debug("Manifest file does not exist in JAR: {}", id); return Optional.empty(); diff --git a/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/GenerateMetricMarkdownTask.java b/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/GenerateMetricMarkdownTask.java index 451efca6..8d9df25f 100644 --- a/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/GenerateMetricMarkdownTask.java +++ b/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/GenerateMetricMarkdownTask.java @@ -25,56 +25,34 @@ import java.util.List; import java.util.Map; import org.gradle.api.DefaultTask; -import org.gradle.api.file.RegularFile; import org.gradle.api.file.RegularFileProperty; import org.gradle.api.provider.Property; -import org.gradle.api.provider.Provider; import org.gradle.api.tasks.CacheableTask; import org.gradle.api.tasks.Input; -import org.gradle.api.tasks.InputFile; -import org.gradle.api.tasks.Optional; +import org.gradle.api.tasks.InputFiles; import org.gradle.api.tasks.OutputFile; import org.gradle.api.tasks.PathSensitive; import org.gradle.api.tasks.PathSensitivity; import org.gradle.api.tasks.TaskAction; @CacheableTask -public class GenerateMetricMarkdownTask extends DefaultTask { - private final RegularFileProperty outputFile = getProject().getObjects().fileProperty(); - private final RegularFileProperty manifestFile = getProject().getObjects().fileProperty(); - private final Property localCoordinates = getProject() - .getObjects() - .property(String.class) - .value(getProject() - .provider(() -> - "" + getProject().getGroup() + ':' + getProject().getName())); +public abstract class GenerateMetricMarkdownTask extends DefaultTask { - @InputFile - @PathSensitive(PathSensitivity.RELATIVE) - public final RegularFileProperty getManifestFile() { - return manifestFile; - } + static final String NAME = "generateMetricsMarkdown"; - @Optional - @InputFile + @InputFiles @PathSensitive(PathSensitivity.RELATIVE) - public final Provider getMarkdownFile() { - return ProviderUtils.filterNonExistentFile(getProject(), outputFile); - } + public abstract RegularFileProperty getManifestFile(); @Input - public final Property getLocalCoordinates() { - return localCoordinates; - } + public abstract Property getLocalCoordinates(); @OutputFile - public final RegularFileProperty getOutputFile() { - return outputFile; - } + public abstract RegularFileProperty getMarkdownFile(); @TaskAction public final void generate() throws IOException { - File markdown = outputFile.get().getAsFile(); + File markdown = getMarkdownFile().get().getAsFile(); File manifest = getManifestFile().getAsFile().get(); Map> schemas = ObjectMappers.mapper.readValue(manifest, new TypeReference<>() {}); @@ -85,7 +63,7 @@ public final void generate() throws IOException { return; } - String upToDateContents = MarkdownRenderer.render(localCoordinates.get(), schemas); + String upToDateContents = MarkdownRenderer.render(getLocalCoordinates().get(), schemas); Files.writeString(markdown.toPath(), upToDateContents); } diff --git a/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/GenerateMetricSchemaTask.java b/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/GenerateMetricsTask.java similarity index 55% rename from gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/GenerateMetricSchemaTask.java rename to gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/GenerateMetricsTask.java index 0630aa68..0a7e234d 100644 --- a/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/GenerateMetricSchemaTask.java +++ b/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/GenerateMetricsTask.java @@ -22,15 +22,10 @@ import com.palantir.logsafe.exceptions.SafeRuntimeException; import com.palantir.metric.schema.JavaGenerator; import com.palantir.metric.schema.JavaGeneratorArgs; -import com.palantir.sls.versions.OrderableSlsVersion; -import com.palantir.sls.versions.SlsVersion; -import com.palantir.sls.versions.SlsVersionType; import java.io.File; import java.io.IOException; import java.nio.file.Path; -import java.util.Objects; import java.util.Optional; -import javax.annotation.Nullable; import org.gradle.api.DefaultTask; import org.gradle.api.file.DirectoryProperty; import org.gradle.api.file.RegularFileProperty; @@ -44,31 +39,21 @@ import org.gradle.api.tasks.TaskAction; @CacheableTask -public abstract class GenerateMetricSchemaTask extends DefaultTask { - private final Property libraryName = getProject() - .getObjects() - .property(String.class) - .convention(getProject().provider(this::defaultLibraryName)); +public abstract class GenerateMetricsTask extends DefaultTask { - @org.gradle.api.tasks.Optional - private final Property libraryVersion = getProject() - .getObjects() - .property(String.class) - .convention(getProject().provider(this::defaultLibraryVersion)); + static final String NAME = "generateMetrics"; @InputFile @PathSensitive(PathSensitivity.RELATIVE) public abstract RegularFileProperty getInputFile(); @Input - public final Property getLibraryName() { - return libraryName; - } + @org.gradle.api.tasks.Optional + public abstract Property getLibraryName(); @Input - public final Property getLibraryVersion() { - return libraryVersion; - } + @org.gradle.api.tasks.Optional + public abstract Property getLibraryVersion(); @OutputDirectory public abstract DirectoryProperty getOutputDir(); @@ -82,8 +67,8 @@ public final void generate() { JavaGenerator.generate(JavaGeneratorArgs.builder() .input(getInputFile().getAsFile().get().toPath()) .output(output.toPath()) - .libraryName(Optional.ofNullable(libraryName.getOrNull())) - .libraryVersion(Optional.ofNullable(libraryVersion.getOrNull())) + .libraryName(Optional.ofNullable(getLibraryName().getOrNull())) + .libraryVersion(Optional.ofNullable(getLibraryVersion().getOrNull())) // TODO(forozco): probably want something better .defaultPackageName(getProject().getGroup().toString()) .build()); @@ -96,25 +81,4 @@ private static void clearOutput(Path outputPath) { throw new SafeRuntimeException("Unable to clean output directory", SafeArg.of("output", outputPath)); } } - - private String defaultLibraryName() { - String rootProjectName = getProject().getRootProject().getName(); - return rootProjectName.replaceAll("-root$", ""); - } - - @Nullable - private String defaultLibraryVersion() { - // Gradle returns 'unspecified' when there is no version information, which is not orderable. - String version = Objects.toString(getProject().getRootProject().getVersion()); - return OrderableSlsVersion.safeValueOf(version) - // Only provide version data for releases and release candidates, not snapshots. This way we - // don't invalidate build caches on each commit. - // We prefer passing along version information at this level because it will not be mutated - // by shading, where the fallback based on 'package.getImplementationVersion()' will reflect - // the shadow jar version. - .filter(ver -> - ver.getType() == SlsVersionType.RELEASE || ver.getType() == SlsVersionType.RELEASE_CANDIDATE) - .map(SlsVersion::getValue) - .orElse(null); - } } diff --git a/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/MetricSchemaMarkdownPlugin.java b/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/MetricSchemaMarkdownPlugin.java index 089201bf..7e015a57 100644 --- a/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/MetricSchemaMarkdownPlugin.java +++ b/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/MetricSchemaMarkdownPlugin.java @@ -21,6 +21,8 @@ import org.gradle.StartParameter; import org.gradle.api.Plugin; import org.gradle.api.Project; +import org.gradle.api.file.RegularFile; +import org.gradle.api.provider.Provider; import org.gradle.api.tasks.TaskProvider; import org.gradle.language.base.plugins.LifecycleBasePlugin; @@ -32,33 +34,41 @@ public void apply(Project project) { project.getPluginManager().apply(MetricSchemaPlugin.class); TaskProvider createMetricsManifest = - project.getTasks().named(MetricSchemaPlugin.CREATE_METRICS_MANIFEST, CreateMetricsManifestTask.class); + project.getTasks().named(CreateMetricsManifestTask.NAME, CreateMetricsManifestTask.class); - TaskProvider checkMetricsMarkdown = project.getTasks() - .register(MetricSchemaPlugin.CHECK_METRICS_MARKDOWN, CheckMetricMarkdownTask.class, task -> { - task.getManifestFile().set(createMetricsManifest.flatMap(CreateMetricsManifestTask::getOutputFile)); - task.setMarkdownFile(project.file("metrics.md")); - task.dependsOn(createMetricsManifest); + Provider manifestFile = createMetricsManifest.flatMap(CreateMetricsManifestTask::getOutputFile); + Provider localCoordinates = project.provider(() -> "" + project.getGroup() + ':' + project.getName()); + RegularFile markdownFile = project.getLayout().getProjectDirectory().file("metrics.md"); + + TaskProvider generateMetricMarkdownTask = project.getTasks() + .register(GenerateMetricMarkdownTask.NAME, GenerateMetricMarkdownTask.class, task -> { + task.setGroup(MetricSchemaPlugin.TASK_GROUP); + task.getManifestFile().set(manifestFile); + task.getLocalCoordinates().set(localCoordinates); + task.getMarkdownFile().set(markdownFile); }); - project.getTasks() - .register(MetricSchemaPlugin.GENERATE_METRICS_MARKDOWN, GenerateMetricMarkdownTask.class, task -> { - task.getManifestFile().set(createMetricsManifest.flatMap(CreateMetricsManifestTask::getOutputFile)); - task.getOutputFile().set(project.file("metrics.md")); - task.dependsOn(createMetricsManifest); - // Avoid a race when both checkMetricsMarkdown and generateMetricsMarkdown are requested - task.mustRunAfter(checkMetricsMarkdown); + TaskProvider checkMetricsMarkdownTask = project.getTasks() + .register(CheckMetricMarkdownTask.NAME, CheckMetricMarkdownTask.class, task -> { + task.setGroup(MetricSchemaPlugin.TASK_GROUP); + task.getManifestFile().set(manifestFile); + task.getLocalCoordinates().set(localCoordinates); + task.getMarkdownFile().set(markdownFile); + + task.mustRunAfter(generateMetricMarkdownTask); }); - project.getTasks().named("check").configure(check -> check.dependsOn(checkMetricsMarkdown)); + project.getTasks().named(LifecycleBasePlugin.CHECK_TASK_NAME).configure(check -> { + check.dependsOn(checkMetricsMarkdownTask); + }); // Wire up dependencies so running `./gradlew --write-locks` will update the markdown StartParameter startParam = project.getGradle().getStartParameter(); if (startParam.isWriteDependencyLocks() - && !startParam.getTaskNames().contains(MetricSchemaPlugin.GENERATE_METRICS_MARKDOWN)) { + && !startParam.getTaskNames().contains(GenerateMetricMarkdownTask.NAME)) { List taskNames = ImmutableList.builder() .addAll(startParam.getTaskNames()) - .add(MetricSchemaPlugin.GENERATE_METRICS_MARKDOWN) + .add(GenerateMetricMarkdownTask.NAME) .build(); startParam.setTaskNames(taskNames); } diff --git a/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/MetricSchemaPlugin.java b/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/MetricSchemaPlugin.java index 3ccb036f..8f6d37b2 100644 --- a/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/MetricSchemaPlugin.java +++ b/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/MetricSchemaPlugin.java @@ -16,11 +16,13 @@ package com.palantir.metric.schema.gradle; +import com.palantir.sls.versions.OrderableSlsVersion; +import com.palantir.sls.versions.SlsVersion; +import com.palantir.sls.versions.SlsVersionType; +import java.util.Objects; +import javax.annotation.Nullable; import org.gradle.api.Plugin; import org.gradle.api.Project; -import org.gradle.api.Task; -import org.gradle.api.UnknownDomainObjectException; -import org.gradle.api.artifacts.Configuration; import org.gradle.api.file.Directory; import org.gradle.api.file.RegularFile; import org.gradle.api.file.SourceDirectorySet; @@ -31,127 +33,90 @@ import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.TaskProvider; import org.gradle.plugins.ide.idea.model.IdeaModel; -import org.gradle.plugins.ide.idea.model.IdeaModule; public final class MetricSchemaPlugin implements Plugin { - private static final String TASK_GROUP = "MetricSchema"; - public static final String METRIC_SCHEMA_RESOURCE = "metric-schema/metrics.json"; - public static final String COMPILE_METRIC_SCHEMA = "compileMetricSchema"; - public static final String CHECK_METRICS_MARKDOWN = "checkMetricsMarkdown"; - public static final String GENERATE_METRICS_MARKDOWN = "generateMetricsMarkdown"; - public static final String CREATE_METRICS_MANIFEST = "createMetricsManifest"; - public static final String GENERATE_METRICS = "generateMetrics"; + public static final String TASK_GROUP = "MetricSchema"; + + public static final String METRICS_JSON_FILE = "metric-schema/metrics.json"; @Override public void apply(Project project) { project.getPluginManager().apply(JavaLibraryPlugin.class); - SourceDirectorySet sourceSet = createSourceSet(project); + + Provider generatedJavaDir = + project.getLayout().getBuildDirectory().dir("generated/sources/metric-schema/java/main"); + Provider generatedResourcesDir = + project.getLayout().getBuildDirectory().dir("generated/sources/metric-schema/resources/main"); Provider metricSchemaDir = project.getLayout().getBuildDirectory().dir("metricSchema"); - TaskProvider compileSchemaTask = - createCompileSchemaTask(project, metricSchemaDir, sourceSet); + SourceDirectorySet metricSchemaSourceDirectorySet = + project.getObjects().sourceDirectorySet("metricSchema", "Metric Schema source set"); + metricSchemaSourceDirectorySet.srcDir("src/main/metrics"); + metricSchemaSourceDirectorySet.include("**/*.yml"); - Provider generatedJavaOutputDir = metricSchemaDir.map(file -> file.dir("generated_src")); - TaskProvider generateMetricSchemaTask = project.getTasks() - .register(GENERATE_METRICS, GenerateMetricSchemaTask.class, task -> { + TaskProvider compileMetricSchemaTask = project.getTasks() + .register(CompileMetricSchemaTask.NAME, CompileMetricSchemaTask.class, task -> { task.setGroup(TASK_GROUP); - task.setDescription("Generates bindings for producing well defined metrics"); - task.getInputFile().set(compileSchemaTask.flatMap(CompileMetricSchemaTask::getOutputFile)); - task.getOutputDir().set(generatedJavaOutputDir); + task.getSource().from(metricSchemaSourceDirectorySet); + task.getOutputDir().set(generatedResourcesDir); }); - configureJavaSource(project, generateMetricSchemaTask); - configureIdea(project, generateMetricSchemaTask, generatedJavaOutputDir); - configureEclipse(project, generateMetricSchemaTask); - configureProjectDependencies(project); + Provider metricsFiles = + compileMetricSchemaTask.flatMap(CompileMetricSchemaTask::getMetricsJsonFile); - createManifestTask(project, metricSchemaDir, compileSchemaTask); - project.getPluginManager() - .withPlugin("com.palantir.sls-java-service-distribution", _plugin -> project.getPluginManager() - .apply(MetricSchemaMarkdownPlugin.class)); - } + TaskProvider generateMetricsTask = project.getTasks() + .register(GenerateMetricsTask.NAME, GenerateMetricsTask.class, task -> { + task.setGroup(TASK_GROUP); + task.setDescription("Generates bindings for producing well defined metrics"); + task.getInputFile().set(metricsFiles); + task.getLibraryName().convention(defaultLibraryName(project)); + task.getLibraryVersion().convention(defaultLibraryVersion(project)); + task.getOutputDir().set(generatedJavaDir); + }); - private static void createManifestTask( - Project project, - Provider metricSchemaDir, - TaskProvider compileSchemaTask) { - Provider manifestFile = metricSchemaDir.map(dir -> dir.file("manifest.json")); - Configuration runtimeClasspath = - project.getConfigurations().getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME); - project.getTasks().register(CREATE_METRICS_MANIFEST, CreateMetricsManifestTask.class, task -> { - task.getMetricsFile().set(compileSchemaTask.flatMap(CompileMetricSchemaTask::getOutputFile)); - task.getOutputFile().set(manifestFile); - task.getConfiguration().set(runtimeClasspath); + project.getTasks().register(CreateMetricsManifestTask.NAME, CreateMetricsManifestTask.class, task -> { + task.setGroup(TASK_GROUP); + task.getMetricsFile().set(metricsFiles); + task.getOutputFile().set(metricSchemaDir.map(dir -> dir.file("manifest.json"))); + task.getConfiguration() + .set(project.getConfigurations().getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME)); }); - } - private static TaskProvider createCompileSchemaTask( - Project project, Provider metricSchemaDir, SourceDirectorySet sourceSet) { - Provider schemaFile = metricSchemaDir.map(dir -> dir.file(METRIC_SCHEMA_RESOURCE)); - - TaskProvider compileMetricSchemaTask = project.getTasks() - .register(COMPILE_METRIC_SCHEMA, CompileMetricSchemaTask.class, task -> { - task.getSource().from(sourceSet); - task.getOutputFile().set(schemaFile); - }); - project.getTasks() - .named(JavaPlugin.PROCESS_RESOURCES_TASK_NAME) - .configure(processResources -> processResources.dependsOn(compileMetricSchemaTask)); + JavaPluginExtension javaPluginExtension = project.getExtensions().getByType(JavaPluginExtension.class); - project.getExtensions() - .getByType(JavaPluginExtension.class) + javaPluginExtension .getSourceSets() - .getByName(SourceSet.MAIN_SOURCE_SET_NAME) - .resources(resources -> { - SourceDirectorySet sourceDir = project.getObjects() - .sourceDirectorySet("metricSchema", "metric schema") - .srcDir(metricSchemaDir); - sourceDir.include("metric-schema/**"); - resources.source(sourceDir); + .named(SourceSet.MAIN_SOURCE_SET_NAME) + .configure(sourceSet -> { + sourceSet.getJava().srcDir(generateMetricsTask); + sourceSet.getResources().srcDir(compileMetricSchemaTask.map(CompileMetricSchemaTask::getOutputDir)); }); - return compileMetricSchemaTask; - } - - private static SourceDirectorySet createSourceSet(Project project) { - SourceDirectorySet sourceSet = - project.getObjects().sourceDirectorySet("metricSchema", "Metric Schema source set"); - sourceSet.srcDir("src/main/metrics"); - sourceSet.include("**/*.yml"); - return sourceSet; - } + project.getPluginManager().withPlugin("com.palantir.sls-java-service-distribution", _plugin -> { + project.getPluginManager().apply(MetricSchemaMarkdownPlugin.class); + }); - private static void configureJavaSource( - Project project, TaskProvider generateMetricSchemaTask) { - project.getExtensions() - .getByType(JavaPluginExtension.class) - .getSourceSets() - .getByName(SourceSet.MAIN_SOURCE_SET_NAME) - .getJava() - .srcDir(generateMetricSchemaTask); - } + configureIdea(project, generatedJavaDir, generatedResourcesDir); - private static void configureEclipse(Project project, TaskProvider generateMetricsTask) { - project.getPluginManager().withPlugin("eclipse", _plugin -> { - try { - project.getTasks().named("eclipseClasspath").configure(t -> t.dependsOn(generateMetricsTask)); - } catch (UnknownDomainObjectException e) { - // eclipseClasspath is not always registered - } - }); + configureProjectDependencies(project); } private static void configureIdea( - Project project, TaskProvider generateMetricsTask, Provider outputDir) { + Project project, Provider generatedJavaDir, Provider generatedResourcesDir) { project.getPluginManager().withPlugin("idea", _plugin -> { - project.getTasks().named("ideaModule").configure(task -> task.dependsOn(generateMetricsTask)); - project.getExtensions().configure(IdeaModel.class, idea -> { - IdeaModule module = idea.getModule(); - module.getSourceDirs().add(outputDir.get().getAsFile()); - module.getGeneratedSourceDirs().add(outputDir.get().getAsFile()); - }); + IdeaModel ideaModel = project.getExtensions().getByType(IdeaModel.class); + + ideaModel.getModule().getSourceDirs().add(generatedJavaDir.get().getAsFile()); + ideaModel + .getModule() + .getGeneratedSourceDirs() + .add(generatedJavaDir.get().getAsFile()); + ideaModel + .getModule() + .getResourceDirs() + .add(generatedResourcesDir.get().getAsFile()); }); } @@ -160,4 +125,25 @@ private static void configureProjectDependencies(Project project) { project.getDependencies().add("api", "com.palantir.safe-logging:preconditions"); project.getDependencies().add("api", "com.google.errorprone:error_prone_annotations"); } + + private String defaultLibraryName(Project project) { + String rootProjectName = project.getRootProject().getName(); + return rootProjectName.replaceAll("-root$", ""); + } + + @Nullable + private String defaultLibraryVersion(Project project) { + // Gradle returns 'unspecified' when there is no version information, which is not orderable. + String version = Objects.toString(project.getRootProject().getVersion()); + return OrderableSlsVersion.safeValueOf(version) + // Only provide version data for releases and release candidates, not snapshots. This way we + // don't invalidate build caches on each commit. + // We prefer passing along version information at this level because it will not be mutated + // by shading, where the fallback based on 'package.getImplementationVersion()' will reflect + // the shadow jar version. + .filter(ver -> + ver.getType() == SlsVersionType.RELEASE || ver.getType() == SlsVersionType.RELEASE_CANDIDATE) + .map(SlsVersion::getValue) + .orElse(null); + } } diff --git a/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/ProviderUtils.java b/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/ProviderUtils.java deleted file mode 100644 index 87802085..00000000 --- a/gradle-metric-schema/src/main/java/com/palantir/metric/schema/gradle/ProviderUtils.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * (c) Copyright 2019 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.metric.schema.gradle; - -import org.gradle.api.Project; -import org.gradle.api.file.RegularFile; -import org.gradle.api.provider.Provider; - -public final class ProviderUtils { - - public static Provider filterNonExistentFile(Project project, Provider provider) { - return provider.flatMap(file -> file.getAsFile().exists() - ? project.provider(() -> file) - : project.getObjects().fileProperty()); - } - - private ProviderUtils() {} -} diff --git a/gradle-metric-schema/src/test/groovy/com/palantir/metric/schema/gradle/MetricSchemaPluginIntegrationSpec.groovy b/gradle-metric-schema/src/test/groovy/com/palantir/metric/schema/gradle/MetricSchemaPluginIntegrationSpec.groovy index 46490072..8b316606 100644 --- a/gradle-metric-schema/src/test/groovy/com/palantir/metric/schema/gradle/MetricSchemaPluginIntegrationSpec.groovy +++ b/gradle-metric-schema/src/test/groovy/com/palantir/metric/schema/gradle/MetricSchemaPluginIntegrationSpec.groovy @@ -101,7 +101,7 @@ class MetricSchemaPluginIntegrationSpec extends IntegrationSpec { then: def result = runTasksSuccessfully('classes') result.wasExecuted(':generateMetrics') - fileExists("build/metricSchema/generated_src/com/palantir/test/ServerMetrics.java") + fileExists("build/generated/sources/metric-schema/java/main/com/palantir/test/ServerMetrics.java") } def 'build cache works'() { @@ -110,7 +110,7 @@ class MetricSchemaPluginIntegrationSpec extends IntegrationSpec { file('src/main/metrics/metrics.yml') << METRICS runTasksSuccessfully('generateMetrics') - fileExists("build/metricSchema/generated_src/com/palantir/test/ServerMetrics.java") + fileExists("build/generated/sources/metric-schema/java/main/com/palantir/test/ServerMetrics.java") // we want cache hits no matter where the project is checked out on disk File originalProjectDir = getProjectDir() @@ -119,7 +119,7 @@ class MetricSchemaPluginIntegrationSpec extends IntegrationSpec { FileUtils.copyDirectory(originalProjectDir, newProjectDir) setProjectDir(newProjectDir) FileUtils.deleteDirectory(originalProjectDir) - FileUtils.deleteDirectory(file("build/metricSchema/generated_src/")) + FileUtils.deleteDirectory(file("build/generated/sources/metric-schema")) then: ExecutionResult result = runTasksSuccessfully('generateMetrics') diff --git a/gradle-metric-schema/src/test/groovy/com/palantir/metric/schema/gradle/Versions.groovy b/gradle-metric-schema/src/test/groovy/com/palantir/metric/schema/gradle/Versions.groovy index e28a1223..3a2b46f4 100644 --- a/gradle-metric-schema/src/test/groovy/com/palantir/metric/schema/gradle/Versions.groovy +++ b/gradle-metric-schema/src/test/groovy/com/palantir/metric/schema/gradle/Versions.groovy @@ -19,7 +19,6 @@ package com.palantir.metric.schema.gradle class Versions { public static final String TRITIUM = "0.16.3" public static final String SAFE_LOGGING = "1.11.0" - public static final String SLS_PACKAGING = "4.3.3" private Versions() { } }