remix-project mirror
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
remix-project/apps/remix-ide/ci/makeMockCompiler.js

89 lines
2.8 KiB

'use strict'
var fs = require('fs')
var compiler = require('solc')
var compilerInput = require('@remix-project/remix-solidity').CompilerInput
var defaultVersion = 'soljson-v0.8.22+commit.4fc1097e.js'
5 years ago
const path = require('path')
compiler.loadRemoteVersion(defaultVersion, (error, solcSnapshot) => {
5 years ago
console.log('solcSnapshot: ', solcSnapshot)
if (error) console.log(error)
var compilationResult = {}
const testsFolder = path.resolve(__dirname + '/../test-browser/tests/') + '/' // eslint-disable-line
5 years ago
gatherCompilationResults(testsFolder, compilationResult, solcSnapshot)
replaceSolCompiler(compilationResult, solcSnapshot)
})
7 years ago
function gatherCompilationResults (dir, compilationResult, solcSnapshot) {
7 years ago
var filenames = fs.readdirSync(dir, 'utf8')
filenames.map(function (item, i) {
if (item.endsWith('.js')) {
5 years ago
var testDef = require(dir + item)
7 years ago
if ('@sources' in testDef) {
var sources = testDef['@sources']()
for (var files in sources) {
compile(solcSnapshot, sources[files], true, function (result) {
7 years ago
compilationResult[result.key] = result
})
compile(solcSnapshot, sources[files], false, function (result) {
7 years ago
compilationResult[result.key] = result
})
}
7 years ago
}
}
})
7 years ago
return compilationResult
8 years ago
}
function compile (solcSnapshot, source, optimization, runs, addCompilationResult) {
var missingInputs = []
try {
var input = compilerInput(source, {optimize: optimization, runs: runs})
var result = solcSnapshot.compileStandardWrapper(input, function (path) {
missingInputs.push(path)
})
7 years ago
input = input.replace(/(\t)|(\n)|(\\n)|( )/g, '')
} catch (e) {
console.log(e)
}
if (result && (result.error || (result.errors && result.errors.length > 0))) {
console.log(result.error, result.errors)
}
if (result) {
console.log(result.error, result.errors)
}
var ret = {
7 years ago
key: input,
8 years ago
source: source,
optimization: optimization,
missingInputs: missingInputs,
result: result
}
addCompilationResult(ret)
8 years ago
}
function replaceSolCompiler (results, solcSnapshot) {
const compilerPath = path.resolve(__dirname + '/../test-browser/mockcompiler/compiler.js') // eslint-disable-line
const soljsonPath = path.resolve(__dirname + '/../soljson.js') // eslint-disable-line
5 years ago
fs.readFile(compilerPath, 'utf8', function (error, data) {
8 years ago
if (error) {
console.log(error)
process.exit(1)
return
8 years ago
}
console.log(solcSnapshot.version())
data = data + '\n\nvar mockCompilerVersion = \'' + solcSnapshot.version() + '\''
data = data + '\n\nvar mockData = ' + JSON.stringify(results) + ';\n'
5 years ago
fs.writeFile(soljsonPath, data, 'utf8', function (error) {
8 years ago
if (error) {
console.log(error)
process.exit(1)
return
8 years ago
}
})
})
8 years ago
}