Skip to content

Commit

Permalink
chore: Handle module imports across languages
Browse files Browse the repository at this point in the history
  • Loading branch information
mutahhir committed Jun 22, 2023
1 parent dc8af2c commit 420ae5f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ class MyConvertedCode(constructs.Construct):
"import imports.aws.rdsCluster as RdsCluster"
);
});

it("fixes module imports", () => {
const code = `import ...gen.modules.hello_module as HelloModule`;

expect(replacePythonImports(code)).toEqual(
"import imports.hello_module as HelloModule"
);
});
});

describe("replaceCsharpImports", () => {
Expand All @@ -57,6 +65,16 @@ class MyConvertedCode(constructs.Construct):

expect(replaceCsharpImports(code)).toEqual("using aws.RdsCluster;");
});

it("fixes module imports", () => {
const code = `using Gen.Modules.HelloModule`;

expect(replaceCsharpImports(code)).toEqual("using HelloModule");

const anotherCode = `using Gen.Modules.Hello.Module`;

expect(replaceCsharpImports(anotherCode)).toEqual("using Hello.Module");
});
});

describe("replaceJavaImports", () => {
Expand All @@ -81,6 +99,20 @@ class MyConvertedCode(constructs.Construct):
"import imports.aws.s3Bucket.S3Bucket;"
);
});

it("fixes module imports", () => {
const code = `import gen.modules.hello.module.*;`;

expect(replaceJavaImports(code)).toEqual(
"import imports.hello.module.*;"
);

const anotherCode = `import gen.modules.helloModule.*;`;

expect(replaceJavaImports(anotherCode)).toEqual(
"import imports.helloModule.*;"
);
});
});

describe("replaceGoImports", () => {
Expand All @@ -99,5 +131,14 @@ class MyConvertedCode(constructs.Construct):
`import "cdk.tf/go/stack/generated/aws/dbInstance"`
);
});

it("fixes module imports", () => {
const code = `import helloModule "github.com/aws-samples/dummy/gen/modules/hello_module"`;

expect(replaceGoImports(code)).toEqual(
// "github.com/hashicorp/terraform-cdk/examples/go/documentation/generated/terraform-aws-modules/aws/vpc"
`import helloModule "cdk.tf/go/stack/generated/hello_module"`
);
});
});
});
27 changes: 27 additions & 0 deletions packages/@cdktf/hcl2cdk/lib/jsii-rosetta-workarounds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export function replacePythonImports(code: string) {
return line.replace("import ...gen.providers.", "import imports.");
}

// Replace modules
if (line.startsWith("import ...gen.modules.")) {
return line.replace("import ...gen.modules.", "import imports.");
}

return line;
})
.join("\n");
Expand All @@ -54,6 +59,14 @@ export function replaceJavaImports(code: string) {
return `import imports.${provider}.${resource}.*;`;
}

// Replace using lines
const importModules = /import gen\.modules\.(.+)\.\*;/;
const importModulesMatch = line.match(importModules);
if (importModulesMatch) {
const [, module] = importModulesMatch;
return `import imports.${module}.*;`;
}

return line;
})
.join("\n");
Expand Down Expand Up @@ -81,6 +94,12 @@ export function replaceCsharpImports(code: string) {
.toLocaleLowerCase()}${importLine.substring(1)}`;
}

if (line.startsWith("using Gen.Modules.")) {
const importLine = line.replace("using Gen.Modules.", "");

return `using ${importLine}`;
}

return line;
})
.join("\n");
Expand Down Expand Up @@ -108,6 +127,14 @@ export function replaceGoImports(code: string) {
return `import "cdk.tf/go/stack/generated/${provider}/${resource}"`;
}

const importModulesRegex =
/import (.*) \"github.com\/aws-samples\/dummy\/gen\/modules\/(.*)\"/;
const matchModules = line.match(importModulesRegex);
if (matchModules) {
const [, name, module] = matchModules;
return `import ${name} "cdk.tf/go/stack/generated/${module}"`;
}

return line;
})
.join("\n");
Expand Down

0 comments on commit 420ae5f

Please sign in to comment.