aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/luarocks/core/path.tl (renamed from src/luarocks/core/path.lua)46
1 files changed, 20 insertions, 26 deletions
diff --git a/src/luarocks/core/path.lua b/src/luarocks/core/path.tl
index 2f037b41..680c3296 100644
--- a/src/luarocks/core/path.lua
+++ b/src/luarocks/core/path.tl
@@ -1,22 +1,22 @@
1
2--- Core LuaRocks-specific path handling functions. 1--- Core LuaRocks-specific path handling functions.
3local path = {} 2local record path
3end
4 4
5local cfg = require("luarocks.core.cfg") 5local cfg = require("luarocks.core.cfg")
6local dir = require("luarocks.core.dir") 6local dir = require("luarocks.core.dir")
7local require = nil 7
8local type Tree = require("luarocks.core.types.tree").Tree
8 9
9local dir_sep = package.config:sub(1, 1) 10local dir_sep = package.config:sub(1, 1)
10-------------------------------------------------------------------------------- 11--------------------------------------------------------------------------------
11 12
12function path.rocks_dir(tree) 13function path.rocks_dir(tree: string | Tree): string
13 if tree == nil then 14 if tree == nil then
14 tree = cfg.root_dir 15 tree = cfg.root_dir
15 end 16 end
16 if type(tree) == "string" then 17 if tree is string then
17 return dir.path(tree, cfg.rocks_subdir) 18 return dir.path(tree, cfg.rocks_subdir)
18 end 19 end
19 assert(type(tree) == "table")
20 return tree.rocks_dir or dir.path(tree.root, cfg.rocks_subdir) 20 return tree.rocks_dir or dir.path(tree.root, cfg.rocks_subdir)
21end 21end
22 22
@@ -26,10 +26,8 @@ end
26-- @param name string: Rock name 26-- @param name string: Rock name
27-- @param version string: Rock version 27-- @param version string: Rock version
28-- @return string: a pathname with the same directory parts and a versioned basename. 28-- @return string: a pathname with the same directory parts and a versioned basename.
29function path.versioned_name(file, prefix, name, version) 29function path.versioned_name(file: string, prefix: string, name: string, version: string): string
30 assert(type(file) == "string") 30 assert(not name:match(dir_sep))
31 assert(type(name) == "string" and not name:match(dir_sep))
32 assert(type(version) == "string")
33 31
34 local rest = file:sub(#prefix+1):gsub("^" .. dir_sep .. "*", "") 32 local rest = file:sub(#prefix+1):gsub("^" .. dir_sep .. "*", "")
35 local name_version = (name.."_"..version):gsub("%-", "_"):gsub("%.", "_") 33 local name_version = (name.."_"..version):gsub("%-", "_"):gsub("%.", "_")
@@ -43,8 +41,7 @@ end
43-- @return string: The module identifier, or nil if given path is 41-- @return string: The module identifier, or nil if given path is
44-- not a conformant module path (the function does not check if the 42-- not a conformant module path (the function does not check if the
45-- path actually exists). 43-- path actually exists).
46function path.path_to_module(file) 44function path.path_to_module(file: string): string
47 assert(type(file) == "string")
48 45
49 local exts = {} 46 local exts = {}
50 local paths = package.path .. ";" .. package.cpath 47 local paths = package.path .. ";" .. package.cpath
@@ -55,7 +52,7 @@ function path.path_to_module(file)
55 end 52 end
56 end 53 end
57 54
58 local name 55 local name: string
59 for ext, _ in pairs(exts) do 56 for ext, _ in pairs(exts) do
60 name = file:match("(.*)%." .. ext .. "$") 57 name = file:match("(.*)%." .. ext .. "$")
61 if name then 58 if name then
@@ -72,25 +69,23 @@ function path.path_to_module(file)
72 return name 69 return name
73end 70end
74 71
75function path.deploy_lua_dir(tree) 72function path.deploy_lua_dir(tree: string | Tree): string
76 if type(tree) == "string" then 73 if tree is string then
77 return dir.path(tree, cfg.lua_modules_path) 74 return dir.path(tree, cfg.lua_modules_path)
78 else 75 else
79 assert(type(tree) == "table")
80 return tree.lua_dir or dir.path(tree.root, cfg.lua_modules_path) 76 return tree.lua_dir or dir.path(tree.root, cfg.lua_modules_path)
81 end 77 end
82end 78end
83 79
84function path.deploy_lib_dir(tree) 80function path.deploy_lib_dir(tree: string | Tree): string
85 if type(tree) == "string" then 81 if tree is string then
86 return dir.path(tree, cfg.lib_modules_path) 82 return dir.path(tree, cfg.lib_modules_path)
87 else 83 else
88 assert(type(tree) == "table")
89 return tree.lib_dir or dir.path(tree.root, cfg.lib_modules_path) 84 return tree.lib_dir or dir.path(tree.root, cfg.lib_modules_path)
90 end 85 end
91end 86end
92 87
93local is_src_extension = { [".lua"] = true, [".tl"] = true, [".tld"] = true, [".moon"] = true } 88local is_src_extension: {string: boolean} = { [".lua"] = true, [".tl"] = true, [".tld"] = true, [".moon"] = true }
94 89
95--- Return the pathname of the file that would be loaded for a module, indexed. 90--- Return the pathname of the file that would be loaded for a module, indexed.
96-- @param file_name string: module file name as in manifest (eg. "socket/core.so") 91-- @param file_name string: module file name as in manifest (eg. "socket/core.so")
@@ -100,8 +95,8 @@ local is_src_extension = { [".lua"] = true, [".tl"] = true, [".tld"] = true, [".
100-- @param i number: the index, 1 if version is the current default, > 1 otherwise. 95-- @param i number: the index, 1 if version is the current default, > 1 otherwise.
101-- This is done this way for use by select_module in luarocks.loader. 96-- This is done this way for use by select_module in luarocks.loader.
102-- @return string: filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so") 97-- @return string: filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so")
103function path.which_i(file_name, name, version, tree, i) 98function path.which_i(file_name: string, name: string, version: string, tree: string | Tree, i: number): string
104 local deploy_dir 99 local deploy_dir: string
105 local extension = file_name:match("%.[a-z]+$") 100 local extension = file_name:match("%.[a-z]+$")
106 if is_src_extension[extension] then 101 if is_src_extension[extension] then
107 deploy_dir = path.deploy_lua_dir(tree) 102 deploy_dir = path.deploy_lua_dir(tree)
@@ -116,11 +111,10 @@ function path.which_i(file_name, name, version, tree, i)
116 return file_name 111 return file_name
117end 112end
118 113
119function path.rocks_tree_to_string(tree) 114function path.rocks_tree_to_string(tree: string | Tree): string
120 if type(tree) == "string" then 115 if tree is string then
121 return tree 116 return tree
122 else 117 else
123 assert(type(tree) == "table")
124 return tree.root 118 return tree.root
125 end 119 end
126end 120end
@@ -132,7 +126,7 @@ end
132-- @param fn function: function to be applied, with the tree dir (string) as the first 126-- @param fn function: function to be applied, with the tree dir (string) as the first
133-- argument and the remaining varargs of map_trees as the following arguments. 127-- argument and the remaining varargs of map_trees as the following arguments.
134-- @return a table with all results of invocations of fn collected. 128-- @return a table with all results of invocations of fn collected.
135function path.map_trees(deps_mode, fn, ...) 129function path.map_trees(deps_mode: string, fn: function, ...: string): {any}
136 local result = {} 130 local result = {}
137 local current = cfg.root_dir or cfg.rocks_trees[1] 131 local current = cfg.root_dir or cfg.rocks_trees[1]
138 if deps_mode == "one" then 132 if deps_mode == "one" then