summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <daniel.silverstone@codethink.co.uk>2014-03-10 12:41:07 +0000
committerDaniel Silverstone <daniel.silverstone@codethink.co.uk>2014-03-11 13:54:00 +0000
commit1b141c8359c690756b0f9612fd6ccfc7ca5e9f99 (patch)
treef243d8b2649353466be3a82c3ee38c551f387c58
parent2a078069dcfd01c7bf573d53340c8264c3dc4263 (diff)
downloadgitano-1b141c8359c690756b0f9612fd6ccfc7ca5e9f99.tar.bz2
Plugin support in Gitano
This patch adds support for Gitano to load plugins from a colon separated set of paths. While colon separation is not the best mechanism, it is in common use. We explicitly do not add any escaping rules such as :: -> : in order to remain compatible with things like PATH processing. Signed-off-by: Daniel Silverstone <daniel.silverstone@codethink.co.uk>
-rw-r--r--Makefile2
-rw-r--r--lib/gitano.lua4
-rw-r--r--lib/gitano/plugins.lua63
3 files changed, 67 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index 7fa7d11..775f5cc 100644
--- a/Makefile
+++ b/Makefile
@@ -43,7 +43,7 @@ MODS := gitano \
gitano.actions gitano.config gitano.lace gitano.log \
gitano.markdown gitano.repository gitano.supple \
gitano.command gitano.admincommand gitano.usercommand \
- gitano.repocommand gitano.copycommand gitano.auth
+ gitano.repocommand gitano.copycommand gitano.auth gitano.plugins
SKEL_FILES := gitano-admin/rules/selfchecks.lace \
gitano-admin/rules/aschecks.lace \
diff --git a/lib/gitano.lua b/lib/gitano.lua
index b57bd71..31e62d5 100644
--- a/lib/gitano.lua
+++ b/lib/gitano.lua
@@ -15,6 +15,7 @@ local lace = require 'gitano.lace'
local markdown = require 'gitano.markdown'
local supple = require 'gitano.supple'
local auth = require 'gitano.auth'
+local plugins = require 'gitano.plugins'
return {
util = util,
@@ -26,5 +27,6 @@ return {
lace = lace,
markdown = markdown,
supple = supple,
- auth = auth
+ auth = auth,
+ plugins = plugins,
}
diff --git a/lib/gitano/plugins.lua b/lib/gitano/plugins.lua
new file mode 100644
index 0000000..bdc6d1e
--- /dev/null
+++ b/lib/gitano/plugins.lua
@@ -0,0 +1,63 @@
+-- gitano.plugins
+--
+-- Plugin loading support for Gitano
+--
+-- Copyright 2014 Daniel Silverstone <daniel.silverstone@codethink.co.uk>
+
+local util = require "gitano.util"
+local log = require "gitano.log"
+
+local luxio = require "luxio"
+local sio = require "luxio.simple"
+
+local gfind = string.gfind
+
+local plugin_name_pattern = "^(.+)%.lua$"
+
+local function find_plugins(path)
+ local ret = {}
+ for _, entry in ipairs(path) do
+ local dirp, err = sio.opendir(entry)
+ if not dirp then
+ log.warning(("Unable to scan plugin directory '%s': %s")
+ :format(entry, err))
+ else
+ for filename, fileinfo in dirp:iterate() do
+ local plugin_name = filename:match(plugin_name_pattern)
+ if plugin_name and fileinfo.d_type == luxio.DT_REG then
+ if not ret[plugin_name] then
+ ret[plugin_name] = entry
+ ret[#ret + 1] = plugin_name
+ end
+ end
+ end
+ end
+ end
+ table.sort(ret)
+ return ret
+end
+
+local function load_plugins(path)
+ local to_load = find_plugins(path)
+ for _, plugin_name in ipairs(to_load) do
+ local filepath = util.path_join(to_load[plugin_name],
+ plugin_name .. ".lua")
+ local chunk, err = loadfile(filepath)
+ if not chunk then
+ log.warning(("Failure loading plugin '%s' from '%s': %s")
+ :format(plugin_name, to_load[plugin_name],
+ err))
+ else
+ local ok, err = pcall(chunk)
+ if not ok then
+ log.warning(("Failure running plugin '%s' from '%s': %s")
+ :format(plugin_name, to_load[plugin_name],
+ err))
+ end
+ end
+ end
+end
+
+return {
+ load_plugins = load_plugins,
+}