This repository was archived by the owner on Jan 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathSolidityProjectCompiler.ts
More file actions
208 lines (183 loc) · 7.18 KB
/
Copy pathSolidityProjectCompiler.ts
File metadata and controls
208 lines (183 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import path from 'path';
import max from 'lodash.max';
import maxBy from 'lodash.maxby';
import pick from 'lodash.pick';
import omitBy from 'lodash.omitby';
import isUndefined from 'lodash.isundefined';
import { readJsonSync, ensureDirSync, readJSON, writeJson, unlink } from 'fs-extra';
import { statSync, existsSync, lstatSync } from 'fs';
import readdirSync from 'fs-readdir-recursive';
import { Loggy, Contracts } from '@openzeppelin/upgrades';
import {
RawContract,
CompiledContract,
CompilerOptions,
resolveCompilerVersion,
compileWith,
DEFAULT_OPTIMIZER,
DEFAULT_EVM_VERSION,
} from './SolidityContractsCompiler';
import { ImportsFsEngine } from '@openzeppelin/resolver-engine-imports-fs';
import { gatherSources } from './ResolverEngineGatherer';
import { SolcBuild } from './CompilerProvider';
import { compilerVersionsMatch, compilerSettingsMatch } from '../../../utils/solidity';
import { tryFunc } from '../../../utils/try';
export async function compileProject(options: ProjectCompilerOptions = {}): Promise<ProjectCompileResult> {
const projectCompiler = new SolidityProjectCompiler({
...options,
inputDir: options.inputDir || Contracts.getLocalContractsDir(),
outputDir: options.outputDir || Contracts.getLocalBuildDir(),
});
await projectCompiler.call();
return {
contracts: projectCompiler.contracts,
compilerVersion: projectCompiler.compilerVersion,
};
}
export interface ProjectCompilerOptions extends CompilerOptions {
manager?: string;
inputDir?: string;
outputDir?: string;
force?: boolean;
}
export interface ProjectCompileResult {
compilerVersion: SolcBuild;
contracts: RawContract[];
}
class SolidityProjectCompiler {
public roots: string[];
public contracts: RawContract[];
public compilerOutput: CompiledContract[];
public compilerVersion: SolcBuild;
public options: ProjectCompilerOptions;
public constructor(options: ProjectCompilerOptions = {}) {
this.roots = [];
this.contracts = [];
this.compilerOutput = [];
this.options = options;
}
public get inputDir(): string {
return this.options.inputDir;
}
public get outputDir(): string {
return this.options.outputDir;
}
public async call(): Promise<void> {
await this._loadSoliditySourcesFromDir();
await this._loadDependencies();
if (this.contracts.length === 0) {
Loggy.noSpin(__filename, 'call', 'compile-contracts', 'No contracts found to compile.');
return;
}
await this._resolveCompilerVersion();
if (!this._shouldCompile()) {
Loggy.noSpin(__filename, 'call', `compile-contracts`, 'Nothing to compile, all contracts are up to date.');
return;
}
Loggy.spin(
__filename,
'call',
'compile-contracts',
`Compiling contracts with solc ${this.compilerVersion.version} (${this.compilerVersion.build})`,
);
this.compilerOutput = await compileWith(this.compilerVersion, this.contracts, this.options);
await this._writeOutput();
Loggy.succeed(
'compile-contracts',
`Compiled contracts with solc ${this.compilerVersion.version} (${this.compilerVersion.build})`,
);
}
private _loadSoliditySourcesFromDir(dir = this.inputDir): void {
if (!existsSync(dir) || !lstatSync(dir).isDirectory) return;
// TODO: Replace by a glob expression
readdirSync(dir).forEach(fileName => {
const filePath = path.resolve(dir, fileName);
if (lstatSync(filePath).isDirectory()) {
this._loadSoliditySourcesFromDir(filePath);
} else if (path.extname(filePath).toLowerCase() === '.sol') {
this.roots.push(filePath);
}
});
}
private async _loadDependencies() {
const importFiles = await gatherSources(this.roots, this.inputDir, ImportsFsEngine());
const cwd = process.cwd();
this.contracts = importFiles.map(file => ({
fileName: path.basename(file.url),
filePath: path.isAbsolute(file.url) ? path.relative(cwd, file.url) : file.url,
source: file.source,
lastModified: tryFunc(() => statSync(file.url).mtimeMs),
}));
}
private async _resolveCompilerVersion() {
this.compilerVersion = await resolveCompilerVersion(this.contracts, this.options);
}
private _shouldCompile(): boolean {
if (this.options.force) return true;
const artifacts = this._listArtifacts();
const artifactsWithMtimes = artifacts.map(artifact => ({
artifact,
mtime: statSync(artifact).mtimeMs,
}));
// We pick a single artifact (the most recent one) to get the version it was compiled with
const latestArtifact = maxBy(artifactsWithMtimes, 'mtime');
const latestSchema = latestArtifact && readJsonSync(latestArtifact.artifact);
const artifactCompiledVersion = latestSchema && latestSchema.compiler.version;
const artifactSettings = latestSchema && pick(latestSchema.compiler, 'evmVersion', 'optimizer');
// Build current settings based on defaults
const currentSettings = {
optimizer: DEFAULT_OPTIMIZER,
evmVersion: DEFAULT_EVM_VERSION,
...omitBy(this.options, isUndefined),
};
// Gather artifacts vs sources modified times
const maxArtifactsMtimes = latestArtifact && latestArtifact.mtime;
const maxSourcesMtimes = max(this.contracts.map(({ lastModified }) => lastModified));
// Compile if there are no previous artifacts, or no mtimes could be collected for sources,
// or sources were modified after artifacts, or compiler version changed, or compiler settings changed
return (
!maxArtifactsMtimes ||
!maxSourcesMtimes ||
maxArtifactsMtimes < maxSourcesMtimes ||
!artifactCompiledVersion ||
!compilerVersionsMatch(artifactCompiledVersion, this.compilerVersion.longVersion) ||
!compilerSettingsMatch(currentSettings, artifactSettings)
);
}
private async _writeOutput(): Promise<void> {
// Create directory if not exists, or clear it of artifacts if it does,
// preserving networks deployment info
const networksInfo = {};
if (!existsSync(this.outputDir)) {
ensureDirSync(this.outputDir);
} else {
const artifacts = this._listArtifacts();
await Promise.all(
artifacts.map(async filePath => {
const name = path.basename(filePath, '.json');
const schema = await readJSON(filePath);
if (schema.networks) networksInfo[name] = schema.networks;
await unlink(filePath);
}),
);
}
// Write compiler output, saving networks info if present
await Promise.all(
this.compilerOutput.map(async data => {
const name = data.contractName;
const buildDirName = `${this.outputDir}/${data.sourcePath.replace(/^contracts\/(.*)$/, `$1`)}`;
ensureDirSync(buildDirName);
const buildFileName = `${buildDirName}/${name}.json`;
if (networksInfo[name]) Object.assign(data, { networks: networksInfo[name] });
await writeJson(buildFileName, data, { spaces: 2 });
}),
);
}
private _listArtifacts(): string[] {
if (!existsSync(this.outputDir)) return [];
return readdirSync(this.outputDir)
.map(fileName => path.resolve(this.outputDir, fileName))
.filter(fileName => !lstatSync(fileName).isDirectory())
.filter(fileName => path.extname(fileName) === '.json');
}
}