diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2018-06-19 09:58:36 -0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2018-07-01 15:51:09 -0300 |
commit | a55f09fae00cb3c575f926eda4d76e81ff37b2ba (patch) | |
tree | c36d2d475549f76805b3b9bfd08f1429f032e5ac /src | |
parent | 392f67d65a950fcf08a08d10d21e80f359160262 (diff) | |
download | luarocks-a55f09fae00cb3c575f926eda4d76e81ff37b2ba.tar.gz luarocks-a55f09fae00cb3c575f926eda4d76e81ff37b2ba.tar.bz2 luarocks-a55f09fae00cb3c575f926eda4d76e81ff37b2ba.zip |
fs: make module loading side-effect free, require explicit init()
Diffstat (limited to 'src')
-rw-r--r-- | src/luarocks/command_line.lua | 2 | ||||
-rw-r--r-- | src/luarocks/core/cfg.lua | 8 | ||||
-rw-r--r-- | src/luarocks/fs.lua | 107 | ||||
-rw-r--r-- | src/luarocks/util.lua | 4 |
4 files changed, 72 insertions, 49 deletions
diff --git a/src/luarocks/command_line.lua b/src/luarocks/command_line.lua index 5ba5ae1b..b542fb9c 100644 --- a/src/luarocks/command_line.lua +++ b/src/luarocks/command_line.lua | |||
@@ -93,6 +93,8 @@ function command_line.run_command(...) | |||
93 | die(flags.ERROR.." See --help.") | 93 | die(flags.ERROR.." See --help.") |
94 | end | 94 | end |
95 | 95 | ||
96 | fs.init() | ||
97 | |||
96 | if flags["from"] then flags["server"] = flags["from"] end | 98 | if flags["from"] then flags["server"] = flags["from"] end |
97 | if flags["only-from"] then flags["only-server"] = flags["only-from"] end | 99 | if flags["only-from"] then flags["only-server"] = flags["only-from"] end |
98 | if flags["only-sources-from"] then flags["only-sources"] = flags["only-sources-from"] end | 100 | if flags["only-sources-from"] then flags["only-sources"] = flags["only-sources-from"] end |
diff --git a/src/luarocks/core/cfg.lua b/src/luarocks/core/cfg.lua index 38b10f6e..00efeaf3 100644 --- a/src/luarocks/core/cfg.lua +++ b/src/luarocks/core/cfg.lua | |||
@@ -763,4 +763,12 @@ function cfg.is_platform(query) | |||
763 | end | 763 | end |
764 | end | 764 | end |
765 | 765 | ||
766 | function cfg.each_platform() | ||
767 | local i = 0 | ||
768 | return function() | ||
769 | i = i + 1 | ||
770 | return cfg.platforms[i] | ||
771 | end | ||
772 | end | ||
773 | |||
766 | return cfg | 774 | return cfg |
diff --git a/src/luarocks/fs.lua b/src/luarocks/fs.lua index 7322e552..999572b8 100644 --- a/src/luarocks/fs.lua +++ b/src/luarocks/fs.lua | |||
@@ -16,62 +16,73 @@ local cfg = require("luarocks.core.cfg") | |||
16 | local pack = table.pack or function(...) return { n = select("#", ...), ... } end | 16 | local pack = table.pack or function(...) return { n = select("#", ...), ... } end |
17 | local unpack = table.unpack or unpack | 17 | local unpack = table.unpack or unpack |
18 | 18 | ||
19 | local old_popen, old_exec | 19 | do |
20 | fs.verbose = function() -- patch io.popen and os.execute to display commands in verbose mode | 20 | local old_popen, old_execute |
21 | if old_popen or old_exec then return end | 21 | |
22 | old_popen = io.popen | 22 | -- patch io.popen and os.execute to display commands in verbose mode |
23 | io.popen = function(one, two) | 23 | function fs.verbose() |
24 | if two == nil then | 24 | if old_popen or old_execute then return end |
25 | print("\nio.popen: ", one) | 25 | old_popen = io.popen |
26 | else | 26 | io.popen = function(one, two) |
27 | print("\nio.popen: ", one, "Mode:", two) | 27 | if two == nil then |
28 | end | 28 | print("\nio.popen: ", one) |
29 | return old_popen(one, two) | 29 | else |
30 | end | 30 | print("\nio.popen: ", one, "Mode:", two) |
31 | 31 | end | |
32 | old_exec = os.execute | 32 | return old_popen(one, two) |
33 | os.execute = function(cmd) | 33 | end |
34 | -- redact api keys if present | ||
35 | print("\nos.execute: ", (cmd:gsub("(/api/[^/]+/)([^/]+)/", function(cap, key) return cap.."<redacted>/" end)) ) | ||
36 | local code = pack(old_exec(cmd)) | ||
37 | print("Results: "..tostring(code.n)) | ||
38 | for i = 1,code.n do | ||
39 | print(" "..tostring(i).." ("..type(code[i]).."): "..tostring(code[i])) | ||
40 | end | ||
41 | return unpack(code, 1, code.n) | ||
42 | end | ||
43 | end | ||
44 | if cfg.verbose then fs.verbose() end | ||
45 | 34 | ||
46 | local function load_fns(fs_table) | 35 | old_execute = os.execute |
47 | for name, fn in pairs(fs_table) do | 36 | os.execute = function(cmd) |
48 | if not fs[name] then | 37 | -- redact api keys if present |
49 | fs[name] = fn | 38 | print("\nos.execute: ", (cmd:gsub("(/api/[^/]+/)([^/]+)/", function(cap, key) return cap.."<redacted>/" end)) ) |
39 | local code = pack(old_execute(cmd)) | ||
40 | print("Results: "..tostring(code.n)) | ||
41 | for i = 1,code.n do | ||
42 | print(" "..tostring(i).." ("..type(code[i]).."): "..tostring(code[i])) | ||
43 | end | ||
44 | return unpack(code, 1, code.n) | ||
50 | end | 45 | end |
51 | end | 46 | end |
52 | end | 47 | end |
53 | 48 | ||
54 | -- Load platform-specific functions | 49 | do |
55 | local loaded_platform = nil | 50 | local function load_fns(fs_table) |
56 | for _, platform in ipairs(cfg.platforms) do | 51 | for name, fn in pairs(fs_table) do |
57 | local ok, fs_plat = pcall(require, "luarocks.fs."..platform) | 52 | if not fs[name] then |
58 | if ok and fs_plat then | 53 | fs[name] = fn |
59 | loaded_platform = platform | 54 | end |
60 | load_fns(fs_plat) | 55 | end |
61 | break | ||
62 | end | 56 | end |
63 | end | ||
64 | 57 | ||
65 | -- Load platform-independent pure-Lua functionality | 58 | function fs.init() |
66 | local fs_lua = require("luarocks.fs.lua") | 59 | if fs.current_dir then |
67 | load_fns(fs_lua) | 60 | -- already initialized |
61 | return | ||
62 | end | ||
63 | |||
64 | -- Load platform-specific functions | ||
65 | local loaded_platform = nil | ||
66 | for platform in cfg.each_platform() do | ||
67 | local ok, fs_plat = pcall(require, "luarocks.fs."..platform) | ||
68 | if ok and fs_plat then | ||
69 | loaded_platform = platform | ||
70 | load_fns(fs_plat) | ||
71 | break | ||
72 | end | ||
73 | end | ||
68 | 74 | ||
69 | -- Load platform-specific fallbacks for missing Lua modules | 75 | -- Load platform-independent pure-Lua functionality |
70 | local ok, fs_plat_tools = pcall(require, "luarocks.fs."..loaded_platform..".tools") | 76 | local fs_lua = require("luarocks.fs.lua") |
71 | if ok and fs_plat_tools then | 77 | load_fns(fs_lua) |
72 | load_fns(fs_plat_tools) | ||
73 | load_fns(require("luarocks.fs.tools")) | ||
74 | end | ||
75 | 78 | ||
79 | -- Load platform-specific fallbacks for missing Lua modules | ||
80 | local ok, fs_plat_tools = pcall(require, "luarocks.fs."..loaded_platform..".tools") | ||
81 | if ok and fs_plat_tools then | ||
82 | load_fns(fs_plat_tools) | ||
83 | load_fns(require("luarocks.fs.tools")) | ||
84 | end | ||
85 | end | ||
86 | end | ||
76 | 87 | ||
77 | return fs | 88 | return fs |
diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index ee4a803a..8058a407 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua | |||
@@ -59,7 +59,9 @@ end | |||
59 | -- Functions are executed in the inverse order they were scheduled. | 59 | -- Functions are executed in the inverse order they were scheduled. |
60 | function util.run_scheduled_functions() | 60 | function util.run_scheduled_functions() |
61 | local fs = require("luarocks.fs") | 61 | local fs = require("luarocks.fs") |
62 | fs.change_dir_to_root() | 62 | if fs.change_dir_to_root then |
63 | fs.change_dir_to_root() | ||
64 | end | ||
63 | for i = #scheduled_functions, 1, -1 do | 65 | for i = #scheduled_functions, 1, -1 do |
64 | local item = scheduled_functions[i] | 66 | local item = scheduled_functions[i] |
65 | item.fn(unpack(item.args)) | 67 | item.fn(unpack(item.args)) |