aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorV1K1NGbg <victor@ilchev.com>2024-08-22 17:48:57 -0300
committerHisham Muhammad <hisham@gobolinux.org>2024-10-21 13:30:51 -0300
commit084bc6dd350e585ed48bd2bc454aaea1e8fc8dd7 (patch)
treebbdee2c9a5d3ee0d3b63f727236bd9d5f8bedfff /src
parent66459e17a7f356827d1ee2592e847d0e939acf0f (diff)
downloadluarocks-084bc6dd350e585ed48bd2bc454aaea1e8fc8dd7.tar.gz
luarocks-084bc6dd350e585ed48bd2bc454aaea1e8fc8dd7.tar.bz2
luarocks-084bc6dd350e585ed48bd2bc454aaea1e8fc8dd7.zip
Teal: convert luarocks.path
Diffstat (limited to 'src')
-rw-r--r--src/luarocks/path.tl (renamed from src/luarocks/path.lua)123
1 files changed, 57 insertions, 66 deletions
diff --git a/src/luarocks/path.lua b/src/luarocks/path.tl
index 19657c83..dae3e318 100644
--- a/src/luarocks/path.lua
+++ b/src/luarocks/path.tl
@@ -2,12 +2,23 @@
2--- LuaRocks-specific path handling functions. 2--- LuaRocks-specific path handling functions.
3-- All paths are configured in this module, making it a single 3-- All paths are configured in this module, making it a single
4-- point where the layout of the local installation is defined in LuaRocks. 4-- point where the layout of the local installation is defined in LuaRocks.
5local path = {}
6 5
6local cfg = require("luarocks.core.cfg")
7local core = require("luarocks.core.path") 7local core = require("luarocks.core.path")
8local dir = require("luarocks.dir") 8local dir = require("luarocks.dir")
9local cfg = require("luarocks.core.cfg") 9local util = require("luarocks.core.util")
10local util = require("luarocks.util") 10
11local type Tree = require("luarocks.core.types.tree").Tree
12
13local record path
14 rocks_dir: function(string | Tree): string
15 versioned_name: function(string, string, string, string): string
16 path_to_module: function(string): string
17 deploy_lua_dir: function(string | Tree): string
18 deploy_lib_dir: function(string | Tree): string
19 map_trees: function(string, function(...: any): any..., ...: string): {any}
20 rocks_tree_to_string: function(string | Tree): string
21end
11 22
12path.rocks_dir = core.rocks_dir 23path.rocks_dir = core.rocks_dir
13path.versioned_name = core.versioned_name 24path.versioned_name = core.versioned_name
@@ -17,34 +28,31 @@ path.deploy_lib_dir = core.deploy_lib_dir
17path.map_trees = core.map_trees 28path.map_trees = core.map_trees
18path.rocks_tree_to_string = core.rocks_tree_to_string 29path.rocks_tree_to_string = core.rocks_tree_to_string
19 30
31function path.root_dir(tree: string | Tree): string
32 if tree is string then
33 return tree
34 else
35 return tree.root
36 end
37end
38
20--- Infer rockspec filename from a rock filename. 39--- Infer rockspec filename from a rock filename.
21-- @param rock_name string: Pathname of a rock file. 40-- @param rock_name string: Pathname of a rock file.
22-- @return string: Filename of the rockspec, without path. 41-- @return string: Filename of the rockspec, without path.
23function path.rockspec_name_from_rock(rock_name) 42function path.rockspec_name_from_rock(rock_name: string): string
24 assert(type(rock_name) == "string")
25 local base_name = dir.base_name(rock_name) 43 local base_name = dir.base_name(rock_name)
26 return base_name:match("(.*)%.[^.]*.rock") .. ".rockspec" 44 return base_name:match("(.*)%.[^.]*.rock") .. ".rockspec"
27end 45end
28 46
29function path.root_from_rocks_dir(rocks_dir) 47function path.root_from_rocks_dir(rocks_dir: string): string
30 assert(type(rocks_dir) == "string")
31 return rocks_dir:match("(.*)" .. util.matchquote(cfg.rocks_subdir) .. ".*$") 48 return rocks_dir:match("(.*)" .. util.matchquote(cfg.rocks_subdir) .. ".*$")
32end 49end
33 50
34function path.root_dir(tree) 51function path.deploy_bin_dir(tree: string | Tree): string
35 if type(tree) == "string" then
36 return tree
37 else
38 assert(type(tree) == "table")
39 return tree.root
40 end
41end
42
43function path.deploy_bin_dir(tree)
44 return dir.path(path.root_dir(tree), "bin") 52 return dir.path(path.root_dir(tree), "bin")
45end 53end
46 54
47function path.manifest_file(tree) 55function path.manifest_file(tree: string | Tree): string
48 return dir.path(path.rocks_dir(tree), "manifest") 56 return dir.path(path.rocks_dir(tree), "manifest")
49end 57end
50 58
@@ -53,8 +61,8 @@ end
53-- @return string: The resulting path -- does not guarantee that 61-- @return string: The resulting path -- does not guarantee that
54-- @param tree string or nil: If given, specifies the local tree to use. 62-- @param tree string or nil: If given, specifies the local tree to use.
55-- the package (and by extension, the path) exists. 63-- the package (and by extension, the path) exists.
56function path.versions_dir(name, tree) 64function path.versions_dir(name: string, tree?: string | Tree): string
57 assert(type(name) == "string" and not name:match("/")) 65 assert(not name:match("/"))
58 return dir.path(path.rocks_dir(tree), name) 66 return dir.path(path.rocks_dir(tree), name)
59end 67end
60 68
@@ -64,9 +72,8 @@ end
64-- @param tree string or nil: If given, specifies the local tree to use. 72-- @param tree string or nil: If given, specifies the local tree to use.
65-- @return string: The resulting path -- does not guarantee that 73-- @return string: The resulting path -- does not guarantee that
66-- the package (and by extension, the path) exists. 74-- the package (and by extension, the path) exists.
67function path.install_dir(name, version, tree) 75function path.install_dir(name: string, version: string, tree?: string | Tree): string
68 assert(type(name) == "string" and not name:match("/")) 76 assert(not name:match("/"))
69 assert(type(version) == "string")
70 return dir.path(path.rocks_dir(tree), name, version) 77 return dir.path(path.rocks_dir(tree), name, version)
71end 78end
72 79
@@ -76,9 +83,8 @@ end
76-- @param tree string or nil: If given, specifies the local tree to use. 83-- @param tree string or nil: If given, specifies the local tree to use.
77-- @return string: The resulting path -- does not guarantee that 84-- @return string: The resulting path -- does not guarantee that
78-- the package (and by extension, the file) exists. 85-- the package (and by extension, the file) exists.
79function path.rockspec_file(name, version, tree) 86function path.rockspec_file(name: string, version: string, tree?: string | Tree): string
80 assert(type(name) == "string" and not name:match("/")) 87 assert(not name:match("/"))
81 assert(type(version) == "string")
82 return dir.path(path.rocks_dir(tree), name, version, name.."-"..version..".rockspec") 88 return dir.path(path.rocks_dir(tree), name, version, name.."-"..version..".rockspec")
83end 89end
84 90
@@ -88,9 +94,8 @@ end
88-- @param tree string or nil: If given, specifies the local tree to use. 94-- @param tree string or nil: If given, specifies the local tree to use.
89-- @return string: The resulting path -- does not guarantee that 95-- @return string: The resulting path -- does not guarantee that
90-- the package (and by extension, the file) exists. 96-- the package (and by extension, the file) exists.
91function path.rock_manifest_file(name, version, tree) 97function path.rock_manifest_file(name: string, version: string, tree?: string | Tree): string
92 assert(type(name) == "string" and not name:match("/")) 98 assert(not name:match("/"))
93 assert(type(version) == "string")
94 return dir.path(path.rocks_dir(tree), name, version, "rock_manifest") 99 return dir.path(path.rocks_dir(tree), name, version, "rock_manifest")
95end 100end
96 101
@@ -100,9 +105,8 @@ end
100-- @param tree string or nil: If given, specifies the local tree to use. 105-- @param tree string or nil: If given, specifies the local tree to use.
101-- @return string: The resulting path -- does not guarantee that 106-- @return string: The resulting path -- does not guarantee that
102-- the package (and by extension, the file) exists. 107-- the package (and by extension, the file) exists.
103function path.rock_namespace_file(name, version, tree) 108function path.rock_namespace_file(name: string, version: string, tree?: string | Tree): string
104 assert(type(name) == "string" and not name:match("/")) 109 assert(not name:match("/"))
105 assert(type(version) == "string")
106 return dir.path(path.rocks_dir(tree), name, version, "rock_namespace") 110 return dir.path(path.rocks_dir(tree), name, version, "rock_namespace")
107end 111end
108 112
@@ -112,9 +116,8 @@ end
112-- @param tree string or nil: If given, specifies the local tree to use. 116-- @param tree string or nil: If given, specifies the local tree to use.
113-- @return string: The resulting path -- does not guarantee that 117-- @return string: The resulting path -- does not guarantee that
114-- the package (and by extension, the path) exists. 118-- the package (and by extension, the path) exists.
115function path.lib_dir(name, version, tree) 119function path.lib_dir(name: string, version: string, tree?: string | Tree): string
116 assert(type(name) == "string" and not name:match("/")) 120 assert(not name:match("/"))
117 assert(type(version) == "string")
118 return dir.path(path.rocks_dir(tree), name, version, "lib") 121 return dir.path(path.rocks_dir(tree), name, version, "lib")
119end 122end
120 123
@@ -124,9 +127,8 @@ end
124-- @param tree string or nil: If given, specifies the local tree to use. 127-- @param tree string or nil: If given, specifies the local tree to use.
125-- @return string: The resulting path -- does not guarantee that 128-- @return string: The resulting path -- does not guarantee that
126-- the package (and by extension, the path) exists. 129-- the package (and by extension, the path) exists.
127function path.lua_dir(name, version, tree) 130function path.lua_dir(name: string, version: string, tree?: string | Tree): string
128 assert(type(name) == "string" and not name:match("/")) 131 assert(not name:match("/"))
129 assert(type(version) == "string")
130 return dir.path(path.rocks_dir(tree), name, version, "lua") 132 return dir.path(path.rocks_dir(tree), name, version, "lua")
131end 133end
132 134
@@ -136,9 +138,8 @@ end
136-- @param tree string or nil: If given, specifies the local tree to use. 138-- @param tree string or nil: If given, specifies the local tree to use.
137-- @return string: The resulting path -- does not guarantee that 139-- @return string: The resulting path -- does not guarantee that
138-- the package (and by extension, the path) exists. 140-- the package (and by extension, the path) exists.
139function path.doc_dir(name, version, tree) 141function path.doc_dir(name: string, version: string, tree?: string | Tree): string
140 assert(type(name) == "string" and not name:match("/")) 142 assert(not name:match("/"))
141 assert(type(version) == "string")
142 return dir.path(path.rocks_dir(tree), name, version, "doc") 143 return dir.path(path.rocks_dir(tree), name, version, "doc")
143end 144end
144 145
@@ -148,9 +149,8 @@ end
148-- @param tree string or nil: If given, specifies the local tree to use. 149-- @param tree string or nil: If given, specifies the local tree to use.
149-- @return string: The resulting path -- does not guarantee that 150-- @return string: The resulting path -- does not guarantee that
150-- the package (and by extension, the path) exists. 151-- the package (and by extension, the path) exists.
151function path.conf_dir(name, version, tree) 152function path.conf_dir(name: string, version: string, tree?: string | Tree): string
152 assert(type(name) == "string" and not name:match("/")) 153 assert(not name:match("/"))
153 assert(type(version) == "string")
154 return dir.path(path.rocks_dir(tree), name, version, "conf") 154 return dir.path(path.rocks_dir(tree), name, version, "conf")
155end 155end
156 156
@@ -161,9 +161,8 @@ end
161-- @param tree string or nil: If given, specifies the local tree to use. 161-- @param tree string or nil: If given, specifies the local tree to use.
162-- @return string: The resulting path -- does not guarantee that 162-- @return string: The resulting path -- does not guarantee that
163-- the package (and by extension, the path) exists. 163-- the package (and by extension, the path) exists.
164function path.bin_dir(name, version, tree) 164function path.bin_dir(name: string, version: string, tree?: string | Tree): string
165 assert(type(name) == "string" and not name:match("/")) 165 assert(not name:match("/"))
166 assert(type(version) == "string")
167 return dir.path(path.rocks_dir(tree), name, version, "bin") 166 return dir.path(path.rocks_dir(tree), name, version, "bin")
168end 167end
169 168
@@ -172,8 +171,7 @@ end
172-- @param file_name string: pathname of a rock or rockspec 171-- @param file_name string: pathname of a rock or rockspec
173-- @return (string, string, string) or nil: name, version and arch 172-- @return (string, string, string) or nil: name, version and arch
174-- or nil if name could not be parsed 173-- or nil if name could not be parsed
175function path.parse_name(file_name) 174function path.parse_name(file_name: string): string, string, string
176 assert(type(file_name) == "string")
177 if file_name:match("%.rock$") then 175 if file_name:match("%.rock$") then
178 return dir.base_name(file_name):match("(.*)-([^-]+-%d+)%.([^.]+)%.rock$") 176 return dir.base_name(file_name):match("(.*)-([^-]+-%d+)%.([^.]+)%.rock$")
179 else 177 else
@@ -187,12 +185,8 @@ end
187-- @param version string: Package version. 185-- @param version string: Package version.
188-- @param arch string: Architecture identifier, or "rockspec" or "installed". 186-- @param arch string: Architecture identifier, or "rockspec" or "installed".
189-- @return string: A URL or pathname following LuaRocks naming conventions. 187-- @return string: A URL or pathname following LuaRocks naming conventions.
190function path.make_url(pathname, name, version, arch) 188function path.make_url(pathname: string, name: string, version: string, arch: string): string
191 assert(type(pathname) == "string") 189 assert(not name:match("/"))
192 assert(type(name) == "string" and not name:match("/"))
193 assert(type(version) == "string")
194 assert(type(arch) == "string")
195
196 local filename = name.."-"..version 190 local filename = name.."-"..version
197 if arch == "installed" then 191 if arch == "installed" then
198 filename = dir.path(name, version, filename..".rockspec") 192 filename = dir.path(name, version, filename..".rockspec")
@@ -208,12 +202,11 @@ end
208-- For example, on Unix, "foo.bar.baz" will return "foo/bar". 202-- For example, on Unix, "foo.bar.baz" will return "foo/bar".
209-- @param mod string: A module name in Lua dot-separated format. 203-- @param mod string: A module name in Lua dot-separated format.
210-- @return string: A directory name using the platform's separator. 204-- @return string: A directory name using the platform's separator.
211function path.module_to_path(mod) 205function path.module_to_path(mod: string): string
212 assert(type(mod) == "string")
213 return (mod:gsub("[^.]*$", ""):gsub("%.", "/")) 206 return (mod:gsub("[^.]*$", ""):gsub("%.", "/"))
214end 207end
215 208
216function path.use_tree(tree) 209function path.use_tree(tree: string | Tree)
217 cfg.root_dir = tree 210 cfg.root_dir = tree
218 cfg.rocks_dir = path.rocks_dir(tree) 211 cfg.rocks_dir = path.rocks_dir(tree)
219 cfg.deploy_bin_dir = path.deploy_bin_dir(tree) 212 cfg.deploy_bin_dir = path.deploy_bin_dir(tree)
@@ -221,7 +214,7 @@ function path.use_tree(tree)
221 cfg.deploy_lib_dir = path.deploy_lib_dir(tree) 214 cfg.deploy_lib_dir = path.deploy_lib_dir(tree)
222end 215end
223 216
224function path.add_to_package_paths(tree) 217function path.add_to_package_paths(tree: string | Tree)
225 package.path = dir.path(path.deploy_lua_dir(tree), "?.lua") .. ";" 218 package.path = dir.path(path.deploy_lua_dir(tree), "?.lua") .. ";"
226 .. dir.path(path.deploy_lua_dir(tree), "?/init.lua") .. ";" 219 .. dir.path(path.deploy_lua_dir(tree), "?/init.lua") .. ";"
227 .. package.path 220 .. package.path
@@ -234,12 +227,10 @@ end
234-- @param version string: The rock version. 227-- @param version string: The rock version.
235-- @param tree string: The local tree to use. 228-- @param tree string: The local tree to use.
236-- @return string?: The namespace if it exists, or nil. 229-- @return string?: The namespace if it exists, or nil.
237function path.read_namespace(name, version, tree) 230function path.read_namespace(name: string, version: string, tree: string | Tree): string
238 assert(type(name) == "string" and not name:match("/")) 231 assert(not name:match("/"))
239 assert(type(version) == "string")
240 assert(type(tree) == "string")
241 232
242 local namespace 233 local namespace: string
243 local fd = io.open(path.rock_namespace_file(name, version, tree), "r") 234 local fd = io.open(path.rock_namespace_file(name, version, tree), "r")
244 if fd then 235 if fd then
245 namespace = fd:read("*a") 236 namespace = fd:read("*a")
@@ -248,10 +239,10 @@ function path.read_namespace(name, version, tree)
248 return namespace 239 return namespace
249end 240end
250 241
251function path.package_paths(deps_mode) 242function path.package_paths(deps_mode: string): string, string
252 local lpaths = {} 243 local lpaths = {}
253 local lcpaths = {} 244 local lcpaths = {}
254 path.map_trees(deps_mode, function(tree) 245 path.map_trees(deps_mode, function(tree: string | Tree)
255 local root = path.root_dir(tree) 246 local root = path.root_dir(tree)
256 table.insert(lpaths, dir.path(root, cfg.lua_modules_path, "?.lua")) 247 table.insert(lpaths, dir.path(root, cfg.lua_modules_path, "?.lua"))
257 table.insert(lpaths, dir.path(root, cfg.lua_modules_path, "?/init.lua")) 248 table.insert(lpaths, dir.path(root, cfg.lua_modules_path, "?/init.lua"))