Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions packages/cli/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@
"commands_codegen_options_m": "Path to the Polywrap manifest file (default: {default})",
"commands_codegen_options_o": "Output directory for custom generated types (default: 'types/')",
"commands_codegen_options_o_path": "path",
"commands_codegen_options_b": "Uri for custom bindgen wrap (must implement wrap-abi-bindgen interface; see https://github.com/polywrap/wrap-abi-bindgen)",
"commands_codegen_success": "Types were generated successfully",
"commands_codegen_invalid_uri": "Invalid WRAP URI format: {uri}.",
"commands_codegen_project_load_error": "Failed to load project, please make sure {manifestPath} is a valid Project manifest",
"commands_codegen_options_publish": "Output path for the built schema and manifest (default: {default})",
"commands_create_description": "Create New Projects",
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/lang/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@
"commands_codegen_options_m": "Path to the Polywrap manifest file (default: {default})",
"commands_codegen_options_o": "Output directory for custom generated types (default: 'types/')",
"commands_codegen_options_o_path": "path",
"commands_codegen_options_b": "Uri for custom bindgen wrap (must implement wrap-abi-bindgen interface; see https://github.com/polywrap/wrap-abi-bindgen)",
"commands_codegen_success": "Types were generated successfully",
"commands_codegen_invalid_uri": "Invalid WRAP URI format: {uri}.",
"commands_codegen_project_load_error": "Failed to load project, please make sure {manifestPath} is a valid Project manifest",
"commands_codegen_options_publish": "Output path for the built schema and manifest (default: {default})",
"commands_create_description": "Create New Projects",
Expand Down
22 changes: 22 additions & 0 deletions packages/cli/src/__tests__/e2e/p2/codegen.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Options:
(default: polywrap.yaml | polywrap.yml)
-g, --codegen-dir <path> Output directory for the generated code
(default: ./src/wrap)
-b, --bindgen <URI> Uri for custom bindgen wrap (must
implement wrap-abi-bindgen interface; see
https://github.com/polywrap/wrap-abi-bindgen)
-s, --script <path> Path to a custom generation script
(JavaScript | TypeScript)
-c, --client-config <config-path> Add custom configuration to the
Expand Down Expand Up @@ -188,4 +191,23 @@ describe("e2e tests for codegen command", () => {

rimraf.sync(`${getTestCaseDir(1)}/types`);
});

it("Should successfully generate types with custom bindgen wrap", async () => {
rimraf.sync(`${getTestCaseDir(0)}/types`);

const { exitCode: code, stdout: output, stderr: error } = await Commands.codegen({
bindgen: "https://github.com/polywrap/wrap-abi-bindgen/tree/wrap-0.1/implementations/wrap-rust"
}, {
cwd: getTestCaseDir(0),
cli: polywrapCli,
});

expect(code).toEqual(0);
expect(error).toBe("");
expect(clearStyle(output)).toContain(
`🔥 Types were generated successfully 🔥`
);

rimraf.sync(`${getTestCaseDir(0)}/types`);
});
});
8 changes: 7 additions & 1 deletion packages/cli/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from "../lib/build-strategies";
import { DEFAULT_CODEGEN_DIR } from "../lib/defaults";
import { watchProject } from "../lib/watchProject";
import { parseUriOption } from "../lib/option-parsers/uri";

import { PolywrapClient } from "@polywrap/client-js";
import { PolywrapManifest } from "@polywrap/polywrap-manifest-types-js";
Expand All @@ -46,6 +47,7 @@ const supportedProjectTypes = [
export interface BuildCommandOptions extends BaseCommandOptions {
manifestFile: string;
outputDir: string;
bindgen: string | false;
clientConfig: string | false;
wrapperEnvs: string | false;
noCodegen: boolean;
Expand All @@ -72,6 +74,7 @@ export const build: Command = {
default: defaultOutputDir,
})}`
)
.option(`-b, --bindgen <URI>`, `${intlMsg.commands_codegen_options_b()}`)
.option(
`-c, --client-config <${intlMsg.commands_common_options_configPath()}>`,
`${intlMsg.commands_common_options_config()}`
Expand Down Expand Up @@ -112,6 +115,7 @@ export const build: Command = {
clientConfig: options.clientConfig || false,
wrapperEnvs: options.wrapperEnvs || false,
outputDir: parseDirOption(options.outputDir, defaultOutputDir),
bindgen: options.bindgen || false,
noCodegen: !options.codegen || false,
codegenDir: parseDirOptionNoDefault(options.codegenDir),
strategy: options.strategy || defaultStrategy,
Expand Down Expand Up @@ -167,6 +171,7 @@ async function run(options: Required<BuildCommandOptions>) {
clientConfig,
wrapperEnvs,
outputDir,
bindgen,
strategy,
noCodegen,
codegenDir,
Expand All @@ -176,7 +181,7 @@ async function run(options: Required<BuildCommandOptions>) {
} = options;

const logger = createLogger({ verbose, quiet, logFile });

const bindgenUri = parseUriOption(bindgen);
const envs = await parseWrapperEnvsOption(wrapperEnvs);
const configBuilder = await parseClientConfigOption(clientConfig);

Expand Down Expand Up @@ -243,6 +248,7 @@ async function run(options: Required<BuildCommandOptions>) {
project,
schemaComposer,
codegenDirAbs: codegenDir || undefined,
bindgenUri,
});
const codegenSuccess = await codeGenerator.generate();

Expand Down
8 changes: 7 additions & 1 deletion packages/cli/src/commands/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { ScriptCodegenerator } from "../lib/codegen/ScriptCodeGenerator";
import { DEFAULT_CODEGEN_DIR } from "../lib/defaults";
import { watchProject } from "../lib/watchProject";
import { parseUriOption } from "../lib/option-parsers/uri";

import { PolywrapClient } from "@polywrap/client-js";

Expand All @@ -27,6 +28,7 @@ const defaultManifestStr = defaultPolywrapManifestFiles.join(" | ");
export interface CodegenCommandOptions extends BaseCommandOptions {
manifestFile: string;
codegenDir: string | false;
bindgen: string | false;
script: string | false;
clientConfig: string | false;
wrapperEnvs: string | false;
Expand All @@ -51,6 +53,7 @@ export const codegen: Command = {
default: DEFAULT_CODEGEN_DIR,
})}`
)
.option(`-b, --bindgen <URI>`, `${intlMsg.commands_codegen_options_b()}`)
.option(
`-s, --script <${pathStr}>`,
`${intlMsg.commands_codegen_options_s()}`
Expand All @@ -77,6 +80,7 @@ export const codegen: Command = {
defaultProjectManifestFiles
),
codegenDir: parseDirOptionNoDefault(options.codegenDir),
bindgen: options.bindgen || false,
script: parseCodegenScriptOption(options.script),
clientConfig: options.clientConfig || false,
wrapperEnvs: options.wrapperEnvs || false,
Expand All @@ -95,14 +99,15 @@ async function run(options: Required<CodegenCommandOptions>) {
clientConfig,
wrapperEnvs,
codegenDir,
bindgen,
script,
verbose,
quiet,
logFile,
watch,
} = options;
const logger = createLogger({ verbose, quiet, logFile });

const bindgenUri = parseUriOption(bindgen);
const envs = await parseWrapperEnvsOption(wrapperEnvs);
const configBuilder = await parseClientConfigOption(clientConfig);

Expand Down Expand Up @@ -142,6 +147,7 @@ async function run(options: Required<CodegenCommandOptions>) {
codegenDirAbs: codegenDir || undefined,
schemaComposer,
project,
bindgenUri,
});

const execute = async (): Promise<boolean> => {
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/lib/codegen/CodeGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import { CodegenOverrides, tryGetCodegenOverrides } from "./CodegenOverrides";
import path from "path";
import { BindLanguage } from "@polywrap/schema-bind";
import { writeDirectorySync } from "@polywrap/os-js";
import { Uri } from "@polywrap/core-js";

export interface CodeGeneratorConfig {
project: Project<AnyProjectManifest>;
schemaComposer: SchemaComposer;
codegenDirAbs?: string;
bindgenUri?: Uri;
}

export class CodeGenerator {
Expand Down Expand Up @@ -89,6 +91,7 @@ export class CodeGenerator {
const binding = await this._config.project.generateSchemaBindings(
abi,
codegenDir,
this._config.bindgenUri?.toString(),
bindConfig
);

Expand Down
17 changes: 17 additions & 0 deletions packages/cli/src/lib/option-parsers/uri.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { intlMsg } from "../intl";

import { Uri } from "@polywrap/core-js";

export function parseUriOption(
uri: string | undefined | false
): Uri | undefined {
if (uri) {
try {
return Uri.from(uri);
} catch {
console.error(intlMsg.commands_codegen_invalid_uri({ uri }));
process.exit(1);
}
}
return undefined;
}
5 changes: 3 additions & 2 deletions packages/cli/src/lib/project/AppProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ export class AppProject extends Project<AppManifest> {

public async generateSchemaBindings(
abi: WrapAbi,
generationSubPath?: string
generationSubPath?: string,
bindgenUri?: string
): Promise<BindOutput> {
const bindLanguage = appManifestLanguageToBindLanguage(
await this.getManifestLanguage()
Expand All @@ -128,7 +129,7 @@ export class AppProject extends Project<AppManifest> {
},
outputDirAbs: await this.getGenerationDirectory(generationSubPath),
};
return bindSchema(options);
return bindSchema(options, bindgenUri);
}

private _getGenerationDirectory(
Expand Down
5 changes: 3 additions & 2 deletions packages/cli/src/lib/project/PluginProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ export class PluginProject extends Project<PluginManifest> {

public async generateSchemaBindings(
abi: WrapAbi,
generationSubPath?: string
generationSubPath?: string,
bindgenUri?: string
): Promise<BindOutput> {
const moduleDirectory = await this.getGenerationDirectory(
generationSubPath
Expand All @@ -141,7 +142,7 @@ export class PluginProject extends Project<PluginManifest> {
outputDirAbs: moduleDirectory,
};

return bindSchema(options);
return bindSchema(options, bindgenUri);
}

private _getGenerationDirectory(
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/lib/project/PolywrapProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export class PolywrapProject extends Project<PolywrapManifest> {
public async generateSchemaBindings(
abi: WrapAbi,
generationSubPath?: string,
bindgenUri?: string,
bindConfig?: Record<string, unknown>
): Promise<BindOutput> {
const codegenDirectory = await this.getGenerationDirectory(
Expand All @@ -178,7 +179,7 @@ export class PolywrapProject extends Project<PolywrapManifest> {
config: bindConfig,
};

return bindSchema(options);
return bindSchema(options, bindgenUri);
}

/// Polywrap Build Manifest (polywrap.build.yaml)
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/lib/project/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export abstract class Project<TManifest extends AnyProjectManifest> {
public abstract generateSchemaBindings(
abi: Abi,
generationSubPath?: string,
bindgenUri?: string,
bindConfig?: Record<string, unknown>
): Promise<BindOutput>;

Expand Down
11 changes: 9 additions & 2 deletions packages/schema/bind/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BindOptions, BindOutput } from "./types";
import { getGenerateBindingFn } from "./bindings";
import * as WrapBindgen from "./bindings/wrap-bindgen";

import Mustache from "mustache";

Expand All @@ -9,6 +10,12 @@ Mustache.escape = (value) => value;
export * from "./types";
export * from "./bindings";

export async function bindSchema(options: BindOptions): Promise<BindOutput> {
return await getGenerateBindingFn(options.bindLanguage)(options);
export async function bindSchema(
options: BindOptions,
uri?: string
): Promise<BindOutput> {
if (uri) {
return WrapBindgen.getGenerateBindingFn(uri)(options);
}
return getGenerateBindingFn(options.bindLanguage)(options);
}
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4450,9 +4450,9 @@ ecc-jsbn@~0.1.1:
safer-buffer "^2.1.0"

electron-to-chromium@^1.4.431:
version "1.4.461"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.461.tgz#6b14af66042732bf883ab63a4d82cac8f35eb252"
integrity sha512-1JkvV2sgEGTDXjdsaQCeSwYYuhLRphRpc+g6EHTFELJXEiznLt3/0pZ9JuAOQ5p2rI3YxKTbivtvajirIfhrEQ==
version "1.4.462"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.462.tgz#4faf5072bb5f55269d35ca9dc7475e7bf91b1ac3"
integrity sha512-ux2LqN9JKRBDKXMT+78jtiBLPiXf+rLtYlsrOg5Qn7uv6Cbg7+9JyIalE3wcqkOdB2wPCUYNWAuL7suKRMHe9w==

elliptic@6.5.4:
version "6.5.4"
Expand Down