Skip to content

Commit

Permalink
refactor: standardise toml parsers (#3910)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexghr authored Jan 9, 2024
1 parent 730766b commit 963035f
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 42 deletions.
1 change: 0 additions & 1 deletion yarn-project/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"@aztec/types": "workspace:^",
"@iarna/toml": "^2.2.5",
"@libp2p/peer-id-factory": "^3.0.4",
"@ltd/j-toml": "^1.38.0",
"commander": "^9.0.0",
"jszip": "^3.10.1",
"lodash.startcase": "^4.4.0",
Expand Down
8 changes: 5 additions & 3 deletions yarn-project/cli/src/cmds/unbox.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { LogFn } from '@aztec/foundation/log';

import { parse, stringify } from '@iarna/toml';
import { parse } from '@iarna/toml';
import { execSync } from 'child_process';
import { appendFileSync, cpSync, existsSync, readFileSync, writeFileSync } from 'fs';
import { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';

import { prettyPrintNargoToml } from '../utils.js';

const resolutions: { [key: string]: string } = {
'@aztec/accounts': 'portal:.aztec-packages/yarn-project/accounts',
'@aztec/aztec.js': 'portal:.aztec-packages/yarn-project/aztec.js',
Expand Down Expand Up @@ -127,7 +129,7 @@ function nargoTomlUpdateToGithubDeps(path: string, cliVersion: string) {
}
});

const updatedToml = stringify(content);
const updatedToml = prettyPrintNargoToml(content);

writeFileSync(path, updatedToml, 'utf-8');
}
Expand All @@ -148,7 +150,7 @@ function nargoTomlUpdateToDevPath(path: string) {
}
});

const updatedToml = stringify(content);
const updatedToml = prettyPrintNargoToml(content);

writeFileSync(path, updatedToml, 'utf-8');
}
31 changes: 4 additions & 27 deletions yarn-project/cli/src/update/noir.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { LogFn } from '@aztec/foundation/log';
import { NoirPackageConfig, parseNoirPackageConfig } from '@aztec/foundation/noir';
import { parseNoirPackageConfig } from '@aztec/foundation/noir';

import TOML from '@ltd/j-toml';
import TOML from '@iarna/toml';
import { readFile } from 'fs/promises';
import { EOL } from 'os';
import { join, relative, resolve } from 'path';

import { atomicUpdateFile } from '../utils.js';
import { atomicUpdateFile, prettyPrintNargoToml } from '../utils.js';
import { DependencyChanges } from './common.js';

/**
Expand Down Expand Up @@ -49,31 +48,9 @@ export async function updateAztecNr(contractPath: string, tag: string, log: LogF
}

if (changes.dependencies.length > 0) {
const contents = prettyPrintTOML(packageConfig);
const contents = prettyPrintNargoToml(packageConfig);
await atomicUpdateFile(configFilepath, contents);
}

return changes;
}

/**
* Pretty prints a NoirPackageConfig to a string
* @param packageConfig - Nargo.toml contents
* @returns The Nargo.toml contents as a string
*/
function prettyPrintTOML(packageConfig: NoirPackageConfig): string {
// hint to TOML.stringify how we want the file to look like
return TOML.stringify(
{
package: TOML.Section(packageConfig.package),
dependencies: TOML.Section(
Object.fromEntries(Object.entries(packageConfig.dependencies).map(([name, dep]) => [name, TOML.inline(dep)])),
),
},
{
indent: 2,
newline: EOL as any,
newlineAround: 'section',
},
);
}
19 changes: 19 additions & 0 deletions yarn-project/cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { AztecAddress } from '@aztec/aztec.js/aztec_address';
import { type L1ContractArtifactsForDeployment } from '@aztec/aztec.js/ethereum';
import { type PXE } from '@aztec/aztec.js/interfaces/pxe';
import { DebugLogger, LogFn } from '@aztec/foundation/log';
import { NoirPackageConfig } from '@aztec/foundation/noir';

import TOML from '@iarna/toml';
import { CommanderError, InvalidArgumentError } from 'commander';
import { readFile, rename, writeFile } from 'fs/promises';

Expand Down Expand Up @@ -205,3 +207,20 @@ export async function atomicUpdateFile(filePath: string, contents: string) {
}
}
}

/**
* Pretty prints Nargo.toml contents to a string
* @param config - Nargo.toml contents
* @returns The Nargo.toml contents as a string
*/
export function prettyPrintNargoToml(config: NoirPackageConfig): string {
const withoutDependencies = Object.fromEntries(Object.entries(config).filter(([key]) => key !== 'dependencies'));

const partialToml = TOML.stringify(withoutDependencies);
const dependenciesToml = Object.entries(config.dependencies).map(([name, dep]) => {
const depToml = TOML.stringify.value(dep);
return `${name} = ${depToml}`;
});

return partialToml + '\n[dependencies]\n' + dependenciesToml.join('\n') + '\n';
}
2 changes: 1 addition & 1 deletion yarn-project/noir-compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"dependencies": {
"@aztec/circuits.js": "workspace:^",
"@aztec/foundation": "workspace:^",
"@ltd/j-toml": "^1.38.0",
"@iarna/toml": "^2.2.5",
"@noir-lang/noir_wasm": "portal:../../noir/packages/noir_wasm",
"base64-js": "^1.5.1",
"commander": "^9.0.0",
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/noir-compiler/src/compile/noir/package.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NoirDependencyConfig, NoirPackageConfig, parseNoirPackageConfig } from '@aztec/foundation/noir';

import { parse } from '@ltd/j-toml';
import { parse } from '@iarna/toml';
import { join } from 'node:path';

import { FileManager } from './file-manager/file-manager.js';
Expand Down
10 changes: 1 addition & 9 deletions yarn-project/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ __metadata:
"@iarna/toml": ^2.2.5
"@jest/globals": ^29.5.0
"@libp2p/peer-id-factory": ^3.0.4
"@ltd/j-toml": ^1.38.0
"@types/jest": ^29.5.0
"@types/lodash.startcase": ^4.4.7
"@types/node": ^18.7.23
Expand Down Expand Up @@ -576,8 +575,8 @@ __metadata:
dependencies:
"@aztec/circuits.js": "workspace:^"
"@aztec/foundation": "workspace:^"
"@iarna/toml": ^2.2.5
"@jest/globals": ^29.5.0
"@ltd/j-toml": ^1.38.0
"@noir-lang/noir_wasm": "portal:../../noir/packages/noir_wasm"
"@types/fs-extra": ^11.0.1
"@types/jest": ^29.5.0
Expand Down Expand Up @@ -2529,13 +2528,6 @@ __metadata:
languageName: node
linkType: hard

"@ltd/j-toml@npm:^1.38.0":
version: 1.38.0
resolution: "@ltd/j-toml@npm:1.38.0"
checksum: 34f5d0ec652e790a7a733f0d3a8d9957d63997bd0efc13a61beb9d772bae75519453884fbc3fd6a2d5fe15674834bdd57ca1824bb1de8f829e5ce195fc5fa3ea
languageName: node
linkType: hard

"@microsoft/tsdoc-config@npm:0.16.2":
version: 0.16.2
resolution: "@microsoft/tsdoc-config@npm:0.16.2"
Expand Down

0 comments on commit 963035f

Please sign in to comment.