A few small refactors and cleanups on thrifty-gradle-plugin (#526)

This commit is contained in:
Ben Bader 2023-02-13 10:28:34 -07:00 коммит произвёл GitHub
Родитель 3957065950
Коммит 7be847e70c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
8 изменённых файлов: 43 добавлений и 25 удалений

Просмотреть файл

@ -7,8 +7,10 @@ and have Thrifty generate Kotlin or Java code from them as part of regular build
Incorporate it into your build like so:
```groovy
apply plugin: 'kotlin'
apply plugin: 'com.microsoft.thrifty'
plugins {
id 'org.jetbrains.kotlin.jvm'
id 'com.microsoft.thrifty'
}
// usual gradle stuff

Просмотреть файл

@ -61,7 +61,7 @@ def versionTask = tasks.register("generateVersionProps") { t ->
}
}
tasks.named("clean") {
clean {
delete versionTask
}
@ -92,6 +92,6 @@ def installLocal = tasks.register("installForTesting") {
.findAll { it != null }
}
tasks.withType(Test) {
tasks.withType(Test).configureEach {
dependsOn installLocal
}

Просмотреть файл

@ -50,14 +50,13 @@ import java.nio.file.attribute.BasicFileAttributes;
/**
* A {@link WorkAction} that actually generates the Thrifty sources.
*
* We're doing this via the Worker API to ensure that Gradle's hard-coded Kotlin
* <p>We're doing this via the Worker API to ensure that Gradle's hard-coded Kotlin
* version doesn't cause us grief. Thrifty is entirely written in Kotlin, and
* there's no guarantee that we'll be using a version compatible with whatever
* Gradle happens to have bundled. According to some of their engineers, this
* (with classpath-level isolation) is the only safe way to use Kotlin in the context
* of a Gradle plugin.
*/
@SuppressWarnings("UnstableApiUsage")
public abstract class GenerateThriftSourcesWorkAction implements WorkAction<GenerateThriftSourcesWorkParams> {
private static final Logger LOGGER = LoggerFactory.getLogger(GenerateThriftSourcesWorkAction.class);

Просмотреть файл

@ -33,7 +33,6 @@ import java.io.File;
* Encapsulates all input to Thrifty compilation, in a {@link java.io.Serializable Serializable}
* form.
*/
@SuppressWarnings("UnstableApiUsage")
public interface GenerateThriftSourcesWorkParams extends WorkParameters {
DirectoryProperty getOutputDirectory();
ListProperty<File> getIncludePath();

Просмотреть файл

@ -23,7 +23,7 @@ package com.microsoft.thrifty.gradle;
/**
* Represents a pattern-filterable source directory.
*
* Pattern syntax is as in {@link org.gradle.api.tasks.util.PatternFilterable}.
* <p>Pattern syntax is as in {@link org.gradle.api.tasks.util.PatternFilterable}.
*/
public interface ThriftSourceDirectory {
/**
@ -34,7 +34,7 @@ public interface ThriftSourceDirectory {
/**
* Excludes all files described by the given pattern.
*
* Exclusions take precedence over inclusions.
* <p>Exclusions take precedence over inclusions.
*/
void exclude(String pattern);
}

Просмотреть файл

@ -21,6 +21,7 @@
package com.microsoft.thrifty.gradle;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import org.gradle.api.Action;
import org.gradle.api.file.Directory;
import org.gradle.api.file.DirectoryProperty;
@ -41,10 +42,9 @@ import java.util.stream.Collectors;
/**
* Implements the 'thrifty' Gradle extension.
*
* This is the public interface of our Gradle plugin to build scripts. Renaming
* <p>This is the public interface of our Gradle plugin to build scripts. Renaming
* or removing a method is a breaking change!
*/
@SuppressWarnings("UnstableApiUsage")
public abstract class ThriftyExtension {
private static final String DEFAULT_SOURCE_DIR = Joiner.on(File.separator).join("src", "main", "thrift");
private static final String DEFAULT_OUTPUT_DIR = Joiner.on(File.separator).join("generated", "sources", "thrifty");
@ -79,19 +79,34 @@ public abstract class ThriftyExtension {
.include("**/*.thrift");
}
public Provider<List<Directory>> getIncludePathEntries() {
Provider<List<Directory>> getIncludePathEntries() {
return includePathEntries;
}
public Provider<List<DefaultThriftSourceDirectory>> getSources() {
Provider<List<File>> getIncludePath() {
return getIncludePathEntries().map(
dirs -> dirs.stream()
.map(Directory::getAsFile)
.collect(Collectors.toList())
);
}
Provider<List<DefaultThriftSourceDirectory>> getSources() {
return sources;
}
public Provider<ThriftOptions> getThriftOptions() {
Provider<List<SourceDirectorySet>> getSourceDirectorySets() {
return getSources().map(
ss -> ss.stream()
.map(DefaultThriftSourceDirectory::getSourceDirectorySet)
.collect(Collectors.toList()));
}
Provider<ThriftOptions> getThriftOptions() {
return thriftOptions;
}
public Provider<Directory> getOutputDirectory() {
Provider<Directory> getOutputDirectory() {
return outputDirectory;
}
@ -122,9 +137,10 @@ public abstract class ThriftyExtension {
public void includePath(String... paths) {
for (String path : paths) {
Directory dir = layout.getProjectDirectory().dir(path);
if (!dir.getAsFile().isDirectory()) {
throw new IllegalArgumentException("Include-path entries must be directories");
}
Preconditions.checkArgument(
dir.getAsFile().isDirectory(),
"Include-path '%s' is not a directory",
path);
includePathEntries.add(dir);
}
}

Просмотреть файл

@ -21,11 +21,12 @@
package com.microsoft.thrifty.gradle;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.io.ByteSource;
import com.google.common.io.Resources;
import org.gradle.api.GradleException;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.file.Directory;
import org.gradle.api.plugins.JavaBasePlugin;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.provider.Provider;
@ -34,8 +35,8 @@ import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import java.util.stream.Collectors;
/**
* The plugin makes everything happen.
@ -58,12 +59,12 @@ public abstract class ThriftyGradlePlugin implements Plugin<Project> {
TaskProvider<ThriftyTask> thriftTaskProvider = project.getTasks().register("generateThriftFiles", ThriftyTask.class, t -> {
t.setGroup("thrifty");
t.setDescription("Generate Thrifty thrift implementations for .thrift files");
t.getIncludePath().set(ext.getIncludePathEntries().map(dirs -> dirs.stream().map(Directory::getAsFile).collect(Collectors.toList())));
t.getIncludePath().set(ext.getIncludePath());
t.getOutputDirectory().set(ext.getOutputDirectory());
t.getThriftOptions().set(ext.getThriftOptions());
t.getShowStacktrace().set(project.getGradle().getStartParameter().getShowStacktrace());
t.getThriftyClasspath().from(thriftyConfig);
t.source(ext.getSources().map(ss -> ss.stream().map(it -> it.getSourceDirectorySet()).collect(Collectors.toList())));
t.source(ext.getSourceDirectorySets());
});
project.getPlugins().withType(JavaBasePlugin.class).configureEach(plugin -> {
@ -78,7 +79,9 @@ public abstract class ThriftyGradlePlugin implements Plugin<Project> {
@VisibleForTesting
static Properties loadVersionProps() {
try (InputStream is = ThriftyGradlePlugin.class.getClassLoader().getResourceAsStream("thrifty-version.properties")) {
URL url = Resources.getResource("thrifty-version.properties");
ByteSource byteSource = Resources.asByteSource(url);
try (InputStream is = byteSource.openBufferedStream()) {
Properties props = new Properties();
props.load(is);
return props;

Просмотреть файл

@ -41,7 +41,7 @@ import java.io.File;
/**
* The Gradle task responsible for triggering generation of Thrifty source files.
*
* In practice, just a thin layer around a Worker API action which does the heavy
* <p>In practice, just a thin layer around a Worker API action which does the heavy
* lifting.
*/
public abstract class ThriftyTask extends SourceTask {
@ -63,7 +63,6 @@ public abstract class ThriftyTask extends SourceTask {
@Inject
abstract public WorkerExecutor getWorkerExecutor();
@SuppressWarnings("UnstableApiUsage")
@TaskAction
public void run() {
WorkQueue workQueue = getWorkerExecutor().classLoaderIsolation(spec -> {