Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: build cache #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@types/debug": "^4.1.7",
"@types/fs-extra": "^5.0.4",
"@types/glob": "^7.2.0",
"@types/lodash": "^4.14.182",
"@types/mocha": "^5.2.6",
"@types/node": "^8.10.38",
"@types/semver": "^7.3.9",
Expand All @@ -54,6 +55,7 @@
},
"dependencies": {
"glob": "^8.0.1",
"lodash": "^4.17.21",
"semver": "^7.3.7"
},
"bugs": {
Expand Down
131 changes: 131 additions & 0 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import type { LoDashStatic } from "lodash";
import type { ProjectPathsConfig } from "hardhat/types/config";
import type { FeConfig } from "./types";

import path from "path";
import fsExtra from "fs-extra";
import * as t from "io-ts";

import { CACHE_FORMAT_VERSION, FE_FILES_CACHE_FILENAME } from "./constants";
import { getLogger } from "./util";

const log = getLogger("cache");

const CacheEntryCodec = t.type({
lastModificationDate: t.number,
contentHash: t.string,
sourceName: t.string,
feConfig: t.any,
versionPragma: t.string,
artifacts: t.array(t.string),
});

const CacheCodec = t.type({
_format: t.string,
files: t.record(t.string, CacheEntryCodec),
});

export interface CacheEntry {
lastModificationDate: number;
contentHash: string;
sourceName: string;
feConfig: FeConfig;
versionPragma: string;
artifacts: string[];
}

export interface Cache {
_format: string;
files: Record<string, CacheEntry>;
}

export class FeFilesCache {
constructor(private _cache: Cache) { }

public static createEmpty(): FeFilesCache {
return new FeFilesCache({ _format: CACHE_FORMAT_VERSION, files: {} });
}

public static async readFromFile(
feFilesCachePath: string
): Promise<FeFilesCache> {
const cacheRaw: Cache = fsExtra.existsSync(feFilesCachePath)
? fsExtra.readJSONSync(feFilesCachePath)
: {
_format: CACHE_FORMAT_VERSION,
files: {},
};

const result = CacheCodec.decode(cacheRaw);

if (result.isRight()) {
const feFilesCache = new FeFilesCache(result.value);
await feFilesCache.removeNonExistingFiles();
return feFilesCache;
}

log("There was a problem reading the cache");

return FeFilesCache.createEmpty();
}

public async removeNonExistingFiles() {
for (const absolutePath of Object.keys(this._cache.files)) {
if (!fsExtra.existsSync(absolutePath)) {
this.removeEntry(absolutePath);
}
}
}

public async writeToFile(feFilesCachePath: string) {
await fsExtra.outputJson(feFilesCachePath, this._cache, { spaces: 2 });
}

public addFile(absolutePath: string, entry: CacheEntry) {
this._cache.files[absolutePath] = entry;
}

public getEntries(): CacheEntry[] {
return Object.values(this._cache.files);
}

public getEntry(file: string): CacheEntry | undefined {
return this._cache.files[file];
}

public removeEntry(file: string) {
delete this._cache.files[file];
}

public hasFileChanged(
absolutePath: string,
contentHash: string,
feConfig?: FeConfig
): boolean {
const { isEqual }: LoDashStatic = require("lodash");

const cacheEntry = this.getEntry(absolutePath);

if (cacheEntry === undefined) {
// new file or no cache available, assume it's new
return true;
}

if (cacheEntry.contentHash !== contentHash) {
return true;
}

if (
feConfig !== undefined &&
!isEqual(feConfig, cacheEntry.feConfig)
) {
return true;
}

return false;
}
}

export function getFeFilesCachePath(paths: ProjectPathsConfig): string {
return path.join(paths.cache, FE_FILES_CACHE_FILENAME);
}
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ export const DEBUG_NAMESPACE = "hardhat:plugin:fe";

export const GITHUB_RELEASES_URL =
"https://api.github.com/repos/ethereum/fe/releases?per_page=100";

export const CACHE_FORMAT_VERSION = "hh-fe-cache-1";
export const FE_FILES_CACHE_FILENAME = "fe-files-cache.json";
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ export interface FeBuild {
compilerPath: string;
}

export interface FeConfig {
version: string;
}

export enum CompilerPlatform {
LINUX = "amd64",
MACOS = "mac",
Expand Down