From a467b9601fabe3f6038d9231132f910079763a9b Mon Sep 17 00:00:00 2001 From: Dan Lee <142251406+dan-aztec@users.noreply.github.com> Date: Fri, 20 Oct 2023 04:33:23 -0700 Subject: [PATCH] chore: token box copies noir source files from noir-contracts on bootstrap (#2940) saves a manual copying over whenever the noir source files update --- .../noir-contracts/scripts/compile.sh | 46 ----------- yarn-project/noir-contracts/scripts/types.sh | 2 + .../noir-contracts/src/scripts/copy_source.ts | 82 +++++++++++++++++++ 3 files changed, 84 insertions(+), 46 deletions(-) delete mode 100755 yarn-project/noir-contracts/scripts/compile.sh create mode 100644 yarn-project/noir-contracts/src/scripts/copy_source.ts diff --git a/yarn-project/noir-contracts/scripts/compile.sh b/yarn-project/noir-contracts/scripts/compile.sh deleted file mode 100755 index 09c1d896f6c..00000000000 --- a/yarn-project/noir-contracts/scripts/compile.sh +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/bash - -# Compiles Aztec.nr contracts in parallel, bubbling any compilation errors - -source ./scripts/catch.sh -source ./scripts/nargo_check.sh - -ROOT=$(pwd) - -# Error flag file -error_file="/tmp/error.$$" -# Array of child PIDs -pids=() - -# Set SIGCHLD handler -trap handle_sigchld SIGCHLD # Trap any ERR signal and call the custom error handler - -build() { - CONTRACT_NAME=$1 - CONTRACT_FOLDER="${CONTRACT_NAME}_contract" - echo "Compiling $CONTRACT_NAME..." - rm -f target/${CONTRACT_FOLDER}-* - rm -f target/debug_${CONTRACT_FOLDER}-* - - # If the compilation fails, rerun the compilation with 'nargo' and show the compiler output. - nargo compile --package $CONTRACT_FOLDER --output-debug; -} - -# Check nargo version -nargo_check - -# Build contracts -for CONTRACT_NAME in "$@"; do - build $CONTRACT_NAME & - pids+=($!) -done - -# Wait for all background processes to finish -wait - -# If error file exists, exit with error -if [ -f "$error_file" ]; then - rm "$error_file" - echo "Error occurred in one or more child processes. Exiting..." - exit 1 -fi diff --git a/yarn-project/noir-contracts/scripts/types.sh b/yarn-project/noir-contracts/scripts/types.sh index 881978d17d9..2747a38ef9b 100755 --- a/yarn-project/noir-contracts/scripts/types.sh +++ b/yarn-project/noir-contracts/scripts/types.sh @@ -44,6 +44,8 @@ process() { CONTRACT=$1 cd $ROOT + NODE_OPTIONS=--no-warnings yarn ts-node --esm src/scripts/copy_source.ts $CONTRACT_NAME + echo "Creating types for $CONTRACT" NODE_OPTIONS=--no-warnings yarn ts-node --esm src/scripts/copy_output.ts $CONTRACT_NAME } diff --git a/yarn-project/noir-contracts/src/scripts/copy_source.ts b/yarn-project/noir-contracts/src/scripts/copy_source.ts new file mode 100644 index 00000000000..dccd72f379c --- /dev/null +++ b/yarn-project/noir-contracts/src/scripts/copy_source.ts @@ -0,0 +1,82 @@ +/* eslint-disable jsdoc/require-jsdoc */ +import { createConsoleLogger } from '@aztec/foundation/log'; + +import * as fs from 'fs'; +import snakeCase from 'lodash.snakecase'; +import * as path from 'path'; +import { format } from 'util'; + +// heavily copying yarn-project/noir-contracts/src/scripts/copy_output.ts +const log = createConsoleLogger('aztec:noir-contracts:source_copy'); + +/** + * for the typechecker... + */ +interface NoirSourceCopy { + name: string; + target: string; + exclude: string[]; +} + +const NOIR_SOURCE_COPIES: NoirSourceCopy[] = [ + { name: 'PrivateToken', target: '../boxes/private-token/src/artifacts', exclude: [] }, +]; + +/** + * Sometimes we want to duplicate the noir source code elsewhere, + * for example in the boxes we provide as Aztec Quickstarts. + * @param contractName - UpperCamelCase contract name that we check need copying + */ +function copyNrFilesExceptInterface(contractName: string): void { + // stored in `noir-contracts` under snake case nameing + const snakeCaseContractName = `${snakeCase(contractName)}_contract`; + const projectDirPath = `src/contracts/${snakeCaseContractName}`; + + for (const noirCopy of NOIR_SOURCE_COPIES) { + if (noirCopy.name === contractName) { + const target = noirCopy.target; + + try { + // Ensure target directory exists + if (!fs.existsSync(target)) { + throw Error(`target copy path ${target} doesnt exist`); + } + // Read the project directory + const files = fs.readdirSync(projectDirPath); + + // Filter and copy *.nr files except interface.nr + files + .filter( + file => + file.endsWith('.nr') && + file !== 'interface.nr' && + (!noirCopy.exclude || !noirCopy.exclude.includes(file)), + ) + .forEach(file => { + const sourcePath = path.join(projectDirPath, file); + const targetPath = path.join(target, file); + log(`copying ${sourcePath} to ${targetPath}`); + fs.copyFileSync(sourcePath, targetPath); + }); + + log(`Copied .nr files from ${contractName} to ${target} successfully!`); + } catch (err) { + log(format(`Error copying files from ${contractName} to ${target}:`, err)); + } + } + } +} + +const main = () => { + const contractName = process.argv[2]; + if (!contractName) throw new Error(`Missing argument contract name`); + + copyNrFilesExceptInterface(contractName); +}; + +try { + main(); +} catch (err: unknown) { + log(format(`Error copying build output`, err)); + process.exit(1); +}