update compatibility to AntOS next

This commit is contained in:
mrsang
2021-04-21 11:37:58 +02:00
parent bc33f7ff10
commit 190c2fd346
1014 changed files with 96281 additions and 2092 deletions

View File

@ -0,0 +1,15 @@
# libantosdk
This is an example project, generated by AntOS Development Kit
## Howto
Use the CodePad command palette to access to the SDK functionalities:
1. Create new project
2. Init the project from the current folder located in side bar
3. Build and run the project
4. Release the project in zip package
## Set up build target
Open the `project.json` file from the current project tree and add/remove
build target entries. Save the file

View File

@ -0,0 +1,52 @@
importScripts('coffeescript.js');
class CompileCoffeeJob extends AntOSDKBaseJob {
constructor(data)
{
super(data);
}
execute()
{
if(!CoffeeScript)
{
const e_msg = "CoffeeScript module is not loaded";
this.log_error(e_msg);
return this.error(e_msg);
}
// very files all data
this.read_files(this.job.data.src)
.then((contents) => {
const errors = [];
for(let i in contents)
{
const data = contents[i];
const file = this.job.data.src[i];
try {
CoffeeScript.nodes(data);
this.log_info(`File ${file} verified`);
} catch (ex) {
errors.push(ex);
this.log_error(`${file}: ${ex.toString()}`);
}
}
if(errors.length > 0)
{
return this.error(errors);
}
const code = contents.join("\n");
const jsrc = CoffeeScript.compile(code);
// write to file
this.save_file(this.job.data.dest,jsrc)
.then(r => {
this.log_info(`File ${this.job.data.dest} generated`);
this.result(this.job.data.dest);
})
.catch(e1 => {
this.error(e1);
});
})
.catch(e => this.error(e));
}
}
API.jobhandle["coffee-compile"] = CompileCoffeeJob;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,50 @@
importScripts('terser.min.js');
class UglifyCodeJob extends AntOSDKBaseJob {
constructor(data)
{
super(data);
}
execute() {
if(!Terser)
{
const e_msg = "Terser module is not loaded";
this.log_error(e_msg);
return this.error(e_m);
}
this.read_files(this.job.data)
.then((contents) => {
const promises = [];
const options = {
toplevel: false,
compress: {
passes: 3,
},
mangle: true,
output: {
//beautify: true,
},
};
for(let i in contents)
{
const result = Terser.minify(contents[i], options);
if (result.error) {
this.log_error(`${this.job.data[i]}:${result.error}`);
promises.push(new Promise((r,e) => e(result.error)));
} else {
this.log_info(`File ${this.job.data[i]} uglified`);
promises.push(this.save_file(this.job.data[i],result.code));
}
}
Promise.all(promises)
.then((r) => this.result(r))
.catch((e) => this.error(e));
})
.catch((e1) => this.error(e1));
}
}
API.jobhandle["terser-uglify"] = UglifyCodeJob;

View File

@ -0,0 +1,131 @@
importScripts('typescript.min.js');
const tslib = {};
class TSJob extends AntOSDKBaseJob {
constructor(data)
{
super(data);
}
execute(){
if(!ts)
{
const e_msg = "typescript module is not loaded";
this.log_error(e_msg);
return this.error(e_msg);
}
switch (this.job.cmd) {
case 'ts-import':
this.importlib();
break;
case 'ts-compile':
this.compile();
break;
default:
const err_msg = `Unkown job ${this.job.cmd}`;
this.log_error(err_msg);
return this.error(err_msg);
}
}
importlib()
{
if(!ts)
{
const e_msg = "typescript module is not loaded";
this.log_error(e_msg);
return this.error(e_msg);
}
this.read_files(this.job.data)
.then((results) => {
for(let i in this.job.data)
{
const lib = this.job.data[i];
if(!tslib[lib])
{
tslib[lib] = ts.createSourceFile(lib, results[i], ts.ScriptTarget.Latest);
this.log_info(`Typescript library ${lib} loaded`);
}
}
this.result("Typescript libraries loaded");
})
.catch((e) => {
this.log_error("Fail to load Typescript module");
this.error(e);
});
}
compile()
{
let files = [];
const src_files = {};
for(let i in tslib)
{
files.push(i);
src_files[i] = tslib[i];
}
files = files.concat(this.job.data.src);
this.read_files(this.job.data.src)
.then((contents) => {
for(let i in contents)
{
const m = this.job.data.src[i];
src_files[m] = ts.createSourceFile(m, contents[i], ts.ScriptTarget.Latest);
}
let js_code = "";
const host = {
fileExists: (path) => {
return src_files[path] !== undefined;
},
directoryExists: (path) => {
return true;
},
getCurrentDirectory: () => "/",
getDirectories: () => [],
getCanonicalFileName: (path) => path,
getNewLine: () => "\n",
getDefaultLibFileName: () => "",
getSourceFile: (path) => src_files[path],
readFile: (path) => undefined,
useCaseSensitiveFileNames: () => true,
writeFile: (path, data) => js_code = `${js_code}\n${data}`
};
const program = ts.createProgram(files, {
"target": "es6",
"skipLibCheck": true,
}, host);
const result = program.emit();
const diagnostics = result.diagnostics.concat((ts.getPreEmitDiagnostics(program)));
const errors = [];
if (diagnostics.length > 0) {
diagnostics.forEach(diagnostic => {
let err_msg = "";
if (diagnostic.file) {
let { line, character } = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);
let message = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
err_msg = `${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`;
} else {
err_msg = ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n");
}
errors.push(err_msg);
this.log_error(err_msg);
});
return this.error(errors);
}
// write to file
this.save_file(this.job.data.dest,js_code)
.then(r => {
if(r.error)
{
this.log_error(error);
return this.error(r.error);
}
this.result(this.job.data.dest);
})
.catch(e1 => this.error(e1));
})
.catch(e => this.error(e));
}
}
API.jobhandle["ts-import"] = TSJob;
API.jobhandle["ts-compile"] = TSJob;

10079
libantosdk/build/debug/core/ts/antos.d.ts vendored Normal file

File diff suppressed because it is too large Load Diff

27299
libantosdk/build/debug/core/ts/core.d.ts vendored Normal file

File diff suppressed because it is too large Load Diff

33478
libantosdk/build/debug/core/ts/jquery.d.ts vendored Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,485 @@
String.prototype.getlink = function(root)
{
return API.REST + "/VFS/get/" + this.abspath(root);
}
String.prototype.asBase64 = function () {
const tmp = encodeURIComponent(this);
return btoa(
tmp.replace(/%([0-9A-F]{2})/g, (match, p1) =>
String.fromCharCode(parseInt(p1, 16))
)
);
};
String.prototype.abspath = function(root)
{
const list = this.split("://");
if(list.length == 1)
{
return root + "/" + this;
}
const proto = list[0];
const arr = list[1].split("/");
if(proto === "pkg")
{
const pkg = arr.shift();
if(API.pkgs[pkg]) {
return API.pkgs[pkg].path + "/" + arr.join("/");
}
}
if(proto === "sdk")
{
return `pkg://libantosdk/${arr.join("/")}`.abspath(root);
}
return this;
}
const API = {
REST: "",
jobhandle: {},
modules: {},
pkgs:{}
};
class AntOSDKBaseJob {
constructor(data)
{
this.job = data;
}
result(data) {
const result = {
id: this.job.id,
type: "result",
error: false,
result: data
};
postMessage(result);
}
error(msg) {
const result = {
id: this.job.id,
type: "result",
error: msg,
result: false
};
postMessage(result);
}
log_info(data) {
postMessage({
id: this.job.id,
type: "log",
error: false,
result: data
});
}
log_error(data) {
postMessage({
id: this.job.id,
type: "log",
error: true,
result: data
});
}
get(url, type) {
return new Promise((resolve, reject) => {
const req = new XMLHttpRequest();
req.open("GET", url, true);
if(type)
{
req.responseType = type;
}
req.onload = function() {
if (req.readyState === 4 && req.status === 200) {
resolve(req.response);
}
else
{
this.log_error(req.statusText);
reject(req.statusText);
}
};
req.send(null);
});
}
post(url, data, type) {
return new Promise((resolve, reject) => {
const req = new XMLHttpRequest();
req.open("POST", url, true);
req.setRequestHeader("Content-Type", "application/json");
if(type)
{
req.responseType = type;
}
req.onload = function() {
if (req.readyState === 4 && req.status === 200) {
try {
const json = JSON.parse(req.response);
resolve(json);
} catch (e) {
resolve(req.response);
}
}
else
{
this.log_error(req.statusText);
reject(req.statusText);
}
};
req.send(JSON.stringify(data));
});
}
read_files(files)
{
return new Promise( async (resolve, reject) => {
try{
let promises = [];
for(let file of files)
{
promises.push(this.meta(file.abspath(this.job.root)));
}
await Promise.all(promises);
promises = [];
for(let file of files)
{
promises.push(this.get(file.getlink(this.job.root)));
}
const contents = await Promise.all(promises);
resolve(contents);
} catch (e)
{
this.log_error(e.toString());
reject(e);
}
});
}
cat(files, data)
{
return new Promise((resolve, reject) => {
this.read_files(files)
.then((results) => {
resolve(`${data}\n${results.join("\n")}`);
})
.catch((e) => {
reject(e);
this.log_error(e.toString());
});
});
}
save_file(file, data, t)
{
return new Promise((res,rej) => {
this.b64(t,data)
.then((enc) => {
this.post(API.REST+"/VFS/write", {
path: file.abspath(this.job.root),
data: enc
})
.then(d => {
if(d.error){
this.log_error(`Unable to saved to ${file}: ${d.error}`);
return rej(d.error);
}
this.log_info(`${file} saved`);
res(d);
})
.catch(e1 => {
this.log_error(`Unable to saved to ${file}: ${e1.toString()}`);
rej(e1);
});
})
.catch((e) => {
this.log_error(`Unable to saved to ${file}: ${e.toString()}`);
rej(e);
});
});
}
b64(t, data) {
return new Promise((resolve, reject) => {
if(t === "base64")
return resolve(data);
let m = t === "object" ? "text/plain" : t;
if(!t)
m = "text/plain";
if (t === "object" || typeof data === "string") {
let b64;
if (t === "object") {
b64 = JSON.stringify(data, undefined, 4).asBase64();
}
else {
b64 = data.asBase64();
}
b64 = `data:${m};base64,${b64}`;
return resolve(b64);
}
else {
//blob
const reader = new FileReader();
reader.readAsDataURL(data);
reader.onload = () => resolve(reader.result);
return (reader.onerror = (e) => reject(e));
}
});
}
delete(files)
{
const promises = [];
for(const file of files)
{
promises.push(new Promise((resolve, reject) =>{
this.post(API.REST+"/VFS/delete", {path: file.abspath(this.job.root)})
.then(r => {
if(r.error)
{
this.log_error(`${file}:${r.error}`);
return reject(r.error);
}
this.log_info(`${file} deleted`);
return resolve(r.result);
})
.catch(e =>{
this.log_error(e.toString());
reject(e);
});
}));
}
return Promise.all(promises);
}
mkdir(list) {
return new Promise( async (resolve, reject) => {
try {
if (list.length === 0) {
return resolve(true);
}
const dir = list.splice(0, 1)[0];
const ret = await this.post(API.REST+"/VFS/mkdir", {path: dir.abspath(this.job.root)});
if(ret.error)
{
this.log_error(`${dir}: ${ret.error}`);
return reject(ret.error);
}
this.log_info(`${dir} created`);
await this.mkdir(list);
resolve(true);
}
catch (e) {
this.log_error(e.toString());
reject(e);
}
});
}
copy(files, to) {
const promises = [];
for (const path of files.map(f =>f.abspath(this.job.root))) {
promises.push(new Promise(async (resolve, reject) => {
try {
const file = path.split("/").filter(s=>s!="").pop();
const tof = `${to}/${file}`;
const meta = await this.meta(path);
if (meta.type === "dir") {
await this.mkdir([tof]);
const dirs = await this.scandir(path);
const files = dirs.map((v) => v.path);
if (files.length > 0) {
await this.copy(files, tof);
}
resolve(undefined);
}
else {
const ret = await this.read_files([path], "arraybuffer");
const blob = new Blob([ret[0]], {
type: meta.mime});
await this.save_file(tof, blob,"binary");
this.log_info(`COPIED: ${path} -> ${tof}`);
resolve(undefined);
}
} catch (error) {
this.log_error(error.toString());
reject(error);
}
}));
}
return Promise.all(promises);
}
meta(path)
{
const file = path.abspath(this.job.root);
return new Promise((resolve,reject) =>{
this.post(API.REST+"/VFS/fileinfo", {path:file})
.then(json =>{
if(json.error)
{
this.log_error(`${file}: ${json.error}`);
return reject(json.error);
}
return resolve(json.result);
})
.catch(e => {
this.log_error(e.toString());
resolve(e);
});
});
}
scandir(path)
{
return new Promise((resolve,reject) =>{
this.post(API.REST+"/VFS/scandir", {path:path.abspath(this.job.root)})
.then(json =>{
if(json.error)
return reject(json.error);
return resolve(json.result);
})
.catch(e =>{
this.log_error(e.toString());
resolve(e);
});
});
}
execute() {}
}
class UnknownJob extends AntOSDKBaseJob {
constructor(data)
{
super(data);
}
execute() {
this.log_error("Unknown job " + this.job.cmd);
this.error("Unknown job " + this.job.cmd);
}
}
class SDKSetup extends AntOSDKBaseJob {
constructor(data)
{
super(data);
}
execute() {
for(let k in this.job.data)
{
API[k] = this.job.data[k];
}
this.result("ANTOS Sdk set up");
}
}
class LoadScritpJob extends AntOSDKBaseJob {
constructor(data)
{
super(data);
}
execute() {
try
{
for(let lib of this.job.data)
{
if(!API.modules[lib])
{
this.log_info("Importing module:" + lib);
importScripts(lib);
API.modules[lib] = true;
}
else
{
console.log("Module " + lib + " is already loaded");
}
}
this.log_info("All Modules loaded" );
this.result(this.job.data);
}
catch(e)
{
this.error(e);
}
}
}
class VFSJob extends AntOSDKBaseJob {
constructor(data)
{
super(data);
}
execute() {
const arr = this.job.cmd.split("-");
if(arr.length > 1)
{
switch (arr[1]) {
case 'cat':
this.cat(this.job.data.src,"")
.then(data => {
this.save_file(this.job.data.dest, data)
.then(r => this.result(r))
.catch(e1 => this.error(e1));
})
.catch(e => this.error(e));
break;
case 'rm':
this.delete(this.job.data)
.then(d => this.result(d))
.catch(e => this.error(e));
break;
case 'mkdir':
this.mkdir(this.job.data)
.then(d => this.result(d))
.catch(e => this.error(e));
break;
case 'cp':
this.copy(this.job.data.src, this.job.data.dest)
.then(d => this.result(d))
.catch(e => this.error(e));
break;
default:
this.error("Unknown command: " + this.job.cmd);
}
}
else
{
this.error("Unknown command: " + this.job.cmd);
}
}
}
API.jobhandle["sdk-import"] = LoadScritpJob;
API.jobhandle["sdk-setup"] = SDKSetup;
API.jobhandle["vfs-cat"] = VFSJob;
API.jobhandle["vfs-rm"] = VFSJob;
API.jobhandle["vfs-mkdir"] = VFSJob;
API.jobhandle["vfs-cp"] = VFSJob;
onmessage = (e) => {
try{
if(API.jobhandle[e.data.cmd])
{
return (new API.jobhandle[e.data.cmd](e.data)).execute();
}
(new UnknownJob(e.data)).execute();
}
catch(error)
{
const result = {
id: e.data.id,
type: "result",
error: error.toString(),
result: false
};
postMessage(result);
}
}

View File

@ -0,0 +1,103 @@
importScripts("os://scripts/jszip.min.js".getlink());
class ZipJob extends AntOSDKBaseJob {
constructor(data)
{
super(data);
}
execute()
{
if(!JSZip)
{
const e_msg = "JSZip module is not loaded";
this.log_error(e_msg);
return this.error(e_msg);
}
switch (this.job.cmd) {
case 'zip-mk':
this.mkar()
.then(d => this.result(d))
.catch(e => this.error(e));
break;
default:
const err_msg = `Unkown job ${this.job.cmd}`;
this.log_error(err_msg);
return this.error(err_msg);
}
}
aradd(list, zip, base)
{
const promises = [];
for (const file of list) {
promises.push(new Promise(async (resolve, reject) => {
try {
const basename = file.split("/").pop();
const meta = await this.meta(file);
if (meta.type == "dir") {
const ret = await this.scandir(file);
const dirs = ret.map(v => v.path);
if (dirs.length > 0) {
await this.aradd(dirs, zip, `${base}${basename}/`);
resolve(undefined);
}
else {
resolve(undefined);
}
}
else {
const ret = await this.read_files([file], "arraybuffer");
const blob = new Blob( [ret[0]], {
type: "octet/stream",
});
const b64 = await this.b64("binary", blob);
const z_path = `${base}${basename}`.replace(
/^\/+|\/+$/g,
"");
zip.file(z_path, b64.replace("data:octet/stream;base64,",""), { base64: true, compression : "DEFLATE" });
this.log_info(`${file} added to zip`);
resolve(undefined);
}
} catch (error) {
this.log_error(`${file}: ${error.toString()}`);
reject(error);
}
}));
}
return Promise.all(promises);
}
mkar()
{
const src = this.job.data.src;
const dest = this.job.data.dest;
return new Promise(async (resolve, reject) => {
try {
const zip = new JSZip();
const meta = await this.meta(this.job.data.src);
if(meta.type === "file")
{
await this.aradd([src], zip, "/");
}
else
{
const ret = await this.scandir(src);
await this.aradd(ret.map(v => v.path), zip, "/");
}
const z_data = await zip.generateAsync({ type: "arraybuffer" });
const blob = new Blob([z_data], {
type: "application/octet-stream"});
await this.save_file(dest, blob,"binary");
this.log_info(`Zip archive saved in ${dest}`);
resolve(dest);
} catch (error) {
this.log_error(`Unable to commpress ${src} -> ${dest}: ${error.toString()}`);
reject(error);
}
});
}
}
API.jobhandle["zip-mk"] = ZipJob;

View File

@ -0,0 +1 @@
var OS;!function(e){let t;!function(t){class r{constructor(t){this.worker=new Worker(t.asFileHandle().getlink()),this.jobs={},this.worker.onmessage=e=>{let t=e.data,r=this.jobs[t.id];r?"log"===t.type?r.logger&&(t.error?r.logger.error(t.result):r.logger.info(t.result)):(r.callback(t),delete this.jobs[t.id]):console.log("Unable to identify result of job",t.id,t)};const r={};for(const t in e.setting.system.packages){const o=e.setting.system.packages[t];r[t]={path:o.path,name:o.pkgname}}this.submit("sdk-setup",{REST:e.API.REST,pkgs:r})}newJobID(){return"job_"+Math.random().toString(36).replace(".","")}exectue_job(e,t,r,o,s){const n=this.newJobID(),i={id:n,cmd:e,data:t,root:r};this.jobs[n]={callback:o,logger:s},this.worker.postMessage(i)}submit(e,t,r,o){return new Promise((s,n)=>{this.exectue_job(e,t,r,e=>{if(e.error)return n(e.error);s(e.result)},o)})}terminate(){this.worker.terminate()}}class o{constructor(e,t){this.root=t,this.logger=e,o.worker||(o.worker=new r("pkg://libantosdk/core/worker.js"))}require(e){return this.run("sdk-import",e.map(e=>e+".worker.js"))}compile(e,t){return new Promise(async(r,o)=>{try{await this.require([e]),r(await this.run(e+"-compile",t))}catch(e){o(__e(e))}})}run(t,r){return"sdk-run-app"===t?new Promise(async(t,o)=>{try{let o=r;1==o.split("://").length&&(o=`${this.root}/${r}`);const s=await(o+"/package.json").asFileHandle().read("json");return s.text=s.name,s.path=o,s.filename=s.pkgname,s.type="app",s.mime="antos/app",s.icon&&(s.icon=`${s.path}/${s.icon}`),s.iconclass||s.icon||(s.iconclass="fa fa-adn"),this.logger.info(__("Installing...")),e.setting.system.packages[s.pkgname]=s,s.app?(this.logger.info(__("Running {0}...",s.app)),e.GUI.forceLaunch(s.app,[])):this.logger.error(__("{0} is not an application",s.pkgname)),t(void 0)}catch(e){o(e)}}):o.worker.submit(t,r,this.root,this.logger)}batch(e,t){return t.root&&(this.root=t.root),new Promise(async(r,o)=>{try{t.targets||o("No target found");for(const r of e){const e=t.targets[r];if(!e)return o(__("No target: "+r));if(e.depend&&await this.batch(e.depend,t),e.require&&await this.require(e.require),this.logger&&this.logger.info(__(`### RUNNING STAGE: ${r}###`).__()),e.jobs)for(const t of e.jobs)await this.run(t.name,t.data)}r(void 0)}catch(e){o(e)}})}}let s;t.AntOSDKBuilder=o,function(e){class t extends e.RemoteFileHandle{constructor(e){super(e);const t="pkg://libantosdk/"+this.genealogy.join("/");this.setPath(t.asFileHandle().path)}}e.SDKFileHandle=t,e.register("^sdk$",t)}(s=t.VFS||(t.VFS={}))}(t=e.API||(e.API={}))}(OS||(OS={}));

View File

@ -0,0 +1,15 @@
{
"pkgname": "libantosdk",
"name":"AntOSDK",
"description":"AntOS Software Development Kit",
"info":{
"author": "Xuan Sang LE",
"email": "mrsang@iohub.dev"
},
"version":"0.0.3-a",
"category":"Other",
"iconclass":"fa fa-cog",
"mimes":["none"],
"dependencies":[],
"locale": {}
}