Skip to content

Commit

Permalink
fix: Package name collisions (#22)
Browse files Browse the repository at this point in the history
* fix: Package name collisions

Fixes: cdk8s-team/cdk8s#273

Signed-off-by: campionfellin <[email protected]>

* chore: Update jsii version to 1.6

Signed-off-by: campionfellin <[email protected]>

* fix: fallback to hash for default packageName and fix hyphens in python package names

Signed-off-by: campionfellin <[email protected]>

* fix: hypens to underscores for python package names

Signed-off-by: campionfellin <[email protected]>

* chore: sync up dependencies with projen

Signed-off-by: campionfellin <[email protected]>

* chore: clean up snapshots

Signed-off-by: campionfellin <[email protected]>

* Revert "chore: sync up dependencies with projen"

This reverts commit 58e1784.

* Revert "chore: Update jsii version to 1.6"

This reverts commit 824bb84.

* fix: remove weird characters from snapshot

Signed-off-by: campionfellin <[email protected]>

* fix: back to yarn.lock from mainline

Signed-off-by: campionfellin <[email protected]>

* fix: Respond to comments

Signed-off-by: campionfellin <[email protected]>

* fix: Update test utils to ignore all jsii.tgz binary files

Signed-off-by: campionfellin <[email protected]>

* fix: update snapshots

Signed-off-by: campionfellin <[email protected]>

* fix: snapshots

Signed-off-by: campionfellin <[email protected]>

* fix: snapshots

Signed-off-by: campionfellin <[email protected]>

* fix: snapshots

Signed-off-by: campionfellin <[email protected]>

* fix: validate input options for python module name and java package

Signed-off-by: campionfellin <[email protected]>

Co-authored-by: Elad Ben-Israel <[email protected]>
  • Loading branch information
campionfellin and Elad Ben-Israel authored Aug 3, 2020
1 parent 6b1f015 commit 766de96
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 48 deletions.
10 changes: 7 additions & 3 deletions src/compile.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import * as fs from 'fs-extra';
import * as path from 'path';
import { exec } from './util';
import { exec, validateOptions } from './util';
import { Options } from './options';
import * as crypto from 'crypto';

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

/**
* Compiles the source files in `workdir` with jsii.
*/
export async function compile(workdir: string, options: Options) {
validateOptions(options);

const args = [ '--silence-warnings', 'reserved-word' ];
const entrypoint = options.entrypoint ?? 'index.ts';

Expand All @@ -23,6 +26,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 moduleKey = options.moduleKey?.replace(/\./g, '').replace(/\//g, '') ?? crypto.createHash('sha256').update(basepath, 'utf8').digest('hex');

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

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


const pkg = {
name: 'generated',
name: moduleKey,
version: '0.0.0',
author: '[email protected]',
main: `${basepath}.js`,
Expand Down
15 changes: 15 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ export interface Options {
*/
jsii?: JsiiOutputOptions;

/**
* Key for the module to prevent JSII collisions.
*
* Use your own if it's project-unique, otherwise use default.
*
* @default - hash of the basepath to the module
*/
moduleKey?: string

/**
* Produce python code.
* @default - python is not generated
Expand Down Expand Up @@ -47,6 +56,9 @@ export interface PythonOutputOptions {

/**
* The name of the the python module to generate.
*
* This must follow the standard Python module name conventions.
* For example, it cannot include a hyphen ('-')
*/
moduleName: string;
}
Expand All @@ -59,6 +71,9 @@ export interface JavaOutputOptions {

/**
* The name of the java package to generate
*
* This must follow standard Java package conventions.
* For example, it cannot include a hyphen ('-')
*/
package: string;
}
18 changes: 18 additions & 0 deletions src/util.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 * as os from 'os';
import { spawn, SpawnOptions } from 'child_process';
import { Options } from './options';

export async function mkdtemp(closure: (dir: string) => Promise<void>) {
const workdir = await fs.mkdtemp(path.join(os.tmpdir(), 'temp-'));
Expand Down Expand Up @@ -55,3 +56,20 @@ export async function exec(moduleName: string, args: string[] = [], options: Spa
});
});
}

/**
* This validates that the Python module name and Java package name
* conform to language-specific constraints.
*
* @param options Options set by the consumer
* @throws error if options do not conform
*/
export function validateOptions(options: Options) {
if (options.python?.moduleName.includes('-')) {
throw new Error(`Python moduleName [${options.python.moduleName}] may not contain "-"`);
}

if (options.java?.package.includes('-')) {
throw new Error(`Java package [${options.java.package}] may not contain "-"`);
}
}
44 changes: 22 additions & 22 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": "64TWv4C2KsEvkRVfUjD1gQTbNC2rsTlISDNv4F9JUw0=",
"homepage": "http://generated",
"jsiiVersion": "1.6.0 (build 248e75b)",
"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,7 +449,7 @@ 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,7 +488,7 @@ 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'})
@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.
Expand Down Expand Up @@ -538,7 +538,7 @@ 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
32 changes: 16 additions & 16 deletions test/__snapshots__/srcmak.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(\\"javapackage\\", \\"0.0.0\\", $Module.class, \\"javapackage@0.0.0.jsii.tgz\\");
}
@Override
Expand Down Expand Up @@ -73,7 +73,7 @@ public final class $Module extends JsiiModule {
"src/main/java/hello/world/Hello.java": "package hello.world;
@software.amazon.jsii.Jsii(module = hello.world.$Module.class, fqn = \\"generated.Hello\\")
@software.amazon.jsii.Jsii(module = hello.world.$Module.class, fqn = \\"javapackage.Hello\\")
public class Hello extends software.amazon.jsii.JsiiObject {
protected Hello(final software.amazon.jsii.JsiiObjectRef objRef) {
Expand All @@ -97,7 +97,7 @@ public class Hello extends software.amazon.jsii.JsiiObject {
"src/main/java/hello/world/Operands.java": "package hello.world;
@software.amazon.jsii.Jsii(module = hello.world.$Module.class, fqn = \\"generated.Operands\\")
@software.amazon.jsii.Jsii(module = hello.world.$Module.class, fqn = \\"javapackage.Operands\\")
@software.amazon.jsii.Jsii.Proxy(Operands.Jsii$Proxy.class)
public interface Operands extends software.amazon.jsii.JsiiSerializable {
Expand Down Expand Up @@ -194,7 +194,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(\\"javapackage.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 @@ -223,8 +223,8 @@ public interface Operands extends software.amazon.jsii.JsiiSerializable {
}
}
",
"src/main/resources/hello/world/$Module.txt": "generated.Hello=hello.world.Hello
generated.Operands=hello.world.Operands
"src/main/resources/hello/world/$Module.txt": "javapackage.Hello=hello.world.Hello
javapackage.Operands=hello.world.Operands
",
}
`;
Expand All @@ -237,26 +237,26 @@ Object {
"author",
],
},
"description": "generated",
"fingerprint": "cNtb65kyZQOiBulmzr+bs6dCGdv4uhea1tFTkbRGJTw=",
"description": "1bc04b5291c26a46d918139138b992d2de976d6851d0893b0476b85bfbdfc6e6",
"fingerprint": "i19np+HVFMpwiHENNJeGk6AC3MVvpkkG7VtDtf9ouXs=",
"homepage": "http://generated",
"jsiiVersion": "1.6.0 (build 248e75b)",
"license": "Apache-2.0",
"name": "generated",
"name": "1bc04b5291c26a46d918139138b992d2de976d6851d0893b0476b85bfbdfc6e6",
"repository": Object {
"type": "git",
"url": "http://generated",
},
"schema": "jsii/0.10.0",
"targets": Object {
"js": Object {
"npm": "generated",
"npm": "1bc04b5291c26a46d918139138b992d2de976d6851d0893b0476b85bfbdfc6e6",
},
},
"types": Object {
"generated.Foo": Object {
"assembly": "generated",
"fqn": "generated.Foo",
"1bc04b5291c26a46d918139138b992d2de976d6851d0893b0476b85bfbdfc6e6.Foo": Object {
"assembly": "1bc04b5291c26a46d918139138b992d2de976d6851d0893b0476b85bfbdfc6e6",
"fqn": "1bc04b5291c26a46d918139138b992d2de976d6851d0893b0476b85bfbdfc6e6.Foo",
"initializer": Object {},
"kind": "class",
"locationInModule": Object {
Expand Down Expand Up @@ -300,7 +300,7 @@ import publication
from ._jsii import *
class Hello(metaclass=jsii.JSIIMeta, jsii_type=\\"generated.Hello\\"):
class Hello(metaclass=jsii.JSIIMeta, jsii_type=\\"pythonpackage.Hello\\"):
def __init__(self) -> None:
jsii.create(Hello, self, [])
Expand All @@ -315,7 +315,7 @@ class Hello(metaclass=jsii.JSIIMeta, jsii_type=\\"generated.Hello\\"):
return jsii.invoke(self, \\"add\\", [ops])
@jsii.data_type(jsii_type=\\"generated.Operands\\", jsii_struct_bases=[], name_mapping={'lhs': 'lhs', 'rhs': 'rhs'})
@jsii.data_type(jsii_type=\\"pythonpackage.Operands\\", jsii_struct_bases=[], name_mapping={'lhs': 'lhs', 'rhs': 'rhs'})
class Operands():
def __init__(self, *, lhs: jsii.Number, rhs: jsii.Number) -> None:
\\"\\"\\"
Expand Down Expand Up @@ -362,7 +362,7 @@ 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(\\"pythonpackage\\", \\"0.0.0\\", __name__[0:-6], \\"pythonpackage@0.0.0.jsii.tgz\\")
__all__ = [
\\"__jsii_assembly__\\",
Expand Down
4 changes: 2 additions & 2 deletions test/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ test('python output', async () => {
);

expect(await snapshotDirectory(outdir, {
excludeFiles: [ 'generated@0.0.0.jsii.tgz' ],
excludeFiles: [ '@0.0.0.jsii.tgz' ],
})).toMatchSnapshot();
});
});
Expand All @@ -92,7 +92,7 @@ test('java output', async () => {

expect(await snapshotDirectory(outdir, {
excludeLines: [ /.*@javax.annotation.Generated.*/ ],
excludeFiles: [ 'generated@0.0.0.jsii.tgz' ],
excludeFiles: [ '@0.0.0.jsii.tgz' ],
})).toMatchSnapshot();
});
});
Expand Down
Loading

0 comments on commit 766de96

Please sign in to comment.