Skip to content

Commit 9e84d6e

Browse files
feat: Java support (#2)
Add java support
1 parent dd95f7a commit 9e84d6e

10 files changed

Lines changed: 701 additions & 7 deletions

File tree

README.md

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,73 @@ The output directory will include a python module that corresponds to the
6767
original module. This code depends on the following python modules:
6868

6969
- [jsii](https://pypi.org/project/jsii/)
70-
- [publication](https://pypi.org/project/publication/)
70+
71+
### Java Output
72+
73+
To produce a Java module from your source, use the `java` option:
74+
75+
```ts
76+
await srcmak('srcdir', {
77+
java: {
78+
outdir: '/path/to/project/root',
79+
package: 'hello.world'
80+
}
81+
});
82+
```
83+
84+
Or the `--java-*` switches in the CLI:
85+
86+
```bash
87+
$ jsii-srcmak /src/dir --java-outdir=dir --java-package=hello.world
88+
```
89+
90+
* The `outdir`/`--java-outdir` option points to the root directory of your Java project.
91+
* The `package`/`--java-package` option is the java package name.
92+
93+
The output directory will include a java module that corresponds to the
94+
original module. This code depends on the following maven package (should be defined directly or indirectly in the project's `pom.xml` file):
95+
96+
- [jsii](https://mvnrepository.com/artifact/software.amazon.jsii)
97+
98+
The output directory will also include a tarbell `generated@0.0.0.jsii.tgz` that must be bundled in your project. Here is example snippet of how your `pom.xml` can take a dependency on these sources:
99+
100+
1. Using the `build-helper-maven-plugin` generate source files for your import.
101+
102+
```xml
103+
<plugin>
104+
<groupId>org.codehaus.mojo</groupId>
105+
<artifactId>build-helper-maven-plugin</artifactId>
106+
<version>3.0.0</version>
107+
<executions>
108+
<execution>
109+
<phase>generate-sources</phase>
110+
<goals>
111+
<goal>add-source</goal>
112+
</goals>
113+
<configuration>
114+
<sources>
115+
<source>imports/src/main/k8s/main/java</source>
116+
</sources>
117+
</configuration>
118+
</execution>
119+
</executions>
120+
</plugin>
121+
```
122+
123+
2. Set additional classpath elements for your import.
124+
125+
```xml
126+
<plugin>
127+
<groupId>org.apache.maven.plugins</groupId>
128+
<artifactId>maven-surefire-plugin</artifactId>
129+
<version>2.12.4</version>
130+
<configuration>
131+
<additionalClasspathElements>
132+
<additionalClasspathElement>imports/src/main/k8s/main/java</additionalClasspathElement>
133+
</additionalClasspathElements>
134+
</configuration>
135+
</plugin>
136+
```
71137

72138
### Entrypoint
73139

@@ -115,6 +181,21 @@ Or through the CLI:
115181
$ jsii-srcmak /src/dir --dep node_modules/@types/node --dep node_modules/constructs
116182
```
117183

184+
## Contributing
185+
186+
To build this project, you must first generate the `package.json`:
187+
188+
```
189+
npx projen
190+
```
191+
192+
Then you can install your dependencies and build:
193+
194+
```
195+
yarn install
196+
yarn build
197+
```
198+
118199
## What's with this name?
119200

120201
It's a silly little pun that stems from another pun: jsii has `jsii-pacmak`

bin/jsii-srcmak.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ async function main() {
99
.option('jsii-path', { desc: 'write .jsii output to this path', type: 'string' })
1010
.option('python-outdir', { desc: 'python output directory (requires --python-module-name)', type: 'string' })
1111
.option('python-module-name', { desc: 'python module name', type: 'string' })
12+
.option('java-outdir', { desc: 'java output directory (requires --java-package)', type: 'string' })
13+
.option('java-package', { desc: 'the java package (namespace) to use for all generated types', type: 'string' })
1214
.showHelpOnFail(true)
1315
.help();
1416

@@ -27,6 +29,7 @@ async function main() {
2729
...parseDepOption(),
2830
...parseJsiiOptions(),
2931
...parsePythonOptions(),
32+
...parseJavaOptions(),
3033
});
3134

3235
function parseJsiiOptions() {
@@ -53,6 +56,20 @@ async function main() {
5356
}
5457
}
5558

59+
function parseJavaOptions() {
60+
const outdir = argv['java-outdir'];
61+
const packageName = argv['java-package'];
62+
if (!outdir && !packageName) { return undefined; }
63+
if (!outdir) { throw new Error('--java-outdir is required'); }
64+
if (!packageName) { throw new Error('--java-package is required'); }
65+
return {
66+
java: {
67+
outdir: outdir,
68+
package: packageName,
69+
},
70+
}
71+
}
72+
5673
function parseDepOption() {
5774
if (argv.dep?.length === 0) { return undefined; }
5875
return {

lib/compile.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ export async function compile(workdir: string, options: Options) {
7070
};
7171
}
7272

73+
if (options.java) {
74+
targets.java = {
75+
package: options.java.package,
76+
maven: {
77+
groupId: 'generated',
78+
artifactId: 'generated',
79+
},
80+
};
81+
}
82+
7383
await fs.writeFile(path.join(workdir, 'package.json'), JSON.stringify(pkg, undefined, 2));
7484

7585
await exec(compilerModule, args, {

lib/options.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ export interface Options {
2323
* @default - python is not generated
2424
*/
2525
python?: PythonOutputOptions;
26+
27+
/**
28+
* Produces java code under src/main/yourpackage/main/java/yourpackage/
29+
*
30+
* @default - java is not generated
31+
*/
32+
java?: JavaOutputOptions;
2633
}
2734

2835
export interface JsiiOutputOptions {
@@ -43,3 +50,15 @@ export interface PythonOutputOptions {
4350
*/
4451
moduleName: string;
4552
}
53+
54+
export interface JavaOutputOptions {
55+
/**
56+
* Base root directory.
57+
*/
58+
outdir: string;
59+
60+
/**
61+
* The name of the java package to generate
62+
*/
63+
package: string;
64+
}

lib/srcmak.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,12 @@ export async function srcmak(srcdir: string, options: Options = { }) {
3333
const target = path.join(options.python.outdir, reldir);
3434
await fs.move(source, target, { overwrite: true });
3535
}
36+
37+
if (options.java) {
38+
const reldir = options.java.package.replace(/\./g, '/');
39+
const source = path.resolve(path.join(workdir, 'dist/java/src/'));
40+
const target = path.join(options.java.outdir, 'src/main', reldir);
41+
await fs.move(source, target, { overwrite: true });
42+
}
3643
});
3744
}

0 commit comments

Comments
 (0)