mirror of
https://github.com/lazuscripts/locator.git
synced 2024-11-19 10:54:24 +00:00
should be ready for initial use
This commit is contained in:
parent
643a0bcfb6
commit
7d460c6daa
9
example.locator_config.moon
Normal file
9
example.locator_config.moon
Normal file
@ -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
|
||||
}
|
||||
}
|
73
init.moon
Normal file
73
init.moon
Normal file
@ -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]
|
||||
}
|
Loading…
Reference in New Issue
Block a user