Skip to content

Commit

Permalink
inline update mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-Symbroson committed Nov 7, 2023
1 parent 7d3c209 commit 849c175
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 8 deletions.
115 changes: 115 additions & 0 deletions docs/docs/js/update.js
Original file line number Diff line number Diff line change
@@ -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("<br>"));
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();
}
5 changes: 3 additions & 2 deletions docs/docs/v257/Docs.htm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<script src="../app.js"></script>
<script src="js/common.js"></script>
<script src="js/example.js"></script>
<script src="js/update.js"></script>
<script src="js/jquery.mobile-1.2.0.min.js"></script>

<link rel="stylesheet" href="../font-awesome/css/font-awesome.min.css"/>
Expand Down Expand Up @@ -92,8 +93,8 @@ <h1>Documentation</h1>
<p style="width:100%;text-align:right">
<small>
<i class="fa fa-warning"></i> <a href="https://github.com/DroidScript/Docs/issues">Report Issue</a><br>
DS version: 250<br>
Docs version: 265<br>
DS version: 265<br>
Docs version: 264<br>
by <a class="no-ui-link normal-link" target="_blank" onclick="return OpenUrl(this.href)" href="https://github.com/alex-Symbroson">Symbroson</a>, <a class="no-ui-link normal-link" target="_blank" onclick="return OpenUrl(this.href)" href="https://github.com/DroidScript">DroidScript</a> and <a class="no-ui-link normal-link" target="_blank" onclick="return OpenUrl(this.href)" href="https://github.com/hamacjumar">Hamac Jumar</a>,
</small>
</p>
Expand Down
115 changes: 115 additions & 0 deletions docs/docs/v257/js/update.js
Original file line number Diff line number Diff line change
@@ -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("<br>"));
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();
}
2 changes: 1 addition & 1 deletion docs/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
19667264.6
19668264
5 changes: 3 additions & 2 deletions files/docs-base/Docs.htm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<script src="../app.js"></script>
<script src="js/common.js"></script>
<script src="js/example.js"></script>
<script src="js/update.js"></script>
<script src="js/jquery.mobile-1.2.0.min.js"></script>

<link rel="stylesheet" href="../font-awesome/css/font-awesome.min.css"/>
Expand Down Expand Up @@ -92,8 +93,8 @@ <h1>Documentation</h1>
<p style="width:100%;text-align:right">
<small>
<i class="fa fa-warning"></i> <a href="https://github.com/DroidScript/Docs/issues">Report Issue</a><br>
DS version: 250<br>
Docs version: 220<br>
DS version: 265<br>
Docs version: 264.6<br>
by <a class="no-ui-link normal-link" target="_blank" onclick="return OpenUrl(this.href)" href="https://github.com/alex-Symbroson">Symbroson</a>, <a class="no-ui-link normal-link" target="_blank" onclick="return OpenUrl(this.href)" href="https://github.com/DroidScript">DroidScript</a> and <a class="no-ui-link normal-link" target="_blank" onclick="return OpenUrl(this.href)" href="https://github.com/hamacjumar">Hamac Jumar</a>,
</small>
</p>
Expand Down
2 changes: 1 addition & 1 deletion files/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
4 changes: 2 additions & 2 deletions files/updatePages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 849c175

Please sign in to comment.