Skip to content

Commit

Permalink
chore: remove dependency on jsii-srcmak
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMSchmidt committed May 13, 2024
1 parent 25cf0a3 commit cae9fe4
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr-depcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ jobs:
- uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
- name: "Run Depcheck"
run: |
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"
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"
4 changes: 2 additions & 2 deletions .github/workflows/yarn-upgrade.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ jobs:
# Upgrade all the packages
lerna exec --scope='{${{ join(matrix.pr.packages, ',') }}}' ncu -- --upgrade --filter=@types/node,@types/fs-extra --target=minor
lerna exec --scope='{${{ join(matrix.pr.packages, ',') }}}' ncu -- --upgrade --filter=typescript --target=patch
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
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
# This will ensure the current lockfile is up-to-date with the dependency specifications (necessary for "yarn upgrade" to run)
- name: Run "yarn install"
run: yarn install --prefer-offline
Expand Down Expand Up @@ -222,7 +222,7 @@ jobs:
- name: Run "ncu -u"
run: |-
# Upgrade all the packages
lerna exec ncu -- --upgrade --filter='jsii,jsii-srcmak,jsii-pacmak,jsii-rosetta,jsii-docgen,codemaker,constructs' --target=minor
lerna exec ncu -- --upgrade --filter='jsii,jsii-pacmak,jsii-rosetta,jsii-docgen,codemaker,constructs' --target=minor
# This will ensure the current lockfile is up-to-date with the dependency specifications (necessary for "yarn upgrade" to run)
- name: Run "yarn install"
run: yarn install --prefer-offline
Expand Down
1 change: 0 additions & 1 deletion packages/@cdktf/cli-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
"ink-use-stdout-dimensions": "1.0.5",
"jsii": "5.3.29",
"jsii-pacmak": "1.95.0",
"jsii-srcmak": "0.1.1039",
"lodash.isequal": "4.5.0",
"log4js": "6.9.1",
"minimatch": "5.1.6",
Expand Down
191 changes: 186 additions & 5 deletions packages/@cdktf/provider-generator/lib/get/constructs-maker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import * as fs from "fs-extra";
import * as path from "path";
import { CodeMaker } from "codemaker";
import { mkdtemp } from "@cdktf/commons";
import * as srcmak from "jsii-srcmak";
import { exec, mkdtemp } from "@cdktf/commons";
import {
TerraformDependencyConstraint,
logger,
Expand All @@ -22,9 +21,59 @@ import { ModuleGenerator } from "./generator/module-generator";
import { glob } from "glob";
import { readSchema } from "@cdktf/provider-schema";

const pacmakModule = require.resolve("jsii-pacmak/bin/jsii-pacmak");
const jsiiModule = require.resolve("jsii/bin/jsii");

export interface GenerateJSIIOptions {
entrypoint: string;
deps: string[];
moduleKey: string;
exports?: Record<string, ExportDefinition | string>;
jsii?: JsiiOutputOptions;
python?: PythonOutputOptions;
java?: JavaOutputOptions;
csharp?: CSharpOutputOptions;
golang?: GoLangOutputOptions;
}

export interface JsiiOutputOptions {
path: string;
}

export interface PythonOutputOptions {
outdir: string;
moduleName: string;
}

export interface JavaOutputOptions {
outdir: string;
package: string;
}

export interface CSharpOutputOptions {
outdir: string;
namespace: string;
}

export interface GoLangOutputOptions {
outdir: string;
moduleName: string;
packageName: string;
}

/**
* See https://nodejs.org/api/packages.html#conditional-exports for more information
*/
export interface ExportDefinition {
node?: string;
import?: string;
require?: string;
default?: string;
}

export async function generateJsiiLanguage(
code: CodeMaker,
opts: srcmak.Options,
opts: GenerateJSIIOptions,
outputPath: string,
disallowedFileGlobs: string[] = []
) {
Expand All @@ -43,7 +92,139 @@ export async function generateJsiiLanguage(
filesToDelete.map((file) => fs.remove(path.join(staging, file)))
);

await srcmak.srcmak(staging, opts);
// Compile with JSII
const jsiiArgs = ["--silence-warnings", "reserved-word"];
const jsiiEntrypoint = opts.entrypoint;
const basepath = path.join(
path.dirname(jsiiEntrypoint),
path.basename(jsiiEntrypoint, ".ts")
);

const moduleKey = opts.moduleKey.replace(/\./g, "").replace(/\//g, "");
const moduleDirs = opts.deps;
const targets: Record<string, any> = {};
const deps: Record<string, string> = {};
for (const dir of moduleDirs) {
// read module metadata
const metadata = await fs.readJson(path.join(dir, "package.json"));
const moduleName: string = metadata.name;
const moduleVersion: string = metadata.version;

const targetdir = path.join(
path.join(staging, "node_modules"),
moduleName
);
await fs.mkdirp(path.dirname(targetdir));
await fs.copy(dir, targetdir);

// add to "deps" and "peer deps"
if (!moduleName.startsWith("@types/")) {
deps[moduleName] = moduleVersion;
}
}
const pkg = {
name: moduleKey,
version: "0.0.0",
author: "generated@generated.com",
main: `${basepath}.js`,
types: `${basepath}.d.ts`,
license: "UNLICENSED",
repository: { url: "http://generated", type: "git" },
jsii: {
outdir: "dist",
targets: targets,
},
dependencies: deps,
peerDependencies: deps,
};

if (opts.exports) {
(pkg as Record<string, any>).exports = opts.exports;
}
if (opts.python) {
targets.python = {
distName: "generated",
module: opts.python.moduleName,
};
}

if (opts.java) {
targets.java = {
package: opts.java.package,
maven: {
groupId: "generated",
artifactId: "generated",
},
};
}

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

if (opts.golang) {
targets.go = {
moduleName: opts.golang.moduleName,
packageName: opts.golang.packageName,
};
}

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

const endJsiiTimer = logTimespan("jsii");
await exec(jsiiModule, jsiiArgs, {
cwd: staging,
});
endJsiiTimer();

// extract .jsii if requested
if (opts.jsii) {
await fs.copy(path.join(staging, ".jsii"), opts.jsii.path);
}

// run pacmak to generate code
const endJsiiPacmakTimer = logTimespan("jsii-pacmak");
await exec(pacmakModule, ["--code-only"], { cwd: staging });
endJsiiPacmakTimer();

if (opts.python) {
const reldir = opts.python.moduleName.replace(/\./g, "/"); // jsii replaces "." with "/"
const source = path.resolve(
path.join(staging, "dist/python/src", reldir)
);
const target = path.join(opts.python.outdir, reldir);
await fs.move(source, target, { overwrite: true });
}

if (opts.java) {
const source = path.resolve(path.join(staging, "dist/java/src/"));
const target = path.join(opts.java.outdir, "src/");
await fs.mkdirp(target); // make sure target directory exists
await fs.copy(source, target, { recursive: true, overwrite: false });
}

if (opts.csharp) {
const reldir = opts.csharp.namespace;
const source = path.resolve(path.join(staging, "dist/dotnet/", reldir));
const target = path.join(opts.csharp.outdir, reldir);
await fs.move(source, target, { overwrite: true });
}

if (opts.golang) {
const reldir = opts.golang.packageName;
const source = path.resolve(path.join(staging, "dist/go/", reldir));
const target = path.join(opts.golang.outdir, reldir);
await fs.move(source, target, { overwrite: true });
// remove go.mod as this would make it a submodule
await fs.remove(path.join(target, "go.mod"));
}

["versions.json", "constraints.json"].forEach((file) => {
try {
fs.copySync(
Expand Down Expand Up @@ -381,7 +562,7 @@ export class ConstructsMaker {
private async generateJsiiLanguage(target: ConstructsMakerTarget) {
// these are the module dependencies we compile against
const deps = ["@types/node", "constructs", "cdktf"];
const opts: srcmak.Options = {
const opts: GenerateJSIIOptions = {
entrypoint: target.fileName,
deps: deps.map((dep) =>
path.dirname(require.resolve(`${dep}/package.json`))
Expand Down
9 changes: 5 additions & 4 deletions packages/@cdktf/provider-generator/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ export {
export { sanitizeClassOrNamespaceName } from "./get/generator/resource-parser";

import { CodeMaker } from "codemaker";
import * as srcmak from "jsii-srcmak";

import { generateJsiiLanguage } from "./get/constructs-maker";
import {
GenerateJSIIOptions,
generateJsiiLanguage,
} from "./get/constructs-maker";
export { escapeAttributeName } from "./get/generator/models";
import { TerraformProviderGenerator } from "./get/generator/provider-generator";
import { ProviderSchema } from "@cdktf/commons";
Expand All @@ -23,7 +24,7 @@ export { TerraformProviderGenerator, CodeMaker };
export async function generateProviderBindingsFromSchema(
targetPath: string,
schemaJSON: ProviderSchema,
options?: srcmak.Options
options?: GenerateJSIIOptions
): Promise<void> {
const code = new CodeMaker();
const generator = new TerraformProviderGenerator(code, schemaJSON);
Expand Down
5 changes: 2 additions & 3 deletions packages/@cdktf/provider-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
"@types/node": "18.19.31",
"codemaker": "1.95.0",
"fs-extra": "8.1.0",
"glob": "10.3.12",
"jsii-srcmak": "0.1.1039"
"glob": "10.3.12"
},
"devDependencies": {
"@types/deepmerge": "2.2.0",
Expand All @@ -52,4 +51,4 @@
"ts-jest": "29.1.2",
"typescript": "5.2.2"
}
}
}
3 changes: 1 addition & 2 deletions packages/cdktf-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@
"ink-testing-library": "2.1.0",
"ink-use-stdout-dimensions": "1.0.5",
"jest": "29.7.0",
"jsii-srcmak": "0.1.1039",
"lodash.isequal": "4.5.0",
"log4js": "6.9.1",
"nock": "13.5.4",
Expand All @@ -173,4 +172,4 @@
"utility-types": "3.11.0",
"uuid": "8.3.2"
}
}
}
Loading

0 comments on commit cae9fe4

Please sign in to comment.