Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
28 changes: 22 additions & 6 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,26 @@ jobs:
java-version: ${{env.JAVA_VERSION}}
- uses: actions/cache@v4
with:
path: ~/.m2/repository
key: build_maven
path: |
~/.m2/repository
~/.gradle
key: manual_build_cache_key
- name: Build And Test
run: ./mvnw install -e --ntp -B
- name: Test JPMS
run: ./mvnw clean verify -pl it/java8 -P it-jpms -e --ntp -B
- name: Test Maven 3.2.5 compatibility
working-directory: maven-plugin/it
working-directory: build-tool-plugins/maven-plugin/it
run: ./mvnw stype:gen -e -B
- name: Build and Test Gradle plugin
working-directory: build-tool-plugins/gradle-plugin
run: ./gradlew --no-daemon test publishToMavenLocal --stacktrace
- name: Download Gradle in Gradle Plugin IT
working-directory: build-tool-plugins/gradle-plugin/it
run: ./gradlew --no-daemon --version
# - name: Test Gradle plugin # Gradle 7 does not support JDK21
# working-directory: build-tool-plugins/gradle-plugin/it
# run: ./gradlew stypeGen test --stacktrace
- name: Javadoc
run: ./mvnw -P release javadoc:javadoc --ntp -B
- name: Upload generated sources
Expand Down Expand Up @@ -92,13 +103,18 @@ jobs:
# run: ls -lhR ~/.m2/repository/online/sharedtype
- uses: actions/cache/restore@v4
with:
path: ~/.m2/repository
key: build_maven
path: |
~/.m2/repository
~/.gradle
key: manual_build_cache_key
- name: Test
run: ./mvnw verify -pl it/java8 -e --ntp -B
- name: Maven Plugin Test
working-directory: maven-plugin/it
working-directory: build-tool-plugins/maven-plugin/it
run: ./mvnw stype:gen -e -B
- name: Gradle Plugin Test
working-directory: build-tool-plugins/gradle-plugin/it
run: ./gradlew stypeGen test --stacktrace

client_test:
needs: [build_and_test]
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ jobs:
MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
SONATYPE_CENTRAL_USER: ${{ secrets.SONATYPE_CENTRAL_USER }}
SONATYPE_CENTRAL_PASS: ${{ secrets.SONATYPE_CENTRAL_PASS }}
GRADLE_PUBLISH_KEY: ${{ secrets.GRADLE_PUBLISH_KEY }}
GRADLE_PUBLISH_SECRET: ${{ secrets.GRADLE_PUBLISH_SECRET }}
run: |
. ./misc/release.sh
echo "new_version=$(cat NEW_VERSION.cache)" >> "$GITHUB_OUTPUT"
Expand Down
17 changes: 1 addition & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,14 @@ export interface User {
email: string;
}
```
Go:
```golang
type User struct {
Name string
Age int
Email string
}
```
Rust:
```rust
pub struct User {
name: String,
age: i32,
email: String,
}
```

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

## Documentation
* [User Guide](doc/Usage.md)
Expand Down
2 changes: 1 addition & 1 deletion annotation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>online.sharedtype</groupId>
<artifactId>sharedtype-parent</artifactId>
<version>0.14.0-SNAPSHOT</version>
<version>0.13.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
57 changes: 57 additions & 0 deletions build-tool-plugins/exec/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>online.sharedtype</groupId>
<artifactId>sharedtype-parent</artifactId>
<version>0.13.1-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>sharedtype-ap-exec</artifactId>
<version>0.13.1-SNAPSHOT</version>
<name>SharedType Annotation Processor Executor</name>
<description>Call javac to execute annotation processing.</description>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit-pioneer</groupId>
<artifactId>junit-pioneer</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
-javaagent:${settings.localRepository}/org/mockito/mockito-core/${mockito.version}/mockito-core-${mockito.version}.jar
</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package online.sharedtype.exec.common;

import javax.annotation.processing.Processor;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import javax.tools.ToolProvider;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;

/**
* Generic annotation processor executor. This module does not depend on sharedtype-ap.
* @see SharedTypeApCompilerOptions
* @author Cause Chung
*/
public final class AnnotationProcessorExecutor {
private static final String JAVA8_VERSION = "1.8";
private final Processor processor;
private final Logger log;
private final DependencyResolver dependencyResolver;

public AnnotationProcessorExecutor(Processor processor, Logger log, DependencyResolver dependencyResolver) {
this.processor = processor;
this.log = log;
this.dependencyResolver = dependencyResolver;
}

public void execute(Path projectBaseDir,
Path outputDir,
Iterable<Path> compileSourceRoots,
String sourceEncoding,
Iterable<String> compilerOptions) throws Exception {
SimpleDiagnosticListener diagnosticListener = new SimpleDiagnosticListener(log, projectBaseDir);
JavaCompiler compiler = getJavaCompiler();

StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, getCharset(sourceEncoding));
try (SimpleLoggerWriter logger = new SimpleLoggerWriter(log)) {
if (Files.notExists(outputDir)) {
Files.createDirectories(outputDir);
}
fileManager.setLocation(StandardLocation.SOURCE_OUTPUT, Collections.singleton(outputDir.toFile()));
fileManager.setLocation(StandardLocation.CLASS_PATH, dependencyResolver.getClasspathDependencies());
Iterable<? extends JavaFileObject> sources = fileManager.getJavaFileObjectsFromFiles(walkAllSourceFiles(compileSourceRoots));

JavaCompiler.CompilationTask task = compiler.getTask(logger, fileManager, diagnosticListener, compilerOptions, null, sources);
task.setProcessors(Collections.singleton(processor));
task.call();
}
}

private static List<File> walkAllSourceFiles(Iterable<Path> compileSourceRoots) throws IOException {
SourceFileVisitor visitor = new SourceFileVisitor();
for (Path compileSourceRoot : compileSourceRoots) {
if (Files.isDirectory(compileSourceRoot)) {
Files.walkFileTree(compileSourceRoot, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, visitor);
}
}
return visitor.getFiles();
}

private static JavaCompiler getJavaCompiler() throws Exception {
String javaVersion = System.getProperty("java.specification.version");
JavaCompiler compiler;
if (JAVA8_VERSION.equals(javaVersion)) {
Class<?> javacToolClass = Class.forName("com.sun.tools.javac.api.JavacTool");
compiler = (JavaCompiler) javacToolClass.getConstructor().newInstance();
} else {
compiler = ToolProvider.getSystemJavaCompiler();
}
if (compiler != null) {
return compiler;
}
throw new ClassNotFoundException("Java compiler not found, currently only compiler from jdk.compiler module is supported.");
}

private static Charset getCharset(String encoding) {
if (encoding != null) {
return Charset.forName(encoding);
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package online.sharedtype.exec.common;

import java.io.File;
import java.util.List;

/**
* Adapter interface for retrieving classpath dependencies.
* @author Cause Chung
*/
public interface DependencyResolver {
List<File> getClasspathDependencies() throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package online.sharedtype.exec.common;

/**
* Adapter for logging.
* @author Cause Chung
*/
public interface Logger {
void info(String message);
void warn(String message);
void error(String message);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package online.sharedtype.exec.common;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* Assumed options for sharedtype annotation processor.
* @author Cause Chung
*/
public final class SharedTypeApCompilerOptions {
private static final List<String> DEFAULT_COMPILER_OPTIONS = Arrays.asList("-proc:only", "-Asharedtype.enabled=true");
private static final String OPTION_PROPS_FILE_KEY = "-Asharedtype.propsFile=";
private final String propertyFile;

public SharedTypeApCompilerOptions(String propertyFile) {
this.propertyFile = propertyFile;
}
public SharedTypeApCompilerOptions(File propertyFile) {
this(propertyFile == null ? null : propertyFile.getAbsolutePath());
}

public List<String> toList() {
List<String> options = new ArrayList<>(DEFAULT_COMPILER_OPTIONS.size() + 1);
options.addAll(DEFAULT_COMPILER_OPTIONS);
if (propertyFile != null) {
if (Files.notExists(Paths.get(propertyFile))) {
throw new IllegalArgumentException("Property file not found: " + propertyFile);
}
options.add(OPTION_PROPS_FILE_KEY + propertyFile);
}
return options;
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package online.sharedtype.maven;

import com.google.common.annotations.VisibleForTesting;
import online.sharedtype.processor.support.annotation.SideEffect;
import org.apache.maven.plugin.logging.Log;
package online.sharedtype.exec.common;

import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener;
import javax.tools.JavaFileObject;
import java.nio.file.Path;
import java.nio.file.Paths;

final class SharedTypeDiagnosticListener implements DiagnosticListener<JavaFileObject> {
private final Log log;
/**
* @author Cause Chung
*/
final class SimpleDiagnosticListener implements DiagnosticListener<JavaFileObject> {
private final Logger log;
private final Path projectBaseDir;
SharedTypeDiagnosticListener(Log log, Path projectBaseDir) {
SimpleDiagnosticListener(Logger log, Path projectBaseDir) {
this.log = log;
this.projectBaseDir = projectBaseDir;
}
Expand All @@ -40,8 +39,7 @@ public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
}
}

@VisibleForTesting
void addSourceInfo(@SideEffect StringBuilder sb, Diagnostic<? extends JavaFileObject> diagnostic) {
void addSourceInfo(StringBuilder sb, Diagnostic<? extends JavaFileObject> diagnostic) {
if (diagnostic.getSource() == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package online.sharedtype.maven;

import org.apache.maven.plugin.logging.Log;
package online.sharedtype.exec.common;

import java.io.Writer;

final class SharedTypeLogger extends Writer {
private final Log log;
/**
* @author Cause Chung
*/
final class SimpleLoggerWriter extends Writer {
private final Logger log;
private final StringBuffer buffer = new StringBuffer();

SharedTypeLogger(Log log) {
SimpleLoggerWriter(Logger log) {
this.log = log;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package online.sharedtype.maven;
package online.sharedtype.exec.common;

import java.io.File;
import java.nio.file.FileVisitResult;
Expand Down
Loading