File size: 2,775 Bytes
7e9dc27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

--- Proxy module for filesystem and platform abstractions.
-- All code using "fs" code should require "luarocks.fs",
-- and not the various platform-specific implementations.
-- However, see the documentation of the implementation
-- for the API reference.

local pairs = pairs

local fs = {}
-- To avoid a loop when loading the other fs modules.
package.loaded["luarocks.fs"] = fs

local cfg = require("luarocks.core.cfg")

local pack = table.pack or function(...) return { n = select("#", ...), ... } end
local unpack = table.unpack or unpack

math.randomseed(os.time())

do
   local old_popen, old_execute

   -- patch io.popen and os.execute to display commands in verbose mode
   function fs.verbose()
      if old_popen or old_execute then return end
      old_popen = io.popen
      io.popen = function(one, two)
         if two == nil then
            print("\nio.popen: ", one)
         else
            print("\nio.popen: ", one, "Mode:", two)
         end
         return old_popen(one, two)
      end

      old_execute = os.execute
      os.execute = function(cmd)
         -- redact api keys if present
         print("\nos.execute: ", (cmd:gsub("(/api/[^/]+/)([^/]+)/", function(cap, key) return cap.."<redacted>/" end)) )
         local code = pack(old_execute(cmd))
         print("Results: "..tostring(code.n))
         for i = 1,code.n do
            print("  "..tostring(i).." ("..type(code[i]).."): "..tostring(code[i]))
         end
         return unpack(code, 1, code.n)
      end
   end
end

do
   local function load_fns(fs_table)
      for name, fn in pairs(fs_table) do
         if not fs[name] then
            fs[name] = fn
         end
      end
   end

   function fs.init()
      if fs.current_dir then
         -- already initialized
         return
      end

      if not cfg.each_platform then
         error("cfg is not initialized, please run cfg.init() first")
      end

      -- Load platform-specific functions
      local loaded_platform = nil
      for platform in cfg.each_platform() do
         local ok, fs_plat = pcall(require, "luarocks.fs."..platform)
         if ok and fs_plat then
            loaded_platform = platform
            load_fns(fs_plat)
            break
         end
      end

      -- Load platform-independent pure-Lua functionality
      local fs_lua = require("luarocks.fs.lua")
      load_fns(fs_lua)

      -- Load platform-specific fallbacks for missing Lua modules
      local ok, fs_plat_tools = pcall(require, "luarocks.fs."..loaded_platform..".tools")
      if ok and fs_plat_tools then
         load_fns(fs_plat_tools)
         load_fns(require("luarocks.fs.tools"))
      end
   end
end

return fs