mirror of https://github.com/espruino/BangleApps
ability to package all files up into a binary blob
parent
3979665634
commit
c9559961b4
|
@ -0,0 +1,98 @@
|
|||
#!/usr/bin/nodejs
|
||||
/*
|
||||
Mashes together a bunch of different apps into a big binary blob.
|
||||
We then store this *inside* the Bangle.js firmware and can use it
|
||||
to populate Storage initially.
|
||||
|
||||
Bangle.js 1 doesn't really have anough flash space for this,
|
||||
but we have enough on v2.
|
||||
*/
|
||||
var SETTINGS = {
|
||||
pretokenise : true
|
||||
};
|
||||
|
||||
var path = require('path');
|
||||
var ROOTDIR = path.join(__dirname, '..');
|
||||
var APPDIR = ROOTDIR+'/apps';
|
||||
var APPJSON = ROOTDIR+'/apps.json';
|
||||
var OUTFILE = path.join(ROOTDIR, '../Espruino/libs/banglejs/banglejs2_storage_default.c');
|
||||
var APPS = [ // IDs of apps to install
|
||||
"boot","launchb2","s7clk","setting",
|
||||
"about","alarm","widlock","widbat","widbt"
|
||||
];
|
||||
var MINIFY = true;
|
||||
|
||||
var fs = require("fs");
|
||||
global.Const = {
|
||||
/* Are we only putting a single app on a device? If so
|
||||
apps should all be saved as .bootcde and we write info
|
||||
about the current app into app.info */
|
||||
SINGLE_APP_ONLY : false,
|
||||
};
|
||||
|
||||
var AppInfo = require(ROOTDIR+"/core/js/appinfo.js");
|
||||
var appjson = JSON.parse(fs.readFileSync(APPJSON).toString());
|
||||
var appfiles = [];
|
||||
|
||||
function fileGetter(url) {
|
||||
console.log("Loading "+url)
|
||||
if (MINIFY) {
|
||||
if (url.endsWith(".json")) {
|
||||
var f = url.slice(0,-5);
|
||||
console.log("MINIFYING JSON "+f);
|
||||
var j = eval("("+fs.readFileSync(url).toString("binary")+")");
|
||||
var code = JSON.stringify(j);
|
||||
//console.log(code);
|
||||
url = f+".min.json";
|
||||
fs.writeFileSync(url, code);
|
||||
}
|
||||
}
|
||||
return Promise.resolve(fs.readFileSync(url).toString("binary"));
|
||||
}
|
||||
|
||||
Promise.all(APPS.map(appid => {
|
||||
var app = appjson.find(app=>app.id==appid);
|
||||
if (app===undefined) throw new Error(`App ${appid} not found`);
|
||||
return AppInfo.getFiles(app, {
|
||||
fileGetter : fileGetter,
|
||||
settings : SETTINGS
|
||||
}).then(files => {
|
||||
appfiles = appfiles.concat(files);
|
||||
});
|
||||
})).then(() => {
|
||||
// work out what goes in storage
|
||||
var storageContent = "";
|
||||
appfiles.forEach((file) => {
|
||||
var fileLength = file.content.length;
|
||||
console.log(file.name+" -> "+fileLength+"b");
|
||||
// set up header
|
||||
var header = new Uint8Array(32);
|
||||
header.fill(0);
|
||||
header.set([fileLength,fileLength>>8,fileLength>>16,fileLength>>24],0); // length
|
||||
for (var i=0;i<file.name.length;i++)
|
||||
header[4+i] = file.name.charCodeAt(i);
|
||||
// add header and file content
|
||||
storageContent += String.fromCharCode.apply(String, header);
|
||||
storageContent += file.content;
|
||||
// pad length
|
||||
while (fileLength&3) {
|
||||
storageContent += "\xff";
|
||||
fileLength++;
|
||||
}
|
||||
});
|
||||
// work out file contents
|
||||
var cfile = `// Initial storage contents for Bangle.js 2.0
|
||||
// Generated by BangleApps/bin/build_bangles_c.js
|
||||
|
||||
const int jsfStorageInitialContentLength = ${storageContent.length};
|
||||
const char jsfStorageInitialContents[] = {
|
||||
`;
|
||||
for (var i=0;i<storageContent.length;i+=32) {
|
||||
var chunk = storageContent.substr(i,32);
|
||||
cfile += chunk.split("").map(c=>c.charCodeAt()).join(",")+",\n";
|
||||
}
|
||||
cfile += `};
|
||||
`;
|
||||
fs.writeFileSync(OUTFILE, cfile);
|
||||
console.log("Output written to "+OUTFILE);
|
||||
});
|
Loading…
Reference in New Issue