From 178176d02e8402bcbafc010a7c86f619a654657e Mon Sep 17 00:00:00 2001 From: dOrgJelli Date: Tue, 3 Jan 2023 01:18:58 -0500 Subject: [PATCH 1/4] fix: remove exclamations from output strings --- packages/cli/lang/en.json | 4 ++-- packages/cli/lang/es.json | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/cli/lang/en.json b/packages/cli/lang/en.json index 79633ce515..a38cb86dc5 100644 --- a/packages/cli/lang/en.json +++ b/packages/cli/lang/en.json @@ -24,7 +24,7 @@ "commands_build_options_s": "Strategy to use for building the wrapper", "commands_build_options_s_strategy": "strategy", "commands_build_options_l": "Log file to save console output to", - "commands_build_error_codegen_failed": "Code generation failed!", + "commands_build_error_codegen_failed": "Code generation failed", "commands_infra_description": "Modular Infrastructure-As-Code Orchestrator", "commands_infra_actions_subtitle": "Infra allows you to execute the following commands:", "commands_infra_options_options": "options", @@ -90,7 +90,7 @@ "commands_codegen_options_o": "Output directory for custom generated types (default: 'types/')", "commands_codegen_options_o_path": "path", "commands_codegen_success": "Types were generated successfully", - "commands_codegen_project_load_error": "Failed to load project! Please make sure {manifestPath} is a valid Project manifest!", + "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_docgen_description": "Generate wrapper documentation", "commands_docgen_success": "Docs were generated successfully", diff --git a/packages/cli/lang/es.json b/packages/cli/lang/es.json index 79633ce515..a38cb86dc5 100644 --- a/packages/cli/lang/es.json +++ b/packages/cli/lang/es.json @@ -24,7 +24,7 @@ "commands_build_options_s": "Strategy to use for building the wrapper", "commands_build_options_s_strategy": "strategy", "commands_build_options_l": "Log file to save console output to", - "commands_build_error_codegen_failed": "Code generation failed!", + "commands_build_error_codegen_failed": "Code generation failed", "commands_infra_description": "Modular Infrastructure-As-Code Orchestrator", "commands_infra_actions_subtitle": "Infra allows you to execute the following commands:", "commands_infra_options_options": "options", @@ -90,7 +90,7 @@ "commands_codegen_options_o": "Output directory for custom generated types (default: 'types/')", "commands_codegen_options_o_path": "path", "commands_codegen_success": "Types were generated successfully", - "commands_codegen_project_load_error": "Failed to load project! Please make sure {manifestPath} is a valid Project manifest!", + "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_docgen_description": "Generate wrapper documentation", "commands_docgen_success": "Docs were generated successfully", From 3ec283561465e9dccc4bfdac0caf4327eeae9090 Mon Sep 17 00:00:00 2001 From: dOrgJelli Date: Tue, 3 Jan 2023 01:48:43 -0500 Subject: [PATCH 2/4] chore: move plugin build logic into compiler --- packages/cli/lang/en.json | 4 +- packages/cli/lang/es.json | 4 +- packages/cli/src/commands/build.ts | 94 +++++++--------------------- packages/cli/src/lib/Compiler.ts | 36 ++++++----- packages/cli/src/lib/helpers/wrap.ts | 18 +++++- 5 files changed, 67 insertions(+), 89 deletions(-) diff --git a/packages/cli/lang/en.json b/packages/cli/lang/en.json index a38cb86dc5..2281a0c78f 100644 --- a/packages/cli/lang/en.json +++ b/packages/cli/lang/en.json @@ -216,7 +216,8 @@ "lib_compiler_noNodeModules": "could not locate {folder} in parent directories of polywrap manifest", "lib_compiler_noInvoke": "WASM module is missing the _wrap_invoke export. This should never happen...", "lib_compiler_invalid_module": "Invalid Wasm module found. {modulePath} is invalid. Error: {error}", - "lib_compiler_cannotBuildInterfaceModules": "Cannot build modules for an Interface Polywrap", + "lib_compiler_cannotBuildModule": "Cannot build the module for a project of type {project}", + "lib_compiler_missingBuildStrategy": "The compiler is missing a build strategy", "lib_compiler_copyResourcesFolderText": "Copying resources folder: {folder}", "lib_compiler_copyResourcesFolderError": "Failed to copy resources folder: {folder}", "lib_compiler_copyResourcesFolderWarning": "Warnings copying resources folder: {folder}", @@ -255,6 +256,7 @@ "lib_helpers_docker_projectFolderMissing": "Build image `{image}` missing /project folder", "lib_helpers_docker_projectBuildFolderMissing": "Build image `{image}` missing build artifact `{artifact}` in folder /project/build", "lib_helpers_parameters_unexpectedValue": "Unexpected value provided for one or more of {options}. See --help for more information.", + "lib_helpers_wrap_unsupportedType": "Unsupported WRAP type {type}", "lib_language_unsupportedManifestLanguage": "The manifest's language {language} is not supported. Please use one of the supported lanauges: {supported}", "lib_language_noInterfaceCodegen": "Code generation cannot be run for Polywrap Interface projects", "lib_system_env_var_not_found": "Environment variable not found: `{variableName}`", diff --git a/packages/cli/lang/es.json b/packages/cli/lang/es.json index a38cb86dc5..2281a0c78f 100644 --- a/packages/cli/lang/es.json +++ b/packages/cli/lang/es.json @@ -216,7 +216,8 @@ "lib_compiler_noNodeModules": "could not locate {folder} in parent directories of polywrap manifest", "lib_compiler_noInvoke": "WASM module is missing the _wrap_invoke export. This should never happen...", "lib_compiler_invalid_module": "Invalid Wasm module found. {modulePath} is invalid. Error: {error}", - "lib_compiler_cannotBuildInterfaceModules": "Cannot build modules for an Interface Polywrap", + "lib_compiler_cannotBuildModule": "Cannot build the module for a project of type {project}", + "lib_compiler_missingBuildStrategy": "The compiler is missing a build strategy", "lib_compiler_copyResourcesFolderText": "Copying resources folder: {folder}", "lib_compiler_copyResourcesFolderError": "Failed to copy resources folder: {folder}", "lib_compiler_copyResourcesFolderWarning": "Warnings copying resources folder: {folder}", @@ -255,6 +256,7 @@ "lib_helpers_docker_projectFolderMissing": "Build image `{image}` missing /project folder", "lib_helpers_docker_projectBuildFolderMissing": "Build image `{image}` missing build artifact `{artifact}` in folder /project/build", "lib_helpers_parameters_unexpectedValue": "Unexpected value provided for one or more of {options}. See --help for more information.", + "lib_helpers_wrap_unsupportedType": "Unsupported WRAP type {type}", "lib_language_unsupportedManifestLanguage": "The manifest's language {language} is not supported. Please use one of the supported lanauges: {supported}", "lib_language_noInterfaceCodegen": "Code generation cannot be run for Polywrap Interface projects", "lib_system_env_var_not_found": "Environment variable not found: `{variableName}`", diff --git a/packages/cli/src/commands/build.ts b/packages/cli/src/commands/build.ts index a2d47e316b..eb692727aa 100644 --- a/packages/cli/src/commands/build.ts +++ b/packages/cli/src/commands/build.ts @@ -15,8 +15,6 @@ import { parseLogFileOption, getProjectFromManifest, isPolywrapManifestLanguage, - isPluginManifestLanguage, - generateWrapFile, polywrapManifestLanguages, pluginManifestLanguages, parseWrapperEnvsOption, @@ -31,8 +29,6 @@ import { } from "../lib/build-strategies"; import { defaultCodegenDir } from "../lib/defaults/defaultCodegenDir"; -import fs from "fs"; -import path from "path"; import readline from "readline"; import { Env, PolywrapClient } from "@polywrap/client-js"; import { PolywrapManifest } from "@polywrap/polywrap-manifest-types-js"; @@ -197,23 +193,34 @@ async function run(options: BuildCommandOptions) { const manifest = await project.getManifest(); const language = manifest.project.type; - let execute: () => Promise; + if (supportedProjectTypes.indexOf(language) === -1) { + logger.error( + intlMsg.commands_build_error_unsupportedProjectType({ + supportedTypes: supportedProjectTypes.join(", "), + }) + ); + process.exit(1); + } + + let buildStrategy: BuildStrategy; if (isPolywrapManifestLanguage(language)) { await validateManifestModules(manifest as PolywrapManifest); - const buildStrategy = createBuildStrategy( + buildStrategy = createBuildStrategy( strategy, outputDir, project as PolywrapProject ); + } - execute = async (): Promise => { + const execute = async (): Promise => { + try { const schemaComposer = new SchemaComposer({ project, client, }); - + if (codegen) { const codeGenerator = new CodeGenerator({ project, @@ -221,79 +228,26 @@ async function run(options: BuildCommandOptions) { codegenDirAbs: codegenDir, }); const codegenSuccess = await codeGenerator.generate(); - + if (!codegenSuccess) { logger.error(intlMsg.commands_build_error_codegen_failed()); return false; } } - + const compiler = new Compiler({ project: project as PolywrapProject, outputDir, schemaComposer, buildStrategy, }); - - const result = await compiler.compile(); - - if (!result) { - return result; - } - - return true; - }; - } else if (isPluginManifestLanguage(language)) { - execute = async (): Promise => { - const schemaComposer = new SchemaComposer({ - project, - client, - }); - - // Output the built manifest - const manifestPath = path.join(outputDir, "wrap.info"); - - try { - if (codegen) { - const codeGenerator = new CodeGenerator({ - project, - schemaComposer, - codegenDirAbs: codegenDir, - }); - const codegenSuccess = await codeGenerator.generate(); - - if (!codegenSuccess) { - logger.error(intlMsg.commands_build_error_codegen_failed()); - return false; - } - } - - if (!fs.existsSync(outputDir)) { - fs.mkdirSync(outputDir); - } - - await generateWrapFile( - await schemaComposer.getComposedAbis(), - await project.getName(), - "plugin", - manifestPath, - logger - ); - } catch (err) { - logger.error(err.message); - return false; - } - - return true; - }; - } else { - logger.error( - intlMsg.commands_build_error_unsupportedProjectType({ - supportedTypes: supportedProjectTypes.join(", "), - }) - ); - return; - } + + return await compiler.compile(); + } catch (err) { + logger.error(err.message); + return false; + } + }; if (!watch) { const result = await execute(); diff --git a/packages/cli/src/lib/Compiler.ts b/packages/cli/src/lib/Compiler.ts index 639a9132d9..a5c8442385 100644 --- a/packages/cli/src/lib/Compiler.ts +++ b/packages/cli/src/lib/Compiler.ts @@ -6,6 +6,7 @@ import { generateWrapFile, intlMsg, PolywrapProject, + PluginProject, resetDir, SchemaComposer, logActivity, @@ -21,8 +22,8 @@ import path from "path"; export interface CompilerConfig { outputDir: string; - project: PolywrapProject; - buildStrategy: BuildStrategy; + project: PolywrapProject | PluginProject; + buildStrategy?: BuildStrategy; schemaComposer: SchemaComposer; } @@ -39,13 +40,13 @@ export class Compiler { // Output: wrap.info await this._outputWrapManifest(); - if (!(await this._isInterface())) { - // Compile the Wrapper + if (await this._isWasm()) { + // Build & Output: wasm.wrap await this._buildModules(); - } - // Copy: Resources folder - await this._copyResourcesFolder(); + // Copy: Resources folder + await this._copyResourcesFolder(); + } }; try { @@ -64,17 +65,24 @@ export class Compiler { } } - private async _isInterface(): Promise { + private async _isWasm(): Promise { const { project } = this._config; const manifest = await project.getManifest(); - return manifest.project.type === "interface"; + return manifest.project.type.startsWith("wasm/"); } private async _buildModules(): Promise { - const { outputDir } = this._config; + const { outputDir, project } = this._config; - if (await this._isInterface()) { - throw Error(intlMsg.lib_compiler_cannotBuildInterfaceModules()); + if (!this._config.buildStrategy) { + throw Error(intlMsg.lib_compiler_missingBuildStrategy()); + } + + if (!await this._isWasm()) { + const manifest = await project.getManifest(); + throw Error(intlMsg.lib_compiler_cannotBuildModule({ + project: manifest.project.type + })); } // Build the sources @@ -90,7 +98,7 @@ export class Compiler { const run = async () => { const manifest = await project.getManifest(); - const type = (await this._isInterface()) ? "interface" : "wasm"; + const type = manifest.project.type.split("/")[0]; const abi = await schemaComposer.getComposedAbis(); await generateWrapFile( abi, @@ -122,7 +130,7 @@ export class Compiler { private async _copyResourcesFolder(): Promise { const { outputDir, project } = this._config; - const projectManifest = await project.getManifest(); + const projectManifest = await (project as PolywrapProject).getManifest(); if (!projectManifest || !projectManifest.resources) { return Promise.resolve(); diff --git a/packages/cli/src/lib/helpers/wrap.ts b/packages/cli/src/lib/helpers/wrap.ts index 8522f869d2..f96ed3f0fa 100644 --- a/packages/cli/src/lib/helpers/wrap.ts +++ b/packages/cli/src/lib/helpers/wrap.ts @@ -10,10 +10,16 @@ import { } from "@polywrap/wrap-manifest-types-js"; import { normalizePath, writeFileSync } from "@polywrap/os-js"; +export const supportedWrapTypes: WrapManifest["type"][] = [ + "interface", + "wasm", + "plugin" +]; + const run = async ( abi: WrapAbi, name: string, - type: "interface" | "wasm" | "plugin", + type: WrapManifest["type"], path: string ): Promise => { const manifest: WrapManifest = { @@ -31,10 +37,16 @@ const run = async ( export const generateWrapFile = async ( abi: WrapAbi, name: string, - type: "interface" | "wasm" | "plugin", + type: string, path: string, logger: Logger ): Promise => { + if (supportedWrapTypes.indexOf(type as WrapManifest["type"]) === -1) { + throw Error(intlMsg.lib_helpers_wrap_unsupportedType({ type })); + } + + const wrapType = type as WrapManifest["type"]; + const relativePath = displayPath(path); return await logActivity( logger, @@ -48,7 +60,7 @@ export const generateWrapFile = async ( path: normalizePath(relativePath), }), async (): Promise => { - await run(abi, name, type, path); + await run(abi, name, wrapType, path); } ); }; From fe10e6a643e54e9caca4f59bd17e982f0f23414e Mon Sep 17 00:00:00 2001 From: dOrgJelli Date: Tue, 3 Jan 2023 01:50:03 -0500 Subject: [PATCH 3/4] lint fix --- packages/cli/src/commands/build.ts | 8 ++++---- packages/cli/src/lib/Compiler.ts | 10 ++++++---- packages/cli/src/lib/helpers/wrap.ts | 2 +- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/cli/src/commands/build.ts b/packages/cli/src/commands/build.ts index eb692727aa..f77fc33b75 100644 --- a/packages/cli/src/commands/build.ts +++ b/packages/cli/src/commands/build.ts @@ -220,7 +220,7 @@ async function run(options: BuildCommandOptions) { project, client, }); - + if (codegen) { const codeGenerator = new CodeGenerator({ project, @@ -228,20 +228,20 @@ async function run(options: BuildCommandOptions) { codegenDirAbs: codegenDir, }); const codegenSuccess = await codeGenerator.generate(); - + if (!codegenSuccess) { logger.error(intlMsg.commands_build_error_codegen_failed()); return false; } } - + const compiler = new Compiler({ project: project as PolywrapProject, outputDir, schemaComposer, buildStrategy, }); - + return await compiler.compile(); } catch (err) { logger.error(err.message); diff --git a/packages/cli/src/lib/Compiler.ts b/packages/cli/src/lib/Compiler.ts index a5c8442385..463d303e67 100644 --- a/packages/cli/src/lib/Compiler.ts +++ b/packages/cli/src/lib/Compiler.ts @@ -78,11 +78,13 @@ export class Compiler { throw Error(intlMsg.lib_compiler_missingBuildStrategy()); } - if (!await this._isWasm()) { + if (!(await this._isWasm())) { const manifest = await project.getManifest(); - throw Error(intlMsg.lib_compiler_cannotBuildModule({ - project: manifest.project.type - })); + throw Error( + intlMsg.lib_compiler_cannotBuildModule({ + project: manifest.project.type, + }) + ); } // Build the sources diff --git a/packages/cli/src/lib/helpers/wrap.ts b/packages/cli/src/lib/helpers/wrap.ts index f96ed3f0fa..946a087080 100644 --- a/packages/cli/src/lib/helpers/wrap.ts +++ b/packages/cli/src/lib/helpers/wrap.ts @@ -13,7 +13,7 @@ import { normalizePath, writeFileSync } from "@polywrap/os-js"; export const supportedWrapTypes: WrapManifest["type"][] = [ "interface", "wasm", - "plugin" + "plugin", ]; const run = async ( From 966a6e65109d162bb4f3437a23ee45679caaca58 Mon Sep 17 00:00:00 2001 From: Pileks Date: Tue, 3 Jan 2023 14:57:48 +0100 Subject: [PATCH 4/4] change array.indexOf to includes --- packages/cli/src/lib/helpers/wrap.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/src/lib/helpers/wrap.ts b/packages/cli/src/lib/helpers/wrap.ts index 946a087080..5af8df3d07 100644 --- a/packages/cli/src/lib/helpers/wrap.ts +++ b/packages/cli/src/lib/helpers/wrap.ts @@ -41,7 +41,7 @@ export const generateWrapFile = async ( path: string, logger: Logger ): Promise => { - if (supportedWrapTypes.indexOf(type as WrapManifest["type"]) === -1) { + if (!supportedWrapTypes.includes(type as WrapManifest["type"])) { throw Error(intlMsg.lib_helpers_wrap_unsupportedType({ type })); }