Skip to content

Commit

Permalink
chore(boxes): adding clone contract option
Browse files Browse the repository at this point in the history
  • Loading branch information
signorecello committed Mar 6, 2024
1 parent 8ecc3cc commit 5654d65
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 38 deletions.
4 changes: 2 additions & 2 deletions boxes/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Command } from "commander";
const program = new Command();
import axios from "axios";
import { chooseAndCloneBox } from "./scripts/steps/chooseBox.js";
import { chooseProject } from "./scripts/steps/chooseBox.js";
import { sandboxRun } from "./scripts/steps/sandbox/run.js";
import { sandboxInstallOrUpdate } from "./scripts/steps/sandbox/install.js";

Expand Down Expand Up @@ -34,7 +34,7 @@ const tagToUse = versionToInstall.match(/^\d+\.\d+\.\d+$/)

program.action(async () => {
// STEP 1: Choose the boilerplate
await chooseAndCloneBox(tagToUse, versionToInstall);
await chooseProject(tagToUse, versionToInstall);

// STEP 2: Install the Sandbox
await sandboxInstallOrUpdate(latestStable, versionToInstall);
Expand Down
1 change: 1 addition & 0 deletions boxes/scripts/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const CONTRACTS_TO_SHOW = ["token_contract"];
111 changes: 88 additions & 23 deletions boxes/scripts/steps/chooseBox.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,34 @@
import select from "@inquirer/select";
import input from "@inquirer/input";
import tiged from "tiged";
import { getAvailableBoxes, replacePaths } from "../utils.js";
import {
getAvailableBoxes,
replacePaths,
getAvailableContracts,
} from "../utils.js";
import chalk from "chalk";
import ora from "ora";
const { log } = console;

export async function chooseAndCloneBox(tag, version) {
const availableBoxes = await getAvailableBoxes(tag, version);
const appType = await select({
message: `Please choose your Aztec boilerplate:`,
choices: [
...availableBoxes.map((box) => {
return { value: box.name, name: box.description };
}),
{ value: "skip", name: "Skip this step" },
],
});

if (appType === "skip") return;

log(chalk.yellow(`You chose: ${appType}`));

async function clone({ path, choice, type, tag, version }) {
const spinner = ora({
text: "Cloning the boilerplate code...",
text: `Cloning the ${type} code...`,
color: "blue",
});

try {
// STEP 1: Clone the box
// STEP 1: Clone the contract
const appName = await input({
message: "Your app name:",
default: "my-aztec-app",
message: `Your ${type} name:`,
default: `my-aztec-${type}`,
});

spinner.start();

const emitter = tiged(
// same as the nargo dependencies above:
// but if the user has set a semver version, we want that tag (i.e. aztec-packages-v0.23.0)
`AztecProtocol/aztec-packages/boxes/${appType}${tag && `#${tag}`}`,
`AztecProtocol/aztec-packages/${path}/${choice}${tag && `#${tag}`}`,
{
verbose: true,
},
Expand All @@ -50,7 +39,12 @@ export async function chooseAndCloneBox(tag, version) {
});

await emitter.clone(`./${appName}`).then(() => {
replacePaths(`./${appName}`, tag, version);
replacePaths({
rootDir: `./${appName}`,
tag,
version,
prefix: type === "contract" ? "/noir-projects" : "",
});
log(chalk.bgGreen("Your code is ready!"));
});
} catch (error) {
Expand All @@ -60,3 +54,74 @@ export async function chooseAndCloneBox(tag, version) {
spinner.stop();
}
}

async function chooseAndCloneBox(tag, version) {
const availableBoxes = await getAvailableBoxes(tag, version);
const appType = await select({
message: `Please choose your Aztec boilerplate:`,
choices: [
...availableBoxes.map((box) => {
return { value: box.name, name: box.description };
}),
{ value: "skip", name: "Skip this step" },
],
});

log(chalk.yellow(`You chose: ${appType}`));

// TODO: Once the downstream zpedro/npx_improvs is merged, this path will change to boxes/boxes
await clone({
path: "boxes",
choice: appType,
type: "box",
tag,
version,
});
}

async function chooseAndCloneContract(tag, version) {
const availableContracts = await getAvailableContracts(tag, version);
// let user choose one of the contracts in noir-projects
const contract = await select({
message: `Please choose your Aztec boilerplate:`,
choices: [
...availableContracts.map((contract) => {
return { value: contract.name, name: contract.name };
}),
{ value: "skip", name: "Skip this step" },
],
});

// clone that specific contract into the user's folder

log(chalk.yellow(`You chose: ${contract}`));

await clone({
path: "noir-projects/noir-contracts/contracts",
choice: contract,
type: "contract",
tag,
version,
});

// get the e2e test for that contract from yarn-project/end-to-end
}

export async function chooseProject(tag, version) {
const projectType = await select({
message: `Please choose your type of project:`,
choices: [
{ value: "fs_app", name: "Boilerplate project with frontend" },
{ value: "contract_only", name: "Just a contract example" },
{ value: "skip", name: "Skip this step" },
],
});

if (projectType === "skip") {
return;
} else if (projectType === "contract_only") {
await chooseAndCloneContract(tag, version);
} else if (projectType === "fs_app") {
await chooseAndCloneBox(tag, version);
}
}
47 changes: 34 additions & 13 deletions boxes/scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import os from "os";
import fs from "fs";
import { parse, stringify } from "@iarna/toml";
import axios from "axios";
import { CONTRACTS_TO_SHOW } from "./config.js";

const { log, warn, info } = console;
const targetDir = path.join(os.homedir(), ".aztec/bin"); // Use os.homedir() to get $HOME
Expand All @@ -14,20 +15,15 @@ export async function getAvailableBoxes(tag, version) {
axiosOpts.headers = { Authorization: `token ${GITHUB_TOKEN}` };
}

// TODO: Remove this try catch. Boxes are currently in "boxes" but from this PR on, they will be in "boxes/boxes"
// TODO: Once the downstream zpedro/npx_improvs is merged, this path will change to boxes/boxes
let data;
try {
({ data } = await axios.get(
`https://api.github.com/repos/AztecProtocol/aztec-packages/contents/boxes/boxes${tag == "master" ? "" : `?ref=${tag}`}`,
`https://api.github.com/repos/AztecProtocol/aztec-packages/contents/boxes${tag == "master" ? "" : `?ref=${tag}`}`,
axiosOpts,
));
} catch (e) {
if (e.response.statusText === "Not Found") {
({ data } = await axios.get(
`https://api.github.com/repos/AztecProtocol/aztec-packages/contents/boxes${tag == "master" ? "" : `?ref=${tag}`}`,
axiosOpts,
));
}
log(e);
}

let availableBoxes = data
Expand All @@ -49,6 +45,30 @@ export async function getAvailableBoxes(tag, version) {
return await Promise.all(availableBoxes);
}

export async function getAvailableContracts(tag, version) {
const { GITHUB_TOKEN } = process.env;
const axiosOpts = {};
if (GITHUB_TOKEN) {
axiosOpts.headers = { Authorization: `token ${GITHUB_TOKEN}` };
}

let data;
try {
({ data } = await axios.get(
`https://api.github.com/repos/AztecProtocol/aztec-packages/contents/noir-projects/noir-contracts/contracts${tag == "master" ? "" : `?ref=${tag}`}`,
axiosOpts,
));
} catch (e) {
log(e);
}

let availableContracts = data.filter((content) =>
CONTRACTS_TO_SHOW.includes(content.name),
);

return await Promise.all(availableContracts);
}

export function prettyPrintNargoToml(config) {
const withoutDependencies = Object.fromEntries(
Object.entries(config).filter(([key]) => key !== "dependencies"),
Expand Down Expand Up @@ -95,15 +115,16 @@ export function updatePathEnvVar() {
info(`Added ${targetDir} to PATH in ${shellProfile}.`);
}

export async function replacePaths(rootDir, tag, version) {
const files = fs.readdirSync(path.resolve(".", rootDir), {
export async function replacePaths({ rootDir, tag, version, prefix = "" }) {
console.log("rootDir", rootDir);
const files = fs.readdirSync(path.resolve(rootDir), {
withFileTypes: true,
});

files.forEach((file) => {
const filePath = path.join(rootDir, file.name);
if (file.isDirectory()) {
replacePaths(filePath, tag, version); // Recursively search subdirectories
replacePaths({ rootDir: filePath, tag, version, prefix }); // Recursively search subdirectories
} else if (file.name === "Nargo.toml") {
let content = parse(fs.readFileSync(filePath, "utf8"));

Expand All @@ -114,9 +135,9 @@ export async function replacePaths(rootDir, tag, version) {
"",
);
content.dependencies[dep] = {
git: "https://github.com/AztecProtocol/aztec-packages/",
git: "https://github.com/AztecProtocol/aztec-packages",
tag,
directory,
directory: `${prefix}/${directory}`,
};
});
} catch (e) {
Expand Down

0 comments on commit 5654d65

Please sign in to comment.