From 7d460c6daa22791d449cc5546552b282633e20b4 Mon Sep 17 00:00:00 2001 From: Paul Liverman III Date: Sun, 11 Feb 2018 14:18:07 -0800 Subject: [PATCH] should be ready for initial use --- example.locator_config.moon | 9 +++++ init.moon | 73 +++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 example.locator_config.moon create mode 100644 init.moon diff --git a/example.locator_config.moon b/example.locator_config.moon new file mode 100644 index 0000000..77ed5b2 --- /dev/null +++ b/example.locator_config.moon @@ -0,0 +1,9 @@ +{ + { + path: "applications.users" -- there is a sub-application at this location relative to the repository's root + migrations: {after: 1518414112} -- don't run any migrations earlier than this one + } + { + path: "utility" -- another sub-application is here + } +} diff --git a/init.moon b/init.moon new file mode 100644 index 0000000..e2a990e --- /dev/null +++ b/init.moon @@ -0,0 +1,73 @@ +config = require "locator_config" + +import insert, sort from table + +-- locates and returns a module, or errors +-- if priority (path) specified, will check it before other paths +-- checks project root first, then each path specified in locator_config +try_require = (path, priority) -> + if priority + ok, value = pcall -> require "#{priority}.#{path}" + return value if ok + + ok, value = pcall -> require path + return value if ok + + for item in *config + ok, value = pcall -> require "#{item.path}.#{path}" + return value if ok + + error "locator could not find '#{path}'" + +-- works like Lapis's autoload, but +-- includes trying sub-application paths & can be called to access a value +autoload = (path, tab={}) -> + return setmetatable tab, { + __call: (t, name) -> + t[name] = try_require name, path + return t[name] + __index: (t, name) -> + t[name] = try_require name, path + return t[name] + } + +-- pass your migrations, it returns them + all sub-application migrations +-- (legacy) see example config for how to specify to not include early migrations +migrations = (app_migrations={}) -> + for item in *config + ok, migrations = pcall -> require "#{item.path}.migrations" + if ok + sorted = {} + for m in pairs migrations + insert sorted, m + sort sorted + for i in *sorted + -- only allow migrations after specified config value, or if no 'after' is specified + if (item.migrations and ((item.migrations.after and i > item.migrations.after) or not item.migrations.after)) or not item.migrations + -- if your migrations and theirs share a value, combine them + if app_fn = app_migrations[i] + app_migrations[i] = (...) -> + app_fn(...) + migrations[i](...) + -- else just add them + else + app_migrations[i] = migrations[i] + + return app_migrations + +-- return access to autoload and migrations functions, +-- and metamethods for getting autoloaders +return setmetatable { + :autoload, :migrations +}, { + __call: (t, here) -> + if "init" == here\sub -4 + here = here\sub 1, -6 + unless here + here = "" + return autoload here + + __index: (t, name) -> + t[name] = autoload name + return t[name] +}