1
0

Fixed indent

This commit is contained in:
Jon Michael Aanes 2018-01-06 18:01:52 +01:00
parent 05d5a494f8
commit ed3af9b3b0

View File

@ -25,47 +25,47 @@
local SCAN_DIR_TEMPLATE, SCAN_DIR_SEP_PATTERN local SCAN_DIR_TEMPLATE, SCAN_DIR_SEP_PATTERN
if package.config:sub(1, 1) == '/' then if package.config:sub(1, 1) == '/' then
-- On unix -- On unix
SCAN_DIR_TEMPLATE = 'find -L "%s" -maxdepth 1 -mindepth 1 -print0' SCAN_DIR_TEMPLATE = 'find -L "%s" -maxdepth 1 -mindepth 1 -print0'
SCAN_DIR_SEP_PATTERN = '%z' SCAN_DIR_SEP_PATTERN = '%z'
else else
-- On windows -- On windows
SCAN_DIR_TEMPLATE = 'dir "%s" /b /ad' SCAN_DIR_TEMPLATE = 'dir "%s" /b /ad'
SCAN_DIR_SEP_PATTERN = '\n' SCAN_DIR_SEP_PATTERN = '\n'
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Util -- Util
local function get_module_paths (path_str) local function get_module_paths (path_str)
-- Gets the paths of contained in `path_str` based on the format in -- Gets the paths of contained in `path_str` based on the format in
-- package.config. -- package.config.
-- Error check -- Error check
local path_str = path_str or package.path local path_str = path_str or package.path
assert(type(package.config) == 'string') assert(type(package.config) == 'string')
assert(type(path_str) == 'string') assert(type(path_str) == 'string')
-- Work work -- Work work
local paths = {} local paths = {}
for path in path_str:gmatch '[^;]+' do for path in path_str:gmatch '[^;]+' do
paths[#paths+1] = path paths[#paths+1] = path
end end
-- Return -- Return
return paths return paths
end end
local function get_modules_fitting_path (root_path, module_names) local function get_modules_fitting_path (root_path, module_names)
-- First builds up a list of paths to files in `root_path`, and then goes -- First builds up a list of paths to files in `root_path`, and then goes
-- through and ensures they match the possible paths for modules. -- through and ensures they match the possible paths for modules.
assert(type(root_path) == 'string') assert(type(root_path) == 'string')
assert(type(module_names) == 'table') assert(type(module_names) == 'table')
-- Use `find` to find files in folders below the given matching. -- Use `find` to find files in folders below the given matching.
local pfile = io.popen ('find -L "'..(root_path:match '^(.-)?' or root_path)..'" -type f -not -path \'*/\\.*\' -print0') local pfile = io.popen ('find -L "'..(root_path:match '^(.-)?' or root_path)..'" -type f -not -path \'*/\\.*\' -print0')
local list_str = pfile:read '*all' local list_str = pfile:read '*all'
pfile:close() pfile:close()
-- Construct a pattern for the expected path of possible modules. -- Construct a pattern for the expected path of possible modules.
local module_path_pattern = '^' local module_path_pattern = '^'
@ -74,23 +74,23 @@ local function get_modules_fitting_path (root_path, module_names)
.. '$' .. '$'
-- Look through the file list, and find importable modules. -- Look through the file list, and find importable modules.
for path in list_str:gmatch '[^%z]+' do for path in list_str:gmatch '[^%z]+' do
local matches = { path:match(module_path_pattern) } local matches = { path:match(module_path_pattern) }
local identical = #matches > 0 local identical = #matches > 0
for i = 1, #matches do for i = 1, #matches do
if matches[1] ~= matches[i] then if matches[1] ~= matches[i] then
identical = false identical = false
break break
end end
end end
-- Check if match -- Check if match
if identical then if identical then
module_names[#module_names+1] = matches[1]:gsub('/', '.') module_names[#module_names+1] = matches[1]:gsub('/', '.')
end end
end end
end end
@ -98,78 +98,78 @@ end
-- Finding Module Names -- Finding Module Names
local function get_loaded_module_names () local function get_loaded_module_names ()
-- Get the names of modules already loaded. -- Get the names of modules already loaded.
local l = {} local l = {}
for k in pairs(package.loaded) do l[#l+1] = k end for k in pairs(package.loaded) do l[#l+1] = k end
return l return l
end end
local function get_preloaded_module_names () local function get_preloaded_module_names ()
-- Get the names of preloaded modules. -- Get the names of preloaded modules.
local l = {} local l = {}
for k in pairs(package.loaded) do l[#l+1] = k end for k in pairs(package.loaded) do l[#l+1] = k end
return l return l
end end
local function get_module_names_from_path () local function get_module_names_from_path ()
-- Get the names of modules not loaded yet. -- Get the names of modules not loaded yet.
local paths = get_module_paths(package.path) local paths = get_module_paths(package.path)
local modules = {} local modules = {}
for _, path in pairs(paths) do for _, path in pairs(paths) do
get_modules_fitting_path(path, modules) get_modules_fitting_path(path, modules)
end end
return modules return modules
end end
local function get_c_module_names_from_path () local function get_c_module_names_from_path ()
-- Get the names of c-modules not loaded yet. -- Get the names of c-modules not loaded yet.
local paths = get_module_paths(package.cpath) local paths = get_module_paths(package.cpath)
local modules = {} local modules = {}
for _, path in pairs(paths) do for _, path in pairs(paths) do
get_modules_fitting_path(path, modules) get_modules_fitting_path(path, modules)
end end
return modules return modules
end end
local PACKAGE_SEARCH_METHODS = { local PACKAGE_SEARCH_METHODS = {
get_loaded_module_names, get_loaded_module_names,
get_preloaded_module_names, get_preloaded_module_names,
get_module_names_from_path, get_module_names_from_path,
get_c_module_names_from_path, get_c_module_names_from_path,
} }
local function get_available_module_names () local function get_available_module_names ()
-- Searches through the package system to determine which modules can be -- Searches through the package system to determine which modules can be
-- imported by using `require`. -- imported by using `require`.
-- Returns a sequence of strings, each of which can be directly used by -- Returns a sequence of strings, each of which can be directly used by
-- `require` to import the module. -- `require` to import the module.
assert(type(package) == 'table') assert(type(package) == 'table')
-- Find and De-duplicate -- Find and De-duplicate
local dedub = {} local dedub = {}
for _, method in ipairs(PACKAGE_SEARCH_METHODS) do for _, method in ipairs(PACKAGE_SEARCH_METHODS) do
for _, name in ipairs(method()) do dedub[name] = true end for _, name in ipairs(method()) do dedub[name] = true end
end end
-- Sort -- Sort
local module_names = {} local module_names = {}
for name in pairs(dedub) do module_names[#module_names+1] = name end for name in pairs(dedub) do module_names[#module_names+1] = name end
table.sort(module_names) table.sort(module_names)
-- Output assertions -- Output assertions
assert(type(module_names) == 'table') assert(type(module_names) == 'table')
for i = 1, #module_names do for i = 1, #module_names do
assert(type(module_names[i]) == 'string') assert(type(module_names[i]) == 'string')
end end
-- Return -- Return
return module_names return module_names
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -179,13 +179,14 @@ end
-- if imported using "require", will return itself as a library. -- if imported using "require", will return itself as a library.
if ... then if ... then
return get_available_module_names return get_available_module_names
else else
local names = get_available_module_names() local names = get_available_module_names()
io.write 'Following modules are available: \n' io.write 'Following modules are available: \n'
for _, module_name in ipairs(names) do for _, module_name in ipairs(names) do
io.write ' - ' io.write ' - '
io.write(module_name) io.write(module_name)
io.write '\n' io.write '\n'
end end
end end