Skip to content

Commit 61c9f86

Browse files
committed
Add index.d.ts generation. Change core types to use tsbindOptions
1 parent 822e1d3 commit 61c9f86

File tree

10 files changed

+60
-24
lines changed

10 files changed

+60
-24
lines changed

.github/workflows/build.yml

+2-7
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,10 @@ jobs:
1818
sudo apt-get install -y openjdk-11-source
1919
- name: Generate java.base types
2020
run: >-
21-
java -jar build/libs/java-ts-bind-all.jar
22-
--in /usr/lib/jvm/openjdk-11/src.zip --out java-core-types --offset java.base
23-
--include java.lang,java.util,java.io,java.nio
24-
--exclude java.lang.Math,java.lang.StrictMath,java.util.concurrent,java.util.spi
21+
java -jar build/libs/java-ts-bind-all.jar --packageJson java-core-types/package.json
2522
- name: Generate java.net.http types
2623
run: >-
27-
java -jar build/libs/java-ts-bind-all.jar
28-
--in /usr/lib/jvm/openjdk-11/src.zip --out httpclient-types --offset java.net.http
29-
--exclude jdk
24+
java -jar build/libs/java-ts-bind-all.jar --packageJson httpclient-types/package.json
3025
- uses: actions/setup-node@master
3126
with:
3227
registry-url: 'https://registry.npmjs.org'

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,4 @@ gradle-app.setting
115115
# End of https://www.toptal.com/developers/gitignore/api/eclipse,java,gradle
116116

117117
.classpath
118+
*.d.ts

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ This is a command-line application.
3333
* The options should be placed under `tsbindOptions` object
3434
* Names of options lack -- prefixes but are otherwise same
3535
* Handy when you already have package.json for publishing
36+
* --index: generate index.d.ts that references other generated files
3637

3738
## Limitations
3839
java-ts-bind does not necessarily generate *valid* TypeScript declarations.

httpclient-types/index.d.ts

-1
This file was deleted.

httpclient-types/package.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,12 @@
77
"url": "git+ssh://[email protected]/bensku/java-ts-bind.git",
88
"directory": "httpclient-types"
99
},
10-
"author": "bensku"
10+
"author": "bensku",
11+
"tsbindOptions": {
12+
"in": "/usr/lib/jvm/openjdk-11/src.zip",
13+
"out": "httpclient-types",
14+
"offset": "java.net.http",
15+
"exclude": ["jdk"],
16+
"index": true
17+
}
1118
}

java-core-types/index.d.ts

-4
This file was deleted.

java-core-types/package.json

+14-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,18 @@
77
"url": "git+ssh://[email protected]/bensku/java-ts-bind.git",
88
"directory": "java-core-types"
99
},
10-
"author": "bensku"
10+
"author": "bensku",
11+
"tsbindOptions": {
12+
"in": "/usr/lib/jvm/openjdk-11/src.zip",
13+
"out": "java-core-types",
14+
"offset": "java.base",
15+
"include": ["java.lang", "java.util", "java.io", "java.nio"],
16+
"exclude": ["java.lang.Math",
17+
"java.lang.StrictMath",
18+
"java.util.concurrent",
19+
"java.util.jar.Pack200",
20+
"java.util.spi"
21+
],
22+
"index": true
23+
}
1124
}

src/main/java/io/github/bensku/tsbind/binding/BindingGenerator.java

+19
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ public class BindingGenerator implements AstConsumer<String> {
3131
EXCLUDED_TYPES.add(TypeRef.OBJECT);
3232
}
3333

34+
/**
35+
* Whether or not index.d.ts should be generated.
36+
*/
37+
private final boolean buildIndex;
38+
39+
public BindingGenerator(boolean buildIndex) {
40+
this.buildIndex = buildIndex;
41+
}
42+
3443
@Override
3544
public Stream<Result<String>> consume(Map<String, TypeDefinition> types) {
3645
Map<String, TsModule> modules = new HashMap<>();
@@ -44,6 +53,16 @@ public Stream<Result<String>> consume(Map<String, TypeDefinition> types) {
4453
StringBuilder out = outputs.computeIfAbsent(basePkg, key -> new StringBuilder());
4554
module.write(types, out);
4655
}
56+
57+
// If requested, generate index.d.ts that references other files
58+
if (buildIndex) {
59+
StringBuilder index = new StringBuilder("// auto-generated references to packages\n");
60+
for (String pkg : outputs.keySet()) {
61+
index.append("/// <reference path='").append(pkg).append(".d.ts").append("' />\n");
62+
}
63+
outputs.put("index", index);
64+
}
65+
4766
return outputs.entrySet().stream().map(entry
4867
-> new Result<>(entry.getKey() + ".d.ts", entry.getValue().toString()));
4968
}

src/main/java/io/github/bensku/tsbind/cli/Args.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.List;
55

66
import com.beust.jcommander.Parameter;
7+
import com.google.common.base.Function;
78

89
import io.github.bensku.tsbind.AstConsumer;
910
import io.github.bensku.tsbind.JsonEmitter;
@@ -12,21 +13,21 @@
1213
public class Args {
1314

1415
public enum OutputFormat {
15-
JSON(new JsonEmitter()),
16-
TS_TYPES(new BindingGenerator());
16+
JSON((args) -> new JsonEmitter()),
17+
TS_TYPES((args) -> new BindingGenerator(args.index));
1718

18-
public final AstConsumer<String> consumer;
19+
public final Function<Args, AstConsumer<String>> consumerSource;
1920

20-
OutputFormat(AstConsumer<String> consumer) {
21-
this.consumer = consumer;
21+
OutputFormat(Function<Args, AstConsumer<String>> consumer) {
22+
this.consumerSource = consumer;
2223
}
2324
}
2425

2526
@Parameter(names = "--format")
2627
public OutputFormat format = OutputFormat.TS_TYPES;
2728

2829
@Parameter(names = "--in")
29-
public Path inputPath;
30+
public Path in;
3031

3132
@Parameter(names = "--symbols")
3233
public List<Path> symbols = List.of();
@@ -50,8 +51,11 @@ public enum OutputFormat {
5051
public List<String> blacklist = List.of();
5152

5253
@Parameter(names = "--out")
53-
public Path outDir = Path.of("");
54+
public Path out = Path.of("");
5455

5556
@Parameter(names = "--packageJson")
5657
public Path packageJson;
58+
59+
@Parameter(names = "--index")
60+
public boolean index;
5761
}

src/main/java/io/github/bensku/tsbind/cli/BindGenApp.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public Path read(JsonReader in) throws IOException {
6161
MavenResolver resolver = new MavenResolver(args.repo);
6262
inputPath = resolver.downloadSources(args.artifact);
6363
} else {
64-
inputPath = args.inputPath;
64+
inputPath = args.in;
6565
}
6666

6767
// Create path to root of input files we have
@@ -79,7 +79,7 @@ public Path read(JsonReader in) throws IOException {
7979
// Walk over input Java source files
8080
List<String> include = args.include;
8181
List<String> exclude = args.exclude;
82-
Path outDir = args.outDir;
82+
Path outDir = args.out;
8383
try (Stream<Path> files = Files.walk(inputDir)
8484
.filter(Files::isRegularFile)
8585
.filter(f -> f.getFileName().toString().endsWith(".java"))
@@ -99,7 +99,8 @@ public Path read(JsonReader in) throws IOException {
9999
}).map(astGenerator::parseType)
100100
.flatMap(Optional::stream).forEach(type -> types.put(type.name(), type));
101101

102-
Stream<Result<String>> results = args.format.consumer.consume(types);
102+
Stream<Result<String>> results = args.format.consumerSource.apply(args)
103+
.consume(types);
103104
results.forEach(result -> {
104105
try {
105106
Files.writeString(outDir.resolve(result.name), result.result);

0 commit comments

Comments
 (0)