Skip to content

Commit

Permalink
refactor: use filemanager in NoirPackage
Browse files Browse the repository at this point in the history
  • Loading branch information
alexghr committed Oct 8, 2023
1 parent ac2921d commit 87c8d75
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 31 deletions.
44 changes: 26 additions & 18 deletions yarn-project/noir-compiler/src/compile/noir/dependency-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>();
dependencies = new Map<string, NoirPackage>();
#fm: Filemanager;
#log: LogFn;

Expand All @@ -24,37 +25,44 @@ 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<void> {
const path =
'git' in dependency ? await this.#fetchRemoteDependency(dependency) : resolve(pkgLocation, dependency.path);
this.#libs.set(name, path);
public async recursivelyResolveDependencies(noirPackage: NoirPackage): Promise<void> {
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()];
}

/**
* Looks up a dependency
* @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;
}
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/noir-compiler/src/compile/noir/filemanager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Filemanager {
*/
public async writeFile(name: string, stream: Readable): Promise<void> {
if (isAbsolute(name)) {
throw new Error("can't check absolute path");
throw new Error("can't write absolute path");
}

const path = this.#getPath(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,20 @@ export class NoirWasmContractCompiler {
* Compiles the project.
*/
public async compile(): Promise<NoirCompilationArtifacts[]> {
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 '';
Expand Down
9 changes: 5 additions & 4 deletions yarn-project/noir-compiler/src/compile/noir/package.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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<NoirPackage> {
const fileContents = await readFile(join(path, CONFIG_FILE_NAME), 'utf-8');
public static async new(path: string, fm: Filemanager): Promise<NoirPackage> {
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);
}
}

0 comments on commit 87c8d75

Please sign in to comment.