AntosSDK support server side command executing

This commit is contained in:
lxsang 2021-05-06 14:25:50 +02:00
parent 2ab1fb9e27
commit 12c55d1735
11 changed files with 54 additions and 10 deletions

View File

@ -2,4 +2,5 @@
AntOSDK: development API for AntOS based applications/projects AntOSDK: development API for AntOS based applications/projects
## Change logs ## Change logs
- 0.0.5: add API that supports running Linux commands on server
- 0.0.4: support automatic locale generation - 0.0.4: support automatic locale generation

View File

@ -78,6 +78,18 @@
} }
} }
] ]
},
"list": {
"require": ["linux"],
"jobs": [
{
"name": "linux-exec",
"data": {
"cmd": "ls -al .",
"pwd": "home://"
}
}
]
} }
} }
} }

View File

@ -2,4 +2,5 @@
AntOSDK: development API for AntOS based applications/projects AntOSDK: development API for AntOS based applications/projects
## Change logs ## Change logs
- 0.0.5: add API that supports running Linux commands on server
- 0.0.4: support automatic locale generation - 0.0.4: support automatic locale generation

View File

@ -72,7 +72,18 @@ class AntOSDKBaseJob {
id: this.job.id, id: this.job.id,
type: "log", type: "log",
error: false, error: false,
result: data result: data,
show_time: true
});
}
log_print(data) {
postMessage({
id: this.job.id,
type: "log",
error: false,
result: data,
show_time: false
}); });
} }
@ -81,7 +92,8 @@ class AntOSDKBaseJob {
id: this.job.id, id: this.job.id,
type: "log", type: "log",
error: true, error: true,
result: data result: data,
show_time: true
}); });
} }

View File

@ -1 +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: {0}",r));if(e.depend&&await this.batch(e.depend,t),e.require&&await this.require(e.require),this.logger&&this.logger.info(__("### RUNNING STAGE: {0}###",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={})); 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):!1===t.show_time&&r.logger.print?r.logger.print(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 i=this.newJobID(),n={id:i,cmd:e,data:t,root:r};this.jobs[i]={callback:o,logger:s},this.worker.postMessage(n)}submit(e,t,r,o){return new Promise((s,i)=>{this.exectue_job(e,t,r,e=>{if(e.error)return i(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: {0}",r));if(e.depend&&await this.batch(e.depend,t),e.require&&await this.require(e.require),this.logger&&this.logger.info(__("### RUNNING STAGE: {0}###",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

@ -6,7 +6,7 @@
"author": "Xuan Sang LE", "author": "Xuan Sang LE",
"email": "mrsang@iohub.dev" "email": "mrsang@iohub.dev"
}, },
"version": "0.0.4-a", "version": "0.0.5-a",
"category": "Other", "category": "Other",
"iconclass": "fa fa-cog", "iconclass": "fa fa-cog",
"mimes": [ "mimes": [

View File

@ -72,7 +72,18 @@ class AntOSDKBaseJob {
id: this.job.id, id: this.job.id,
type: "log", type: "log",
error: false, error: false,
result: data result: data,
show_time: true
});
}
log_print(data) {
postMessage({
id: this.job.id,
type: "log",
error: false,
result: data,
show_time: false
}); });
} }
@ -81,7 +92,8 @@ class AntOSDKBaseJob {
id: this.job.id, id: this.job.id,
type: "log", type: "log",
error: true, error: true,
result: data result: data,
show_time: true
}); });
} }

View File

@ -6,7 +6,7 @@
"author": "Xuan Sang LE", "author": "Xuan Sang LE",
"email": "mrsang@iohub.dev" "email": "mrsang@iohub.dev"
}, },
"version": "0.0.4-a", "version": "0.0.5-a",
"category": "Other", "category": "Other",
"iconclass": "fa fa-cog", "iconclass": "fa fa-cog",
"mimes": [ "mimes": [

View File

@ -13,6 +13,7 @@ namespace OS {
type: "result"|"log"; type: "result"|"log";
error?: any; error?: any;
result?: any; result?: any;
show_time?:boolean;
} }
interface AntOSDKWorkerJob { interface AntOSDKWorkerJob {
@ -43,10 +44,15 @@ namespace OS {
if(job.logger){ if(job.logger){
if(ret.error) if(ret.error)
job.logger.error(ret.result); job.logger.error(ret.result);
else
{
if(ret.show_time === false && job.logger.print)
job.logger.print(ret.result);
else else
job.logger.info(ret.result); job.logger.info(ret.result);
} }
} }
}
else else
{ {
job.callback(ret); job.callback(ret);
@ -77,7 +83,7 @@ namespace OS {
return `job_${Math.random().toString(36).replace(".","")}`; return `job_${Math.random().toString(36).replace(".","")}`;
} }
private exectue_job(cmd: string, data: any, root:string, callback: (AntOSDKWorkerResult) => void, logger?: AntOSDKLogger): void private exectue_job(cmd: string, data: any, root:string, callback: (arg: AntOSDKWorkerResult) => void, logger?: AntOSDKLogger): void
{ {
const id = this.newJobID(); const id = this.newJobID();
const job: AntOSDKWorkerJob = { const job: AntOSDKWorkerJob = {

View File

@ -195,7 +195,7 @@
"description": "https://raw.githubusercontent.com/lxsang/antosdk-apps/master/libantosdk/README.md", "description": "https://raw.githubusercontent.com/lxsang/antosdk-apps/master/libantosdk/README.md",
"category": "Other", "category": "Other",
"author": "Xuan Sang LE", "author": "Xuan Sang LE",
"version": "0.0.4-a", "version": "0.0.5-a",
"dependencies": [], "dependencies": [],
"download": "https://raw.githubusercontent.com/lxsang/antosdk-apps/master/libantosdk/build/release/libantosdk.zip" "download": "https://raw.githubusercontent.com/lxsang/antosdk-apps/master/libantosdk/build/release/libantosdk.zip"
}, },