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
|
2018-01-06 17:01:52 +00:00
|
|
|
-- On unix
|
|
|
|
SCAN_DIR_TEMPLATE = 'find -L "%s" -maxdepth 1 -mindepth 1 -print0'
|
|
|
|
SCAN_DIR_SEP_PATTERN = '%z'
|
2017-09-29 07:36:56 +00:00
|
|
|
else
|
2018-01-06 17:01:52 +00:00
|
|
|
-- On windows
|
|
|
|
SCAN_DIR_TEMPLATE = 'dir "%s" /b /ad'
|
|
|
|
SCAN_DIR_SEP_PATTERN = '\n'
|
2017-09-29 07:36:56 +00:00
|
|
|
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)
|
2018-01-06 17:01:52 +00:00
|
|
|
-- Gets the paths of contained in `path_str` based on the format in
|
|
|
|
-- package.config.
|
|
|
|
|
|
|
|
-- 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
|
2017-09-29 07:36:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function get_modules_fitting_path (root_path, module_names)
|
2018-01-06 17:01:52 +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-10-11 09:57:42 +00:00
|
|
|
|
2018-01-06 17:01:52 +00:00
|
|
|
assert(type(root_path) == 'string')
|
|
|
|
assert(type(module_names) == 'table')
|
2017-09-29 07:36:56 +00:00
|
|
|
|
|
|
|
-- Use `find` to find files in folders below the given matching.
|
2018-01-06 17:01:52 +00:00
|
|
|
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.
|
2018-01-06 17:01:52 +00:00
|
|
|
for path in list_str:gmatch '[^%z]+' do
|
|
|
|
|
|
|
|
local matches = { path:match(module_path_pattern) }
|
|
|
|
local identical = #matches > 0
|
|
|
|
|
|
|
|
for i = 1, #matches do
|
|
|
|
if matches[1] ~= matches[i] then
|
|
|
|
identical = false
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Check if match
|
|
|
|
if identical then
|
|
|
|
module_names[#module_names+1] = matches[1]:gsub('/', '.')
|
|
|
|
end
|
|
|
|
end
|
2017-09-29 07:36:56 +00:00
|
|
|
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 ()
|
2018-01-06 17:01:52 +00:00
|
|
|
-- Get the names of modules already loaded.
|
|
|
|
local l = {}
|
|
|
|
for k in pairs(package.loaded) do l[#l+1] = k end
|
|
|
|
return l
|
2017-09-29 07:36:56 +00:00
|
|
|
end
|
|
|
|
|
2017-09-30 12:39:53 +00:00
|
|
|
local function get_preloaded_module_names ()
|
2018-01-06 17:01:52 +00:00
|
|
|
-- Get the names of preloaded modules.
|
|
|
|
local l = {}
|
|
|
|
for k in pairs(package.loaded) do l[#l+1] = k end
|
|
|
|
return l
|
2017-09-30 12:39:53 +00:00
|
|
|
end
|
|
|
|
|
2017-09-29 07:36:56 +00:00
|
|
|
local function get_module_names_from_path ()
|
2018-01-06 17:01:52 +00:00
|
|
|
-- Get the names of modules not loaded yet.
|
|
|
|
local paths = get_module_paths(package.path)
|
2017-09-29 07:36:56 +00:00
|
|
|
|
2018-01-06 17:01:52 +00:00
|
|
|
local modules = {}
|
|
|
|
for _, path in pairs(paths) do
|
|
|
|
get_modules_fitting_path(path, modules)
|
|
|
|
end
|
2017-09-29 07:36:56 +00:00
|
|
|
|
2018-01-06 17:01:52 +00:00
|
|
|
return modules
|
2017-09-29 07:36:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function get_c_module_names_from_path ()
|
2018-01-06 17:01:52 +00:00
|
|
|
-- Get the names of c-modules not loaded yet.
|
|
|
|
local paths = get_module_paths(package.cpath)
|
2017-09-29 07:36:56 +00:00
|
|
|
|
2018-01-06 17:01:52 +00:00
|
|
|
local modules = {}
|
|
|
|
for _, path in pairs(paths) do
|
|
|
|
get_modules_fitting_path(path, modules)
|
|
|
|
end
|
2017-09-29 07:36:56 +00:00
|
|
|
|
2018-01-06 17:01:52 +00:00
|
|
|
return modules
|
2017-09-29 07:36:56 +00:00
|
|
|
end
|
|
|
|
|
2017-09-30 12:39:53 +00:00
|
|
|
local PACKAGE_SEARCH_METHODS = {
|
2018-01-06 17:01:52 +00:00
|
|
|
get_loaded_module_names,
|
|
|
|
get_preloaded_module_names,
|
|
|
|
get_module_names_from_path,
|
|
|
|
get_c_module_names_from_path,
|
2017-09-30 12:39:53 +00:00
|
|
|
}
|
|
|
|
|
2017-09-29 07:36:56 +00:00
|
|
|
local function get_available_module_names ()
|
2018-01-06 17:01:52 +00:00
|
|
|
-- Searches through the package system to determine which modules can be
|
|
|
|
-- imported by using `require`.
|
2017-10-11 09:57:42 +00:00
|
|
|
|
2018-01-06 17:01:52 +00:00
|
|
|
-- 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
|
|
|
|
2018-01-06 17:01:52 +00:00
|
|
|
assert(type(package) == 'table')
|
2017-09-29 07:36:56 +00:00
|
|
|
|
2018-01-06 17:01:52 +00:00
|
|
|
-- Find and De-duplicate
|
|
|
|
local dedub = {}
|
|
|
|
for _, method in ipairs(PACKAGE_SEARCH_METHODS) do
|
|
|
|
for _, name in ipairs(method()) do dedub[name] = true end
|
|
|
|
end
|
2017-09-30 12:39:53 +00:00
|
|
|
|
2018-01-06 17:01:52 +00:00
|
|
|
-- Sort
|
|
|
|
local module_names = {}
|
|
|
|
for name in pairs(dedub) do module_names[#module_names+1] = name end
|
|
|
|
table.sort(module_names)
|
2017-09-29 07:36:56 +00:00
|
|
|
|
2018-01-06 17:01:52 +00:00
|
|
|
-- Output assertions
|
|
|
|
assert(type(module_names) == 'table')
|
|
|
|
for i = 1, #module_names do
|
|
|
|
assert(type(module_names[i]) == 'string')
|
|
|
|
end
|
2017-09-29 07:36:56 +00:00
|
|
|
|
2018-01-06 17:01:52 +00:00
|
|
|
-- Return
|
|
|
|
return module_names
|
2017-09-29 07:36:56 +00:00
|
|
|
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
|
2018-01-06 17:01:52 +00:00
|
|
|
return get_available_module_names
|
2017-09-29 07:36:56 +00:00
|
|
|
else
|
2018-01-06 17:01:52 +00:00
|
|
|
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
|
2017-09-29 07:36:56 +00:00
|
|
|
end
|
2018-01-06 17:01:52 +00:00
|
|
|
|