From c79ce50f956d8d9f1454ec871dc7b0469f194c07 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Sat, 7 May 2016 14:06:35 +0300 Subject: fs.tools: move common directory stack functions --- src/luarocks/fs/tools.lua | 47 +++++++++++++++++++++++++++++++++++++++++ src/luarocks/fs/unix/tools.lua | 46 ---------------------------------------- src/luarocks/fs/win32/tools.lua | 47 ----------------------------------------- 3 files changed, 47 insertions(+), 93 deletions(-) diff --git a/src/luarocks/fs/tools.lua b/src/luarocks/fs/tools.lua index 0283d8f7..08fa74a4 100644 --- a/src/luarocks/fs/tools.lua +++ b/src/luarocks/fs/tools.lua @@ -8,6 +8,53 @@ local cfg = require("luarocks.cfg") local vars = cfg.variables +local dir_stack = {} + +--- Obtain current directory. +-- Uses the module's internal directory stack. +-- @return string: the absolute pathname of the current directory. +function tools.current_dir() + local current = cfg.cache_pwd + if not current then + local pipe = io.popen(fs.quiet_stderr(fs.Q(vars.PWD))) + current = pipe:read("*l") + pipe:close() + cfg.cache_pwd = current + end + for _, directory in ipairs(dir_stack) do + current = fs.absolute_name(directory, current) + end + return current +end + +--- Change the current directory. +-- Uses the module's internal directory stack. This does not have exact +-- semantics of chdir, as it does not handle errors the same way, +-- but works well for our purposes for now. +-- @param directory string: The directory to switch to. +-- @return boolean or (nil, string): true if successful, (nil, error message) if failed. +function tools.change_dir(directory) + assert(type(directory) == "string") + if fs.is_dir(directory) then + table.insert(dir_stack, directory) + return true + end + return nil, "directory not found: "..directory +end + +--- Change directory to root. +-- Allows leaving a directory (e.g. for deleting it) in +-- a crossplatform way. +function tools.change_dir_to_root() + table.insert(dir_stack, "/") +end + +--- Change working directory to the previous in the directory stack. +function tools.pop_dir() + local directory = table.remove(dir_stack) + return directory ~= nil +end + --- Download a remote file. -- @param url string: URL to be fetched. -- @param filename string or nil: this function attempts to detect the diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index 8eaa9361..2b5280d6 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -7,31 +7,12 @@ local fs = require("luarocks.fs") local dir = require("luarocks.dir") local cfg = require("luarocks.cfg") -local dir_stack = {} - local vars = cfg.variables local function command_at(directory, cmd) return "cd " .. fs.Q(fs.absolute_name(directory)) .. " && " .. cmd end ---- Obtain current directory. --- Uses the module's internal directory stack. --- @return string: the absolute pathname of the current directory. -function tools.current_dir() - local current = cfg.cache_pwd - if not current then - local pipe = io.popen(fs.quiet_stderr(fs.Q(vars.PWD))) - current = pipe:read("*l") - pipe:close() - cfg.cache_pwd = current - end - for _, directory in ipairs(dir_stack) do - current = fs.absolute_name(directory, current) - end - return current -end - --- Run the given command. -- The command is executed in the current directory in the directory stack. -- @param cmd string: No quoting/escaping is applied to the command. @@ -48,33 +29,6 @@ function tools.execute_string(cmd) end end ---- Change the current directory. --- Uses the module's internal directory stack. This does not have exact --- semantics of chdir, as it does not handle errors the same way, --- but works well for our purposes for now. --- @param directory string: The directory to switch to. -function tools.change_dir(directory) - assert(type(directory) == "string") - if fs.is_dir(directory) then - table.insert(dir_stack, directory) - return true - end - return nil, "directory not found: "..directory -end - ---- Change directory to root. --- Allows leaving a directory (e.g. for deleting it) in --- a crossplatform way. -function tools.change_dir_to_root() - table.insert(dir_stack, "/") -end - ---- Change working directory to the previous in the directory stack. -function tools.pop_dir() - local directory = table.remove(dir_stack) - return directory ~= nil -end - --- Create a directory if it does not already exist. -- If any of the higher levels in the path name does not exist -- too, they are created as well. diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index cc0da0e9..19038e1f 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua @@ -9,8 +9,6 @@ local fs = require("luarocks.fs") local dir = require("luarocks.dir") local cfg = require("luarocks.cfg") -local dir_stack = {} - local vars = cfg.variables --- Strip the last extension of a filename. @@ -33,23 +31,6 @@ local function command_at(directory, cmd) return cmd end ---- Obtain current directory. --- Uses the module's internal directory stack. --- @return string: the absolute pathname of the current directory. -function tools.current_dir() - local current = cfg.cache_pwd - if not current then - local pipe = io.popen(fs.quiet_stderr(fs.Q(vars.PWD))) - current = pipe:read("*l") - pipe:close() - cfg.cache_pwd = current - end - for _, directory in ipairs(dir_stack) do - current = fs.absolute_name(directory, current) - end - return current -end - --- Run the given command. -- The command is executed in the current directory in the directory stack. -- @param cmd string: No quoting/escaping is applied to the command. @@ -67,34 +48,6 @@ function tools.execute_string(cmd) end end ---- Change the current directory. --- Uses the module's internal directory stack. This does not have exact --- semantics of chdir, as it does not handle errors the same way, --- but works well for our purposes for now. --- @param directory string: The directory to switch to. --- @return boolean or (nil, string): true if successful, (nil, error message) if failed. -function tools.change_dir(directory) - assert(type(directory) == "string") - if fs.is_dir(directory) then - table.insert(dir_stack, directory) - return true - end - return nil, "directory not found: "..directory -end - ---- Change directory to root. --- Allows leaving a directory (e.g. for deleting it) in --- a crossplatform way. -function tools.change_dir_to_root() - table.insert(dir_stack, "/") -end - ---- Change working directory to the previous in the directory stack. -function tools.pop_dir() - local directory = table.remove(dir_stack) - return directory ~= nil -end - --- Create a directory if it does not already exist. -- If any of the higher levels in the path name does not exist -- too, they are created as well. -- cgit v1.2.3-55-g6feb