Skip to content

Commit c22e150

Browse files
committed
update script: Add --reuse-hashes option to allow skipping hash recalculation when list.json already exists
1 parent e21e303 commit c22e150

File tree

1 file changed

+60
-17
lines changed

1 file changed

+60
-17
lines changed

update

+60-17
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,6 @@ function updateCopy (srcRelativeToRoot, destRelativeToRoot) {
7474
})
7575
}
7676

77-
function readFile (dir, file) {
78-
return fs.readFileSync(path.join(__dirname, dir, file))
79-
}
80-
8177
function buildVersion (build) {
8278
let version = build.version
8379
if (build.prerelease && build.prerelease.length > 0) {
@@ -89,7 +85,10 @@ function buildVersion (build) {
8985
return version
9086
}
9187

92-
function makeEntry (dir, parsedFileName) {
88+
function makeEntry (dir, parsedFileName, oldList) {
89+
const pathRelativeToRoot = path.join(dir, parsedFileName[0])
90+
const absolutePath = path.join(__dirname, pathRelativeToRoot)
91+
9392
const build = {
9493
path: parsedFileName[0],
9594
version: parsedFileName[1],
@@ -98,19 +97,45 @@ function makeEntry (dir, parsedFileName) {
9897
}
9998
build.longVersion = buildVersion(build)
10099

101-
const fileContent = readFile(dir, parsedFileName.path)
102-
parsedFileName.longVersion = buildVersion(parsedFileName)
103-
parsedFileName.keccak256 = '0x' + ethUtil.keccak(fileContent).toString('hex')
104-
parsedFileName.urls = ['bzzr://' + swarmhash(fileContent).toString('hex')]
105-
return parsedFileName
100+
if (oldList) {
101+
const entries = oldList.builds.filter(entry => (entry.path === parsedFileName[0]))
102+
if (entries) {
103+
if (entries.length >= 2) {
104+
throw Error("Found multiple list.json entries for binary '" + pathRelativeToRoot + "'")
105+
} else if (entries.length === 1) {
106+
build.keccak256 = entries[0].keccak256
107+
build.urls = entries[0].urls
108+
}
109+
}
110+
}
111+
112+
if (!build.keccak256 || !build.urls) {
113+
const fileContent = fs.readFileSync(absolutePath)
114+
build.keccak256 = '0x' + ethUtil.keccak(fileContent).toString('hex')
115+
build.urls = ['bzzr://' + swarmhash(fileContent).toString('hex')]
116+
117+
console.log("Hashing '" + pathRelativeToRoot + "'")
118+
}
119+
120+
return build
106121
}
107122

108-
function processDir (dir, listCallback) {
123+
function processDir (dir, options, listCallback) {
109124
fs.readdir(path.join(__dirname, dir), { withFileTypes: true }, function (err, files) {
110125
if (err) {
111126
throw err
112127
}
113128

129+
let oldList
130+
if (options.reuseHashes) {
131+
try {
132+
oldList = JSON.parse(fs.readFileSync(path.join(__dirname, dir, '/list.json')))
133+
} catch (err) {
134+
// Not being able to read the existing list is not a critical error.
135+
// We'll just recreate it from scratch.
136+
}
137+
}
138+
114139
const binaryPrefix = (dir === '/bin' || dir === '/wasm' ? 'soljson' : 'solc-' + dir.slice(1))
115140
const binaryExtension = {
116141
'/bin': '.js',
@@ -135,7 +160,7 @@ function processDir (dir, listCallback) {
135160
return file.match(new RegExp('^' + binaryPrefix + '-v([0-9.]*)(-([^+]*))?(\\+(.*))?' + escapedExtension + '$'))
136161
})
137162
.filter(function (version) { return version })
138-
.map(function (pars) { return makeEntry(dir, pars) })
163+
.map(function (pars) { return makeEntry(dir, pars, oldList) })
139164
.sort(function (a, b) {
140165
if (a.longVersion === b.longVersion) {
141166
return 0
@@ -227,18 +252,36 @@ function processDir (dir, listCallback) {
227252
})
228253
}
229254

255+
function parseCommandLine () {
256+
if (process.argv.length > 3) {
257+
console.error('Expected at most one argument, got ' + (process.argv.length - 2))
258+
process.exit(1)
259+
}
260+
261+
if (process.argv.length === 3 && process.argv[2] !== '--reuse-hashes') {
262+
console.error('Invalid argument: ' + process.argv[2])
263+
process.exit(1)
264+
}
265+
266+
return {
267+
reuseHashes: process.argv.length === 3 && process.argv[2] === '--reuse-hashes'
268+
}
269+
}
270+
230271
const DIRS = [
231272
'/bin',
232273
'/linux-amd64',
233274
'/macosx-amd64',
234275
'/windows-amd64'
235276
]
236277

278+
const options = parseCommandLine()
279+
237280
DIRS.forEach(function (dir) {
238281
if (dir !== '/bin') {
239-
processDir(dir)
282+
processDir(dir, options)
240283
} else {
241-
processDir(dir, function (parsedList) {
284+
processDir(dir, options, function (parsedList) {
242285
// Any new releases added to bin/ need to be linked in other directories before we can start processing them.
243286
parsedList.forEach(function (release) {
244287
if (release.prerelease === undefined) {
@@ -257,8 +300,8 @@ DIRS.forEach(function (dir) {
257300
}
258301
})
259302

260-
processDir('/emscripten-asmjs')
261-
processDir('/wasm', function (parsedList) {
303+
processDir('/emscripten-asmjs', options)
304+
processDir('/wasm', options, function (parsedList) {
262305
// Any new releases added to wasm/ need to be linked in emscripten-wasm32/ first.
263306
parsedList.forEach(function (release) {
264307
if (release.prerelease === undefined) {
@@ -269,7 +312,7 @@ DIRS.forEach(function (dir) {
269312
}
270313
})
271314

272-
processDir('/emscripten-wasm32')
315+
processDir('/emscripten-wasm32', options)
273316
})
274317
})
275318
}

0 commit comments

Comments
 (0)