Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,45 @@ original module. This code depends on the following NuGet package (It is already

The output directory will also include a tarball `[email protected]` that must be bundled in your project (It is already included as an embedded resource in the generated project).

### Go Output

To produce a Go module from your source, use the `golang` option:

```ts
await srcmak('srcdir', {
golang: {
outdir: '/path/to/project/root',
moduleName: 'github.com/yourorg/your-root-project',
packageName: 'helloworld'
}
});
```

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

```bash
$ jsii-srcmak /src/dir --golang-outdir=dir --golang-module="github.com/yourorg/your-root-project" --golang-package="helloworld"
```

* The `outdir`/`--golang-outdir` option points to the root directory of your base Go project (where your `go.mod` is in, if you have one).
* The `moduleName`/`--golang-module` option must match the Go module name of the project that includes the generated source code e.g. `github.com/yourorg/your-root-project`. This is currently required, because the generated code needs to reference a submodule which is generated in a nested directory (see also upstream issue https://github.com/aws/jsii/issues/2847 for more information).
* The `packageName`/`--golang-package` is the package in which the generated Go code will be in. It will be placed in the submodule. So the import path becomes e.g. `github.com/yourorg/your-root-project/yourpackage`.

The output directory will include a directory named with the `packageName`/`--golang-package` containing the generated Go code.
This code depends on the following Go module:

- [jsii-runtime-go](github.com/aws/jsii-runtime-go)

which you need to include in your `go.mod`:
```
require github.com/aws/jsii-runtime-go v1.29.0 # update the version to match the jsii version used in your version of jsii-srcmak
```


#### Nested output directories
It is possible to set the `outdir`/`--golang-outdir` option to a directory other than the root of your base Go project. For example, if you want to nest the generated code in a folder `generated` or similar.
In that case you need to append the subdirectory to the module name (e.g. `github.com/yourorg/your-root-project/generated`). Your import path will then become e.g. `github.com/yourorg/your-root-project/generated/yourpackage`.

### Entrypoint

The `entrypoint` option can be used to customize the name of the typescript entrypoint (default is `index.ts`).
Expand Down
21 changes: 21 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ async function main() {
.option('java-package', { desc: 'the java package (namespace) to use for all generated types', type: 'string' })
.option('csharp-outdir', { desc: 'C# output directory (requires --csharp-namespace)', type: 'string' })
.option('csharp-namespace', { desc: 'the C# namespace to use for all generated types', type: 'string' })
.option('golang-outdir', { desc: 'golang output directory (requires --golang-module)', type: 'string' })
.option('golang-module', { desc: 'the golang module to use for all generated types', type: 'string' })
.option('golang-package', { desc: 'the golang package name to use for all generated types', type: 'string' })
.showHelpOnFail(true)
.help();

Expand All @@ -33,6 +36,7 @@ async function main() {
...parsePythonOptions(),
...parseJavaOptions(),
...parseCSharpOptions(),
...parseGoLangOptions(),
});

function parseJsiiOptions() {
Expand Down Expand Up @@ -87,6 +91,23 @@ async function main() {
};
}

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

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

if (options.golang) {
targets.go = {
moduleName: options.golang.moduleName,
packageName: options.golang.packageName,
};
}

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

await exec(compilerModule, args, {
Expand Down
33 changes: 33 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ export interface Options {
* @default - C# is not generated
*/
csharp?: CSharpOutputOptions;

/**
* Produces Golang code.
*
* @default - go is not generated
*/
golang?: GoLangOutputOptions;
}

export interface JsiiOutputOptions {
Expand Down Expand Up @@ -99,3 +106,29 @@ export interface CSharpOutputOptions {
*/
namespace: string;
}

export interface GoLangOutputOptions {
/**
* Base root directory.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not accurate in case users want output to go to a nested directory

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some more information here aswell ✅

*/
outdir: string;

/**
* The go module name
*
* This must match the name of the parent go module the source code is generated in (e.g. github.com/yourorg/yourproject).
* See https://github.com/aws/jsii/issues/2847#issue-896419111 for the background on why this is required.
*
* This must follow standard Go module name conventions.
* For example, it cannot include an underscore ('_') or be camelCased
*/
moduleName: string;

/**
* The name of the Go package.
*
* E.g. "tools" would result in something like github.com/yourorg/yourproject/tools
* depeding on the supplied moduleName
*/
packageName: string;
}
10 changes: 10 additions & 0 deletions src/srcmak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,15 @@ export async function srcmak(srcdir: string, options: Options = { }) {
const target = path.join(options.csharp.outdir, reldir);
await fs.move(source, target, { overwrite: true });
}

if (options.golang) {
const reldir = options.golang.packageName;
const source = path.resolve(path.join(workdir, 'dist/go/', reldir));
const target = path.join(options.golang.outdir, reldir);
await fs.move(source, target, { overwrite: true });
// remove go.mod as this would make it a submodule
// awaits https://github.com/aws/jsii/issues/2848
await fs.remove(path.join(target, 'go.mod'));
}
});
}
4 changes: 4 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,8 @@ export function validateOptions(options: Options) {
if (options.csharp?.namespace.includes('-')) {
throw new Error(`C# namespace [${options.csharp.namespace}] may not contain "-"`);
}

if (options.golang?.moduleName.includes('_')) {
throw new Error(`Go module name [${options.golang.moduleName}] may not contain "_"`);
}
}
Loading