Skip to content

Commit 01cafc7

Browse files
DanielMSchmidtxiehan
authored andcommitted
chore: remove dependency on jsii-srcmak
1 parent 25cf0a3 commit 01cafc7

File tree

8 files changed

+202
-88
lines changed

8 files changed

+202
-88
lines changed

.github/workflows/pr-depcheck.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ jobs:
3131
- uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
3232
- name: "Run Depcheck"
3333
run: |
34-
npx lerna exec --scope '${{ matrix.package }}' -- npx -y depcheck --ignores="@types/*,jsii,jsii-pacmak,jsii-srcmak,jsii-docgen,yoga-layout-prebuilt,eslint,jest,tsc-files,typescript,esbuild,esbuild-jest,graphology-types"
34+
npx lerna exec --scope '${{ matrix.package }}' -- npx -y depcheck --ignores="@types/*,jsii,jsii-pacmak,jsii-docgen,yoga-layout-prebuilt,eslint,jest,tsc-files,typescript,esbuild,esbuild-jest,graphology-types"

.github/workflows/yarn-upgrade.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ jobs:
158158
# Upgrade all the packages
159159
lerna exec --scope='{${{ join(matrix.pr.packages, ',') }}}' ncu -- --upgrade --filter=@types/node,@types/fs-extra --target=minor
160160
lerna exec --scope='{${{ join(matrix.pr.packages, ',') }}}' ncu -- --upgrade --filter=typescript --target=patch
161-
lerna exec --scope='{${{ join(matrix.pr.packages, ',') }}}' ncu -- --upgrade --reject='@types/node,@types/fs-extra,constructs,typescript,graphology-types,jsii,jsii-srcmak,jsii-pacmak,jsii-rosetta,jsii-docgen,codemaker,${{ steps.list-packages.outputs.list }}' --target=minor
161+
lerna exec --scope='{${{ join(matrix.pr.packages, ',') }}}' ncu -- --upgrade --reject='@types/node,@types/fs-extra,constructs,typescript,graphology-types,jsii,jsii-pacmak,jsii-rosetta,jsii-docgen,codemaker,${{ steps.list-packages.outputs.list }}' --target=minor
162162
# This will ensure the current lockfile is up-to-date with the dependency specifications (necessary for "yarn upgrade" to run)
163163
- name: Run "yarn install"
164164
run: yarn install --prefer-offline
@@ -222,7 +222,7 @@ jobs:
222222
- name: Run "ncu -u"
223223
run: |-
224224
# Upgrade all the packages
225-
lerna exec ncu -- --upgrade --filter='jsii,jsii-srcmak,jsii-pacmak,jsii-rosetta,jsii-docgen,codemaker,constructs' --target=minor
225+
lerna exec ncu -- --upgrade --filter='jsii,jsii-pacmak,jsii-rosetta,jsii-docgen,codemaker,constructs' --target=minor
226226
# This will ensure the current lockfile is up-to-date with the dependency specifications (necessary for "yarn upgrade" to run)
227227
- name: Run "yarn install"
228228
run: yarn install --prefer-offline

packages/@cdktf/cli-core/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
"ink-use-stdout-dimensions": "1.0.5",
6868
"jsii": "5.3.29",
6969
"jsii-pacmak": "1.95.0",
70-
"jsii-srcmak": "0.1.1039",
7170
"lodash.isequal": "4.5.0",
7271
"log4js": "6.9.1",
7372
"minimatch": "5.1.6",

packages/@cdktf/provider-generator/lib/get/constructs-maker.ts

Lines changed: 186 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import * as fs from "fs-extra";
44
import * as path from "path";
55
import { CodeMaker } from "codemaker";
6-
import { mkdtemp } from "@cdktf/commons";
7-
import * as srcmak from "jsii-srcmak";
6+
import { exec, mkdtemp } from "@cdktf/commons";
87
import {
98
TerraformDependencyConstraint,
109
logger,
@@ -22,9 +21,59 @@ import { ModuleGenerator } from "./generator/module-generator";
2221
import { glob } from "glob";
2322
import { readSchema } from "@cdktf/provider-schema";
2423

24+
const pacmakModule = require.resolve("jsii-pacmak/bin/jsii-pacmak");
25+
const jsiiModule = require.resolve("jsii/bin/jsii");
26+
27+
export interface GenerateJSIIOptions {
28+
entrypoint: string;
29+
deps: string[];
30+
moduleKey: string;
31+
exports?: Record<string, ExportDefinition | string>;
32+
jsii?: JsiiOutputOptions;
33+
python?: PythonOutputOptions;
34+
java?: JavaOutputOptions;
35+
csharp?: CSharpOutputOptions;
36+
golang?: GoLangOutputOptions;
37+
}
38+
39+
export interface JsiiOutputOptions {
40+
path: string;
41+
}
42+
43+
export interface PythonOutputOptions {
44+
outdir: string;
45+
moduleName: string;
46+
}
47+
48+
export interface JavaOutputOptions {
49+
outdir: string;
50+
package: string;
51+
}
52+
53+
export interface CSharpOutputOptions {
54+
outdir: string;
55+
namespace: string;
56+
}
57+
58+
export interface GoLangOutputOptions {
59+
outdir: string;
60+
moduleName: string;
61+
packageName: string;
62+
}
63+
64+
/**
65+
* See https://nodejs.org/api/packages.html#conditional-exports for more information
66+
*/
67+
export interface ExportDefinition {
68+
node?: string;
69+
import?: string;
70+
require?: string;
71+
default?: string;
72+
}
73+
2574
export async function generateJsiiLanguage(
2675
code: CodeMaker,
27-
opts: srcmak.Options,
76+
opts: GenerateJSIIOptions,
2877
outputPath: string,
2978
disallowedFileGlobs: string[] = []
3079
) {
@@ -43,7 +92,139 @@ export async function generateJsiiLanguage(
4392
filesToDelete.map((file) => fs.remove(path.join(staging, file)))
4493
);
4594

46-
await srcmak.srcmak(staging, opts);
95+
// Compile with JSII
96+
const jsiiArgs = ["--silence-warnings", "reserved-word"];
97+
const jsiiEntrypoint = opts.entrypoint;
98+
const basepath = path.join(
99+
path.dirname(jsiiEntrypoint),
100+
path.basename(jsiiEntrypoint, ".ts")
101+
);
102+
103+
const moduleKey = opts.moduleKey.replace(/\./g, "").replace(/\//g, "");
104+
const moduleDirs = opts.deps;
105+
const targets: Record<string, any> = {};
106+
const deps: Record<string, string> = {};
107+
for (const dir of moduleDirs) {
108+
// read module metadata
109+
const metadata = await fs.readJson(path.join(dir, "package.json"));
110+
const moduleName: string = metadata.name;
111+
const moduleVersion: string = metadata.version;
112+
113+
const targetdir = path.join(
114+
path.join(staging, "node_modules"),
115+
moduleName
116+
);
117+
await fs.mkdirp(path.dirname(targetdir));
118+
await fs.copy(dir, targetdir);
119+
120+
// add to "deps" and "peer deps"
121+
if (!moduleName.startsWith("@types/")) {
122+
deps[moduleName] = moduleVersion;
123+
}
124+
}
125+
const pkg = {
126+
name: moduleKey,
127+
version: "0.0.0",
128+
author: "generated@generated.com",
129+
main: `${basepath}.js`,
130+
types: `${basepath}.d.ts`,
131+
license: "UNLICENSED",
132+
repository: { url: "http://generated", type: "git" },
133+
jsii: {
134+
outdir: "dist",
135+
targets: targets,
136+
},
137+
dependencies: deps,
138+
peerDependencies: deps,
139+
};
140+
141+
if (opts.exports) {
142+
(pkg as Record<string, any>).exports = opts.exports;
143+
}
144+
if (opts.python) {
145+
targets.python = {
146+
distName: "generated",
147+
module: opts.python.moduleName,
148+
};
149+
}
150+
151+
if (opts.java) {
152+
targets.java = {
153+
package: opts.java.package,
154+
maven: {
155+
groupId: "generated",
156+
artifactId: "generated",
157+
},
158+
};
159+
}
160+
161+
if (opts.csharp) {
162+
targets.dotnet = {
163+
namespace: opts.csharp.namespace,
164+
packageId: opts.csharp.namespace,
165+
};
166+
}
167+
168+
if (opts.golang) {
169+
targets.go = {
170+
moduleName: opts.golang.moduleName,
171+
packageName: opts.golang.packageName,
172+
};
173+
}
174+
175+
await fs.writeFile(
176+
path.join(staging, "package.json"),
177+
JSON.stringify(pkg, undefined, 2)
178+
);
179+
180+
const endJsiiTimer = logTimespan("jsii");
181+
await exec(jsiiModule, jsiiArgs, {
182+
cwd: staging,
183+
});
184+
endJsiiTimer();
185+
186+
// extract .jsii if requested
187+
if (opts.jsii) {
188+
await fs.copy(path.join(staging, ".jsii"), opts.jsii.path);
189+
}
190+
191+
// run pacmak to generate code
192+
const endJsiiPacmakTimer = logTimespan("jsii-pacmak");
193+
await exec(pacmakModule, ["--code-only"], { cwd: staging });
194+
endJsiiPacmakTimer();
195+
196+
if (opts.python) {
197+
const reldir = opts.python.moduleName.replace(/\./g, "/"); // jsii replaces "." with "/"
198+
const source = path.resolve(
199+
path.join(staging, "dist/python/src", reldir)
200+
);
201+
const target = path.join(opts.python.outdir, reldir);
202+
await fs.move(source, target, { overwrite: true });
203+
}
204+
205+
if (opts.java) {
206+
const source = path.resolve(path.join(staging, "dist/java/src/"));
207+
const target = path.join(opts.java.outdir, "src/");
208+
await fs.mkdirp(target); // make sure target directory exists
209+
await fs.copy(source, target, { recursive: true, overwrite: false });
210+
}
211+
212+
if (opts.csharp) {
213+
const reldir = opts.csharp.namespace;
214+
const source = path.resolve(path.join(staging, "dist/dotnet/", reldir));
215+
const target = path.join(opts.csharp.outdir, reldir);
216+
await fs.move(source, target, { overwrite: true });
217+
}
218+
219+
if (opts.golang) {
220+
const reldir = opts.golang.packageName;
221+
const source = path.resolve(path.join(staging, "dist/go/", reldir));
222+
const target = path.join(opts.golang.outdir, reldir);
223+
await fs.move(source, target, { overwrite: true });
224+
// remove go.mod as this would make it a submodule
225+
await fs.remove(path.join(target, "go.mod"));
226+
}
227+
47228
["versions.json", "constraints.json"].forEach((file) => {
48229
try {
49230
fs.copySync(
@@ -381,7 +562,7 @@ export class ConstructsMaker {
381562
private async generateJsiiLanguage(target: ConstructsMakerTarget) {
382563
// these are the module dependencies we compile against
383564
const deps = ["@types/node", "constructs", "cdktf"];
384-
const opts: srcmak.Options = {
565+
const opts: GenerateJSIIOptions = {
385566
entrypoint: target.fileName,
386567
deps: deps.map((dep) =>
387568
path.dirname(require.resolve(`${dep}/package.json`))

packages/@cdktf/provider-generator/lib/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ export {
1010
export { sanitizeClassOrNamespaceName } from "./get/generator/resource-parser";
1111

1212
import { CodeMaker } from "codemaker";
13-
import * as srcmak from "jsii-srcmak";
14-
15-
import { generateJsiiLanguage } from "./get/constructs-maker";
13+
import {
14+
GenerateJSIIOptions,
15+
generateJsiiLanguage,
16+
} from "./get/constructs-maker";
1617
export { escapeAttributeName } from "./get/generator/models";
1718
import { TerraformProviderGenerator } from "./get/generator/provider-generator";
1819
import { ProviderSchema } from "@cdktf/commons";
@@ -23,7 +24,7 @@ export { TerraformProviderGenerator, CodeMaker };
2324
export async function generateProviderBindingsFromSchema(
2425
targetPath: string,
2526
schemaJSON: ProviderSchema,
26-
options?: srcmak.Options
27+
options?: GenerateJSIIOptions
2728
): Promise<void> {
2829
const code = new CodeMaker();
2930
const generator = new TerraformProviderGenerator(code, schemaJSON);

packages/@cdktf/provider-generator/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@
3939
"@types/node": "18.19.31",
4040
"codemaker": "1.95.0",
4141
"fs-extra": "8.1.0",
42-
"glob": "10.3.12",
43-
"jsii-srcmak": "0.1.1039"
42+
"glob": "10.3.12"
4443
},
4544
"devDependencies": {
4645
"@types/deepmerge": "2.2.0",
@@ -52,4 +51,4 @@
5251
"ts-jest": "29.1.2",
5352
"typescript": "5.2.2"
5453
}
55-
}
54+
}

packages/cdktf-cli/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@
155155
"ink-testing-library": "2.1.0",
156156
"ink-use-stdout-dimensions": "1.0.5",
157157
"jest": "29.7.0",
158-
"jsii-srcmak": "0.1.1039",
159158
"lodash.isequal": "4.5.0",
160159
"log4js": "6.9.1",
161160
"nock": "13.5.4",
@@ -173,4 +172,4 @@
173172
"utility-types": "3.11.0",
174173
"uuid": "8.3.2"
175174
}
176-
}
175+
}

0 commit comments

Comments
 (0)