2017-09-29 07:36:56 +00:00
|
|
|
|
2017-10-11 09:57:42 +00:00
|
|
|
-- ||| Suggest-Require ||| -----------------------------------------------------
|
2017-09-29 07:36:56 +00:00
|
|
|
|
2017-10-11 09:57:42 +00:00
|
|
|
-- Version 1.0.0 ( 11. October 2017 )
|
2017-09-29 07:36:56 +00:00
|
|
|
|
2017-10-11 09:57:42 +00:00
|
|
|
-- This is a small library to discover which modules are importable using
|
|
|
|
-- `require`. It's useful for seeing which modules your Lua environment
|
|
|
|
-- can access. It's intended usage is in an auto-complete system for Lua.
|
|
|
|
|
|
|
|
-- Author: Jmaa
|
|
|
|
-- Email: jonjmaa@gmail.com
|
|
|
|
-- Website: aanes.xyz
|
|
|
|
|
|
|
|
-- "THE BEER-WARE LICENSE" (Revision 42):
|
|
|
|
-- <jonjmaa@gmail.com> wrote this file. As long as you retain this notice you
|
|
|
|
-- can do whatever you want with this stuff. If we meet some day, and you think
|
|
|
|
-- this stuff is worth it, you can buy me a beer in return.
|
|
|
|
|
|
|
|
-- TODO: Ensure it works under both Windows and MacOS.
|
|
|
|
-- TODO: Add support for alternative package.config
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
2017-09-29 07:36:56 +00:00
|
|
|
-- Constants
|
|
|
|
|
|
|
|
local SCAN_DIR_TEMPLATE, SCAN_DIR_SEP_PATTERN
|
|
|
|
|
|
|
|
if package.config:sub(1, 1) == '/' then
|
|
|
|
-- On unix
|
|
|
|
SCAN_DIR_TEMPLATE = 'find -L "%s" -maxdepth 1 -mindepth 1 -print0'
|
|
|
|
SCAN_DIR_SEP_PATTERN = '%z'
|
|
|
|
else
|
|
|
|
-- On windows
|
|
|
|
SCAN_DIR_TEMPLATE = 'dir "%s" /b /ad'
|
|
|
|
SCAN_DIR_SEP_PATTERN = '\n'
|
|
|
|
end
|
|
|
|
|
2017-10-11 09:57:42 +00:00
|
|
|
--------------------------------------------------------------------------------
|
2017-09-29 07:36:56 +00:00
|
|
|
-- Util
|
|
|
|
|
|
|
|
local function get_module_paths (path_str)
|
2017-10-11 09:57:42 +00:00
|
|
|
-- Gets the paths of contained in `path_str` based on the format in
|
|
|
|
-- package.config.
|
2017-09-29 07:36:56 +00:00
|
|
|
|
|
|
|
-- Error check
|
|
|
|
local path_str = path_str or package.path
|
|
|
|
assert(type(package.config) == 'string')
|
|
|
|
assert(type(path_str) == 'string')
|
|
|
|
|
|
|
|
-- Work work
|
|
|
|
local paths = {}
|
|
|
|
for path in path_str:gmatch '[^;]+' do
|
|
|
|
paths[#paths+1] = path
|
|
|
|
end
|
|
|
|
-- Return
|
|
|
|
return paths
|
|
|
|
end
|
|
|
|
|
|
|
|
local function get_modules_fitting_path (root_path, module_names)
|
2017-10-11 09:57:42 +00:00
|
|
|
-- 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.
|
|
|
|
|
2017-09-29 07:36:56 +00:00
|
|
|
assert(type(root_path) == 'string')
|
|
|
|
assert(type(module_names) == 'table')
|
|
|
|
|
|
|
|
-- 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 list_str = pfile:read '*all'
|
|
|
|
pfile:close()
|
2017-10-11 09:57:42 +00:00
|
|
|
|
2017-09-29 07:36:56 +00:00
|
|
|
-- Construct a pattern for the expected path of possible modules.
|
|
|
|
local module_path_pattern = '^'
|
|
|
|
.. root_path:gsub('[%^%(%)%.%[%]%*%+%-]', function (a) return '%' .. a end)
|
|
|
|
:gsub('?', '(.+)')
|
|
|
|
.. '$'
|
2017-10-11 09:57:42 +00:00
|
|
|
|
2017-09-29 07:36:56 +00:00
|
|
|
-- Look through the file list, and find importable modules.
|
|
|
|
for path in list_str:gmatch '[^%z]+' do
|
|
|
|
|
|
|
|
local matches = { path:match(module_path_pattern) }
|
|
|
|
local identical = #matches > 0
|
|
|
|
|
2017-10-11 09:57:42 +00:00
|
|
|
for i = 1, #matches do
|
|
|
|
if matches[1] ~= matches[i] then
|
|
|
|
identical = false
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2017-09-29 07:36:56 +00:00
|
|
|
|
|
|
|
-- Check if match
|
|
|
|
if identical then
|
|
|
|
module_names[#module_names+1] = matches[1]:gsub('/', '.')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2017-10-11 09:57:42 +00:00
|
|
|
--------------------------------------------------------------------------------
|
2017-09-29 07:36:56 +00:00
|
|
|
-- Finding Module Names
|
|
|
|
|
|
|
|
local function get_loaded_module_names ()
|
|
|
|
-- Get the names of modules already loaded.
|
|
|
|
local l = {}
|
|
|
|
for k in pairs(package.loaded) do l[#l+1] = k end
|
|
|
|
return l
|
|
|
|
end
|
|
|
|
|
2017-09-30 12:39:53 +00:00
|
|
|
local function get_preloaded_module_names ()
|
|
|
|
-- Get the names of preloaded modules.
|
|
|
|
local l = {}
|
|
|
|
for k in pairs(package.loaded) do l[#l+1] = k end
|
|
|
|
return l
|
|
|
|
end
|
|
|
|
|
2017-09-29 07:36:56 +00:00
|
|
|
local function get_module_names_from_path ()
|
2017-10-11 09:57:42 +00:00
|
|
|
-- Get the names of modules not loaded yet.
|
2017-09-29 07:36:56 +00:00
|
|
|
local paths = get_module_paths(package.path)
|
|
|
|
|
|
|
|
local modules = {}
|
|
|
|
for _, path in pairs(paths) do
|
|
|
|
get_modules_fitting_path(path, modules)
|
|
|
|
end
|
|
|
|
|
|
|
|
return modules
|
|
|
|
end
|
|
|
|
|
|
|
|
local function get_c_module_names_from_path ()
|
2017-10-11 09:57:42 +00:00
|
|
|
-- Get the names of c-modules not loaded yet.
|
2017-09-29 07:36:56 +00:00
|
|
|
local paths = get_module_paths(package.cpath)
|
|
|
|
|
|
|
|
local modules = {}
|
|
|
|
for _, path in pairs(paths) do
|
|
|
|
get_modules_fitting_path(path, modules)
|
|
|
|
end
|
|
|
|
|
|
|
|
return modules
|
|
|
|
end
|
|
|
|
|
2017-09-30 12:39:53 +00:00
|
|
|
local PACKAGE_SEARCH_METHODS = {
|
|
|
|
get_loaded_module_names,
|
|
|
|
get_preloaded_module_names,
|
|
|
|
get_module_names_from_path,
|
|
|
|
get_c_module_names_from_path,
|
|
|
|
}
|
|
|
|
|
2017-09-29 07:36:56 +00:00
|
|
|
local function get_available_module_names ()
|
2017-09-30 12:39:53 +00:00
|
|
|
-- Searches through the package system to determine which modules can be
|
2017-10-11 09:57:42 +00:00
|
|
|
-- imported by using `require`.
|
|
|
|
|
|
|
|
-- Returns a sequence of strings, each of which can be directly used by
|
|
|
|
-- `require` to import the module.
|
2017-09-29 07:36:56 +00:00
|
|
|
|
2017-09-30 12:39:53 +00:00
|
|
|
assert(type(package) == 'table')
|
2017-09-29 07:36:56 +00:00
|
|
|
|
2017-10-11 09:57:42 +00:00
|
|
|
-- Find and De-duplicate
|
2017-09-29 07:36:56 +00:00
|
|
|
local dedub = {}
|
2017-09-30 12:39:53 +00:00
|
|
|
for _, method in ipairs(PACKAGE_SEARCH_METHODS) do
|
|
|
|
for _, name in ipairs(method()) do dedub[name] = true end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Sort
|
2017-09-29 07:36:56 +00:00
|
|
|
local module_names = {}
|
|
|
|
for name in pairs(dedub) do module_names[#module_names+1] = name end
|
|
|
|
table.sort(module_names)
|
|
|
|
|
|
|
|
-- Output assertions
|
|
|
|
assert(type(module_names) == 'table')
|
|
|
|
for i = 1, #module_names do
|
|
|
|
assert(type(module_names[i]) == 'string')
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Return
|
|
|
|
return module_names
|
|
|
|
end
|
|
|
|
|
2017-10-11 09:57:42 +00:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Run or return
|
|
|
|
|
|
|
|
-- Can be run directly from the terminal to display the available modules, or
|
|
|
|
-- if imported using "require", will return itself as a library.
|
2017-09-29 07:36:56 +00:00
|
|
|
|
|
|
|
if ... then
|
|
|
|
return get_available_module_names
|
|
|
|
else
|
|
|
|
local names = get_available_module_names()
|
|
|
|
io.write 'Following modules are available: \n'
|
|
|
|
for _, module_name in ipairs(names) do
|
|
|
|
io.write ' - '
|
|
|
|
io.write(module_name)
|
|
|
|
io.write '\n'
|
|
|
|
end
|
|
|
|
end
|