/
compiler.ts
369 lines (321 loc) · 18 KB
/
compiler.ts
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
'use strict';
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
import * as fsex from 'fs-extra';
import * as https from 'https';
import { SolcCompiler, compilerType, RemoteCompilerDownloader, RemoteReleases } from './solcCompiler';
import { errorsToDiagnostics } from './solErrorsToDiaganosticsClient';
import { resolve } from 'vscode-languageserver/lib/files';
export class Compiler {
private solcCachePath: string;
private outputChannel: vscode.OutputChannel;
private solc: SolcCompiler;
constructor(solcCachePath: string) {
this.solcCachePath = solcCachePath;
this.outputChannel = vscode.window.createOutputChannel('Solidity compiler');
}
public outputCompilerInfoEnsuringInitialised() {
// initialise compiler outputs the information and validates existing settings
this.initialiseCompiler();
}
public async changeDefaultCompilerType(target: vscode.ConfigurationTarget) {
try {
const compilers: string[] = [compilerType[compilerType.remote], compilerType[compilerType.localFile], compilerType[compilerType.localNodeModule], compilerType[compilerType.embedded]];
const selectedCompiler : string = await vscode.window.showQuickPick(compilers);
vscode.workspace.getConfiguration('solidity').update('defaultCompiler', selectedCompiler, target);
vscode.window.showInformationMessage("Compiler changed to: " + selectedCompiler);
} catch (e) {
vscode.window.showErrorMessage("Error changing default compiler: " + e);
}
}
public async downloadRemoteVersionAndSetLocalPathSetting(target: vscode.ConfigurationTarget, folderPath: string) {
const path = await this.downloadRemoteVersion(folderPath);
vscode.workspace.getConfiguration('solidity').update('compileUsingLocalVersion', path, target);
}
public async downloadRemoteVersion(folderPath: string) : Promise<string> {
try {
const releases = await this.getSolcReleases();
const releasesToSelect: string[] = [];
// tslint:disable-next-line: forin
for (const release in releases) {
releasesToSelect.push(release);
}
const selectedVersion : string = await vscode.window.showQuickPick(releasesToSelect);
let version = '';
const value: string = releases[selectedVersion];
if (value !== 'undefined') {
version = value.replace('soljson-', '');
version = version.replace('.js', '');
}
const pathVersion = path.resolve(path.join(folderPath, 'soljson-' + version + '.js'));
await new RemoteCompilerDownloader().downloadCompilationFile(version, pathVersion);
vscode.window.showInformationMessage("Compiler downloaded: " + pathVersion);
return pathVersion;
} catch (e) {
vscode.window.showErrorMessage("Error downloading compiler: " + e);
}
}
public async selectRemoteVersion(target: vscode.ConfigurationTarget) {
const releases = await this.getSolcReleases();
const releasesToSelect: string[] = ['none', 'latest'];
// tslint:disable-next-line: forin
for (const release in releases) {
releasesToSelect.push(release);
}
vscode.window.showQuickPick(releasesToSelect).then((selected: string) => {
let updateValue = '';
if (selected !== 'none') {
if (selected === 'latest') {
updateValue = selected;
} else {
const value: string = releases[selected];
if (value !== 'undefined') {
updateValue = value.replace('soljson-', '');
updateValue = updateValue.replace('.js', '');
}
}
}
vscode.workspace.getConfiguration('solidity').update('compileUsingRemoteVersion', updateValue, target);
});
}
public getSolcReleases(): Promise<any> {
return new RemoteReleases().getSolcReleases();
}
public async outputSolcReleases() {
this.outputChannel.clear();
this.outputChannel.appendLine('Retrieving solc versions ..');
try {
const releases = await this.getSolcReleases();
// tslint:disable-next-line: forin
for (const release in releases) {
this.outputChannel.appendLine(release + ': ' + releases[release]);
}
} catch (error) {
this.outputChannel.appendLine('Error:' + error);
}
}
public async compile(contracts: any,
diagnosticCollection: vscode.DiagnosticCollection,
buildDir: string, rootDir: string, sourceDir: string, excludePath?: string, singleContractFilePath?: string, overrideDefaultCompiler: compilerType = null): Promise<Array<string>> {
// Did we find any sol files after all?
if (Object.keys(contracts).length === 0) {
vscode.window.showWarningMessage('No solidity files (*.sol) found');
return;
}
return new Promise((resolve, reject) => {
this.initialiseCompiler(overrideDefaultCompiler).then(() => {
try {
const output = this.solc.compile(JSON.stringify(contracts), overrideDefaultCompiler);
resolve(this.processCompilationOutput(output, this.outputChannel, diagnosticCollection, buildDir,
sourceDir, excludePath, singleContractFilePath));
} catch (reason) {
vscode.window.showWarningMessage(reason);
reject(reason);
}
});
});
}
private outputErrorsToChannel(outputChannel: vscode.OutputChannel, errors: any) {
errors.forEach(error => {
outputChannel.appendLine(error.formattedMessage);
});
outputChannel.show();
}
private outputCompilerInfo(overrideDefaultCompiler: compilerType = null) {
this.outputChannel.clear();
this.outputChannel.show();
this.outputChannel.appendLine('Retrieving compiler information:');
let compiler = this.solc.getCompiler(overrideDefaultCompiler);
if (compiler.compilerType === compilerType.localFile) {
this.outputChannel.appendLine("Compiler using local file: '" + compiler.getConfiguration() + "', solidity version: " + compiler.getVersion());
}
if (compiler.compilerType === compilerType.localNodeModule) {
this.outputChannel.appendLine('Compiler using solidity from node_module: ' + compiler.getConfiguration() + ' solidity version: ' + compiler.getVersion());
}
if (compiler.compilerType === compilerType.remote) {
this.outputChannel.appendLine("Compiler using remote version: '" + compiler.getConfiguration() + "', solidity version: " + compiler.getVersion());
}
if (compiler.compilerType === compilerType.embedded) {
this.outputChannel.appendLine('Compiler using default compiler (embedded on extension), solidity version: ' + compiler.getVersion());
}
}
private initialiseCompiler(overrideDefaultCompiler: compilerType = null): Promise<void> {
const rootPath = vscode.workspace.workspaceFolders[0].uri.fsPath;
if (typeof this.solc === 'undefined' || this.solc === null) {
this.solc = new SolcCompiler(rootPath);
this.solc.setSolcCache(this.solcCachePath);
}
this.outputChannel.appendLine(this.solcCachePath);
this.outputChannel.clear();
this.outputChannel.show();
const remoteCompiler = vscode.workspace.getConfiguration('solidity').get<string>('compileUsingRemoteVersion');
const localCompiler = vscode.workspace.getConfiguration('solidity').get<string>('compileUsingLocalVersion');
const nodeModulePackage = vscode.workspace.getConfiguration('solidity').get<string>('nodemodulespackage');
const compilerSetting = vscode.workspace.getConfiguration('solidity').get<string>('defaultCompiler');
const defaultCompiler = compilerType[compilerSetting];
this.outputChannel.appendLine('Initialising compiler with settings:');
this.outputChannel.appendLine('Remote compiler: ' + remoteCompiler);
this.outputChannel.appendLine('Local compiler: ' + localCompiler);
this.outputChannel.appendLine('Node compiler module: ' + nodeModulePackage);
this.outputChannel.appendLine('Default compiler: ' + compilerSetting);
if(overrideDefaultCompiler != null) {
this.outputChannel.appendLine('Compiling with compiler: ' + compilerType[overrideDefaultCompiler]);
}
this.outputChannel.appendLine('This may take a couple of seconds as we may need to download the solc binaries...');
return new Promise((resolve, reject) => {
this.solc.initialiseAllCompilerSettings(remoteCompiler, localCompiler, nodeModulePackage, defaultCompiler)
if(overrideDefaultCompiler == null) {
this.solc.initialiseSelectedCompiler().then(() => {
this.outputCompilerInfo();
resolve();
}).catch((reason: any) => {
vscode.window.showWarningMessage(reason);
reject(reason);
});
} else {
this.solc.initialiseCompiler(overrideDefaultCompiler).then(() => {
this.outputCompilerInfo(overrideDefaultCompiler);
resolve();
}).catch((reason: any) => {
vscode.window.showWarningMessage(reason);
reject(reason);
});
}
});
}
private processCompilationOutput(outputString: any, outputChannel: vscode.OutputChannel, diagnosticCollection: vscode.DiagnosticCollection,
buildDir: string, sourceDir: string, excludePath?: string, singleContractFilePath?: string): Array<string> {
const output = JSON.parse(outputString);
if (Object.keys(output).length === 0) {
const noOutputMessage = `No output by the compiler`;
vscode.window.showWarningMessage(noOutputMessage);
vscode.window.setStatusBarMessage(noOutputMessage);
outputChannel.appendLine(noOutputMessage);
return;
}
diagnosticCollection.clear();
if (output.errors) {
const errorWarningCounts = errorsToDiagnostics(diagnosticCollection, output.errors);
this.outputErrorsToChannel(outputChannel, output.errors);
if (errorWarningCounts.errors > 0) {
const compilationWithErrorsMessage = `Compilation failed with ${errorWarningCounts.errors} errors`;
vscode.window.showErrorMessage(compilationWithErrorsMessage);
vscode.window.setStatusBarMessage(compilationWithErrorsMessage);
outputChannel.appendLine(compilationWithErrorsMessage);
if (errorWarningCounts.warnings > 0) {
vscode.window.showWarningMessage(`Compilation had ${errorWarningCounts.warnings} warnings`);
}
} else if (errorWarningCounts.warnings > 0) {
const files = this.writeCompilationOutputToBuildDirectory(output, buildDir, sourceDir, excludePath, singleContractFilePath);
const compilationWithWarningsMessage = `Compilation completed successfully!, with ${errorWarningCounts.warnings} warnings`;
vscode.window.showWarningMessage(compilationWithWarningsMessage);
vscode.window.setStatusBarMessage(compilationWithWarningsMessage);
outputChannel.appendLine(compilationWithWarningsMessage);
return files;
}
} else {
const files = this.writeCompilationOutputToBuildDirectory(output, buildDir, sourceDir, excludePath, singleContractFilePath);
const compilationSuccessMessage = `Compilation completed successfully!`;
vscode.window.showInformationMessage(compilationSuccessMessage);
vscode.window.setStatusBarMessage(compilationSuccessMessage);
outputChannel.appendLine(compilationSuccessMessage);
return files;
}
}
private ensureDirectoryExistence(filePath: string) {
const dirname = path.dirname(filePath);
if (fs.existsSync(dirname)) {
return true;
}
this.ensureDirectoryExistence(dirname);
fs.mkdirSync(dirname);
}
private writeCompilationOutputToBuildDirectory(output: any, buildDir: string, sourceDir: string,
excludePath?: string, singleContractFilePath?: string): Array<string> {
const rootPath = vscode.workspace.workspaceFolders[0].uri.fsPath;
const binPath = path.join(rootPath, buildDir);
const compiledFiles: Array<string> = new Array<string>();
if (!fs.existsSync(binPath)) {
fs.mkdirSync(binPath);
}
if (typeof singleContractFilePath !== 'undefined' && singleContractFilePath !== null) {
const relativePath = path.relative(rootPath, singleContractFilePath);
const dirName = path.dirname(path.join(binPath, relativePath));
const outputCompilationPath = path.join(dirName, path.basename(singleContractFilePath, '.sol') + '-solc-output' + '.json');
this.ensureDirectoryExistence(outputCompilationPath);
fs.writeFileSync(outputCompilationPath, JSON.stringify(output, null, 4));
} else {
const dirName = binPath;
const outputCompilationPath = path.join(dirName, 'solc-output-compile-all' + '.json');
this.ensureDirectoryExistence(outputCompilationPath);
if (fs.existsSync(outputCompilationPath)) {
fs.unlinkSync(outputCompilationPath);
}
fs.writeFileSync(outputCompilationPath, JSON.stringify(output, null, 4));
}
// iterate through all the sources,
// find contracts and output them into the same folder structure to avoid collisions, named as the contract
for (const source in output.contracts) {
// TODO: ALL this validation to a method
// Output only single contract compilation or all
if (!singleContractFilePath || source === singleContractFilePath) {
if (!excludePath || !source.startsWith(excludePath)) {
// Output only source directory compilation or all (this will exclude external references)
if (!sourceDir || source.startsWith(sourceDir)) {
for (const contractName in output.contracts[source]) {
if (output.contracts[source].hasOwnProperty(contractName)) {
const contract = output.contracts[source][contractName];
const relativePath = path.relative(rootPath, source);
const dirName = path.dirname(path.join(binPath, relativePath));
if (!fs.existsSync(dirName)) {
fsex.mkdirsSync(dirName);
}
const contractAbiPath = path.join(dirName, contractName + '.abi');
const contractBinPath = path.join(dirName, contractName + '.bin');
const contractJsonPath = path.join(dirName, contractName + '.json');
if (fs.existsSync(contractAbiPath)) {
fs.unlinkSync(contractAbiPath);
}
if (fs.existsSync(contractBinPath)) {
fs.unlinkSync(contractBinPath);
}
if (fs.existsSync(contractJsonPath)) {
fs.unlinkSync(contractJsonPath);
}
fs.writeFileSync(contractBinPath, contract.evm.bytecode.object);
fs.writeFileSync(contractAbiPath, JSON.stringify(contract.abi));
let version = '';
try {
version = JSON.parse(contract.metadata).compiler.version;
// tslint:disable-next-line: no-empty
} catch { } // i could do a check for string.empty but this catches (literally :) ) all scenarios
const shortJsonOutput = {
contractName: contractName,
// tslint:disable-next-line:object-literal-sort-keys
abi: contract.abi,
metadata: contract.metadata,
bytecode: contract.evm.bytecode.object,
deployedBytecode: contract.evm.deployedBytecode.object,
sourceMap: contract.evm.bytecode.sourceMap,
deployedSourceMap: contract.evm.deployedBytecode.sourceMap,
sourcePath: source,
compiler: {
name: 'solc',
version: version,
},
ast: output.sources[source].ast,
functionHashes: contract.evm.methodIdentifiers,
gasEstimates: contract.evm.gasEstimates,
};
fs.writeFileSync(contractJsonPath, JSON.stringify(shortJsonOutput, null, 4));
compiledFiles.push(contractJsonPath);
}
}
}
}
}
}
return compiledFiles;
}
}