2014-02-13 14:23:27 +00:00
|
|
|
/*\
|
|
|
|
title: $:/core/modules/utils/pluginmaker.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: utils
|
|
|
|
|
|
|
|
A quick and dirty way to pack up plugins within the browser.
|
|
|
|
|
|
|
|
\*/
|
|
|
|
(function(){
|
|
|
|
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
/*
|
|
|
|
Repack a plugin, and then delete any non-shadow payload tiddlers
|
|
|
|
*/
|
|
|
|
exports.repackPlugin = function(title,additionalTiddlers,excludeTiddlers) {
|
|
|
|
additionalTiddlers = additionalTiddlers || [];
|
|
|
|
excludeTiddlers = excludeTiddlers || [];
|
|
|
|
// Get the plugin tiddler
|
|
|
|
var pluginTiddler = $tw.wiki.getTiddler(title);
|
|
|
|
if(!pluginTiddler) {
|
|
|
|
throw "No such tiddler as " + title;
|
|
|
|
}
|
|
|
|
// Extract the JSON
|
2014-08-30 19:44:26 +00:00
|
|
|
var jsonPluginTiddler;
|
2014-02-13 14:23:27 +00:00
|
|
|
try {
|
2014-08-30 19:44:26 +00:00
|
|
|
jsonPluginTiddler = JSON.parse(pluginTiddler.fields.text);
|
2014-02-13 14:23:27 +00:00
|
|
|
} catch(e) {
|
2016-05-17 20:58:47 +00:00
|
|
|
throw "Cannot parse plugin tiddler " + title + "\n" + $tw.language.getString("Error/Caption") + ": " + e;
|
2014-02-13 14:23:27 +00:00
|
|
|
}
|
|
|
|
// Get the list of tiddlers
|
|
|
|
var tiddlers = Object.keys(jsonPluginTiddler.tiddlers);
|
|
|
|
// Add the additional tiddlers
|
2014-08-30 19:44:26 +00:00
|
|
|
$tw.utils.pushTop(tiddlers,additionalTiddlers);
|
2014-02-13 14:23:27 +00:00
|
|
|
// Remove any excluded tiddlers
|
|
|
|
for(var t=tiddlers.length-1; t>=0; t--) {
|
|
|
|
if(excludeTiddlers.indexOf(tiddlers[t]) !== -1) {
|
|
|
|
tiddlers.splice(t,1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Pack up the tiddlers into a block of JSON
|
|
|
|
var plugins = {};
|
|
|
|
$tw.utils.each(tiddlers,function(title) {
|
|
|
|
var tiddler = $tw.wiki.getTiddler(title),
|
|
|
|
fields = {};
|
|
|
|
$tw.utils.each(tiddler.fields,function (value,name) {
|
|
|
|
fields[name] = tiddler.getFieldString(name);
|
|
|
|
});
|
|
|
|
plugins[title] = fields;
|
|
|
|
});
|
|
|
|
// Retrieve and bump the version number
|
|
|
|
var pluginVersion = $tw.utils.parseVersion(pluginTiddler.getFieldString("version") || "0.0.0") || {
|
|
|
|
major: "0",
|
|
|
|
minor: "0",
|
|
|
|
patch: "0"
|
|
|
|
};
|
|
|
|
pluginVersion.patch++;
|
|
|
|
var version = pluginVersion.major + "." + pluginVersion.minor + "." + pluginVersion.patch;
|
|
|
|
if(pluginVersion.prerelease) {
|
|
|
|
version += "-" + pluginVersion.prerelease;
|
|
|
|
}
|
|
|
|
if(pluginVersion.build) {
|
|
|
|
version += "+" + pluginVersion.build;
|
|
|
|
}
|
|
|
|
// Save the tiddler
|
|
|
|
$tw.wiki.addTiddler(new $tw.Tiddler(pluginTiddler,{text: JSON.stringify({tiddlers: plugins},null,4), version: version}));
|
|
|
|
// Delete any non-shadow constituent tiddlers
|
|
|
|
$tw.utils.each(tiddlers,function(title) {
|
|
|
|
if($tw.wiki.tiddlerExists(title)) {
|
|
|
|
$tw.wiki.deleteTiddler(title);
|
|
|
|
}
|
|
|
|
});
|
2014-08-29 08:58:30 +00:00
|
|
|
// Trigger an autosave
|
2014-09-02 10:16:06 +00:00
|
|
|
$tw.rootWidget.dispatchEvent({type: "tm-auto-save-wiki"});
|
2014-02-13 14:23:27 +00:00
|
|
|
// Return a heartwarming confirmation
|
|
|
|
return "Plugin " + title + " successfully saved";
|
2014-08-30 19:44:26 +00:00
|
|
|
};
|
2014-02-13 14:23:27 +00:00
|
|
|
|
|
|
|
})();
|