Skip to content

Commit

Permalink
Optional YourContract (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
technophile-04 authored Oct 22, 2024
1 parent e950e6f commit 5ad5d8e
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/slow-stingrays-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-eth": patch
---

templates: optional YourContract.sol in extensions
45 changes: 42 additions & 3 deletions src/tasks/copy-template-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import path from "path";
import { promisify } from "util";
import link from "../utils/link";
import { getArgumentFromExternalExtensionOption } from "../utils/external-extensions";
import { BASE_DIR, SOLIDITY_FRAMEWORKS, SOLIDITY_FRAMEWORKS_DIR } from "../utils/consts";
import { BASE_DIR, SOLIDITY_FRAMEWORKS, SOLIDITY_FRAMEWORKS_DIR, EXAMPLE_CONTRACTS_DIR } from "../utils/consts";

const EXTERNAL_EXTENSION_TMP_DIR = "tmp-external-extension";

Expand Down Expand Up @@ -129,6 +129,7 @@ const processTemplatedFiles = async (
{ solidityFramework, externalExtension, dev: isDev }: Options,
basePath: string,
solidityFrameworkPath: string | null,
exampleContractsPath: string | null,
targetDir: string,
) => {
const baseTemplatedFileDescriptors: TemplateDescriptor[] = findFilesRecursiveSync(basePath, path =>
Expand All @@ -151,6 +152,17 @@ const processTemplatedFiles = async (
.flat()
: [];

const starterContractsTemplateFileDescriptors: TemplateDescriptor[] = exampleContractsPath
? findFilesRecursiveSync(exampleContractsPath, filePath => isTemplateRegex.test(filePath))
.map(exampleContractTemplatePath => ({
path: exampleContractTemplatePath,
fileUrl: pathToFileURL(exampleContractTemplatePath).href,
relativePath: exampleContractTemplatePath.split(exampleContractsPath)[1],
source: `example-contracts ${solidityFramework}`,
}))
.flat()
: [];

const externalExtensionFolder = isDev
? typeof externalExtension === "string"
? path.join(basePath, "../../externalExtensions", externalExtension, "extension")
Expand All @@ -174,6 +186,7 @@ const processTemplatedFiles = async (
...baseTemplatedFileDescriptors,
...solidityFrameworkTemplatedFileDescriptors,
...externalExtensionTemplatedFileDescriptors,
...starterContractsTemplateFileDescriptors,
].map(async templateFileDescriptor => {
const templateTargetName = templateFileDescriptor.path.match(isTemplateRegex)?.[1] as string;

Expand All @@ -189,6 +202,14 @@ const processTemplatedFiles = async (
}
}

if (exampleContractsPath) {
const argsFilePath = path.join(exampleContractsPath, argsPath);
const fileExists = fs.existsSync(argsFilePath);
if (fileExists) {
argsFileUrls.push(pathToFileURL(argsFilePath).href);
}
}

if (externalExtension) {
const argsFilePath = isDev
? path.join(basePath, "../../externalExtensions", externalExtension as string, "extension", argsPath)
Expand Down Expand Up @@ -302,11 +323,13 @@ export async function copyTemplateFiles(options: Options, templateDir: string, t
// 2. Copy solidity framework folder
const solidityFrameworkPath =
options.solidityFramework && getSolidityFrameworkPath(options.solidityFramework, templateDir);

if (solidityFrameworkPath) {
await copyExtensionFiles(options, solidityFrameworkPath, targetDir);
}

const exampleContractsPath =
options.solidityFramework && path.resolve(templateDir, EXAMPLE_CONTRACTS_DIR, options.solidityFramework);

// 3. Set up external extension if needed
if (options.externalExtension) {
let externalExtensionPath = path.join(tmpDir, "extension");
Expand All @@ -321,11 +344,27 @@ export async function copyTemplateFiles(options: Options, templateDir: string, t
await setUpExternalExtensionFiles(options, tmpDir);
}

if (options.solidityFramework) {
const externalExtensionSolidityPath = path.join(
externalExtensionPath,
"packages",
options.solidityFramework,
"contracts",
);
// if external extension does not have solidity framework, we copy the example contracts
if (!fs.existsSync(externalExtensionSolidityPath) && exampleContractsPath) {
await copyExtensionFiles(options, exampleContractsPath, targetDir);
}
}

await copyExtensionFiles(options, externalExtensionPath, targetDir);
}

if (!options.externalExtension && options.solidityFramework && exampleContractsPath) {
await copyExtensionFiles(options, exampleContractsPath, targetDir);
}
// 4. Process templated files and generate output
await processTemplatedFiles(options, basePath, solidityFrameworkPath, targetDir);
await processTemplatedFiles(options, basePath, solidityFrameworkPath, exampleContractsPath, targetDir);

// 5. Delete tmp directory
if (options.externalExtension && !options.dev) {
Expand Down
1 change: 1 addition & 0 deletions src/utils/consts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const BASE_DIR = "base";
export const SOLIDITY_FRAMEWORKS_DIR = "solidity-frameworks";
export const EXAMPLE_CONTRACTS_DIR = "example-contracts";

export const SOLIDITY_FRAMEWORKS = {
HARDHAT: "hardhat",
Expand Down
3 changes: 2 additions & 1 deletion src/utils/prompt-for-missing-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ export async function promptForMissingOptions(

const answers = await inquirer.prompt(questions, cliAnswers);

const solidityFramework = options.solidityFramework ?? answers.solidityFramework;
const mergedOptions: Options = {
project: options.project ?? answers.project,
install: options.install,
dev: options.dev ?? defaultOptions.dev,
solidityFramework: options.solidityFramework ?? answers.solidityFramework,
solidityFramework: solidityFramework === "none" ? null : solidityFramework,
externalExtension: options.externalExtension,
};

Expand Down
3 changes: 1 addition & 2 deletions src/utils/show-help-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import chalk from "chalk";

export const showHelpMessage = () => {
console.log(` ${chalk.bold.blue("Usage:")}
${chalk.bold.green("npx create-eth<@version>")} ${chalk.gray("[-i | --install | --skip | --skip-install] [-s <solidity-framework> | --solidity-framework <solidity-framework>] [-e <extension> | --extension <extension>] [-h | --help]")}
${chalk.bold.green("npx create-eth<@version>")} ${chalk.gray("[--skip | --skip-install] [-s <solidity-framework> | --solidity-framework <solidity-framework>] [-e <extension> | --extension <extension>] [-h | --help]")}
`);
console.log(` ${chalk.bold.blue("Options:")}
${chalk.gray("-i, --install")} Install packages
${chalk.gray("--skip, --skip-install")} Skip packages installation
${chalk.gray("-s, --solidity-framework")} Choose solidity framework
${chalk.gray("-e, --extension")} Add curated or third-party extension
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const deploymentsScriptsImports = `import { DeployYourContract } from "./DeployYourContract.s.sol";`;
export const deploymentsLogic = `
DeployYourContract deployYourContract = new DeployYourContract();
deployYourContract.run();
`;
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ const content = ({ deploymentsScriptsImports, deploymentsLogic }) => `//SPDX-Lic
pragma solidity ^0.8.19;
import "./DeployHelpers.s.sol";
import { DeployYourContract } from "./DeployYourContract.s.sol";
${deploymentsScriptsImports.filter(Boolean).join("\n")}
contract DeployScript is ScaffoldETHDeploy {
function run() external {
DeployYourContract deployYourContract = new DeployYourContract();
deployYourContract.run();
${deploymentsLogic.filter(Boolean).join("\n")}
// deploy more contracts here
Expand Down

0 comments on commit 5ad5d8e

Please sign in to comment.