Skip to content

Commit

Permalink
chore: token box copies noir source files from noir-contracts on boot…
Browse files Browse the repository at this point in the history
…strap (#2940)

saves a manual copying over whenever the noir source files update
  • Loading branch information
dan-aztec authored Oct 20, 2023
1 parent 1ea2d4f commit a467b96
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 46 deletions.
46 changes: 0 additions & 46 deletions yarn-project/noir-contracts/scripts/compile.sh

This file was deleted.

2 changes: 2 additions & 0 deletions yarn-project/noir-contracts/scripts/types.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
82 changes: 82 additions & 0 deletions yarn-project/noir-contracts/src/scripts/copy_source.ts
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit a467b96

Please sign in to comment.