diff options
| author | V1K1NGbg <victor@ilchev.com> | 2024-08-22 17:48:57 -0300 |
|---|---|---|
| committer | Hisham Muhammad <hisham@gobolinux.org> | 2024-10-21 13:30:51 -0300 |
| commit | 084bc6dd350e585ed48bd2bc454aaea1e8fc8dd7 (patch) | |
| tree | bbdee2c9a5d3ee0d3b63f727236bd9d5f8bedfff /src | |
| parent | 66459e17a7f356827d1ee2592e847d0e939acf0f (diff) | |
| download | luarocks-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. |
| 5 | local path = {} | ||
| 6 | 5 | ||
| 6 | local cfg = require("luarocks.core.cfg") | ||
| 7 | local core = require("luarocks.core.path") | 7 | local core = require("luarocks.core.path") |
| 8 | local dir = require("luarocks.dir") | 8 | local dir = require("luarocks.dir") |
| 9 | local cfg = require("luarocks.core.cfg") | 9 | local util = require("luarocks.core.util") |
| 10 | local util = require("luarocks.util") | 10 | |
| 11 | local type Tree = require("luarocks.core.types.tree").Tree | ||
| 12 | |||
| 13 | local 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 | ||
| 21 | end | ||
| 11 | 22 | ||
| 12 | path.rocks_dir = core.rocks_dir | 23 | path.rocks_dir = core.rocks_dir |
| 13 | path.versioned_name = core.versioned_name | 24 | path.versioned_name = core.versioned_name |
| @@ -17,34 +28,31 @@ path.deploy_lib_dir = core.deploy_lib_dir | |||
| 17 | path.map_trees = core.map_trees | 28 | path.map_trees = core.map_trees |
| 18 | path.rocks_tree_to_string = core.rocks_tree_to_string | 29 | path.rocks_tree_to_string = core.rocks_tree_to_string |
| 19 | 30 | ||
| 31 | function path.root_dir(tree: string | Tree): string | ||
| 32 | if tree is string then | ||
| 33 | return tree | ||
| 34 | else | ||
| 35 | return tree.root | ||
| 36 | end | ||
| 37 | end | ||
| 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. |
| 23 | function path.rockspec_name_from_rock(rock_name) | 42 | function 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" |
| 27 | end | 45 | end |
| 28 | 46 | ||
| 29 | function path.root_from_rocks_dir(rocks_dir) | 47 | function 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) .. ".*$") |
| 32 | end | 49 | end |
| 33 | 50 | ||
| 34 | function path.root_dir(tree) | 51 | function 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 | ||
| 41 | end | ||
| 42 | |||
| 43 | function path.deploy_bin_dir(tree) | ||
| 44 | return dir.path(path.root_dir(tree), "bin") | 52 | return dir.path(path.root_dir(tree), "bin") |
| 45 | end | 53 | end |
| 46 | 54 | ||
| 47 | function path.manifest_file(tree) | 55 | function 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") |
| 49 | end | 57 | end |
| 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. |
| 56 | function path.versions_dir(name, tree) | 64 | function 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) |
| 59 | end | 67 | end |
| 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. |
| 67 | function path.install_dir(name, version, tree) | 75 | function 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) |
| 71 | end | 78 | end |
| 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. |
| 79 | function path.rockspec_file(name, version, tree) | 86 | function 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") |
| 83 | end | 89 | end |
| 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. |
| 91 | function path.rock_manifest_file(name, version, tree) | 97 | function 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") |
| 95 | end | 100 | end |
| 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. |
| 103 | function path.rock_namespace_file(name, version, tree) | 108 | function 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") |
| 107 | end | 111 | end |
| 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. |
| 115 | function path.lib_dir(name, version, tree) | 119 | function 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") |
| 119 | end | 122 | end |
| 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. |
| 127 | function path.lua_dir(name, version, tree) | 130 | function 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") |
| 131 | end | 133 | end |
| 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. |
| 139 | function path.doc_dir(name, version, tree) | 141 | function 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") |
| 143 | end | 144 | end |
| 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. |
| 151 | function path.conf_dir(name, version, tree) | 152 | function 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") |
| 155 | end | 155 | end |
| 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. |
| 164 | function path.bin_dir(name, version, tree) | 164 | function 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") |
| 168 | end | 167 | end |
| 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 |
| 175 | function path.parse_name(file_name) | 174 | function 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. |
| 190 | function path.make_url(pathname, name, version, arch) | 188 | function 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. |
| 211 | function path.module_to_path(mod) | 205 | function path.module_to_path(mod: string): string |
| 212 | assert(type(mod) == "string") | ||
| 213 | return (mod:gsub("[^.]*$", ""):gsub("%.", "/")) | 206 | return (mod:gsub("[^.]*$", ""):gsub("%.", "/")) |
| 214 | end | 207 | end |
| 215 | 208 | ||
| 216 | function path.use_tree(tree) | 209 | function 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) |
| 222 | end | 215 | end |
| 223 | 216 | ||
| 224 | function path.add_to_package_paths(tree) | 217 | function 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. |
| 237 | function path.read_namespace(name, version, tree) | 230 | function 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 |
| 249 | end | 240 | end |
| 250 | 241 | ||
| 251 | function path.package_paths(deps_mode) | 242 | function 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")) |
