From 87c8d756f3218474a14bfb30c11f0dd9cd808869 Mon Sep 17 00:00:00 2001 From: Alex Gherghisan Date: Sun, 8 Oct 2023 23:21:06 +0100 Subject: [PATCH] refactor: use filemanager in NoirPackage --- .../src/compile/noir/dependency-resolver.ts | 44 +++++++++++-------- .../src/compile/noir/filemanager.ts | 2 +- .../src/compile/noir/noir-wasm-compiler.ts | 13 +++--- .../noir-compiler/src/compile/noir/package.ts | 9 ++-- 4 files changed, 37 insertions(+), 31 deletions(-) diff --git a/yarn-project/noir-compiler/src/compile/noir/dependency-resolver.ts b/yarn-project/noir-compiler/src/compile/noir/dependency-resolver.ts index 01153eb8b27..0c3342e3ff8 100644 --- a/yarn-project/noir-compiler/src/compile/noir/dependency-resolver.ts +++ b/yarn-project/noir-compiler/src/compile/noir/dependency-resolver.ts @@ -8,13 +8,14 @@ import { ReadableStream } from 'node:stream/web'; import { Parse } from 'tar'; import { Filemanager } from './filemanager.js'; -import { NoirGitDependencyConfig, NoirLocalDependencyConfig } from './package-config.js'; +import { NoirGitDependencyConfig } from './package-config.js'; +import { NoirPackage } from './package.js'; /** * Noir Dependency Resolver */ export class NoirDependencyResolver { - #libs = new Map(); + dependencies = new Map(); #fm: Filemanager; #log: LogFn; @@ -24,26 +25,32 @@ export class NoirDependencyResolver { } /** - * Resolves a dependency. - * @param pkgLocation - Location of the package - * @param name - Name of the dependency - * @param dependency - Dependency to resolve + * Resolves dependencies for a package. + * @param noirPackage - The package to resolve dependencies for */ - public async add( - pkgLocation: string, - name: string, - dependency: NoirGitDependencyConfig | NoirLocalDependencyConfig, - ): Promise { - const path = - 'git' in dependency ? await this.#fetchRemoteDependency(dependency) : resolve(pkgLocation, dependency.path); - this.#libs.set(name, path); + public async recursivelyResolveDependencies(noirPackage: NoirPackage): Promise { + for (const [name, config] of Object.entries(noirPackage.getDependencies())) { + // TODO what happens if more than one package has the same name but different versions? + if (this.dependencies.has(name)) { + continue; + } + + const path = + 'git' in config + ? await this.#fetchRemoteDependency(config) + : resolve(noirPackage.getPackagePath(), config.path); + const dependency = await NoirPackage.new(path, this.#fm); + this.dependencies.set(name, dependency); + + await this.recursivelyResolveDependencies(dependency); + } } /** * Gets the names of the crates in this dependency list */ public getCrateNames() { - return [...this.#libs.keys()]; + return [...this.dependencies.keys()]; } /** @@ -51,10 +58,11 @@ export class NoirDependencyResolver { * @param sourceId - The source being resolved * @returns The path to the resolved file */ - public resolve(sourceId: string): string | null { + public findFile(sourceId: string): string | null { const [lib, ...path] = sourceId.split('/').filter(x => x); - if (this.#libs.has(lib)) { - return join(this.#libs.get(lib)!, 'src', ...path); + const pkg = this.dependencies.get(lib); + if (pkg) { + return join(pkg.getSrcPath(), ...path); } else { return null; } diff --git a/yarn-project/noir-compiler/src/compile/noir/filemanager.ts b/yarn-project/noir-compiler/src/compile/noir/filemanager.ts index 166d2612a8c..c0bd56131d1 100644 --- a/yarn-project/noir-compiler/src/compile/noir/filemanager.ts +++ b/yarn-project/noir-compiler/src/compile/noir/filemanager.ts @@ -22,7 +22,7 @@ export class Filemanager { */ public async writeFile(name: string, stream: Readable): Promise { if (isAbsolute(name)) { - throw new Error("can't check absolute path"); + throw new Error("can't write absolute path"); } const path = this.#getPath(name); diff --git a/yarn-project/noir-compiler/src/compile/noir/noir-wasm-compiler.ts b/yarn-project/noir-compiler/src/compile/noir/noir-wasm-compiler.ts index d9e9c0e0798..c190738425a 100644 --- a/yarn-project/noir-compiler/src/compile/noir/noir-wasm-compiler.ts +++ b/yarn-project/noir-compiler/src/compile/noir/noir-wasm-compiler.ts @@ -21,23 +21,20 @@ export class NoirWasmContractCompiler { * Compiles the project. */ public async compile(): Promise { - const noirPackage = await NoirPackage.new(this.#projectPath); + const cacheRoot = process.env.XDG_CACHE_HOME ?? join(process.env.HOME ?? '', '.cache'); + const filemanager = new Filemanager(join(cacheRoot, 'noir_wasm')); + const noirPackage = await NoirPackage.new(this.#projectPath, filemanager); if (noirPackage.getType() !== 'contract') { throw new Error('This is not a contract project'); } - const cacheRoot = process.env.XDG_CACHE_HOME ?? join(process.env.HOME ?? '', '.cache'); - const filemanager = new Filemanager(join(cacheRoot, 'noir_wasm')); const dependencyResolver = new NoirDependencyResolver(filemanager); - - for (const [name, config] of Object.entries(noirPackage.getDependencies())) { - await dependencyResolver.add(noirPackage.getPackagePath(), name, config); - } + await dependencyResolver.recursivelyResolveDependencies(noirPackage); initializeResolver((sourceId: any) => { try { - const libFile = dependencyResolver.resolve(sourceId); + const libFile = dependencyResolver.findFile(sourceId); return filemanager.readFileSync(libFile ?? sourceId, 'utf-8'); } catch (err) { return ''; diff --git a/yarn-project/noir-compiler/src/compile/noir/package.ts b/yarn-project/noir-compiler/src/compile/noir/package.ts index eb4b43dc1d4..771f2959e30 100644 --- a/yarn-project/noir-compiler/src/compile/noir/package.ts +++ b/yarn-project/noir-compiler/src/compile/noir/package.ts @@ -1,7 +1,7 @@ -import { exists, readFile } from 'node:fs/promises'; import { join } from 'node:path'; import { parse } from 'toml'; +import { Filemanager } from './filemanager.js'; import { NoirGitDependencyConfig, NoirLocalDependencyConfig, @@ -78,17 +78,18 @@ export class NoirPackage { /** * Opens a path on the filesystem. * @param path - Path to the package. + * @param fm - Filemanager to use. * @returns The Noir package at the given location */ - public static async new(path: string): Promise { - const fileContents = await readFile(join(path, CONFIG_FILE_NAME), 'utf-8'); + public static async new(path: string, fm: Filemanager): Promise { + const fileContents = fm.readFileSync(join(path, CONFIG_FILE_NAME), 'utf-8'); const config = parse(fileContents); if (!isPackageConfig(config)) { throw new Error('Invalid package configuration'); } - const srcDir = (await exists(join(path, 'src'))) ? join(path, 'src') : path; + const srcDir = (await fm.hasEntry(join(path, 'src'))) ? join(path, 'src') : path; return new NoirPackage(path, srcDir, config); } }