Skip to content

Commit

Permalink
feat: Java support (#2)
Browse files Browse the repository at this point in the history
Add java support
  • Loading branch information
campionfellin authored Jun 2, 2020
1 parent dd95f7a commit 9e84d6e
Show file tree
Hide file tree
Showing 10 changed files with 701 additions and 7 deletions.
83 changes: 82 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,73 @@ The output directory will include a python module that corresponds to the
original module. This code depends on the following python modules:

- [jsii](https://pypi.org/project/jsii/)
- [publication](https://pypi.org/project/publication/)

### Java Output

To produce a Java module from your source, use the `java` option:

```ts
await srcmak('srcdir', {
java: {
outdir: '/path/to/project/root',
package: 'hello.world'
}
});
```

Or the `--java-*` switches in the CLI:

```bash
$ jsii-srcmak /src/dir --java-outdir=dir --java-package=hello.world
```

* The `outdir`/`--java-outdir` option points to the root directory of your Java project.
* The `package`/`--java-package` option is the java package name.

The output directory will include a java module that corresponds to the
original module. This code depends on the following maven package (should be defined directly or indirectly in the project's `pom.xml` file):

- [jsii](https://mvnrepository.com/artifact/software.amazon.jsii)

The output directory will also include a tarbell `[email protected]` that must be bundled in your project. Here is example snippet of how your `pom.xml` can take a dependency on these sources:

1. Using the `build-helper-maven-plugin` generate source files for your import.

```xml
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>imports/src/main/k8s/main/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
```

2. Set additional classpath elements for your import.

```xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>imports/src/main/k8s/main/java</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
```

### Entrypoint

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

## Contributing

To build this project, you must first generate the `package.json`:

```
npx projen
```

Then you can install your dependencies and build:

```
yarn install
yarn build
```

## What's with this name?

It's a silly little pun that stems from another pun: jsii has `jsii-pacmak`
Expand Down
17 changes: 17 additions & 0 deletions bin/jsii-srcmak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ async function main() {
.option('jsii-path', { desc: 'write .jsii output to this path', type: 'string' })
.option('python-outdir', { desc: 'python output directory (requires --python-module-name)', type: 'string' })
.option('python-module-name', { desc: 'python module name', type: 'string' })
.option('java-outdir', { desc: 'java output directory (requires --java-package)', type: 'string' })
.option('java-package', { desc: 'the java package (namespace) to use for all generated types', type: 'string' })
.showHelpOnFail(true)
.help();

Expand All @@ -27,6 +29,7 @@ async function main() {
...parseDepOption(),
...parseJsiiOptions(),
...parsePythonOptions(),
...parseJavaOptions(),
});

function parseJsiiOptions() {
Expand All @@ -53,6 +56,20 @@ async function main() {
}
}

function parseJavaOptions() {
const outdir = argv['java-outdir'];
const packageName = argv['java-package'];
if (!outdir && !packageName) { return undefined; }
if (!outdir) { throw new Error('--java-outdir is required'); }
if (!packageName) { throw new Error('--java-package is required'); }
return {
java: {
outdir: outdir,
package: packageName,
},
}
}

function parseDepOption() {
if (argv.dep?.length === 0) { return undefined; }
return {
Expand Down
10 changes: 10 additions & 0 deletions lib/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ export async function compile(workdir: string, options: Options) {
};
}

if (options.java) {
targets.java = {
package: options.java.package,
maven: {
groupId: 'generated',
artifactId: 'generated',
},
};
}

await fs.writeFile(path.join(workdir, 'package.json'), JSON.stringify(pkg, undefined, 2));

await exec(compilerModule, args, {
Expand Down
19 changes: 19 additions & 0 deletions lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ export interface Options {
* @default - python is not generated
*/
python?: PythonOutputOptions;

/**
* Produces java code under src/main/yourpackage/main/java/yourpackage/
*
* @default - java is not generated
*/
java?: JavaOutputOptions;
}

export interface JsiiOutputOptions {
Expand All @@ -43,3 +50,15 @@ export interface PythonOutputOptions {
*/
moduleName: string;
}

export interface JavaOutputOptions {
/**
* Base root directory.
*/
outdir: string;

/**
* The name of the java package to generate
*/
package: string;
}
7 changes: 7 additions & 0 deletions lib/srcmak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,12 @@ export async function srcmak(srcdir: string, options: Options = { }) {
const target = path.join(options.python.outdir, reldir);
await fs.move(source, target, { overwrite: true });
}

if (options.java) {
const reldir = options.java.package.replace(/\./g, '/');
const source = path.resolve(path.join(workdir, 'dist/java/src/'));
const target = path.join(options.java.outdir, 'src/main', reldir);
await fs.move(source, target, { overwrite: true });
}
});
}
Loading

0 comments on commit 9e84d6e

Please sign in to comment.