moved some sources and icon to separate dirs
[guerillascript.git] / modules / pkg / pkgman.js
blobedd8a6725ad691c084ad1cb1d4eb87aeb25bf2e2
1 /* coded by Ketmar // Invisible Vector (psyc://ketmar.no-ip.org/~Ketmar)
2 * Understanding is not required. Only obedience.
4 * This program is free software. It comes without any warranty, to
5 * the extent permitted by applicable law. You can redistribute it
6 * and/or modify it under the terms of the Do What The Fuck You Want
7 * To Public License, Version 2, as published by Sam Hocevar. See
8 * http://www.wtfpl.net/txt/copying/ for more details.
9 */
10 ////////////////////////////////////////////////////////////////////////////////
11 let dldr = null;
12 let lastUpdateCount = 0;
13 let pkgList = {lmod:-1};
16 ////////////////////////////////////////////////////////////////////////////////
17 exports.getPackageList = function () {
18 if (lastUpdateCount != pkgList.lmod) {
19 pkgList.lmod = lastUpdateCount;
20 pkgList.list = pkgDB.getActivePackages();
21 //for (let pi of pkgList.list) conlog("id=", pi.id, "; name=", pi.name);
23 return pkgList;
27 ////////////////////////////////////////////////////////////////////////////////
28 // queue object:
29 // string name
30 // string url
31 let queue = new Array();
34 ////////////////////////////////////////////////////////////////////////////////
35 exports.__defineGetter__("inProgress", function () (dldr && dldr.running));
38 ////////////////////////////////////////////////////////////////////////////////
39 exports.cancel = function () {
40 if (dldr) {
41 if (dldr.running) dldr.cancel();
42 dldr = null;
44 queue = new Array();
48 ////////////////////////////////////////////////////////////////////////////////
49 function pingQueue () {
50 if (dldr) return; // in progress
51 let qo; // queue object
52 for (;;) {
53 if (queue.length == 0) return; // nothing to do
54 // get queue object
55 qo = queue[0];
56 queue.splice(0, 1); // remove first element
57 if (!qo.url) {
58 //conlog("NO URL for '", qo.name, "'");
59 // no url: update request; find package
60 let pi = pkgDB.getActivePackageByName(qo.name);
61 if (pi) {
63 conlog(
64 "pi.id=", pi.id, "\n",
65 "pi.name=", pi.name, "\n",
66 "pi.dirname=", pi.dirname, "\n",
67 "pi.version=", pi.version, "\n",
68 "pi.url=", pi.url, "\n"
71 qo.url = pi.url;
72 break;
74 logError("can't update unknown package '"+qo.name+"'");
75 } else {
76 break;
79 //conlog("GS: qo.name=", qo.name, "; qo.url=", qo.url);
80 // new downloader
81 dldr = new PkgDownloader(qo.url);
82 dldr.clearTempDir();
83 dldr.onError = function () {
84 /*++lastUpdateCount;*/
85 dldr = null;
86 pingQueue();
88 dldr.onCancel = function () {
89 /*++lastUpdateCount;*/
90 dldr = null;
91 pingQueue();
93 dldr.onComplete = function () {
94 dldr = null;
95 let upd = pkgDB.checkAndUpdate(qo.name, this.pkg);
96 conlog((upd ? "" : "NOT "), "UPDATED: '", qo.name, "'");
97 if (upd) {
98 ++lastUpdateCount;
99 scacheAPI.reset();
101 pingQueue();
103 dldr.onMainReceived = function () {
104 let res = pkgDB.checkAndUpdate(qo.name, this.pkg, {checkOnly:true});
105 if (!res) conlog("package '", qo.name, "' is up-to-date");
106 return res;
108 dldr.start();
112 ////////////////////////////////////////////////////////////////////////////////
113 exports.install = function (name, url) {
114 if (typeof(name) !== "string" || !name) throw new Error("invalid package name");
115 if (typeof(url) !== "string" || !(/https?:/.test(url))) throw new Error("invalid package url");
116 // queue
117 let found = false;
118 for (let qo of queue) {
119 if (qo.name == name) {
120 if (qo.url != url) throw new Error("conflicting URLs for package '"+name+"'");
121 found = true;
122 break;
125 if (!found) queue.push({name:name, url:url});
126 pingQueue();
130 ////////////////////////////////////////////////////////////////////////////////
131 exports.update = function (name) {
132 if (typeof(name) !== "string" || !name) throw new Error("invalid package name");
133 // queue
134 let found = false;
135 for (let qo of queue) {
136 if (qo.name == name) {
137 found = true;
138 break;
141 if (!found) queue.push({name:name, url:null});
142 pingQueue();
146 ////////////////////////////////////////////////////////////////////////////////
147 exports.remove = function (name) {
148 if (typeof(name) !== "string" || !name) throw new Error("invalid package name");
149 if (dldr && dldr.running) throw new Error("package manager is busy");
150 try {
151 pkgDB.removePackage(name);
152 conlog("package '", name, "' removed");
153 } catch (e) { logException("WTF", e); }
154 ++lastUpdateCount;
155 scacheAPI.reset();