Improvement + fix bug:

- improve Tag API
- Improve CodePad UI + Extension API
- Add package dependencies to Market Place
This commit is contained in:
lxsang
2020-12-18 19:51:19 +01:00
parent 6c935e88ee
commit 566592dc8e
15 changed files with 480 additions and 158 deletions

View File

@ -99,7 +99,7 @@ namespace OS {
}
protected onexit(e: BaseEvent): void{
(this.parent as OS.application.MarketPlace).refreshRepoList();
(this.parent as OS.application.MarketPlace).fetchApps();
return super.onexit(e);
}
}

View File

@ -21,8 +21,7 @@ namespace OS {
declare var JSZip: any;
export class MarketPlace extends BaseApplication {
private installdir: string;
private apps_meta: GenericObject<any>[];
private repo: GUI.tag.ListViewTag;
private apps_meta: GenericObject<any>;
private applist: GUI.tag.ListViewTag;
private container: GUI.tag.VBoxTag;
private appname: GUI.tag.LabelTag;
@ -41,16 +40,6 @@ namespace OS {
this.installdir = this.systemsetting.system.pkgpaths.user;
// test repository
this.apps_meta = [];
this.repo = this.find("repo") as GUI.tag.ListViewTag;
this.repo.onlistselect = (e) => {
const data = e.data.item.data;
if (!data) {
return;
}
return this.fetchApps(data);
};
this.refreshRepoList();
this.applist = this.find("applist") as GUI.tag.ListViewTag;
this.applist.onlistselect = (e) => {
@ -109,19 +98,14 @@ namespace OS {
});
$(this.searchbox).keyup((e) => this.search(e));
}
refreshRepoList(): void {
const list = Array.from(this.systemsetting.system.repositories);
list.unshift({
text: "Installed",
url: undefined,
this.fetchApps().then((d) => {
//console.log(d);
});
this.repo.data = list;
}
private search(e: JQuery.KeyboardEventBase) {
let v: GenericObject<any>;
let k: string;
switch (e.which) {
case 37:
return e.preventDefault();
@ -140,8 +124,8 @@ namespace OS {
if (text.length === 2) {
this.applist.data = (() => {
const result1 = [];
for (v of this.apps_meta) {
result1.push(v);
for (k in this.apps_meta) {
result1.push(this.apps_meta[k]);
}
return result1;
})();
@ -151,58 +135,135 @@ namespace OS {
}
var result = [];
var term = new RegExp(text, "i");
for (v of this.apps_meta) {
if (v.text.match(term)) {
result.push(v);
for (k in this.apps_meta) {
if (this.apps_meta[k].text.match(term)) {
result.push(this.apps_meta[k]);
}
}
this.applist.data = result;
}
}
private fetchApps(data: GenericObject<any>): void {
let v: API.PackageMetaType;
if (!data.url) {
/**
* Load package meta-data from remote sources
*
* @private
* @param {string} url repository url
* @return {*} {Promise<GenericObject<any>>}
* @memberof MarketPlace
*/
private loadRemoteRepository(url: string): Promise<GenericObject<any>> {
return new Promise((resolve, reject) => {
url.asFileHandle().read('json')
//this._api
//.get(url + "?_=" + new Date().getTime(), "json")
.then((d) => {
for (let v of d) {
v.text = `${v.name} v${v.version}`;
v.iconclass = "fa fa-adn";
v.dependBy = [];
if (!v.dependencies) {
v.dependencies = [];
}
}
resolve(d);
})
.catch((e) => {
return this.error(
__(
"Fail to fetch packages list from: {0}",
url
),
e
);
reject(e);
});
});
}
/**
* Load packages meta-data from a list of repositories
*
* @private
* @param {string[]} list repositories list
* @return {*} {Promise<GenericObject<any>[]>} a Promise on a list of package meta-data
* @memberof MarketPlace
*/
private loadRemoteRepositories(list: string[]): Promise<GenericObject<any>[]> {
return new Promise((resolve, reject) => {
if (list.length == 0) {
let app_list = [];
for (let k in this.apps_meta) {
for (let dep of this.apps_meta[k].dependencies) {
if (this.apps_meta[dep]) {
this.apps_meta[dep].dependBy.push(k)
}
}
app_list.push(this.apps_meta[k]);
}
return resolve(app_list);
}
let url = list.splice(0, 1)[0];
this.loadRemoteRepository(url)
.then((d: GenericObject<any>[]) => {
for (let pkg of d) {
// check if the package exists
let name = pkg.pkgname ? pkg.pkgname : pkg.app;
name = `${name}@${pkg.version}`;
if (this.apps_meta[name]) {
pkg.icon = this.apps_meta[name].icon;
pkg.iconclass = this.apps_meta[name].iconclass;
}
this.apps_meta[name] = pkg;
}
this.loadRemoteRepositories(list)
.then((l) => {
resolve(l);
});
})
.catch((e) => {
this.error(__("Unable to load repository: {0}: {1}", url, e.toString()), e);
this.loadRemoteRepositories(list)
.then((l) => {
resolve(l);
});
});
});
}
fetchApps(): Promise<GenericObject<any>> {
return new Promise((resolve, reject) => {
let v: API.PackageMetaType;
this.apps_meta = {};
const pkgcache = this.systemsetting.system.packages;
const list = [];
for (let k in pkgcache) {
v = pkgcache[k];
list.push({
this.apps_meta[`${k}@${v.version}`] = {
pkgname: v.pkgname ? v.pkgname : v.app,
name: v.name,
text: v.name,
text: `${v.name} v${v.version}`,
icon: v.icon,
iconclass: v.iconclass,
category: v.category,
author: v.info.author,
version: v.version,
description: `${v.path}/REAME.md`,
});
description: `${v.path}/README.md`,
dependencies: v.dependencies ? Array.from(v.dependencies) : [],
dependBy: []
};
}
this.apps_meta = list;
this.applist.data = list;
return;
}
this._api
.get(data.url + "?_=" + new Date().getTime(), "json")
.then((d) => {
for (v of d) {
v.text = v.name;
v.iconclass = "fa fa-adn";
}
this.apps_meta = d;
return (this.applist.data = d);
})
.catch((e) => {
return this.error(
__(
"Fail to fetch packages list from: {0}",
data.url
),
e
);
});
const list: string[] = []
for (let d of Array.from(this.systemsetting.system.repositories)) {
list.push(d.url);
}
this.loadRemoteRepositories(list)
.then((apps_list) => {
this.applist.data = apps_list;
resolve(this.apps_meta);
});
});
}
private appDetail(d: GenericObject<any>): void {
@ -321,7 +382,119 @@ namespace OS {
default:
}
}
private checkDependencies(pkgname: string, is_uninstall: boolean = false): GenericObject<Set<string>> {
let dep_list = {
install: new Set<string>(),
uninstall: new Set<string>(),
notfound: new Set<string>()
};
let meta = this.apps_meta[pkgname];
if (!meta) {
this.error(__("Invalid package name: {0}", pkgname));
return dep_list;
}
let installed_pkgs = this.systemsetting.system.packages;
const list = is_uninstall ? meta.dependBy : meta.dependencies;
list.push(pkgname);
for (let k in list) {
const arr: string[] = list[k].split("@");
if (is_uninstall) {
// dependencies for uninstall
if (installed_pkgs[arr[0]]) {
let name = `${arr[0]}@${installed_pkgs[arr[0]].version}`;
dep_list.uninstall.add(name);
if(list[k] != pkgname)
{
let subdep = this.checkDependencies(name, true);
dep_list.uninstall = new Set([...dep_list.uninstall, ...subdep.uninstall]);
}
}
}
else {
// dependencies for install
let need_install: boolean = true;
if (installed_pkgs[arr[0]]) {
let name = `${arr[0]}@${installed_pkgs[arr[0]].version}`;
// check version
if (installed_pkgs[arr[0]].version.__v().compare(arr[1].__v()) != 0) {
// this package is to be uninstalled
dep_list.uninstall.add(name);
let subdep = this.checkDependencies(name, true);
dep_list.uninstall = new Set([...dep_list.uninstall, ...subdep.uninstall]);
need_install = true;
}
else {
need_install = false;
}
}
if (need_install) {
if (this.apps_meta[list[k]]) {
// new package should be installed
dep_list.install.add(list[k]);
if(list[k] != pkgname)
{
let subdep = this.checkDependencies(list[k], false);
dep_list.uninstall = new Set([...dep_list.uninstall, ...subdep.uninstall]);
dep_list.notfound = new Set([...dep_list.notfound, ...subdep.notfound]);
dep_list.install = new Set([...dep_list.install, ...subdep.install]);
}
}
else {
// not found
dep_list.notfound.add(meta.dependencies[k]);
}
}
}
}
return dep_list;
}
private installPkg(pkgname:string): Promise<string>
{
return new Promise(async (resolve, reject) =>{
const meta = this.apps_meta[pkgname];
if(!meta || !meta.download)
{
return reject(this._api.throwe(__("Unable to find package: {0}", pkgname)));
}
try {
const data = await this._api.blob(
meta.download + "?_=" + new Date().getTime()
);
try {
const n = await this.install(data, meta);
return resolve(meta);
} catch (e) {
return reject(__e(e));
}
} catch (e_1) {
return reject(__e(e_1));
}
});
}
private bulkInstall(list:string[]): Promise<any>
{
return new Promise((resolve, reject)=>{
if(list.length == 0)
{
return resolve(true);
}
const pkgname = list.splice(0,1)[0];
this.installPkg(pkgname)
.then((meta) =>{
this.bulkInstall(list)
.then((b) =>{
resolve(b);
})
.catch((e) =>{
reject(e);
})
})
.catch((err) =>{
reject(err);
})
});
}
private remoteInstall(): Promise<string> {
const el = this.applist.selectedItem;
if (!el) {
@ -333,19 +506,47 @@ namespace OS {
}
// get blob file
return new Promise(async (resolve, reject) => {
try {
const data = await this._api.blob(
app.download + "?_=" + new Date().getTime()
);
try {
const n = await this.install(data, app);
return resolve(n);
} catch (e) {
return reject(__e(e));
}
} catch (e_1) {
return reject(__e(e_1));
let pkgname = `${el.data.pkgname}@${el.data.version}`;
const dep = this.checkDependencies(pkgname);
if (dep.notfound.size != 0) {
return this.openDialog("TextDialog", {
disable: true,
title: __("Unresolved dependencies"),
value: __(
"Unable to install: The package `{0}` depends on these packages, but they are not found:\n{1}",
pkgname,
[...dep.notfound].join("\n")
)
})
.then((v) => {
reject(__("Unresolved dependencies on: {0}", pkgname))
});
}
this.openDialog("TextDialog", {
title: __("Confirm install"),
disable: true,
value: __(
"Please confirm the following operation:\n\n{0} packages will be removed:\n\n{1}\n\n{2} packages will be installed:\n\n{3}",
dep.uninstall.size.toString(),
[...dep.uninstall].join("\n"),
dep.install.size.toString(),
[...dep.install].join("\n")
)
}).then((v) => {
this.bulkUninstall([...dep.uninstall])
.then((b)=>{
this.bulkInstall([...dep.install])
.then((b1)=>{
resolve(pkgname);
})
.catch((e1) =>{
reject(e1);
})
})
.catch((e2) =>{
reject(e2);
})
})
});
}
@ -361,8 +562,6 @@ namespace OS {
.then((data: Uint8Array) => {
return this.install(data)
.then((n) => {
this.repo.unselect();
this.repo.selected = 0;
const apps = this.applist.data.map(
(v) => v.pkgname
);
@ -393,7 +592,7 @@ namespace OS {
.then((d: string) => {
let name: string;
const v = JSON.parse(d);
const pth = `${this.installdir}/${v.pkgname?v.pkgname:v.app}`;
const pth = `${this.installdir}/${v.pkgname ? v.pkgname : v.app}`;
const dir = [pth];
const files = [];
for (name in zip.files) {
@ -408,13 +607,13 @@ namespace OS {
return this.mkdirs(dir)
.then(() => {
return this.installFile(
v.pkgname?v.pkgname:v.app,
v.pkgname ? v.pkgname : v.app,
zip,
files
)
.then(() => {
const app_meta = {
pkgname: v.pkgname?v.pkgname:v.app,
pkgname: v.pkgname ? v.pkgname : v.app,
name: v.name,
text: v.name,
icon: v.icon,
@ -430,7 +629,7 @@ namespace OS {
: undefined,
};
v.text = v.name;
v.filename = v.pkgname?v.pkgname:v.app;
v.filename = v.pkgname ? v.pkgname : v.app;
v.type = "app";
v.mime = "antos/app";
if (
@ -442,7 +641,7 @@ namespace OS {
}
v.path = pth;
this.systemsetting.system.packages[
v.pkgname?v.pkgname:v.app
v.pkgname ? v.pkgname : v.app
] = v;
this.appDetail(app_meta);
return resolve(v.name);
@ -456,7 +655,74 @@ namespace OS {
.catch((e: Error) => reject(__e(e)));
});
}
private bulkUninstall(list: string[]): Promise<any>
{
return new Promise(async (resolve, reject) => {
if(list.length == 0)
{
return resolve(true);
}
const pkgname = list.splice(0,1)[0];
this.uninstallPkg(pkgname)
.then((meta) =>{
this.bulkUninstall(list)
.then((b)=>{
resolve(b);
})
.catch((e)=>{
reject(e);
})
})
.catch((err) =>{
reject(err);
})
});
}
private uninstallPkg(pkgname: string): Promise<any>
{
return new Promise(async (resolve, reject) => {
const meta = this.apps_meta[pkgname];
if(!meta)
{
return reject(this._api.throwe(__("Unable to find application meta-data: {0}", pkgname)));
}
const app = this.systemsetting.system.packages[meta.pkgname];
if(!app)
{
return reject(this._api.throwe(__("Application {0} is not installed", pkgname)));
}
// got the app meta
try {
const r = await app.path
.asFileHandle()
.remove();
if (r.error) {
return reject(this._api.throwe(__("Cannot uninstall package: {0}", r.error)));
}
this.notify(__("Package uninstalled"));
// stop all the services if any
if (app.services) {
for (let srv of Array.from(app.services)) {
this._gui.unloadApp(srv);
}
}
delete this.systemsetting.system.packages[meta.pkgname];
this._gui.unloadApp(meta.pkgname);
if (meta.download) {
this.appDetail(meta);
}
else {
if(meta.domel)
this.applist.delete(meta.domel);
$(this.container).css("visibility", "hidden");
}
return resolve(meta);
}
catch (e) {
return reject(__e(e));
}
});
}
private uninstall(): Promise<any> {
return new Promise(async (resolve, reject) => {
const el = this.applist.selectedItem;
@ -472,42 +738,24 @@ namespace OS {
if (!app) {
return;
}
const pkgname = `${sel.pkgname}@${app.version}`;
const dep = this.checkDependencies(pkgname, true);
try {
const d = await this.openDialog("YesNoDialog", {
const d = await this.openDialog("TextDialog", {
title: __("Uninstall"),
text: __("Uninstall: {0}?", app.name),
disable: true,
value: __("{0} Packages to be Uninstalled:\n\n{1}", dep.uninstall.size, [...dep.uninstall].join("\n")),
});
if (!d) {
return;
}
try {
const r = await app.path
.asFileHandle()
.remove();
if (r.error) {
return reject(this._api.throwe(__("Cannot uninstall package: {0}", r.error)));
}
this.notify(__("Package uninstalled"));
// stop all the services if any
if (app.services) {
for (let srv of Array.from(app.services)) {
this._gui.unloadApp(srv);
}
}
delete this.systemsetting.system.packages[name];
this._gui.unloadApp(name);
if (sel.download) {
this.appDetail(sel);
}
else {
this.applist.delete(el);
$(this.container).css("visibility", "hidden");
}
return resolve();
}
catch (e) {
return reject(__e(e));
}
this.bulkUninstall([...dep.uninstall])
.then((b)=>{
this.notify(__("Uninstall successfully"));
})
.catch((err)=>{
this.error(__("Unable to uninstall package(s): {0}", err.toString()), err);
});
}
catch (e_1) {
return reject(__e(e_1));
@ -515,17 +763,36 @@ namespace OS {
});
}
private updatePackage(): Promise<any>{
private updatePackage(): Promise<any> {
return new Promise(async (resolve, reject) => {
try {
await this.uninstall();
const el = this.applist.selectedItem;
if (!el) {
return;
}
const sel = el.data;
if (!sel) {
return;
}
const name = sel.pkgname;
const app = this.systemsetting.system.packages[sel.pkgname];
if (!app) {
return;
}
const meta = this.apps_meta[`${sel.pkgname}@${app.version}`];
await this.remoteInstall();
try {
await this.remoteInstall();
return resolve();
if(meta)
{
if(meta.domel)
this.applist.delete(meta.domel);
}
return resolve(true);
}
catch (e) {
return reject(__e(e));
}
}
catch (e_1) {
return reject(__e(e_1));
@ -536,7 +803,7 @@ namespace OS {
private mkdirs(list: string[]): Promise<any> {
return new Promise((resolve, reject) => {
if (list.length === 0) {
return resolve();
return resolve(true);
}
const dir = list.splice(0, 1)[0].asFileHandle();
const path = dir.parent();
@ -556,17 +823,17 @@ namespace OS {
);
}
return this.mkdirs(list)
.then(() => resolve())
.then(() => resolve(true))
.catch((e) => reject(__e(e)));
})
.catch((e) => reject(__e(e)));
});
}
private installFile(n: string, zip: any, files: string[]): Promise<any>{
private installFile(n: string, zip: any, files: string[]): Promise<any> {
return new Promise((resolve, reject) => {
if (files.length === 0) {
return resolve();
return resolve(true);
}
const file = files.splice(0, 1)[0];
const path = `${this.installdir}/${n}/${file}`;
@ -587,7 +854,7 @@ namespace OS {
);
}
return this.installFile(n, zip, files)
.then(() => resolve())
.then(() => resolve(true))
.catch((e) => reject(__e(e)));
})
.catch((e) => reject(__e(e)));

View File

@ -6,7 +6,7 @@
"author": "Xuan Sang LE",
"email": "xsang.le@gmail.com"
},
"version":"0.1.5-a",
"version":"0.2.1-a",
"category":"System",
"iconclass":"fa fa-adn",
"mimes":["none"],

View File

@ -1,7 +1,6 @@
<afx-app-window data-id = "marketplace-win" apptitle="MarketPlace" width="500" height="400">
<afx-app-window data-id = "marketplace-win" apptitle="MarketPlace" width="600" height="400">
<afx-hbox >
<afx-vbox data-width = "172" data-id = "sidebar" min-width="172">
<afx-list-view data-id = "repo" dropdown = "true" data-height= "25" width = "150"></afx-list-view>
<afx-vbox data-width = "200" data-id = "sidebar" min-width="150">
<afx-hbox data-height= "23" data-id="search-container">
<div data-width="17" data-id="searchicon"></div>
<input data-id = "searchbox" />