diff options
author | Peter Melnichenko <mpeterval@gmail.com> | 2016-05-07 14:06:35 +0300 |
---|---|---|
committer | Peter Melnichenko <mpeterval@gmail.com> | 2016-05-22 19:50:40 +0300 |
commit | c79ce50f956d8d9f1454ec871dc7b0469f194c07 (patch) | |
tree | 4b3764b621b912a5fcac2e7aa087ccb2e8f5041d | |
parent | 472e74d582055992cf1f5c75821e083131a895ac (diff) | |
download | luarocks-c79ce50f956d8d9f1454ec871dc7b0469f194c07.tar.gz luarocks-c79ce50f956d8d9f1454ec871dc7b0469f194c07.tar.bz2 luarocks-c79ce50f956d8d9f1454ec871dc7b0469f194c07.zip |
fs.tools: move common directory stack functions
-rw-r--r-- | src/luarocks/fs/tools.lua | 47 | ||||
-rw-r--r-- | src/luarocks/fs/unix/tools.lua | 46 | ||||
-rw-r--r-- | 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") | |||
8 | 8 | ||
9 | local vars = cfg.variables | 9 | local vars = cfg.variables |
10 | 10 | ||
11 | local dir_stack = {} | ||
12 | |||
13 | --- Obtain current directory. | ||
14 | -- Uses the module's internal directory stack. | ||
15 | -- @return string: the absolute pathname of the current directory. | ||
16 | function tools.current_dir() | ||
17 | local current = cfg.cache_pwd | ||
18 | if not current then | ||
19 | local pipe = io.popen(fs.quiet_stderr(fs.Q(vars.PWD))) | ||
20 | current = pipe:read("*l") | ||
21 | pipe:close() | ||
22 | cfg.cache_pwd = current | ||
23 | end | ||
24 | for _, directory in ipairs(dir_stack) do | ||
25 | current = fs.absolute_name(directory, current) | ||
26 | end | ||
27 | return current | ||
28 | end | ||
29 | |||
30 | --- Change the current directory. | ||
31 | -- Uses the module's internal directory stack. This does not have exact | ||
32 | -- semantics of chdir, as it does not handle errors the same way, | ||
33 | -- but works well for our purposes for now. | ||
34 | -- @param directory string: The directory to switch to. | ||
35 | -- @return boolean or (nil, string): true if successful, (nil, error message) if failed. | ||
36 | function tools.change_dir(directory) | ||
37 | assert(type(directory) == "string") | ||
38 | if fs.is_dir(directory) then | ||
39 | table.insert(dir_stack, directory) | ||
40 | return true | ||
41 | end | ||
42 | return nil, "directory not found: "..directory | ||
43 | end | ||
44 | |||
45 | --- Change directory to root. | ||
46 | -- Allows leaving a directory (e.g. for deleting it) in | ||
47 | -- a crossplatform way. | ||
48 | function tools.change_dir_to_root() | ||
49 | table.insert(dir_stack, "/") | ||
50 | end | ||
51 | |||
52 | --- Change working directory to the previous in the directory stack. | ||
53 | function tools.pop_dir() | ||
54 | local directory = table.remove(dir_stack) | ||
55 | return directory ~= nil | ||
56 | end | ||
57 | |||
11 | --- Download a remote file. | 58 | --- Download a remote file. |
12 | -- @param url string: URL to be fetched. | 59 | -- @param url string: URL to be fetched. |
13 | -- @param filename string or nil: this function attempts to detect the | 60 | -- @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") | |||
7 | local dir = require("luarocks.dir") | 7 | local dir = require("luarocks.dir") |
8 | local cfg = require("luarocks.cfg") | 8 | local cfg = require("luarocks.cfg") |
9 | 9 | ||
10 | local dir_stack = {} | ||
11 | |||
12 | local vars = cfg.variables | 10 | local vars = cfg.variables |
13 | 11 | ||
14 | local function command_at(directory, cmd) | 12 | local function command_at(directory, cmd) |
15 | return "cd " .. fs.Q(fs.absolute_name(directory)) .. " && " .. cmd | 13 | return "cd " .. fs.Q(fs.absolute_name(directory)) .. " && " .. cmd |
16 | end | 14 | end |
17 | 15 | ||
18 | --- Obtain current directory. | ||
19 | -- Uses the module's internal directory stack. | ||
20 | -- @return string: the absolute pathname of the current directory. | ||
21 | function tools.current_dir() | ||
22 | local current = cfg.cache_pwd | ||
23 | if not current then | ||
24 | local pipe = io.popen(fs.quiet_stderr(fs.Q(vars.PWD))) | ||
25 | current = pipe:read("*l") | ||
26 | pipe:close() | ||
27 | cfg.cache_pwd = current | ||
28 | end | ||
29 | for _, directory in ipairs(dir_stack) do | ||
30 | current = fs.absolute_name(directory, current) | ||
31 | end | ||
32 | return current | ||
33 | end | ||
34 | |||
35 | --- Run the given command. | 16 | --- Run the given command. |
36 | -- The command is executed in the current directory in the directory stack. | 17 | -- The command is executed in the current directory in the directory stack. |
37 | -- @param cmd string: No quoting/escaping is applied to the command. | 18 | -- @param cmd string: No quoting/escaping is applied to the command. |
@@ -48,33 +29,6 @@ function tools.execute_string(cmd) | |||
48 | end | 29 | end |
49 | end | 30 | end |
50 | 31 | ||
51 | --- Change the current directory. | ||
52 | -- Uses the module's internal directory stack. This does not have exact | ||
53 | -- semantics of chdir, as it does not handle errors the same way, | ||
54 | -- but works well for our purposes for now. | ||
55 | -- @param directory string: The directory to switch to. | ||
56 | function tools.change_dir(directory) | ||
57 | assert(type(directory) == "string") | ||
58 | if fs.is_dir(directory) then | ||
59 | table.insert(dir_stack, directory) | ||
60 | return true | ||
61 | end | ||
62 | return nil, "directory not found: "..directory | ||
63 | end | ||
64 | |||
65 | --- Change directory to root. | ||
66 | -- Allows leaving a directory (e.g. for deleting it) in | ||
67 | -- a crossplatform way. | ||
68 | function tools.change_dir_to_root() | ||
69 | table.insert(dir_stack, "/") | ||
70 | end | ||
71 | |||
72 | --- Change working directory to the previous in the directory stack. | ||
73 | function tools.pop_dir() | ||
74 | local directory = table.remove(dir_stack) | ||
75 | return directory ~= nil | ||
76 | end | ||
77 | |||
78 | --- Create a directory if it does not already exist. | 32 | --- Create a directory if it does not already exist. |
79 | -- If any of the higher levels in the path name does not exist | 33 | -- If any of the higher levels in the path name does not exist |
80 | -- too, they are created as well. | 34 | -- 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") | |||
9 | local dir = require("luarocks.dir") | 9 | local dir = require("luarocks.dir") |
10 | local cfg = require("luarocks.cfg") | 10 | local cfg = require("luarocks.cfg") |
11 | 11 | ||
12 | local dir_stack = {} | ||
13 | |||
14 | local vars = cfg.variables | 12 | local vars = cfg.variables |
15 | 13 | ||
16 | --- Strip the last extension of a filename. | 14 | --- Strip the last extension of a filename. |
@@ -33,23 +31,6 @@ local function command_at(directory, cmd) | |||
33 | return cmd | 31 | return cmd |
34 | end | 32 | end |
35 | 33 | ||
36 | --- Obtain current directory. | ||
37 | -- Uses the module's internal directory stack. | ||
38 | -- @return string: the absolute pathname of the current directory. | ||
39 | function tools.current_dir() | ||
40 | local current = cfg.cache_pwd | ||
41 | if not current then | ||
42 | local pipe = io.popen(fs.quiet_stderr(fs.Q(vars.PWD))) | ||
43 | current = pipe:read("*l") | ||
44 | pipe:close() | ||
45 | cfg.cache_pwd = current | ||
46 | end | ||
47 | for _, directory in ipairs(dir_stack) do | ||
48 | current = fs.absolute_name(directory, current) | ||
49 | end | ||
50 | return current | ||
51 | end | ||
52 | |||
53 | --- Run the given command. | 34 | --- Run the given command. |
54 | -- The command is executed in the current directory in the directory stack. | 35 | -- The command is executed in the current directory in the directory stack. |
55 | -- @param cmd string: No quoting/escaping is applied to the command. | 36 | -- @param cmd string: No quoting/escaping is applied to the command. |
@@ -67,34 +48,6 @@ function tools.execute_string(cmd) | |||
67 | end | 48 | end |
68 | end | 49 | end |
69 | 50 | ||
70 | --- Change the current directory. | ||
71 | -- Uses the module's internal directory stack. This does not have exact | ||
72 | -- semantics of chdir, as it does not handle errors the same way, | ||
73 | -- but works well for our purposes for now. | ||
74 | -- @param directory string: The directory to switch to. | ||
75 | -- @return boolean or (nil, string): true if successful, (nil, error message) if failed. | ||
76 | function tools.change_dir(directory) | ||
77 | assert(type(directory) == "string") | ||
78 | if fs.is_dir(directory) then | ||
79 | table.insert(dir_stack, directory) | ||
80 | return true | ||
81 | end | ||
82 | return nil, "directory not found: "..directory | ||
83 | end | ||
84 | |||
85 | --- Change directory to root. | ||
86 | -- Allows leaving a directory (e.g. for deleting it) in | ||
87 | -- a crossplatform way. | ||
88 | function tools.change_dir_to_root() | ||
89 | table.insert(dir_stack, "/") | ||
90 | end | ||
91 | |||
92 | --- Change working directory to the previous in the directory stack. | ||
93 | function tools.pop_dir() | ||
94 | local directory = table.remove(dir_stack) | ||
95 | return directory ~= nil | ||
96 | end | ||
97 | |||
98 | --- Create a directory if it does not already exist. | 51 | --- Create a directory if it does not already exist. |
99 | -- If any of the higher levels in the path name does not exist | 52 | -- If any of the higher levels in the path name does not exist |
100 | -- too, they are created as well. | 53 | -- too, they are created as well. |