diff --git a/bin/si-gen-cloud-control/src/pipeline-steps/generateAssetFuncs.ts b/bin/si-gen-cloud-control/src/pipeline-steps/generateAssetFuncs.ts index f819ac20ac..6e15156f5d 100644 --- a/bin/si-gen-cloud-control/src/pipeline-steps/generateAssetFuncs.ts +++ b/bin/si-gen-cloud-control/src/pipeline-steps/generateAssetFuncs.ts @@ -5,6 +5,8 @@ import type { import { FuncSpec } from "../../../../lib/si-pkg/bindings/FuncSpec.ts"; import { SchemaVariantSpec } from "../bindings/SchemaVariantSpec.ts"; import _ from "lodash"; +import { PropSpec } from "../bindings/PropSpec.ts"; +import { strippedBase64 } from "../spec/siFuncs.ts"; export function generateAssetFuncs(specs: PkgSpec[]): PkgSpec[] { const newSpecs = [] as PkgSpec[]; @@ -29,9 +31,7 @@ export function generateAssetFuncs(specs: PkgSpec[]): PkgSpec[] { displayName: null, description: null, handler: "main", - codeBase64: btoa( - assetFuncCode, - ).replace(/=/g, ""), + codeBase64: strippedBase64(assetFuncCode), backendKind: "jsSchemaVariantDefinition", responseType: "schemaVariantDefinition", hidden: false, @@ -57,40 +57,73 @@ export function generateAssetFuncs(specs: PkgSpec[]): PkgSpec[] { function generateAssetCodeFromVariantSpec(variant: SchemaVariantSpec): string { if (variant.domain.kind !== "object") throw "Domain prop is not object"; - const queue = _.cloneDeep(variant.domain.entries); - - let propDeclarations = ""; + // Code for Props + let propDeclarations = `${indent(1)}// Props\n`; let propAdds = ""; - while (queue.length > 0) { - const prop = queue.pop(); + for (const prop of variant.domain.entries) { const varName = `${prop.name}Prop`; - switch (prop.kind) { - case "array": - break; - case "map": - break; - case "object": - break; - case "boolean": - case "json": - case "number": - case "string": - propDeclarations += ` - const ${varName} = new PropBuilder() - .setKind("${prop.kind}") - .setName("${prop.name}") - .build(); -`; - - propAdds += ` - .addProp(${varName})`; - break; - } + propDeclarations += `${indent(1)}const ${varName} = ${ + generatePropBuilderString(prop, 2) + };\n`; + propAdds += `${indent(2)}.addProp(${varName})\n`; } - return `function main() {${propDeclarations} - return new AssetBuilder()${propAdds} - .build(); + // TODO add code for sockets + + return `function main() {\n${propDeclarations} + return new AssetBuilder()\n${propAdds}${indent(2)}.build(); }`; } + +function generatePropBuilderString( + prop: PropSpec, + indent_level: number, +): string { + switch (prop.kind) { + case "array": + case "map": { + const entryBlock = `${indent(indent_level)}.setEntry(\n` + + `${indent(indent_level + 1)}${ + generatePropBuilderString(prop.typeProp, indent_level + 1) + }\n` + + `${indent(indent_level)})\n`; + + return `new PropBuilder()\n` + + `${indent(indent_level)}.setKind("${prop.kind}")\n` + + `${indent(indent_level)}.setName("${prop.name}")\n` + + `${entryBlock}` + + `${indent(indent_level)}.build()`; + } + case "object": { + const children = prop.entries.map((p) => + generatePropBuilderString(p, indent_level + 1) + ); + + let addChildBlock = ""; + + for (const child of children) { + addChildBlock += `${indent(indent_level)}.addChild(\n` + + `${indent(indent_level + 1)}${child}\n` + + `${indent(indent_level)})\n`; + } + + return `new PropBuilder()\n` + + `${indent(indent_level)}.setKind("object")\n` + + `${indent(indent_level)}.setName("${prop.name}")\n` + + `${addChildBlock}` + + `${indent(indent_level)}.build()`; + } + case "number": + return `new PropBuilder().setName("${prop.name}").setKind("integer").build()`; + case "boolean": + case "json": + case "string": + return `new PropBuilder().setName("${prop.name}").setKind("${prop.kind}").build()`; + } +} + +function indent(count: number) { + const spaces = count * 4; + return " ".repeat(spaces); +} diff --git a/bin/si-gen-cloud-control/src/spec/siFuncs.ts b/bin/si-gen-cloud-control/src/spec/siFuncs.ts index 5d9bcf7f66..3d3e23083b 100644 --- a/bin/si-gen-cloud-control/src/spec/siFuncs.ts +++ b/bin/si-gen-cloud-control/src/spec/siFuncs.ts @@ -103,3 +103,10 @@ export function createSiFunc(name: string): FuncSpec { export function getSiFuncId(kind: string): string { return funcSpecs[kind].id; } + +// Si uses a version of base64 that removes the padding at the end for some reason +export function strippedBase64(code: string) { + return btoa( + code, + ).replace(/=/g, ""); +} diff --git a/bin/si-gen-cloud-control/src/specPipeline.ts b/bin/si-gen-cloud-control/src/specPipeline.ts index 17a32ea68f..4ae80bc287 100644 --- a/bin/si-gen-cloud-control/src/specPipeline.ts +++ b/bin/si-gen-cloud-control/src/specPipeline.ts @@ -99,7 +99,7 @@ export function pkgSpecFromCf(src: CfSchema): PkgSpec { version, description: src.description, createdAt: new Date().toISOString(), - createdBy: "Cagador", // TODO Figure out a better name + createdBy: "Clover", // TODO this is still subject to change defaultChangeSet: null, workspacePk: null, workspaceName: null,