Skip to content

Commit

Permalink
Merge pull request #22 from pluto-lang/feat-arch-confirm
Browse files Browse the repository at this point in the history
enhance: add arch ref confirmation
  • Loading branch information
Peefy authored Oct 20, 2023
2 parents 88d130e + 0c17f93 commit 95dcabb
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 35 deletions.
3 changes: 2 additions & 1 deletion apps/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"@pluto/static-generator": "workspace:^",
"chalk": "^4.1.2",
"commander": "^11.0.0",
"js-yaml": "^4.1.0"
"js-yaml": "^4.1.0",
"table": "^6.8.1"
},
"devDependencies": {
"@pluto/pluto": "workspace:^",
Expand Down
67 changes: 64 additions & 3 deletions apps/cli/src/commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import path from "path";
import { project } from "@pluto/base";
import { table, TableUserConfig } from "table";
import { confirm } from "@inquirer/prompts";
import { arch, project } from "@pluto/base";
import { BuildAdapterByEngine } from "@pluto/adapters";
import logger from "../log";
import { loadConfig } from "../utils";
Expand Down Expand Up @@ -35,11 +37,17 @@ export async function deploy(files: string[], opts: DeployOptions) {
}

// construct the arch ref from user code
logger.info("Deducing...");
logger.info("Generating reference architecture...");
const archRef = await loadAndDeduce(opts.deducer, files);

const confirmed = await confirmArch(archRef);
if (!confirmed) {
logger.info("You can modify your code and try again.");
process.exit(1);
}

// generate the IR code based on the arch ref
logger.info("Generating...");
logger.info("Generating the IaC Code and computing modules...");
const outdir = path.join(".pluto", sta.name);
const entrypointFile = await loadAndGenerate(opts.generator, archRef, outdir);
if (process.env.DEBUG) {
Expand Down Expand Up @@ -69,3 +77,56 @@ export async function deploy(files: string[], opts: DeployOptions) {
logger.info(`${key}: ${applyResult.outputs[key]}`);
}
}

async function confirmArch(archRef: arch.Architecture): Promise<boolean> {
// Create the resource table for printing.
const resData = [["Name", "Type", "Location"]];
for (let resName in archRef.resources) {
const resource = archRef.resources[resName];
if (resource.type == "Root") continue;

let position = "";
if (resource.locations.length > 0) {
const loc = resource.locations[0];
position = path.basename(loc.file) + `:${loc.linenum.start},${loc.linenum.end}`;
}
resData.push([resName, resource.type, position]);
}

// To display the resource table, which includes the resources in the arch ref
const resConfig: TableUserConfig = {
drawHorizontalLine: (lineIndex: number, rowCount: number) => {
return lineIndex === 0 || lineIndex === 2 || lineIndex === 1 || lineIndex === rowCount;
},
header: {
content: "Resource in Architecture Reference",
},
};
console.log(table(resData, resConfig));

// Create the relationship table for printing.
const relatData = [["From", "To", "Type", "Operation"]];
for (let relat of archRef.relationships) {
if (relat.from.type == "Root") continue;

const typ = relat.type == "access" ? "Access" : "Create";
relatData.push([relat.from.name, relat.to.name, typ, relat.operation]);
}

// To display the relationship table, which includes the relationships among resources in the arch ref.
const relatConfig: TableUserConfig = {
drawHorizontalLine: (lineIndex: number, rowCount: number) => {
return lineIndex === 0 || lineIndex === 2 || lineIndex === 1 || lineIndex === rowCount;
},
header: {
content: "Relationship between Resources",
},
};
console.log(table(relatData, relatConfig));

const result = await confirm({
message: "Does this reference architecture satisfy the design of your application?",
default: true,
});
return result;
}
3 changes: 1 addition & 2 deletions apps/cli/template/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Router, Queue, KVStore, CloudEvent, HttpRequest } from "@pluto/pluto";
import { HttpResponse } from "@pluto/pluto/dist/router";
import { Router, Queue, KVStore, CloudEvent, HttpRequest, HttpResponse } from "@pluto/pluto";

const kvstore = new KVStore("kvstore");
const queue = new Queue("queue");
Expand Down
2 changes: 1 addition & 1 deletion components/deducers/static/src/deducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async function compilePluto(
const startPos = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile));
const endPos = sourceFile.getLineAndCharacterOfPosition(node.getEnd());
const loc: arch.Location = {
file: "main.ts",
file: fileNames[0],
linenum: {
start: `${startPos.line}-${startPos.character}`,
end: `${endPos.line}-${endPos.character}`,
Expand Down
60 changes: 32 additions & 28 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 95dcabb

Please sign in to comment.