Skip to content

Commit

Permalink
Generate code into conventional location (#1160)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkoenig10 authored Apr 8, 2024
1 parent 8a646d4 commit 77b2128
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 269 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<RegularFile> getMarkdownFile() {
return ProviderUtils.filterNonExistentFile(getProject(), markdownFile);
}
public abstract RegularFileProperty getManifestFile();

@Input
public final Property<String> getLocalCoordinates() {
return localCoordinates;
}
public abstract Property<String> getLocalCoordinates();

final void setMarkdownFile(File value) {
markdownFile.set(value);
}
@InputFiles
@PathSensitive(PathSensitivity.RELATIVE)
public abstract RegularFileProperty getMarkdownFile();

@TaskAction
public final void check() throws IOException {
Expand All @@ -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(
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,38 @@
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;
import org.gradle.api.tasks.TaskAction;

@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<RegularFile> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FileCollection> projectDependencyMetrics;
private final RegularFileProperty metricsFile = getProject().getObjects().fileProperty();
private final Property<Configuration> configuration =
Expand Down Expand Up @@ -129,11 +131,8 @@ private Provider<FileCollection> 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())
Expand Down Expand Up @@ -174,10 +173,10 @@ private static Optional<List<MetricSchema>> 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();
Expand All @@ -195,7 +194,7 @@ private static Optional<List<MetricSchema>> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<RegularFile> getMarkdownFile() {
return ProviderUtils.filterNonExistentFile(getProject(), outputFile);
}
public abstract RegularFileProperty getManifestFile();

@Input
public final Property<String> getLocalCoordinates() {
return localCoordinates;
}
public abstract Property<String> 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<String, List<MetricSchema>> schemas = ObjectMappers.mapper.readValue(manifest, new TypeReference<>() {});
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,31 +39,21 @@
import org.gradle.api.tasks.TaskAction;

@CacheableTask
public abstract class GenerateMetricSchemaTask extends DefaultTask {
private final Property<String> 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<String> 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<String> getLibraryName() {
return libraryName;
}
@org.gradle.api.tasks.Optional
public abstract Property<String> getLibraryName();

@Input
public final Property<String> getLibraryVersion() {
return libraryVersion;
}
@org.gradle.api.tasks.Optional
public abstract Property<String> getLibraryVersion();

@OutputDirectory
public abstract DirectoryProperty getOutputDir();
Expand All @@ -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());
Expand All @@ -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);
}
}
Loading

0 comments on commit 77b2128

Please sign in to comment.