-
Notifications
You must be signed in to change notification settings - Fork 5
Lazily configure dependencies #1545
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,14 +16,17 @@ | |
|
|
||
| package com.palantir.metric.schema.gradle; | ||
|
|
||
| import com.google.common.collect.ImmutableSetMultimap; | ||
| import com.palantir.sls.versions.OrderableSlsVersion; | ||
| import com.palantir.sls.versions.SlsVersion; | ||
| import com.palantir.sls.versions.SlsVersionType; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import javax.annotation.Nullable; | ||
| import org.gradle.api.Plugin; | ||
| import org.gradle.api.Project; | ||
| import org.gradle.api.file.Directory; | ||
| import org.gradle.api.file.FileCollection; | ||
| import org.gradle.api.file.RegularFile; | ||
| import org.gradle.api.file.SourceDirectorySet; | ||
| import org.gradle.api.plugins.JavaLibraryPlugin; | ||
|
|
@@ -99,38 +102,49 @@ public void apply(Project project) { | |
| project.getPluginManager().apply(MetricSchemaMarkdownPlugin.class); | ||
| }); | ||
|
|
||
| configureIdea(project, generatedJavaDir, generatedResourcesDir); | ||
| configureIdea(project, generatedJavaDir); | ||
|
|
||
| // Only add dependencies if we're going to generate code | ||
| if (!metricSchemaSourceDirectorySet.getFiles().isEmpty()) { | ||
| configureProjectDependencies(project); | ||
| } | ||
| configureDependencies(project, metricSchemaSourceDirectorySet); | ||
| } | ||
|
|
||
| private static void configureIdea( | ||
| Project project, Provider<Directory> generatedJavaDir, Provider<Directory> generatedResourcesDir) { | ||
| private static void configureIdea(Project project, Provider<Directory> generatedJavaDir) { | ||
| project.getPluginManager().withPlugin("idea", _plugin -> { | ||
| 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()); | ||
| }); | ||
| } | ||
|
|
||
| private static void configureProjectDependencies(Project project) { | ||
| project.getDependencies().add("api", "com.palantir.tritium:tritium-registry"); | ||
| project.getDependencies().add("api", "com.palantir.safe-logging:preconditions"); | ||
| project.getDependencies().add("api", "com.google.errorprone:error_prone_annotations"); | ||
| // Metric types like Gauge are part of the generated code's public API | ||
| project.getDependencies().add("api", "io.dropwizard.metrics:metrics-core"); | ||
| project.getDependencies().add("implementation", "com.palantir.safe-logging:safe-logging"); | ||
| private static void configureDependencies(Project project, FileCollection metricSchemaSourceDirectorySet) { | ||
| ImmutableSetMultimap<String, String> allDependencies = ImmutableSetMultimap.<String, String>builder() | ||
| .put("api", "com.palantir.tritium:tritium-registry") | ||
| .put("api", "com.palantir.safe-logging:preconditions") | ||
| .put("api", "com.google.errorprone:error_prone_annotations") | ||
| // Metric types like Gauge are part of the generated code's public API | ||
| .put("api", "io.dropwizard.metrics:metrics-core") | ||
| .put("implementation", "com.palantir.safe-logging:safe-logging") | ||
| .build(); | ||
|
|
||
| allDependencies.asMap().forEach((configurationName, dependencies) -> { | ||
| project.getConfigurations().named(configurationName, configuration -> { | ||
| configuration | ||
| .getDependencies() | ||
| .addAllLater( | ||
| metricSchemaSourceDirectorySet.getElements().map(files -> { | ||
| // Don't add dependencies if we're not going to generate code | ||
| if (files.isEmpty()) { | ||
| return List.of(); | ||
| } | ||
|
|
||
| return dependencies.stream() | ||
| .map(project.getDependencies()::create) | ||
| .toList(); | ||
| })); | ||
|
Comment on lines
+136
to
+145
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we end up doing this, I wonder if it would be possible to inspect the outputs from
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation seems better to me - I don't think we want dependency resolution to depend on the result of running tasks. If we can know this information without executing the task, that seems preferrable.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried this in b8c9b8f. It looks like this doesn't set up a task dependency, so dependency resolution just fails if the task has not yet been run and the output directory does not exist.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's too bad, but your earlier point makes sense anyway, so this is fine by me |
||
| }); | ||
| }); | ||
| } | ||
|
|
||
| private String defaultLibraryName(Project project) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is unnecessary. The IDEA plugin knows the source and resource directories from the source sets. The only thing it can't know is which source directories should be considered generated sources.