From 949c0e66a12d4f9d6ad9df560647b41329d9a5f7 Mon Sep 17 00:00:00 2001 From: V1K1NGbg Date: Mon, 15 Jul 2024 00:23:01 +0300 Subject: converted path and added to fs and cfg --- src/luarocks/core/cfg.d.tl | 5 + src/luarocks/core/path.tl | 1 - src/luarocks/fs.d.tl | 6 +- src/luarocks/path-original.lua | 263 +++++++++++++++++++++++++++++++++++++++++ src/luarocks/path.lua | 263 ----------------------------------------- src/luarocks/path.tl | 255 +++++++++++++++++++++++++++++++++++++++ src/luarocks/util.tl | 53 ++++++++- 7 files changed, 575 insertions(+), 271 deletions(-) create mode 100644 src/luarocks/path-original.lua delete mode 100644 src/luarocks/path.lua create mode 100644 src/luarocks/path.tl (limited to 'src') diff --git a/src/luarocks/core/cfg.d.tl b/src/luarocks/core/cfg.d.tl index a741baaa..68d110c3 100644 --- a/src/luarocks/core/cfg.d.tl +++ b/src/luarocks/core/cfg.d.tl @@ -4,12 +4,17 @@ local record cfg make_defaults: function(lua_version: string, target_cpu: string, platforms: {any: any}, home: string): {any: any} use_defaults: function(cfg, defaults: {any: any}) root_dir: string | Tree + rocks_dir: string rocks_subdir: string lua_modules_path: string lib_modules_path: string rocks_trees: {string| Tree} lua_version: string deps_mode: string + deploy_bin_dir: string + deploy_lua_dir: string + deploy_lib_dir: string + lib_extension: string record Tree root: string rocks_dir: string diff --git a/src/luarocks/core/path.tl b/src/luarocks/core/path.tl index e1bdc8de..213c0d69 100644 --- a/src/luarocks/core/path.tl +++ b/src/luarocks/core/path.tl @@ -1,4 +1,3 @@ - --- Core LuaRocks-specific path handling functions. local record path end diff --git a/src/luarocks/fs.d.tl b/src/luarocks/fs.d.tl index b0ca56c7..6a8d7520 100644 --- a/src/luarocks/fs.d.tl +++ b/src/luarocks/fs.d.tl @@ -6,8 +6,10 @@ local record fs change_dir_to_root: function record FsTable end - - --... + -- util + is_dir: function(dir: string): boolean + dir: function(dir: string): function(): string --? right iterator + is_file: function(file: string): boolean end return fs diff --git a/src/luarocks/path-original.lua b/src/luarocks/path-original.lua new file mode 100644 index 00000000..19657c83 --- /dev/null +++ b/src/luarocks/path-original.lua @@ -0,0 +1,263 @@ + +--- LuaRocks-specific path handling functions. +-- All paths are configured in this module, making it a single +-- point where the layout of the local installation is defined in LuaRocks. +local path = {} + +local core = require("luarocks.core.path") +local dir = require("luarocks.dir") +local cfg = require("luarocks.core.cfg") +local util = require("luarocks.util") + +path.rocks_dir = core.rocks_dir +path.versioned_name = core.versioned_name +path.path_to_module = core.path_to_module +path.deploy_lua_dir = core.deploy_lua_dir +path.deploy_lib_dir = core.deploy_lib_dir +path.map_trees = core.map_trees +path.rocks_tree_to_string = core.rocks_tree_to_string + +--- Infer rockspec filename from a rock filename. +-- @param rock_name string: Pathname of a rock file. +-- @return string: Filename of the rockspec, without path. +function path.rockspec_name_from_rock(rock_name) + assert(type(rock_name) == "string") + local base_name = dir.base_name(rock_name) + return base_name:match("(.*)%.[^.]*.rock") .. ".rockspec" +end + +function path.root_from_rocks_dir(rocks_dir) + assert(type(rocks_dir) == "string") + return rocks_dir:match("(.*)" .. util.matchquote(cfg.rocks_subdir) .. ".*$") +end + +function path.root_dir(tree) + if type(tree) == "string" then + return tree + else + assert(type(tree) == "table") + return tree.root + end +end + +function path.deploy_bin_dir(tree) + return dir.path(path.root_dir(tree), "bin") +end + +function path.manifest_file(tree) + return dir.path(path.rocks_dir(tree), "manifest") +end + +--- Get the directory for all versions of a package in a tree. +-- @param name string: The package name. +-- @return string: The resulting path -- does not guarantee that +-- @param tree string or nil: If given, specifies the local tree to use. +-- the package (and by extension, the path) exists. +function path.versions_dir(name, tree) + assert(type(name) == "string" and not name:match("/")) + return dir.path(path.rocks_dir(tree), name) +end + +--- Get the local installation directory (prefix) for a package. +-- @param name string: The package name. +-- @param version string: The package version. +-- @param tree string or nil: If given, specifies the local tree to use. +-- @return string: The resulting path -- does not guarantee that +-- the package (and by extension, the path) exists. +function path.install_dir(name, version, tree) + assert(type(name) == "string" and not name:match("/")) + assert(type(version) == "string") + return dir.path(path.rocks_dir(tree), name, version) +end + +--- Get the local filename of the rockspec of an installed rock. +-- @param name string: The package name. +-- @param version string: The package version. +-- @param tree string or nil: If given, specifies the local tree to use. +-- @return string: The resulting path -- does not guarantee that +-- the package (and by extension, the file) exists. +function path.rockspec_file(name, version, tree) + assert(type(name) == "string" and not name:match("/")) + assert(type(version) == "string") + return dir.path(path.rocks_dir(tree), name, version, name.."-"..version..".rockspec") +end + +--- Get the local filename of the rock_manifest file of an installed rock. +-- @param name string: The package name. +-- @param version string: The package version. +-- @param tree string or nil: If given, specifies the local tree to use. +-- @return string: The resulting path -- does not guarantee that +-- the package (and by extension, the file) exists. +function path.rock_manifest_file(name, version, tree) + assert(type(name) == "string" and not name:match("/")) + assert(type(version) == "string") + return dir.path(path.rocks_dir(tree), name, version, "rock_manifest") +end + +--- Get the local filename of the rock_namespace file of an installed rock. +-- @param name string: The package name (without a namespace). +-- @param version string: The package version. +-- @param tree string or nil: If given, specifies the local tree to use. +-- @return string: The resulting path -- does not guarantee that +-- the package (and by extension, the file) exists. +function path.rock_namespace_file(name, version, tree) + assert(type(name) == "string" and not name:match("/")) + assert(type(version) == "string") + return dir.path(path.rocks_dir(tree), name, version, "rock_namespace") +end + +--- Get the local installation directory for C libraries of a package. +-- @param name string: The package name. +-- @param version string: The package version. +-- @param tree string or nil: If given, specifies the local tree to use. +-- @return string: The resulting path -- does not guarantee that +-- the package (and by extension, the path) exists. +function path.lib_dir(name, version, tree) + assert(type(name) == "string" and not name:match("/")) + assert(type(version) == "string") + return dir.path(path.rocks_dir(tree), name, version, "lib") +end + +--- Get the local installation directory for Lua modules of a package. +-- @param name string: The package name. +-- @param version string: The package version. +-- @param tree string or nil: If given, specifies the local tree to use. +-- @return string: The resulting path -- does not guarantee that +-- the package (and by extension, the path) exists. +function path.lua_dir(name, version, tree) + assert(type(name) == "string" and not name:match("/")) + assert(type(version) == "string") + return dir.path(path.rocks_dir(tree), name, version, "lua") +end + +--- Get the local installation directory for documentation of a package. +-- @param name string: The package name. +-- @param version string: The package version. +-- @param tree string or nil: If given, specifies the local tree to use. +-- @return string: The resulting path -- does not guarantee that +-- the package (and by extension, the path) exists. +function path.doc_dir(name, version, tree) + assert(type(name) == "string" and not name:match("/")) + assert(type(version) == "string") + return dir.path(path.rocks_dir(tree), name, version, "doc") +end + +--- Get the local installation directory for configuration files of a package. +-- @param name string: The package name. +-- @param version string: The package version. +-- @param tree string or nil: If given, specifies the local tree to use. +-- @return string: The resulting path -- does not guarantee that +-- the package (and by extension, the path) exists. +function path.conf_dir(name, version, tree) + assert(type(name) == "string" and not name:match("/")) + assert(type(version) == "string") + return dir.path(path.rocks_dir(tree), name, version, "conf") +end + +--- Get the local installation directory for command-line scripts +-- of a package. +-- @param name string: The package name. +-- @param version string: The package version. +-- @param tree string or nil: If given, specifies the local tree to use. +-- @return string: The resulting path -- does not guarantee that +-- the package (and by extension, the path) exists. +function path.bin_dir(name, version, tree) + assert(type(name) == "string" and not name:match("/")) + assert(type(version) == "string") + return dir.path(path.rocks_dir(tree), name, version, "bin") +end + +--- Extract name, version and arch of a rock filename, +-- or name, version and "rockspec" from a rockspec name. +-- @param file_name string: pathname of a rock or rockspec +-- @return (string, string, string) or nil: name, version and arch +-- or nil if name could not be parsed +function path.parse_name(file_name) + assert(type(file_name) == "string") + if file_name:match("%.rock$") then + return dir.base_name(file_name):match("(.*)-([^-]+-%d+)%.([^.]+)%.rock$") + else + return dir.base_name(file_name):match("(.*)-([^-]+-%d+)%.(rockspec)") + end +end + +--- Make a rockspec or rock URL. +-- @param pathname string: Base URL or pathname. +-- @param name string: Package name. +-- @param version string: Package version. +-- @param arch string: Architecture identifier, or "rockspec" or "installed". +-- @return string: A URL or pathname following LuaRocks naming conventions. +function path.make_url(pathname, name, version, arch) + assert(type(pathname) == "string") + assert(type(name) == "string" and not name:match("/")) + assert(type(version) == "string") + assert(type(arch) == "string") + + local filename = name.."-"..version + if arch == "installed" then + filename = dir.path(name, version, filename..".rockspec") + elseif arch == "rockspec" then + filename = filename..".rockspec" + else + filename = filename.."."..arch..".rock" + end + return dir.path(pathname, filename) +end + +--- Obtain the directory name where a module should be stored. +-- For example, on Unix, "foo.bar.baz" will return "foo/bar". +-- @param mod string: A module name in Lua dot-separated format. +-- @return string: A directory name using the platform's separator. +function path.module_to_path(mod) + assert(type(mod) == "string") + return (mod:gsub("[^.]*$", ""):gsub("%.", "/")) +end + +function path.use_tree(tree) + cfg.root_dir = tree + cfg.rocks_dir = path.rocks_dir(tree) + cfg.deploy_bin_dir = path.deploy_bin_dir(tree) + cfg.deploy_lua_dir = path.deploy_lua_dir(tree) + cfg.deploy_lib_dir = path.deploy_lib_dir(tree) +end + +function path.add_to_package_paths(tree) + package.path = dir.path(path.deploy_lua_dir(tree), "?.lua") .. ";" + .. dir.path(path.deploy_lua_dir(tree), "?/init.lua") .. ";" + .. package.path + package.cpath = dir.path(path.deploy_lib_dir(tree), "?." .. cfg.lib_extension) .. ";" + .. package.cpath +end + +--- Get the namespace of a locally-installed rock, if any. +-- @param name string: The rock name, without a namespace. +-- @param version string: The rock version. +-- @param tree string: The local tree to use. +-- @return string?: The namespace if it exists, or nil. +function path.read_namespace(name, version, tree) + assert(type(name) == "string" and not name:match("/")) + assert(type(version) == "string") + assert(type(tree) == "string") + + local namespace + local fd = io.open(path.rock_namespace_file(name, version, tree), "r") + if fd then + namespace = fd:read("*a") + fd:close() + end + return namespace +end + +function path.package_paths(deps_mode) + local lpaths = {} + local lcpaths = {} + path.map_trees(deps_mode, function(tree) + local root = path.root_dir(tree) + table.insert(lpaths, dir.path(root, cfg.lua_modules_path, "?.lua")) + table.insert(lpaths, dir.path(root, cfg.lua_modules_path, "?/init.lua")) + table.insert(lcpaths, dir.path(root, cfg.lib_modules_path, "?." .. cfg.lib_extension)) + end) + return table.concat(lpaths, ";"), table.concat(lcpaths, ";") +end + +return path diff --git a/src/luarocks/path.lua b/src/luarocks/path.lua deleted file mode 100644 index 19657c83..00000000 --- a/src/luarocks/path.lua +++ /dev/null @@ -1,263 +0,0 @@ - ---- LuaRocks-specific path handling functions. --- All paths are configured in this module, making it a single --- point where the layout of the local installation is defined in LuaRocks. -local path = {} - -local core = require("luarocks.core.path") -local dir = require("luarocks.dir") -local cfg = require("luarocks.core.cfg") -local util = require("luarocks.util") - -path.rocks_dir = core.rocks_dir -path.versioned_name = core.versioned_name -path.path_to_module = core.path_to_module -path.deploy_lua_dir = core.deploy_lua_dir -path.deploy_lib_dir = core.deploy_lib_dir -path.map_trees = core.map_trees -path.rocks_tree_to_string = core.rocks_tree_to_string - ---- Infer rockspec filename from a rock filename. --- @param rock_name string: Pathname of a rock file. --- @return string: Filename of the rockspec, without path. -function path.rockspec_name_from_rock(rock_name) - assert(type(rock_name) == "string") - local base_name = dir.base_name(rock_name) - return base_name:match("(.*)%.[^.]*.rock") .. ".rockspec" -end - -function path.root_from_rocks_dir(rocks_dir) - assert(type(rocks_dir) == "string") - return rocks_dir:match("(.*)" .. util.matchquote(cfg.rocks_subdir) .. ".*$") -end - -function path.root_dir(tree) - if type(tree) == "string" then - return tree - else - assert(type(tree) == "table") - return tree.root - end -end - -function path.deploy_bin_dir(tree) - return dir.path(path.root_dir(tree), "bin") -end - -function path.manifest_file(tree) - return dir.path(path.rocks_dir(tree), "manifest") -end - ---- Get the directory for all versions of a package in a tree. --- @param name string: The package name. --- @return string: The resulting path -- does not guarantee that --- @param tree string or nil: If given, specifies the local tree to use. --- the package (and by extension, the path) exists. -function path.versions_dir(name, tree) - assert(type(name) == "string" and not name:match("/")) - return dir.path(path.rocks_dir(tree), name) -end - ---- Get the local installation directory (prefix) for a package. --- @param name string: The package name. --- @param version string: The package version. --- @param tree string or nil: If given, specifies the local tree to use. --- @return string: The resulting path -- does not guarantee that --- the package (and by extension, the path) exists. -function path.install_dir(name, version, tree) - assert(type(name) == "string" and not name:match("/")) - assert(type(version) == "string") - return dir.path(path.rocks_dir(tree), name, version) -end - ---- Get the local filename of the rockspec of an installed rock. --- @param name string: The package name. --- @param version string: The package version. --- @param tree string or nil: If given, specifies the local tree to use. --- @return string: The resulting path -- does not guarantee that --- the package (and by extension, the file) exists. -function path.rockspec_file(name, version, tree) - assert(type(name) == "string" and not name:match("/")) - assert(type(version) == "string") - return dir.path(path.rocks_dir(tree), name, version, name.."-"..version..".rockspec") -end - ---- Get the local filename of the rock_manifest file of an installed rock. --- @param name string: The package name. --- @param version string: The package version. --- @param tree string or nil: If given, specifies the local tree to use. --- @return string: The resulting path -- does not guarantee that --- the package (and by extension, the file) exists. -function path.rock_manifest_file(name, version, tree) - assert(type(name) == "string" and not name:match("/")) - assert(type(version) == "string") - return dir.path(path.rocks_dir(tree), name, version, "rock_manifest") -end - ---- Get the local filename of the rock_namespace file of an installed rock. --- @param name string: The package name (without a namespace). --- @param version string: The package version. --- @param tree string or nil: If given, specifies the local tree to use. --- @return string: The resulting path -- does not guarantee that --- the package (and by extension, the file) exists. -function path.rock_namespace_file(name, version, tree) - assert(type(name) == "string" and not name:match("/")) - assert(type(version) == "string") - return dir.path(path.rocks_dir(tree), name, version, "rock_namespace") -end - ---- Get the local installation directory for C libraries of a package. --- @param name string: The package name. --- @param version string: The package version. --- @param tree string or nil: If given, specifies the local tree to use. --- @return string: The resulting path -- does not guarantee that --- the package (and by extension, the path) exists. -function path.lib_dir(name, version, tree) - assert(type(name) == "string" and not name:match("/")) - assert(type(version) == "string") - return dir.path(path.rocks_dir(tree), name, version, "lib") -end - ---- Get the local installation directory for Lua modules of a package. --- @param name string: The package name. --- @param version string: The package version. --- @param tree string or nil: If given, specifies the local tree to use. --- @return string: The resulting path -- does not guarantee that --- the package (and by extension, the path) exists. -function path.lua_dir(name, version, tree) - assert(type(name) == "string" and not name:match("/")) - assert(type(version) == "string") - return dir.path(path.rocks_dir(tree), name, version, "lua") -end - ---- Get the local installation directory for documentation of a package. --- @param name string: The package name. --- @param version string: The package version. --- @param tree string or nil: If given, specifies the local tree to use. --- @return string: The resulting path -- does not guarantee that --- the package (and by extension, the path) exists. -function path.doc_dir(name, version, tree) - assert(type(name) == "string" and not name:match("/")) - assert(type(version) == "string") - return dir.path(path.rocks_dir(tree), name, version, "doc") -end - ---- Get the local installation directory for configuration files of a package. --- @param name string: The package name. --- @param version string: The package version. --- @param tree string or nil: If given, specifies the local tree to use. --- @return string: The resulting path -- does not guarantee that --- the package (and by extension, the path) exists. -function path.conf_dir(name, version, tree) - assert(type(name) == "string" and not name:match("/")) - assert(type(version) == "string") - return dir.path(path.rocks_dir(tree), name, version, "conf") -end - ---- Get the local installation directory for command-line scripts --- of a package. --- @param name string: The package name. --- @param version string: The package version. --- @param tree string or nil: If given, specifies the local tree to use. --- @return string: The resulting path -- does not guarantee that --- the package (and by extension, the path) exists. -function path.bin_dir(name, version, tree) - assert(type(name) == "string" and not name:match("/")) - assert(type(version) == "string") - return dir.path(path.rocks_dir(tree), name, version, "bin") -end - ---- Extract name, version and arch of a rock filename, --- or name, version and "rockspec" from a rockspec name. --- @param file_name string: pathname of a rock or rockspec --- @return (string, string, string) or nil: name, version and arch --- or nil if name could not be parsed -function path.parse_name(file_name) - assert(type(file_name) == "string") - if file_name:match("%.rock$") then - return dir.base_name(file_name):match("(.*)-([^-]+-%d+)%.([^.]+)%.rock$") - else - return dir.base_name(file_name):match("(.*)-([^-]+-%d+)%.(rockspec)") - end -end - ---- Make a rockspec or rock URL. --- @param pathname string: Base URL or pathname. --- @param name string: Package name. --- @param version string: Package version. --- @param arch string: Architecture identifier, or "rockspec" or "installed". --- @return string: A URL or pathname following LuaRocks naming conventions. -function path.make_url(pathname, name, version, arch) - assert(type(pathname) == "string") - assert(type(name) == "string" and not name:match("/")) - assert(type(version) == "string") - assert(type(arch) == "string") - - local filename = name.."-"..version - if arch == "installed" then - filename = dir.path(name, version, filename..".rockspec") - elseif arch == "rockspec" then - filename = filename..".rockspec" - else - filename = filename.."."..arch..".rock" - end - return dir.path(pathname, filename) -end - ---- Obtain the directory name where a module should be stored. --- For example, on Unix, "foo.bar.baz" will return "foo/bar". --- @param mod string: A module name in Lua dot-separated format. --- @return string: A directory name using the platform's separator. -function path.module_to_path(mod) - assert(type(mod) == "string") - return (mod:gsub("[^.]*$", ""):gsub("%.", "/")) -end - -function path.use_tree(tree) - cfg.root_dir = tree - cfg.rocks_dir = path.rocks_dir(tree) - cfg.deploy_bin_dir = path.deploy_bin_dir(tree) - cfg.deploy_lua_dir = path.deploy_lua_dir(tree) - cfg.deploy_lib_dir = path.deploy_lib_dir(tree) -end - -function path.add_to_package_paths(tree) - package.path = dir.path(path.deploy_lua_dir(tree), "?.lua") .. ";" - .. dir.path(path.deploy_lua_dir(tree), "?/init.lua") .. ";" - .. package.path - package.cpath = dir.path(path.deploy_lib_dir(tree), "?." .. cfg.lib_extension) .. ";" - .. package.cpath -end - ---- Get the namespace of a locally-installed rock, if any. --- @param name string: The rock name, without a namespace. --- @param version string: The rock version. --- @param tree string: The local tree to use. --- @return string?: The namespace if it exists, or nil. -function path.read_namespace(name, version, tree) - assert(type(name) == "string" and not name:match("/")) - assert(type(version) == "string") - assert(type(tree) == "string") - - local namespace - local fd = io.open(path.rock_namespace_file(name, version, tree), "r") - if fd then - namespace = fd:read("*a") - fd:close() - end - return namespace -end - -function path.package_paths(deps_mode) - local lpaths = {} - local lcpaths = {} - path.map_trees(deps_mode, function(tree) - local root = path.root_dir(tree) - table.insert(lpaths, dir.path(root, cfg.lua_modules_path, "?.lua")) - table.insert(lpaths, dir.path(root, cfg.lua_modules_path, "?/init.lua")) - table.insert(lcpaths, dir.path(root, cfg.lib_modules_path, "?." .. cfg.lib_extension)) - end) - return table.concat(lpaths, ";"), table.concat(lcpaths, ";") -end - -return path diff --git a/src/luarocks/path.tl b/src/luarocks/path.tl new file mode 100644 index 00000000..93fb6aae --- /dev/null +++ b/src/luarocks/path.tl @@ -0,0 +1,255 @@ + +--- LuaRocks-specific path handling functions. +-- All paths are configured in this module, making it a single +-- point where the layout of the local installation is defined in LuaRocks. + +local cfg = require("luarocks.core.cfg") +local core = require("luarocks.core.path") +local dir = require("luarocks.dir") +-- local util = require("luarocks.util") --! crashes teal + +local type Tree = cfg.Tree + +local record path + rocks_dir: function(string | Tree): string + versioned_name: function(string, string, string, string): string + path_to_module: function(string): string + deploy_lua_dir: function(string | Tree): string + deploy_lib_dir: function(string | Tree): string + map_trees: function(string, function(...: any): any..., ...: string): {any} + rocks_tree_to_string: function(string | Tree): string +end + +path.rocks_dir = core.rocks_dir +path.versioned_name = core.versioned_name +path.path_to_module = core.path_to_module +path.deploy_lua_dir = core.deploy_lua_dir +path.deploy_lib_dir = core.deploy_lib_dir +path.map_trees = core.map_trees +path.rocks_tree_to_string = core.rocks_tree_to_string + +--- Infer rockspec filename from a rock filename. +-- @param rock_name string: Pathname of a rock file. +-- @return string: Filename of the rockspec, without path. +function path.rockspec_name_from_rock(rock_name: string): string + local base_name = dir.base_name(rock_name) + return base_name:match("(.*)%.[^.]*.rock") .. ".rockspec" +end + +function path.root_from_rocks_dir(rocks_dir: string): string + return "" --! temp + -- return rocks_dir:match("(.*)" .. util.matchquote(cfg.rocks_subdir) .. ".*$") --! only depends here on util +end + +function path.root_dir(tree: string | Tree): string + if tree is string then + return tree + else + return tree.root + end +end + +function path.deploy_bin_dir(tree: string | Tree): string + return dir.path(path.root_dir(tree), "bin") +end + +function path.manifest_file(tree: string | Tree): string + return dir.path(path.rocks_dir(tree), "manifest") +end + +--- Get the directory for all versions of a package in a tree. +-- @param name string: The package name. +-- @return string: The resulting path -- does not guarantee that +-- @param tree string or nil: If given, specifies the local tree to use. +-- the package (and by extension, the path) exists. +function path.versions_dir(name: string, tree: string | Tree): string + assert(not name:match("/")) + return dir.path(path.rocks_dir(tree), name) +end + +--- Get the local installation directory (prefix) for a package. +-- @param name string: The package name. +-- @param version string: The package version. +-- @param tree string or nil: If given, specifies the local tree to use. +-- @return string: The resulting path -- does not guarantee that +-- the package (and by extension, the path) exists. +function path.install_dir(name: string, version: string, tree: string | Tree): string + assert(not name:match("/")) + return dir.path(path.rocks_dir(tree), name, version) +end + +--- Get the local filename of the rockspec of an installed rock. +-- @param name string: The package name. +-- @param version string: The package version. +-- @param tree string or nil: If given, specifies the local tree to use. +-- @return string: The resulting path -- does not guarantee that +-- the package (and by extension, the file) exists. +function path.rockspec_file(name: string, version: string, tree: string | Tree): string + assert(not name:match("/")) + return dir.path(path.rocks_dir(tree), name, version, name.."-"..version..".rockspec") +end + +--- Get the local filename of the rock_manifest file of an installed rock. +-- @param name string: The package name. +-- @param version string: The package version. +-- @param tree string or nil: If given, specifies the local tree to use. +-- @return string: The resulting path -- does not guarantee that +-- the package (and by extension, the file) exists. +function path.rock_manifest_file(name: string, version: string, tree: string | Tree): string + assert(not name:match("/")) + return dir.path(path.rocks_dir(tree), name, version, "rock_manifest") +end + +--- Get the local filename of the rock_namespace file of an installed rock. +-- @param name string: The package name (without a namespace). +-- @param version string: The package version. +-- @param tree string or nil: If given, specifies the local tree to use. +-- @return string: The resulting path -- does not guarantee that +-- the package (and by extension, the file) exists. +function path.rock_namespace_file(name: string, version: string, tree: string | Tree): string + assert(not name:match("/")) + return dir.path(path.rocks_dir(tree), name, version, "rock_namespace") +end + +--- Get the local installation directory for C libraries of a package. +-- @param name string: The package name. +-- @param version string: The package version. +-- @param tree string or nil: If given, specifies the local tree to use. +-- @return string: The resulting path -- does not guarantee that +-- the package (and by extension, the path) exists. +function path.lib_dir(name: string, version: string, tree: string | Tree): string + assert(not name:match("/")) + return dir.path(path.rocks_dir(tree), name, version, "lib") +end + +--- Get the local installation directory for Lua modules of a package. +-- @param name string: The package name. +-- @param version string: The package version. +-- @param tree string or nil: If given, specifies the local tree to use. +-- @return string: The resulting path -- does not guarantee that +-- the package (and by extension, the path) exists. +function path.lua_dir(name: string, version: string, tree: string | Tree): string + assert(not name:match("/")) + return dir.path(path.rocks_dir(tree), name, version, "lua") +end + +--- Get the local installation directory for documentation of a package. +-- @param name string: The package name. +-- @param version string: The package version. +-- @param tree string or nil: If given, specifies the local tree to use. +-- @return string: The resulting path -- does not guarantee that +-- the package (and by extension, the path) exists. +function path.doc_dir(name: string, version: string, tree: string | Tree): string + assert(not name:match("/")) + return dir.path(path.rocks_dir(tree), name, version, "doc") +end + +--- Get the local installation directory for configuration files of a package. +-- @param name string: The package name. +-- @param version string: The package version. +-- @param tree string or nil: If given, specifies the local tree to use. +-- @return string: The resulting path -- does not guarantee that +-- the package (and by extension, the path) exists. +function path.conf_dir(name: string, version: string, tree: string | Tree): string + assert(not name:match("/")) + return dir.path(path.rocks_dir(tree), name, version, "conf") +end + +--- Get the local installation directory for command-line scripts +-- of a package. +-- @param name string: The package name. +-- @param version string: The package version. +-- @param tree string or nil: If given, specifies the local tree to use. +-- @return string: The resulting path -- does not guarantee that +-- the package (and by extension, the path) exists. +function path.bin_dir(name: string, version: string, tree: string | Tree): string + assert(not name:match("/")) + return dir.path(path.rocks_dir(tree), name, version, "bin") +end + +--- Extract name, version and arch of a rock filename, +-- or name, version and "rockspec" from a rockspec name. +-- @param file_name string: pathname of a rock or rockspec +-- @return (string, string, string) or nil: name, version and arch +-- or nil if name could not be parsed +function path.parse_name(file_name: string): string + if file_name:match("%.rock$") then + return dir.base_name(file_name):match("(.*)-([^-]+-%d+)%.([^.]+)%.rock$") + else + return dir.base_name(file_name):match("(.*)-([^-]+-%d+)%.(rockspec)") + end +end + +--- Make a rockspec or rock URL. +-- @param pathname string: Base URL or pathname. +-- @param name string: Package name. +-- @param version string: Package version. +-- @param arch string: Architecture identifier, or "rockspec" or "installed". +-- @return string: A URL or pathname following LuaRocks naming conventions. +function path.make_url(pathname: string, name: string, version: string, arch: string): string + assert(not name:match("/")) + local filename = name.."-"..version + if arch == "installed" then + filename = dir.path(name, version, filename..".rockspec") + elseif arch == "rockspec" then + filename = filename..".rockspec" + else + filename = filename.."."..arch..".rock" + end + return dir.path(pathname, filename) +end + +--- Obtain the directory name where a module should be stored. +-- For example, on Unix, "foo.bar.baz" will return "foo/bar". +-- @param mod string: A module name in Lua dot-separated format. +-- @return string: A directory name using the platform's separator. +function path.module_to_path(mod: string): string + return (mod:gsub("[^.]*$", ""):gsub("%.", "/")) +end + +function path.use_tree(tree: Tree) + cfg.root_dir = tree + cfg.rocks_dir = path.rocks_dir(tree) + cfg.deploy_bin_dir = path.deploy_bin_dir(tree) + cfg.deploy_lua_dir = path.deploy_lua_dir(tree) + cfg.deploy_lib_dir = path.deploy_lib_dir(tree) +end + +function path.add_to_package_paths(tree: string | Tree) + package.path = dir.path(path.deploy_lua_dir(tree), "?.lua") .. ";" + .. dir.path(path.deploy_lua_dir(tree), "?/init.lua") .. ";" + .. package.path + package.cpath = dir.path(path.deploy_lib_dir(tree), "?." .. cfg.lib_extension) .. ";" + .. package.cpath +end + +--- Get the namespace of a locally-installed rock, if any. +-- @param name string: The rock name, without a namespace. +-- @param version string: The rock version. +-- @param tree string: The local tree to use. +-- @return string?: The namespace if it exists, or nil. +function path.read_namespace(name: string, version: string, tree: string | Tree): string + assert(not name:match("/")) + + local namespace: string + local fd = io.open(path.rock_namespace_file(name, version, tree), "r") + if fd then + namespace = fd:read("*a") + fd:close() + end + return namespace +end + +function path.package_paths(deps_mode: string): string, string + local lpaths = {} + local lcpaths = {} + path.map_trees(deps_mode, function(tree: string | Tree) + local root = path.root_dir(tree) + table.insert(lpaths, dir.path(root, cfg.lua_modules_path, "?.lua")) + table.insert(lpaths, dir.path(root, cfg.lua_modules_path, "?/init.lua")) + table.insert(lcpaths, dir.path(root, cfg.lib_modules_path, "?." .. cfg.lib_extension)) + end) + return table.concat(lpaths, ";"), table.concat(lcpaths, ";") +end + +return path diff --git a/src/luarocks/util.tl b/src/luarocks/util.tl index a8233f31..a9fc8394 100644 --- a/src/luarocks/util.tl +++ b/src/luarocks/util.tl @@ -25,14 +25,57 @@ local record util args: {any: any} end - record Rockspec - name: string - version: string --? + record Source + url: string + end + + record Description + summary: string + detailed: string + homepage: string + issues_url: string + maintainer: string + license: string + end + + record Test + type: string + platforms: {{string: any}} + end + + record Rockspec -- luarocks-dev-1.rockspec + rockspec_format: string + name: string --? package + version: string + source: {Source} + description: Description + test_dependencies: {string} + test: Test + end + + record Parser --? + option: function(Parser, ...: string): Parser + argname: function(Parser, string): Parser + choices: function(Parser, {string}): Parser + flag: function(Parser, string): Parser + hidden: function(Parser, boolean): Parser end end +util.cleanup_path = core.cleanup_path +util.split_string = core.split_string +util.sortedpairs = core.sortedpairs +util.deep_merge = core.deep_merge +util.deep_merge_under = core.deep_merge_under +util.popen_read = core.popen_read +util.show_table = core.show_table +util.printerr = core.printerr +util.warning = core.warning +util.keys = core.keys + local type Fn = util.Fn local type Rockspec = util.Rockspec +local type Parser = util.Parser local scheduled_functions: {Fn} = {} --? infered from line 48-51 @@ -229,7 +272,7 @@ function util.format_rock_name(name: string, namespace: string, version: string) return (namespace and namespace.."/" or "")..name..(version and " "..version or "") end -function util.deps_mode_option(parser: {string: any}, program: string) --? type of parser +function util.deps_mode_option(parser: Parser, program: string) local cfg = require("luarocks.core.cfg") parser:option("--deps-mode", "How to handle dependencies. Four modes are supported:\n".. @@ -286,7 +329,7 @@ local function collect_rockspecs(versions: {string: vers.Version}, paths: {strin if fs.is_dir(subdir) then for file in fs.dir(subdir) do - file = dir.path(subdir, file) -- path: function(...: string): string + file = dir.path(subdir, file) if file:match("rockspec$") and fs.is_file(file) then local rock, version = path.parse_name(file) -- cgit v1.2.3-55-g6feb