Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
8 changes: 5 additions & 3 deletions src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as fs from 'fs-extra';
import * as path from 'path';
import { exec } from './util';
import { Options } from './options';
import * as crypto from 'crypto';

const compilerModule = require.resolve('jsii/bin/jsii');

Expand All @@ -23,6 +24,8 @@ export async function compile(workdir: string, options: Options) {
// path to entrypoint without extension
const basepath = path.join(path.dirname(entrypoint), path.basename(entrypoint, '.ts'));

const packageName = options.packageName?.replace(/\./g, '').replace(/\//g, '') ?? crypto.createHash('sha256').update(basepath, 'utf8').digest('hex');

// jsii modules to include
const moduleDirs = options.deps ?? [];

Expand All @@ -46,9 +49,8 @@ export async function compile(workdir: string, options: Options) {
}
}


const pkg = {
name: 'generated',
name: packageName,
version: '0.0.0',
author: 'generated@generated.com',
main: `${basepath}.js`,
Expand All @@ -66,7 +68,7 @@ export async function compile(workdir: string, options: Options) {
if (options.python) {
targets.python = {
distName: 'generated',
module: options.python.moduleName,
module: options.python.moduleName.replace(/-/g, '_'),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I am assuming java would have a similar issue. I wonder if the correct behavior here is to validate per the language constraints and throw an error so the user will be required to pass in a valid name for the module.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah! We can do some validation instead of just correcting it. I'd be worried in some cases though where the user isn't the one explicitly choosing what this value is. In some use-cases, like CRDs in cdk8s from the internet, they might not control the group.

Would we then add this correction behavior to all consumers?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Maybe we can do a warn that lets the user know we're correcting it?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think the consumers of this library (eg cdk8s) should do the name mangling if needed. The constraints should be well documented

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok! I will add validation here then

};
}

Expand Down
6 changes: 6 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ export interface Options {
*/
jsii?: JsiiOutputOptions;

/**
* Package name
* @default - hash of the basepath to the module
*/
packageName?: string
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I am not sure that's the correct name for this because users don't really interact with this value after code generation. Maybe moduleKey or something like that.

Docstring help needs to indicate that it should be project-unique.


/**
* Produce python code.
* @default - python is not generated
Expand Down
2 changes: 1 addition & 1 deletion src/srcmak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export async function srcmak(srcdir: string, options: Options = { }) {

// extract code based on selected languages
if (options.python) {
const reldir = options.python.moduleName.replace(/\./g, '/'); // jsii replaces "." with "/"
const reldir = options.python.moduleName.replace(/\./g, '/').replace(/-/g, '_'); // jsii replaces "." with "/"
const source = path.resolve(path.join(workdir, 'dist/python/src', reldir));
const target = path.join(options.python.outdir, reldir);
await fs.move(source, target, { overwrite: true });
Expand Down
75 changes: 45 additions & 30 deletions test/__snapshots__/cli.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public final class $Module extends JsiiModule {
private final Map<String, Class<?>> cache = new HashMap<>();

public $Module() {
super(\\"generated\\", \\"0.0.0\\", $Module.class, \\"generated@0.0.0.jsii.tgz\\");
super(\\"4c6b576fbe7e27053874813d9754cb2e46811d806c1e0aa9aae8add4c3763060\\", \\"0.0.0\\", $Module.class, \\"4c6b576fbe7e27053874813d9754cb2e46811d806c1e0aa9aae8add4c3763060@0.0.0.jsii.tgz\\");
}

@Override
Expand Down Expand Up @@ -76,7 +76,7 @@ public final class $Module extends JsiiModule {
* A sophisticaed multi-language calculator.
*/

@software.amazon.jsii.Jsii(module = mypackage.$Module.class, fqn = \\"generated.Calculator\\")
@software.amazon.jsii.Jsii(module = mypackage.$Module.class, fqn = \\"4c6b576fbe7e27053874813d9754cb2e46811d806c1e0aa9aae8add4c3763060.Calculator\\")
public class Calculator extends software.amazon.jsii.JsiiObject {

protected Calculator(final software.amazon.jsii.JsiiObjectRef objRef) {
Expand Down Expand Up @@ -126,7 +126,7 @@ public class Calculator extends software.amazon.jsii.JsiiObject {
* Math operands.
*/

@software.amazon.jsii.Jsii(module = mypackage.$Module.class, fqn = \\"generated.Operands\\")
@software.amazon.jsii.Jsii(module = mypackage.$Module.class, fqn = \\"4c6b576fbe7e27053874813d9754cb2e46811d806c1e0aa9aae8add4c3763060.Operands\\")
@software.amazon.jsii.Jsii.Proxy(Operands.Jsii$Proxy.class)
public interface Operands extends software.amazon.jsii.JsiiSerializable {

Expand Down Expand Up @@ -229,7 +229,7 @@ public interface Operands extends software.amazon.jsii.JsiiSerializable {
data.set(\\"rhs\\", om.valueToTree(this.getRhs()));

final com.fasterxml.jackson.databind.node.ObjectNode struct = com.fasterxml.jackson.databind.node.JsonNodeFactory.instance.objectNode();
struct.set(\\"fqn\\", om.valueToTree(\\"generated.Operands\\"));
struct.set(\\"fqn\\", om.valueToTree(\\"4c6b576fbe7e27053874813d9754cb2e46811d806c1e0aa9aae8add4c3763060.Operands\\"));
struct.set(\\"data\\", data);

final com.fasterxml.jackson.databind.node.ObjectNode obj = com.fasterxml.jackson.databind.node.JsonNodeFactory.instance.objectNode();
Expand Down Expand Up @@ -258,8 +258,8 @@ public interface Operands extends software.amazon.jsii.JsiiSerializable {
}
}
",
"src/main/resources/mypackage/$Module.txt": "generated.Calculator=mypackage.Calculator
generated.Operands=mypackage.Operands
"src/main/resources/mypackage/$Module.txt": "4c6b576fbe7e27053874813d9754cb2e46811d806c1e0aa9aae8add4c3763060.Calculator=mypackage.Calculator
4c6b576fbe7e27053874813d9754cb2e46811d806c1e0aa9aae8add4c3763060.Operands=mypackage.Operands
",
}
`;
Expand All @@ -272,29 +272,29 @@ Object {
"author",
],
},
"description": "generated",
"fingerprint": "+fSgo9PQwFAKmX2JcaRILwTXE/p1H1bq+QKuOREAfuE=",
"description": "4c6b576fbe7e27053874813d9754cb2e46811d806c1e0aa9aae8add4c3763060",
"fingerprint": "ViAfgJbT/zOiPr7YHbv+SHTXd1cpQsg/Zcvu6QJ9p5Y=",
"homepage": "http://generated",
"jsiiVersion": "1.6.0 (build 248e75b)",
"jsiiVersion": "1.9.0 (build 5c646d5)",
"license": "Apache-2.0",
"name": "generated",
"name": "4c6b576fbe7e27053874813d9754cb2e46811d806c1e0aa9aae8add4c3763060",
"repository": Object {
"type": "git",
"url": "http://generated",
},
"schema": "jsii/0.10.0",
"targets": Object {
"js": Object {
"npm": "generated",
"npm": "4c6b576fbe7e27053874813d9754cb2e46811d806c1e0aa9aae8add4c3763060",
},
},
"types": Object {
"generated.Calculator": Object {
"assembly": "generated",
"4c6b576fbe7e27053874813d9754cb2e46811d806c1e0aa9aae8add4c3763060.Calculator": Object {
"assembly": "4c6b576fbe7e27053874813d9754cb2e46811d806c1e0aa9aae8add4c3763060",
"docs": Object {
"summary": "A sophisticaed multi-language calculator.",
},
"fqn": "generated.Calculator",
"fqn": "4c6b576fbe7e27053874813d9754cb2e46811d806c1e0aa9aae8add4c3763060.Calculator",
"initializer": Object {},
"kind": "class",
"locationInModule": Object {
Expand All @@ -318,7 +318,7 @@ Object {
},
"name": "ops",
"type": Object {
"fqn": "generated.Operands",
"fqn": "4c6b576fbe7e27053874813d9754cb2e46811d806c1e0aa9aae8add4c3763060.Operands",
},
},
],
Expand All @@ -344,7 +344,7 @@ Object {
},
"name": "ops",
"type": Object {
"fqn": "generated.Operands",
"fqn": "4c6b576fbe7e27053874813d9754cb2e46811d806c1e0aa9aae8add4c3763060.Operands",
},
},
],
Expand All @@ -370,7 +370,7 @@ Object {
},
"name": "ops",
"type": Object {
"fqn": "generated.Operands",
"fqn": "4c6b576fbe7e27053874813d9754cb2e46811d806c1e0aa9aae8add4c3763060.Operands",
},
},
],
Expand All @@ -383,13 +383,13 @@ Object {
],
"name": "Calculator",
},
"generated.Operands": Object {
"assembly": "generated",
"4c6b576fbe7e27053874813d9754cb2e46811d806c1e0aa9aae8add4c3763060.Operands": Object {
"assembly": "4c6b576fbe7e27053874813d9754cb2e46811d806c1e0aa9aae8add4c3763060",
"datatype": true,
"docs": Object {
"summary": "Math operands.",
},
"fqn": "generated.Operands",
"fqn": "4c6b576fbe7e27053874813d9754cb2e46811d806c1e0aa9aae8add4c3763060.Operands",
"kind": "interface",
"locationInModule": Object {
"filename": "lib/main.ts",
Expand Down Expand Up @@ -449,8 +449,12 @@ import publication
from ._jsii import *


class Calculator(metaclass=jsii.JSIIMeta, jsii_type=\\"generated.Calculator\\"):
class Calculator(
metaclass=jsii.JSIIMeta,
jsii_type=\\"4c6b576fbe7e27053874813d9754cb2e46811d806c1e0aa9aae8add4c3763060.Calculator\\",
):
\\"\\"\\"A sophisticaed multi-language calculator.\\"\\"\\"

def __init__(self) -> None:
jsii.create(Calculator, self, [])

Expand Down Expand Up @@ -488,28 +492,32 @@ class Calculator(metaclass=jsii.JSIIMeta, jsii_type=\\"generated.Calculator\\"):
return jsii.invoke(self, \\"sub\\", [ops])


@jsii.data_type(jsii_type=\\"generated.Operands\\", jsii_struct_bases=[], name_mapping={'lhs': 'lhs', 'rhs': 'rhs'})
class Operands():
@jsii.data_type(
jsii_type=\\"4c6b576fbe7e27053874813d9754cb2e46811d806c1e0aa9aae8add4c3763060.Operands\\",
jsii_struct_bases=[],
name_mapping={\\"lhs\\": \\"lhs\\", \\"rhs\\": \\"rhs\\"},
)
class Operands:
def __init__(self, *, lhs: jsii.Number, rhs: jsii.Number) -> None:
\\"\\"\\"Math operands.

:param lhs: Left-hand side operand.
:param rhs: Right-hand side operand.
\\"\\"\\"
self._values = {
'lhs': lhs,
'rhs': rhs,
\\"lhs\\": lhs,
\\"rhs\\": rhs,
}

@builtins.property
def lhs(self) -> jsii.Number:
\\"\\"\\"Left-hand side operand.\\"\\"\\"
return self._values.get('lhs')
return self._values.get(\\"lhs\\")

@builtins.property
def rhs(self) -> jsii.Number:
\\"\\"\\"Right-hand side operand.\\"\\"\\"
return self._values.get('rhs')
return self._values.get(\\"rhs\\")

def __eq__(self, rhs) -> bool:
return isinstance(rhs, self.__class__) and rhs._values == self._values
Expand All @@ -518,7 +526,9 @@ class Operands():
return not (rhs == self)

def __repr__(self) -> str:
return 'Operands(%s)' % ', '.join(k + '=' + repr(v) for k, v in self._values.items())
return \\"Operands(%s)\\" % \\", \\".join(
k + \\"=\\" + repr(v) for k, v in self._values.items()
)


__all__ = [
Expand All @@ -528,7 +538,7 @@ __all__ = [

publication.publish()
",
"my/python/module/_jsii/__init__.py": "import abc
"my/python/module/_jsii/__init__.py": "import abc
import builtins
import datetime
import enum
Expand All @@ -538,7 +548,12 @@ import jsii
import jsii.compat
import publication

__jsii_assembly__ = jsii.JSIIAssembly.load(\\"generated\\", \\"0.0.0\\", __name__[0:-6], \\"generated@0.0.0.jsii.tgz\\")
__jsii_assembly__ = jsii.JSIIAssembly.load(
\\"4c6b576fbe7e27053874813d9754cb2e46811d806c1e0aa9aae8add4c3763060\\",
\\"0.0.0\\",
__name__[0:-6],
\\"4c6b576fbe7e27053874813d9754cb2e46811d806c1e0aa9aae8add4c3763060@0.0.0.jsii.tgz\\",
)

__all__ = [
\\"__jsii_assembly__\\",
Expand Down
Loading