Skip to content

Commit e58439c

Browse files
authored
154 gradle plugin (#156)
* Put processor execution logic in its module * Add ap-exec as depend to gradle plugin * Simplify README * Basic gradle plugin impl and it * Add degradlew for debug gradle * Add CI to execute gradle plugin build and test * Setup gradle plugin publish * Set version to 0.13.1
1 parent 15c48b4 commit e58439c

68 files changed

Lines changed: 1431 additions & 270 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/ci.yaml

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,26 @@ jobs:
4242
java-version: ${{env.JAVA_VERSION}}
4343
- uses: actions/cache@v4
4444
with:
45-
path: ~/.m2/repository
46-
key: build_maven
45+
path: |
46+
~/.m2/repository
47+
~/.gradle
48+
key: manual_build_cache_key
4749
- name: Build And Test
4850
run: ./mvnw install -e --ntp -B
4951
- name: Test JPMS
5052
run: ./mvnw clean verify -pl it/java8 -P it-jpms -e --ntp -B
5153
- name: Test Maven 3.2.5 compatibility
52-
working-directory: maven-plugin/it
54+
working-directory: build-tool-plugins/maven-plugin/it
5355
run: ./mvnw stype:gen -e -B
56+
- name: Build and Test Gradle plugin
57+
working-directory: build-tool-plugins/gradle-plugin
58+
run: ./gradlew --no-daemon test publishToMavenLocal --stacktrace
59+
- name: Download Gradle in Gradle Plugin IT
60+
working-directory: build-tool-plugins/gradle-plugin/it
61+
run: ./gradlew --no-daemon --version
62+
# - name: Test Gradle plugin # Gradle 7 does not support JDK21
63+
# working-directory: build-tool-plugins/gradle-plugin/it
64+
# run: ./gradlew stypeGen test --stacktrace
5465
- name: Javadoc
5566
run: ./mvnw -P release javadoc:javadoc --ntp -B
5667
- name: Upload generated sources
@@ -92,13 +103,18 @@ jobs:
92103
# run: ls -lhR ~/.m2/repository/online/sharedtype
93104
- uses: actions/cache/restore@v4
94105
with:
95-
path: ~/.m2/repository
96-
key: build_maven
106+
path: |
107+
~/.m2/repository
108+
~/.gradle
109+
key: manual_build_cache_key
97110
- name: Test
98111
run: ./mvnw verify -pl it/java8 -e --ntp -B
99112
- name: Maven Plugin Test
100-
working-directory: maven-plugin/it
113+
working-directory: build-tool-plugins/maven-plugin/it
101114
run: ./mvnw stype:gen -e -B
115+
- name: Gradle Plugin Test
116+
working-directory: build-tool-plugins/gradle-plugin/it
117+
run: ./gradlew stypeGen test --stacktrace
102118

103119
client_test:
104120
needs: [build_and_test]

.github/workflows/release.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ jobs:
3434
MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
3535
SONATYPE_CENTRAL_USER: ${{ secrets.SONATYPE_CENTRAL_USER }}
3636
SONATYPE_CENTRAL_PASS: ${{ secrets.SONATYPE_CENTRAL_PASS }}
37+
GRADLE_PUBLISH_KEY: ${{ secrets.GRADLE_PUBLISH_KEY }}
38+
GRADLE_PUBLISH_SECRET: ${{ secrets.GRADLE_PUBLISH_SECRET }}
3739
run: |
3840
. ./misc/release.sh
3941
echo "new_version=$(cat NEW_VERSION.cache)" >> "$GITHUB_OUTPUT"

README.md

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,14 @@ export interface User {
1818
email: string;
1919
}
2020
```
21-
Go:
22-
```golang
23-
type User struct {
24-
Name string
25-
Age int
26-
Email string
27-
}
28-
```
29-
Rust:
30-
```rust
31-
pub struct User {
32-
name: String,
33-
age: i32,
34-
email: String,
35-
}
36-
```
3721

3822
## Features
3923
* Java8+ compatible.
4024
* Generics support.
4125
* Compile-time constant support.
4226
* Fast. (Execution takes milliseconds with `-proc:only`.)
4327
* Simple global + type level configurations.
28+
* Multiple target language options: Typescript, Go, Rust.
4429

4530
## Documentation
4631
* [User Guide](doc/Usage.md)

annotation/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>online.sharedtype</groupId>
66
<artifactId>sharedtype-parent</artifactId>
7-
<version>0.14.0-SNAPSHOT</version>
7+
<version>0.13.1-SNAPSHOT</version>
88
<relativePath>../pom.xml</relativePath>
99
</parent>
1010

build-tool-plugins/exec/pom.xml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>online.sharedtype</groupId>
7+
<artifactId>sharedtype-parent</artifactId>
8+
<version>0.13.1-SNAPSHOT</version>
9+
<relativePath>../../pom.xml</relativePath>
10+
</parent>
11+
12+
<artifactId>sharedtype-ap-exec</artifactId>
13+
<version>0.13.1-SNAPSHOT</version>
14+
<name>SharedType Annotation Processor Executor</name>
15+
<description>Call javac to execute annotation processing.</description>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.junit.jupiter</groupId>
20+
<artifactId>junit-jupiter</artifactId>
21+
<scope>test</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.junit-pioneer</groupId>
25+
<artifactId>junit-pioneer</artifactId>
26+
<scope>test</scope>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.assertj</groupId>
30+
<artifactId>assertj-core</artifactId>
31+
<scope>test</scope>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.mockito</groupId>
35+
<artifactId>mockito-core</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.mockito</groupId>
40+
<artifactId>mockito-junit-jupiter</artifactId>
41+
<scope>test</scope>
42+
</dependency>
43+
</dependencies>
44+
45+
<build>
46+
<plugins>
47+
<plugin>
48+
<artifactId>maven-surefire-plugin</artifactId>
49+
<configuration>
50+
<argLine>
51+
-javaagent:${settings.localRepository}/org/mockito/mockito-core/${mockito.version}/mockito-core-${mockito.version}.jar
52+
</argLine>
53+
</configuration>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
</project>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package online.sharedtype.exec.common;
2+
3+
import javax.annotation.processing.Processor;
4+
import javax.tools.JavaCompiler;
5+
import javax.tools.JavaFileObject;
6+
import javax.tools.StandardJavaFileManager;
7+
import javax.tools.StandardLocation;
8+
import javax.tools.ToolProvider;
9+
import java.io.File;
10+
import java.io.IOException;
11+
import java.nio.charset.Charset;
12+
import java.nio.file.FileVisitOption;
13+
import java.nio.file.Files;
14+
import java.nio.file.Path;
15+
import java.util.Collections;
16+
import java.util.EnumSet;
17+
import java.util.List;
18+
19+
/**
20+
* Generic annotation processor executor. This module does not depend on sharedtype-ap.
21+
* @see SharedTypeApCompilerOptions
22+
* @author Cause Chung
23+
*/
24+
public final class AnnotationProcessorExecutor {
25+
private static final String JAVA8_VERSION = "1.8";
26+
private final Processor processor;
27+
private final Logger log;
28+
private final DependencyResolver dependencyResolver;
29+
30+
public AnnotationProcessorExecutor(Processor processor, Logger log, DependencyResolver dependencyResolver) {
31+
this.processor = processor;
32+
this.log = log;
33+
this.dependencyResolver = dependencyResolver;
34+
}
35+
36+
public void execute(Path projectBaseDir,
37+
Path outputDir,
38+
Iterable<Path> compileSourceRoots,
39+
String sourceEncoding,
40+
Iterable<String> compilerOptions) throws Exception {
41+
SimpleDiagnosticListener diagnosticListener = new SimpleDiagnosticListener(log, projectBaseDir);
42+
JavaCompiler compiler = getJavaCompiler();
43+
44+
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, getCharset(sourceEncoding));
45+
try (SimpleLoggerWriter logger = new SimpleLoggerWriter(log)) {
46+
if (Files.notExists(outputDir)) {
47+
Files.createDirectories(outputDir);
48+
}
49+
fileManager.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outputDir.toFile()));
50+
fileManager.setLocation(StandardLocation.CLASS_PATH, dependencyResolver.getClasspathDependencies());
51+
Iterable<? extends JavaFileObject> sources = fileManager.getJavaFileObjectsFromFiles(walkAllSourceFiles(compileSourceRoots));
52+
53+
JavaCompiler.CompilationTask task = compiler.getTask(logger, fileManager, diagnosticListener, compilerOptions, null, sources);
54+
task.setProcessors(Collections.singleton(processor));
55+
task.call();
56+
}
57+
}
58+
59+
private static List<File> walkAllSourceFiles(Iterable<Path> compileSourceRoots) throws IOException {
60+
SourceFileVisitor visitor = new SourceFileVisitor();
61+
for (Path compileSourceRoot : compileSourceRoots) {
62+
if (Files.isDirectory(compileSourceRoot)) {
63+
Files.walkFileTree(compileSourceRoot, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, visitor);
64+
}
65+
}
66+
return visitor.getFiles();
67+
}
68+
69+
private static JavaCompiler getJavaCompiler() throws Exception {
70+
String javaVersion = System.getProperty("java.specification.version");
71+
JavaCompiler compiler;
72+
if (JAVA8_VERSION.equals(javaVersion)) {
73+
Class<?> javacToolClass = Class.forName("com.sun.tools.javac.api.JavacTool");
74+
compiler = (JavaCompiler) javacToolClass.getConstructor().newInstance();
75+
} else {
76+
compiler = ToolProvider.getSystemJavaCompiler();
77+
}
78+
if (compiler != null) {
79+
return compiler;
80+
}
81+
throw new ClassNotFoundException("Java compiler not found, currently only compiler from jdk.compiler module is supported.");
82+
}
83+
84+
private static Charset getCharset(String encoding) {
85+
if (encoding != null) {
86+
return Charset.forName(encoding);
87+
}
88+
return null;
89+
}
90+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package online.sharedtype.exec.common;
2+
3+
import java.io.File;
4+
import java.util.List;
5+
6+
/**
7+
* Adapter interface for retrieving classpath dependencies.
8+
* @author Cause Chung
9+
*/
10+
public interface DependencyResolver {
11+
List<File> getClasspathDependencies() throws Exception;
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package online.sharedtype.exec.common;
2+
3+
/**
4+
* Adapter for logging.
5+
* @author Cause Chung
6+
*/
7+
public interface Logger {
8+
void info(String message);
9+
void warn(String message);
10+
void error(String message);
11+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package online.sharedtype.exec.common;
2+
3+
import java.io.File;
4+
import java.nio.file.Files;
5+
import java.nio.file.Paths;
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
/**
11+
* Assumed options for sharedtype annotation processor.
12+
* @author Cause Chung
13+
*/
14+
public final class SharedTypeApCompilerOptions {
15+
private static final List<String> DEFAULT_COMPILER_OPTIONS = Arrays.asList("-proc:only", "-Asharedtype.enabled=true");
16+
private static final String OPTION_PROPS_FILE_KEY = "-Asharedtype.propsFile=";
17+
private final String propertyFile;
18+
19+
public SharedTypeApCompilerOptions(String propertyFile) {
20+
this.propertyFile = propertyFile;
21+
}
22+
public SharedTypeApCompilerOptions(File propertyFile) {
23+
this(propertyFile == null ? null : propertyFile.getAbsolutePath());
24+
}
25+
26+
public List<String> toList() {
27+
List<String> options = new ArrayList<>(DEFAULT_COMPILER_OPTIONS.size() + 1);
28+
options.addAll(DEFAULT_COMPILER_OPTIONS);
29+
if (propertyFile != null) {
30+
if (Files.notExists(Paths.get(propertyFile))) {
31+
throw new IllegalArgumentException("Property file not found: " + propertyFile);
32+
}
33+
options.add(OPTION_PROPS_FILE_KEY + propertyFile);
34+
}
35+
return options;
36+
}
37+
}

maven-plugin/src/main/java/online/sharedtype/maven/SharedTypeDiagnosticListener.java renamed to build-tool-plugins/exec/src/main/java/online/sharedtype/exec/common/SimpleDiagnosticListener.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
package online.sharedtype.maven;
2-
3-
import com.google.common.annotations.VisibleForTesting;
4-
import online.sharedtype.processor.support.annotation.SideEffect;
5-
import org.apache.maven.plugin.logging.Log;
1+
package online.sharedtype.exec.common;
62

73
import javax.tools.Diagnostic;
84
import javax.tools.DiagnosticListener;
95
import javax.tools.JavaFileObject;
106
import java.nio.file.Path;
117
import java.nio.file.Paths;
128

13-
final class SharedTypeDiagnosticListener implements DiagnosticListener<JavaFileObject> {
14-
private final Log log;
9+
/**
10+
* @author Cause Chung
11+
*/
12+
final class SimpleDiagnosticListener implements DiagnosticListener<JavaFileObject> {
13+
private final Logger log;
1514
private final Path projectBaseDir;
16-
SharedTypeDiagnosticListener(Log log, Path projectBaseDir) {
15+
SimpleDiagnosticListener(Logger log, Path projectBaseDir) {
1716
this.log = log;
1817
this.projectBaseDir = projectBaseDir;
1918
}
@@ -40,8 +39,7 @@ public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
4039
}
4140
}
4241

43-
@VisibleForTesting
44-
void addSourceInfo(@SideEffect StringBuilder sb, Diagnostic<? extends JavaFileObject> diagnostic) {
42+
void addSourceInfo(StringBuilder sb, Diagnostic<? extends JavaFileObject> diagnostic) {
4543
if (diagnostic.getSource() == null) {
4644
return;
4745
}

0 commit comments

Comments
 (0)