Skip to content

Commit

Permalink
Merge pull request #13 from Autonomous-Finance/templates-refactor
Browse files Browse the repository at this point in the history
Templates refactor
  • Loading branch information
micovi authored Oct 25, 2024
2 parents ce9d78d + 2f8d2e4 commit 0ca256d
Show file tree
Hide file tree
Showing 67 changed files with 4,636 additions and 4,431 deletions.
12 changes: 12 additions & 0 deletions .changeset/bright-trains-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"create-ao-dapp": minor
"site": patch
---

Refactor templates system and Build scripts.

Implement new build scripts that enhance project scaffolding and DX.
Build scripts now check for required dependencies and tries to install them via luarocks.

Teal templates have been updated to use modules and clearer code splitting.

74 changes: 40 additions & 34 deletions create-ao-dapp/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,9 @@ export async function init() {
message: "Enter the name of your project",
placeholder: "sparkling-solid",
}),
processName: () =>
p.text({
message: "What should we name the backend process?",
placeholder: "my-process",
}),
type: ({ results }) =>
p.select({
message: `Pick a starter template within "${results.processName}"`,
message: `Pick a starter template within "${results.projectName}"`,
initialValue: "lua",
maxItems: 5,
options: [
Expand Down Expand Up @@ -80,7 +75,6 @@ export async function init() {
initialValue: "amalg.lua",
options: [
{ value: "amalg.lua", label: "amalg.lua", hint: "recommended" },
{ value: "squishy", label: "squishy" },
],
}),
},
Expand All @@ -94,7 +88,9 @@ export async function init() {

// Change projectName and processName to kebab-case
const projectName = kebabcase(project.projectName);
const processName = kebabcase(project.processName);
const processName = ["lua", "teal"].includes(project.type as string)
? "counter"
: "books";

const templatesDir = resolve(__dirname, "..", "templates");
const destDir = resolve(process.cwd(), projectName);
Expand All @@ -116,38 +112,37 @@ export async function init() {
// Copy process template contents
fs.copySync(processTemplateBase, resolve(destDir, "ao", processName));

// Copy frontend template contents
const frontendTemplate = ["lua", "teal"].includes(project.type as string)
? "lua"
: "lua-sqlite";
// Inject build script into the process dir
const buildSpinner = p.spinner();
buildSpinner.start("Adding build script...");

const baseType = ["teal", "teal-sqlite"].includes(project.type as string)
? "teal"
: "lua";

// Copy the build-lua script
fs.copySync(
resolve(templatesDir, "apps", frontendTemplate),
resolve(destDir, "apps", "frontend")
resolve(templatesDir, "scripts", `build-${baseType}.sh`),
resolve(destDir, "ao", processName, "scripts", "build-lua.sh")
);

// Inject build script into the process dir
const buildSpinner = p.spinner();
buildSpinner.start("Adding build script...");
// Copy the build script
fs.copySync(
resolve(templatesDir, "scripts", "build.sh"),
resolve(destDir, "ao", processName, "scripts", "build.sh")
);

if (project.amalgamation === "squishy") {
fs.copySync(
resolve(templatesDir, "scripts", "build-squishy.sh"),
resolve(destDir, "ao", processName, "scripts", "build.sh")
);
} else {
const buildScriptPath = resolve(
templatesDir,
"scripts",
"build-amalg",
`${project.type}.sh`
);
// Copy the check_deps script
fs.copySync(
resolve(templatesDir, "scripts", `check_deps-${baseType}.sh`),
resolve(destDir, "ao", processName, "scripts", "check_deps.sh")
);

fs.copySync(
buildScriptPath,
resolve(destDir, "ao", processName, "scripts", "build.sh")
);
}
// Copy the install_deps script
fs.copySync(
resolve(templatesDir, "scripts", `install_deps-${baseType}.sh`),
resolve(destDir, "ao", processName, "scripts", "install_deps.sh")
);

buildSpinner.stop("Added build script.");

Expand Down Expand Up @@ -180,6 +175,7 @@ export async function init() {
projectPath: `./${projectName}`,
processName: processName,
packageManager: detectPackageManager(),
processType: project.type as string,
});

spinner.message("Generating aoform.yaml...");
Expand All @@ -192,6 +188,16 @@ export async function init() {

spinner.stop("Added aoform.");

// Copy frontend template contents
const frontendTemplate = ["lua", "teal"].includes(project.type as string)
? "lua"
: "lua-sqlite";

fs.copySync(
resolve(templatesDir, "apps", frontendTemplate),
resolve(destDir, "apps", "frontend")
);

// Replace dotfiles
for (const file of fs.readdirSync(destDir)) {
if (!file.startsWith("_")) continue;
Expand Down
22 changes: 19 additions & 3 deletions create-ao-dapp/src/utils/aoform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,50 @@ import { writeFile } from "node:fs/promises";
import { Document } from "yaml";
import type { PackageManager } from "../types";

const MODULE_BASIC = "bkjb55i07GUCUSWROtKK4HU1mBS_X0TyH3M5jMV6aPg";
const MODULE_SQLITE = "EAIJew2R7aptjpyn7TD7S7ldVW4cTpUhZCaMvcerfWc";
const SCHEDULER = "_GQ33BkPtZrqxA84vM8Zk-N2aO0toNNu_C-l-rawrBA";
const AUTHORITY = "fcoN_xJeisVsPXA-trzVAuIiqO3ydLQxM-L4XbrQKzY";

export default class AoFormGenerator {
projectPath: string;
processName: string;
packageManager: PackageManager;
processType: string;

constructor({
projectPath,
processName,
packageManager,
processType,
}: {
projectPath: string;
processName: string;
packageManager: PackageManager;
processType: string;
}) {
this.projectPath = projectPath;
this.processName = processName;
this.packageManager = packageManager;
this.processType = processType;
}

async generateAoFormYaml() {
const module = ["lua-sqlite", "teal-sqlite"].includes(this.processType)
? MODULE_SQLITE
: MODULE_BASIC;

const doc = new Document([
{
name: this.processName,
file: "build/process.lua",
prerun: "./src/reset_modules.lua",
scheduler: "_GQ33BkPtZrqxA84vM8Zk-N2aO0toNNu_C-l-rawrBA",
module: "GYrbbe0VbHim_7Hi6zrOpHQXrSQz07XNtwCnfbFo2I0",
scheduler: SCHEDULER,
module: module,
tags: [
{
name: "Authority",
value: AUTHORITY,
},
{
name: "Name",
value: this.processName,
Expand Down
3 changes: 3 additions & 0 deletions create-ao-dapp/src/utils/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export function generateScripts({
scripts["frontend:dev"] = `cd ./apps/frontend && ${packageManager} run dev`;
scripts["frontend:build"] = `cd ./apps/frontend && ${packageManager} run build`;
scripts["frontend:deploy"] = `cd ./apps/frontend && ${packageManager} run deploy`;

// Generate postinstall script to chmod scripts
scripts.postinstall = `chmod +x ./ao/${processName}/scripts/* && chmod +x ./apps/frontend/scripts/*`

return scripts;
}
8 changes: 0 additions & 8 deletions create-ao-dapp/templates/ao/lua-sqlite/squishy/squishy

This file was deleted.

31 changes: 31 additions & 0 deletions create-ao-dapp/templates/ao/lua/base/src/lib/counter.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
local mod = {}

--- Handler to increase Counter
--- @param msg Message
--- @return nil
mod.increaseCounter = function(msg)
Counter = Counter + 1

ao.send({
Target = msg.From,
Action = "Increase",
Counter = tostring(Counter),
Data = tostring(Counter)
})
end

--- Handler to reset Counter
--- @param msg Message
--- @return nil
mod.resetCounter = function(msg)
Counter = 0

ao.send({
Target = msg.From,
Action = "Reset",
Counter = tostring(Counter),
Data = tostring(Counter)
})
end

return mod
58 changes: 0 additions & 58 deletions create-ao-dapp/templates/ao/lua/base/src/lib/process_lib.lua

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
local process = require "lib.process_lib"
local process = require "lib.counter"

--- @type string
Version = "0.0.1"

--- @type number
Counter = Counter or 0

--- Handler to get Info
--- @param msg Message
--- @return nil
local function infoHandler(msg)
ao.send({
Target = msg.From,
Version = Version,
Counter = tostring(Counter)
})
end

-- Handler to get Info
Handlers.add("info",
Handlers.utils.hasMatchingTag("Action", "Info"),
process.getInfo
infoHandler
)

-- Handler to increase Counter
Expand Down
1 change: 0 additions & 1 deletion create-ao-dapp/templates/ao/lua/base/src/reset_modules.lua

This file was deleted.

6 changes: 0 additions & 6 deletions create-ao-dapp/templates/ao/lua/squishy/squishy

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 0ca256d

Please sign in to comment.