Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Remove configurability from Extension, move service loading
  • Loading branch information
iamdanfox committed Oct 22, 2019
commit 8a24717145dc9e704443b9516b31cecc494da0e5
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import java.util.regex.Pattern;
import java.util.stream.Stream;

public final class FormatDiff {
final class FormatDiff {
// each section in the git diff output starts like this
private static final Pattern SEPARATOR = Pattern.compile("diff --git");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,35 @@

package com.palantir.javaformat.gradle;

import com.google.common.collect.Iterables;
import com.palantir.javaformat.java.FormatterService;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ServiceLoader;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.provider.Property;

public class JavaFormatExtension {
private final Property<String> implementationVersion;
private static final String IMPLEMENTATION_VERSION =
JavaFormatExtension.class.getPackage().getImplementationVersion();
private final Configuration configuration;

public JavaFormatExtension(Project project) {
implementationVersion = project.getObjects().property(String.class);
implementationVersion.set(IMPLEMENTATION_VERSION);
public JavaFormatExtension(Configuration configuration) {
this.configuration = configuration;
}

public final Property<String> getImplementationVersion() {
return implementationVersion;
public FormatterService serviceLoad() {
URL[] jarUris = configuration.getFiles().stream()
.map(file -> {
try {
return file.toURI().toURL();
} catch (MalformedURLException e) {
throw new RuntimeException("Unable to convert URI to URL: " + file, e);
}
})
.toArray(URL[]::new);

ClassLoader classLoader = new URLClassLoader(jarUris, FormatterService.class.getClassLoader());
return Iterables.getOnlyElement(ServiceLoader.load(FormatterService.class, classLoader));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@

package com.palantir.javaformat.gradle;

import com.google.common.collect.Iterables;
import com.palantir.javaformat.java.FormatterService;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ServiceLoader;
import org.gradle.api.DefaultTask;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
Expand All @@ -49,26 +44,10 @@ public FormatDiffTask() {

@TaskAction
public final void formatDiff() throws IOException, InterruptedException {
URL[] jarUris = getProject()
.getRootProject()
.getConfigurations()
.getByName(PalantirJavaFormatProviderPlugin.CONFIGURATION_NAME)
.getFiles()
.stream()
.map(file -> {
try {
return file.toURI().toURL();
} catch (MalformedURLException e) {
throw new RuntimeException("Unable to convert URI to URL: " + file, e);
}
})
.toArray(URL[]::new);

ClassLoader classLoader = new URLClassLoader(jarUris, PalantirJavaFormatPlugin.class.getClassLoader());
FormatterService formatter =
Iterables.getOnlyElement(ServiceLoader.load(FormatterService.class, classLoader));

FormatDiff.formatDiff(getProject().getProjectDir().toPath(), formatter);
JavaFormatExtension extension =
getProject().getRootProject().getExtensions().getByType(JavaFormatExtension.class);
FormatterService formatterService = extension.serviceLoad();
FormatDiff.formatDiff(getProject().getProjectDir().toPath(), formatterService);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,31 @@
import com.google.common.collect.ImmutableMap;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;

public final class PalantirJavaFormatProviderPlugin implements Plugin<Project> {

public static final String CONFIGURATION_NAME = "palantirJavaFormat";
static final String CONFIGURATION_NAME = "palantirJavaFormat";

@Override
public void apply(Project project) {
public void apply(Project rootProject) {
Preconditions.checkState(
project == project.getRootProject(),
rootProject == rootProject.getRootProject(),
"May only apply com.palantir.java-format-provider to the root project");

JavaFormatExtension extension =
project.getExtensions().create("palantirJavaFormat", JavaFormatExtension.class, project);

project.getConfigurations().create(CONFIGURATION_NAME, conf -> {
Configuration configuration = rootProject.getConfigurations().create(CONFIGURATION_NAME, conf -> {
conf.setDescription("Internal configuration for resolving the palantir-java-format implementation");
conf.setVisible(false);
conf.setCanBeConsumed(false);
// Using addLater instead of afterEvaluate, in order to delay reading the extension until after the user
// has configured it.
conf.defaultDependencies(deps -> deps.addLater(project.provider(() -> {
String version = extension.getImplementationVersion().get();

return project.getDependencies().create(ImmutableMap.of(
conf.defaultDependencies(deps -> {
deps.add(rootProject.getDependencies().create(ImmutableMap.of(
"group", "com.palantir.javaformat",
"name", "palantir-java-format",
"version", version));
})));
"version", JavaFormatExtension.class.getPackage().getImplementationVersion())));
});
});

rootProject.getExtensions().create("palantirJavaFormat", JavaFormatExtension.class, configuration);
}
}