feat: scripts

This commit is contained in:
2025-04-05 16:57:26 +08:00
parent bd4fe63cdc
commit 3e996ffab3
62 changed files with 4905 additions and 0 deletions

65
scripts/updatelibs.js Normal file
View File

@@ -0,0 +1,65 @@
#! /usr/bin/env runjs
var RFile = Packages.me.hatter.tools.commons.io.RFile;
var REPO = 'https://repo.examp1e.org/';
var HPKP = 'YLh1dUR9y6Kja30RrAn7JKnbQG/uEtLMkBgFF2Fuihg=';
var main = () => {
var libDir = $$.file('lib').exists()? $$.file('lib'): $$.file('libs');
if (!libDir.exists()) {
xprintln('[ERROR] Dir lib or libs not exists!');
return;
}
println('- Load file: ' + REPO + 'files.js');
var filesJS = $STR($$.httpTool().hpkp(HPKP).url(REPO + 'files.js').read());
var filesJSON = JSON.parse(filesJS.substring(filesJS.indexOf('[')));
var filesSha1Map = filesJSON.toMap((e) => {
return [e[2], e[3]];
});
var filesSizeMap = filesJSON.toMap((e) => {
return [e[2], e[0]];
});
var fileUpdated = [];
$ARRAY(libDir.list()).forEach((f) => {
var fn = $STR(f);
var remoteFnSha1 = $STR(filesSha1Map.get(fn));
if (remoteFnSha1 == null) {
println('- SKIP file: ' + f);
return;
}
var fi = $$.file(libDir, f);
var fiSha1 = $STR($$.digests().sha1().digest(RFile.from(fi).bytes()).asHex());
if (fiSha1 == remoteFnSha1) {
println('- SHA1 check success: ' + f);
} else {
println('+ SHA1 check failed: ' + f);
println('++ REMOTE:' + remoteFnSha1 + ' LOCAL: ' + fiSha1);
println('++ Load file from: ' + REPO + f + ', size: ' + filesSizeMap.get(fn));
var counter = new Packages.me.hatter.tools.commons.io.DefaultRollCounter().prefix('++ Downloading: ');
var fBytes = $$.httpTool().hpkp(HPKP).url(REPO + f).readBytes(counter);
var downloadSha1 = $STR($$.digests().sha1().digest(fBytes).asHex());
if (downloadSha1 == remoteFnSha1) {
fileUpdated.push(fn + ' - ' + remoteFnSha1);
RFile.from(fi).write(fBytes);
} else {
println('+ ERROR! SHA1 mis-match, Should be: ' + remoteFnSha1 + ', Actural is: ' + downloadSha1);
}
}
});
if (fileUpdated.length > 0) {
println();
println('Total ' + fileUpdated.length + ' file(s) updated:');
fileUpdated.forEach((f) => {
println('- ' + f);
});
}
};
main();