Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: .NET Support #73

Merged
merged 5 commits into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,35 @@ original module. This code depends on the following maven package (should be def

The output directory will also include a tarball `[email protected]` that must be bundled in your project.

### .NET Output

To produce a .NET module from your source, use the `dotnet` option:

```ts
await srcmak('srcdir', {
dotnet: {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd go with csharp here since this is producing source code and not a library (like Pacmak)

outdir: '/path/to/project/root',
namespace: 'HelloWorld'
}
});
```

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

```bash
$ jsii-srcmak /src/dir --dotnet-outdir=dir --dotnet-namespace=HelloWorld
```

* The `outdir`/`--dotnet-outdir` option points to the root directory of your .NET project.
* The `package`/`--dotnet-namespace` option is the .NET root namespace.

The output directory will include a .NET project that corresponds to the
original module. This code depends on the following NuGet package (It is already defined as a dependency in the generated project):

- [jsii](https://www.nuget.org/packages/Amazon.JSII.Runtime/)

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).

### Entrypoint

The `entrypoint` option can be used to customize the name of the typescript entrypoint (default is `index.ts`).
Expand Down
17 changes: 17 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ async function main() {
.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' })
.option('dotnet-outdir', { desc: 'dotnet output directory (requires --dotnet-namespace)', type: 'string' })
.option('dotnet-namespace', { desc: 'the dotnet namespace to use for all generated types', type: 'string' })
.showHelpOnFail(true)
.help();

Expand All @@ -30,6 +32,7 @@ async function main() {
...parseJsiiOptions(),
...parsePythonOptions(),
...parseJavaOptions(),
...parseDotnetOptions(),
});

function parseJsiiOptions() {
Expand Down Expand Up @@ -70,6 +73,20 @@ async function main() {
}
}

function parseDotnetOptions() {
const outdir = argv['dotnet-outdir'];
const namespace = argv['dotnet-namespace'];
if (!outdir && !namespace) { return undefined; }
if (!outdir) { throw new Error('--dotnet-outdir is required'); }
if (!namespace) { throw new Error('--dotnet-namespace is required'); }
return {
dotnet: {
outdir: outdir,
namespace: namespace,
},
}
}

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 @@ -84,6 +84,13 @@ export async function compile(workdir: string, options: Options) {
};
}

if (options.dotnet) {
targets.dotnet = {
namespace: options.dotnet.namespace,
packageId: options.dotnet.namespace,
};
}

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

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

/**
* Produces dotnet code.
*
* @default - dotnet is not generated
*/
dotnet?: DotnetOutputOptions;
}

export interface JsiiOutputOptions {
Expand Down Expand Up @@ -77,3 +84,18 @@ export interface JavaOutputOptions {
*/
package: string;
}

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

/**
* The root namespace to generate types in
*
* This must follow standard dotnet namespace conventions.
* For example, it cannot include a hyphen ('-')
*/
namespace: string;
}
7 changes: 7 additions & 0 deletions src/srcmak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,12 @@ export async function srcmak(srcdir: string, options: Options = { }) {
await fs.mkdirp(target); // make sure target directory exists
await ncp(source, target, { clobber: false });
}

if (options.dotnet) {
const reldir = options.dotnet.namespace;
const source = path.resolve(path.join(workdir, 'dist/dotnet/', reldir));
const target = path.join(options.dotnet.outdir, reldir);
await fs.move(source, target, { overwrite: true });
Copy link
Contributor

Choose a reason for hiding this comment

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

How does dotnet package dependencies work? Just wondering if we should do it similarly to java with:

await ncp(source, target, { clobber: false });

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it makes sense as is since each module generates a standalone project. Applications would then have project dependencies on the generated projects.

Copy link
Contributor

Choose a reason for hiding this comment

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

Great!

}
});
}
6 changes: 5 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export async function exec(moduleName: string, args: string[] = [], options: Spa
}

/**
* This validates that the Python module name and Java package name
* This validates that the Python module name, Java package name, and Dotnet namespace
* conform to language-specific constraints.
*
* @param options Options set by the consumer
Expand All @@ -72,4 +72,8 @@ export function validateOptions(options: Options) {
if (options.java?.package.includes('-')) {
throw new Error(`Java package [${options.java.package}] may not contain "-"`);
}

if (options.dotnet?.namespace.includes('-')) {
throw new Error(`Dotnet namespace [${options.dotnet.namespace}] may not contain "-"`);
}
}
Binary file modified test/__snapshots__/cli.test.ts.snap
Binary file not shown.
Loading