From 849c17536a7d9bea3bc32d055b420e704fe3bb54 Mon Sep 17 00:00:00 2001 From: alex-Symbroson Date: Tue, 7 Nov 2023 04:11:00 +0100 Subject: [PATCH] inline update mechanism --- docs/docs/js/update.js | 115 ++++++++++++++++++++++++++++++++++++ docs/docs/v257/Docs.htm | 5 +- docs/docs/v257/js/update.js | 115 ++++++++++++++++++++++++++++++++++++ docs/version.txt | 2 +- files/docs-base/Docs.htm | 5 +- files/generate.js | 2 +- files/updatePages.js | 4 +- 7 files changed, 240 insertions(+), 8 deletions(-) create mode 100644 docs/docs/js/update.js create mode 100644 docs/docs/v257/js/update.js diff --git a/docs/docs/js/update.js b/docs/docs/js/update.js new file mode 100644 index 000000000..9d59cc3e8 --- /dev/null +++ b/docs/docs/js/update.js @@ -0,0 +1,115 @@ +/* +$(document).ready(function () { alert("event: document ready depr"); }); +$(function () { alert("event: document ready"); }); +$(document).on("mobileinit", function () { alert("event: mobile init"); }); +$(document).load(function () { alert("event: document load"); }); +// page change +$(document).live('pageshow', function () { alert("event: page show"); }); +$(window).load(function () { alert("event: window load"); }); +*/ + +function HttpRequest(method, host, path, header, cb) { + let xmlHttp = new XMLHttpRequest(); + if (cb) xmlHttp.onload = function () { cb(xmlHttp.responseText); }; + xmlHttp.open(method, host + path, true); + xmlHttp.send(); +} + +let localVer = -1, remoteVer = -1, installedVer = -1; +const tmpPath = "/sdcard/.DroidScript/Temp"; +const docsPath = app.GetPath() + "/.edit/"; + +$(window).load(function () { + if (!isMobileIDE) return; + try { + HttpRequest("get", "https://raw.githubusercontent.com", + "/DroidScript/Docs/master/docs/version.txt", + null, OnRemoteVersion); + + localVer = (app.GetDSVersion() + "000").replace(/\D/g, '').slice(0, 3) + '.' + app.GetDSBuild(); + const docsHtm = app.ReadFile(docsPath + "docs/Docs.htm") + ""; + const docsVer = docsHtm.slice(docsHtm.indexOf("Docs version: ") + 14); + installedVer = docsVer.slice(0, docsVer.indexOf("
")); + OnRemoteVersion(); // for safety + } catch (e) { + alert(e); + } +}); + +function OnRemoteVersion(remote) { + try { + if (remote) remoteVer = remote.replace(/^.*(\d\d\d(\.\d+)?$)/, "$1"); + if (remoteVer === -1 || installedVer === -1) return; + console.log(`got installed ${installedVer} - local ${localVer} - remote ${remoteVer}`); + if (remoteVer === installedVer || remoteVer !== localVer) return; + + $('#popupDialog').popup("open"); + $('#popupDialog a').on("click", function (e) { + if (this.innerText === "Yes") InstallUpdate(); + $('#popupDialog').popup("close"); + }); + } catch (e) { + alert(e); + } +} + +function InstallUpdate() { + try { + console.log(`installing ${remoteVer} over ${installedVer}`); + + const dl = app.CreateDownloader(); + dl.SetOnCancel(function (f) { app.ShowPopup("Download aborted"); }); + dl.SetOnError(app.ShowPopup); + + const url = "https://github.com/DroidScript/Docs/archive/master.zip"; + dl.Download(url, tmpPath, "docs-master.zip"); + dl.SetOnDownload(ExtractDocs); + } catch (e) { + alert(e); + } +} + +function ExtractDocs(file) { + try { + app.ShowProgress("Extracting docs"); + if (!app.FileExists(file) || file.endsWith("/")) return; + app.Execute('app.UnzipFile(' + JSON.stringify(file) + ', ' + JSON.stringify(tmpPath) + ')'); + app.ListFolder(tmpPath + "/Docs-master/docs", null, null, "Folders,FullPath") + .forEach(ExtractLang); + } catch (e) { + alert(e); + } + app.HideProgress(); +} + +var cntDocs = 0; +function ExtractLang(file) { + cntDocs++; + const name = file.slice(file.lastIndexOf("/") + 1); + const verDir = file + "/v" + localVer; + if (!app.FolderExists(verDir)) { + const latest = app.ListFolder(file, "v", null, "Folders,AlphaSort"); + const ynd = app.CreateYesNoDialog(`v${localVer} ${name} not found. Latest docs is ${latest[latest.length - 1]}. Install?`); + ynd.SetOnTouch(function (res) { + if (res !== "Yes") return Updated(); + localVer = latest.pop().slice(1); + cntDocs--; + ExtractLang(file); + }); + return ynd.Show(); + } + + app.ShowProgress("Removing old " + name); + app.ListFolder(verDir).forEach(function (d) { app.DeleteFolder(docsPath + name + "/" + d) }); + + app.ShowProgress("Copying new " + name); + app.ListFolder(verDir).forEach(function (d) { app.RenameFolder(verDir + "/" + d, docsPath + name + "/" + d) }); + Updated(); +} + +function Updated(force = false) { + if (!force && --cntDocs) return; + app.HideProgress(); + app.ShowPopup("Done."); + location.reload(); +} \ No newline at end of file diff --git a/docs/docs/v257/Docs.htm b/docs/docs/v257/Docs.htm index b493f6c24..8478d23ec 100644 --- a/docs/docs/v257/Docs.htm +++ b/docs/docs/v257/Docs.htm @@ -17,6 +17,7 @@ + @@ -92,8 +93,8 @@

Documentation

Report Issue
- DS version: 250
- Docs version: 265
+ DS version: 265
+ Docs version: 264
by Symbroson, DroidScript and Hamac Jumar,

diff --git a/docs/docs/v257/js/update.js b/docs/docs/v257/js/update.js new file mode 100644 index 000000000..9d59cc3e8 --- /dev/null +++ b/docs/docs/v257/js/update.js @@ -0,0 +1,115 @@ +/* +$(document).ready(function () { alert("event: document ready depr"); }); +$(function () { alert("event: document ready"); }); +$(document).on("mobileinit", function () { alert("event: mobile init"); }); +$(document).load(function () { alert("event: document load"); }); +// page change +$(document).live('pageshow', function () { alert("event: page show"); }); +$(window).load(function () { alert("event: window load"); }); +*/ + +function HttpRequest(method, host, path, header, cb) { + let xmlHttp = new XMLHttpRequest(); + if (cb) xmlHttp.onload = function () { cb(xmlHttp.responseText); }; + xmlHttp.open(method, host + path, true); + xmlHttp.send(); +} + +let localVer = -1, remoteVer = -1, installedVer = -1; +const tmpPath = "/sdcard/.DroidScript/Temp"; +const docsPath = app.GetPath() + "/.edit/"; + +$(window).load(function () { + if (!isMobileIDE) return; + try { + HttpRequest("get", "https://raw.githubusercontent.com", + "/DroidScript/Docs/master/docs/version.txt", + null, OnRemoteVersion); + + localVer = (app.GetDSVersion() + "000").replace(/\D/g, '').slice(0, 3) + '.' + app.GetDSBuild(); + const docsHtm = app.ReadFile(docsPath + "docs/Docs.htm") + ""; + const docsVer = docsHtm.slice(docsHtm.indexOf("Docs version: ") + 14); + installedVer = docsVer.slice(0, docsVer.indexOf("
")); + OnRemoteVersion(); // for safety + } catch (e) { + alert(e); + } +}); + +function OnRemoteVersion(remote) { + try { + if (remote) remoteVer = remote.replace(/^.*(\d\d\d(\.\d+)?$)/, "$1"); + if (remoteVer === -1 || installedVer === -1) return; + console.log(`got installed ${installedVer} - local ${localVer} - remote ${remoteVer}`); + if (remoteVer === installedVer || remoteVer !== localVer) return; + + $('#popupDialog').popup("open"); + $('#popupDialog a').on("click", function (e) { + if (this.innerText === "Yes") InstallUpdate(); + $('#popupDialog').popup("close"); + }); + } catch (e) { + alert(e); + } +} + +function InstallUpdate() { + try { + console.log(`installing ${remoteVer} over ${installedVer}`); + + const dl = app.CreateDownloader(); + dl.SetOnCancel(function (f) { app.ShowPopup("Download aborted"); }); + dl.SetOnError(app.ShowPopup); + + const url = "https://github.com/DroidScript/Docs/archive/master.zip"; + dl.Download(url, tmpPath, "docs-master.zip"); + dl.SetOnDownload(ExtractDocs); + } catch (e) { + alert(e); + } +} + +function ExtractDocs(file) { + try { + app.ShowProgress("Extracting docs"); + if (!app.FileExists(file) || file.endsWith("/")) return; + app.Execute('app.UnzipFile(' + JSON.stringify(file) + ', ' + JSON.stringify(tmpPath) + ')'); + app.ListFolder(tmpPath + "/Docs-master/docs", null, null, "Folders,FullPath") + .forEach(ExtractLang); + } catch (e) { + alert(e); + } + app.HideProgress(); +} + +var cntDocs = 0; +function ExtractLang(file) { + cntDocs++; + const name = file.slice(file.lastIndexOf("/") + 1); + const verDir = file + "/v" + localVer; + if (!app.FolderExists(verDir)) { + const latest = app.ListFolder(file, "v", null, "Folders,AlphaSort"); + const ynd = app.CreateYesNoDialog(`v${localVer} ${name} not found. Latest docs is ${latest[latest.length - 1]}. Install?`); + ynd.SetOnTouch(function (res) { + if (res !== "Yes") return Updated(); + localVer = latest.pop().slice(1); + cntDocs--; + ExtractLang(file); + }); + return ynd.Show(); + } + + app.ShowProgress("Removing old " + name); + app.ListFolder(verDir).forEach(function (d) { app.DeleteFolder(docsPath + name + "/" + d) }); + + app.ShowProgress("Copying new " + name); + app.ListFolder(verDir).forEach(function (d) { app.RenameFolder(verDir + "/" + d, docsPath + name + "/" + d) }); + Updated(); +} + +function Updated(force = false) { + if (!force && --cntDocs) return; + app.HideProgress(); + app.ShowPopup("Done."); + location.reload(); +} \ No newline at end of file diff --git a/docs/version.txt b/docs/version.txt index a23b1f4bc..56b8de715 100644 --- a/docs/version.txt +++ b/docs/version.txt @@ -1 +1 @@ -19667264.6 \ No newline at end of file +19668264 \ No newline at end of file diff --git a/files/docs-base/Docs.htm b/files/docs-base/Docs.htm index a74f312a9..d75abeadd 100644 --- a/files/docs-base/Docs.htm +++ b/files/docs-base/Docs.htm @@ -17,6 +17,7 @@ + @@ -92,8 +93,8 @@

Documentation

Report Issue
- DS version: 250
- Docs version: 220
+ DS version: 265
+ Docs version: 264.6
by Symbroson, DroidScript and Hamac Jumar,

diff --git a/files/generate.js b/files/generate.js index 6a1cbf70b..d47c73872 100755 --- a/files/generate.js +++ b/files/generate.js @@ -158,7 +158,7 @@ function generateVersion(ver) { // update version number var v = 1000 * (Date.now() / 864e5 | 0); - var vn = Number(ReadFile("../docs/version.txt", "0")) % 1000; + var vn = Number(ReadFile("../docs/version.txt", "0")) % 1000 | 0; if (updateVer) vn++; app.WriteFile(outDir + "version.txt", (v + vn).toString()); } diff --git a/files/updatePages.js b/files/updatePages.js index 1968dd74e..3d2f1de82 100644 --- a/files/updatePages.js +++ b/files/updatePages.js @@ -10,10 +10,10 @@ main(); function main() { const base = path.join(__dirname, '..') + path.sep; - const version = fs.readFileSync(base + "out/version.txt", 'utf8').slice(-3); + const version = Number(fs.readFileSync(base + "out/version.txt", 'utf8')) % 1000; for (const file of glob(path.join(base, 'out/*/*/Docs.htm'))) { const content = fs.readFileSync(file, 'utf8'); - fs.writeFileSync(file, content.replace(/Docs version:\ [0-9]*/, `Docs version: ${version}`)); + fs.writeFileSync(file, content.replace(/Docs version:\ [0-9.]*/, `Docs version: ${version}`)); } console.log("removing old docs");