aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHisham Muhammad <hisham@gobolinux.org>2018-07-09 11:05:17 -0300
committerHisham Muhammad <hisham@gobolinux.org>2018-07-10 19:02:50 -0300
commit9d38b91692d53cc485c55bad8d55357f04b30141 (patch)
tree77aa4ac896b0004e09d9786f2a565ca3151b4097 /src
parent68a54b3f1bd8d26aca41b2a2bada1af7ac32f7ff (diff)
downloadluarocks-9d38b91692d53cc485c55bad8d55357f04b30141.tar.gz
luarocks-9d38b91692d53cc485c55bad8d55357f04b30141.tar.bz2
luarocks-9d38b91692d53cc485c55bad8d55357f04b30141.zip
cfg: detect sysconfdir based on location of sources
This will help a stand-alone luarocks.loader find the system config file without any environment variable or hardcoded value, in most common situations (i.e. unless a non-standard sysconfdir was given during installation -- but then a stand-alone luarocks.loader loaded by a third-party program probably shouldn't assume non-standard configurations anyway; and when they do they should set up their environment accordingly, preloading luarocks.core.hardcoded.)
Diffstat (limited to 'src')
-rw-r--r--src/luarocks/core/cfg.lua59
1 files changed, 39 insertions, 20 deletions
diff --git a/src/luarocks/core/cfg.lua b/src/luarocks/core/cfg.lua
index b8fedd28..de8a93bf 100644
--- a/src/luarocks/core/cfg.lua
+++ b/src/luarocks/core/cfg.lua
@@ -46,6 +46,42 @@ local platform_order = {
46 "mingw32", 46 "mingw32",
47} 47}
48 48
49local function detect_sysconfdir(lua_version)
50 local src = debug.getinfo(1, "S").source:gsub("\\", "/"):gsub("/+", "/")
51 local basedir = src:match("^(.*)/luarocks/core/cfg.lua$")
52 if not basedir then
53 return
54 end
55 -- If installed in a Unix-like tree, use a Unix-like sysconfdir
56 local installdir = basedir:match("^(.*)/share/lua/" .. lua_version .. "$")
57 if installdir then
58 if installdir == "/usr" then
59 return "/etc/luarocks"
60 end
61 return installdir .. "/etc/luarocks"
62 end
63 -- Otherwise, use base directory of sources
64 return basedir
65end
66
67local function set_confdirs(cfg, platforms, hardcoded_sysconfdir)
68 local sysconfdir = os.getenv("LUAROCKS_SYSCONFDIR") or hardcoded_sysconfdir
69 if not sysconfdir then
70 sysconfdir = detect_sysconfdir(cfg.lua_version)
71 end
72 if platforms.windows then
73 cfg.home = os.getenv("APPDATA") or "c:"
74 cfg.home_tree = cfg.home.."/luarocks"
75 cfg.homeconfdir = cfg.home_tree
76 cfg.sysconfdir = sysconfdir or ((os.getenv("PROGRAMFILES") or "c:") .. "/luarocks")
77 else
78 cfg.home = os.getenv("HOME") or ""
79 cfg.home_tree = (os.getenv("USER") ~= "root") and cfg.home.."/.luarocks"
80 cfg.homeconfdir = cfg.home.."/.luarocks"
81 cfg.sysconfdir = sysconfdir or "/etc/luarocks"
82 end
83end
84
49local load_config_file 85local load_config_file
50do 86do
51 -- Create global environment for the config files; 87 -- Create global environment for the config files;
@@ -575,27 +611,10 @@ function cfg.init(lua_data, project_dir, warning)
575 local home_config_file 611 local home_config_file
576 local project_config_file 612 local project_config_file
577 do 613 do
578 local sysconfdir = os.getenv("LUAROCKS_SYSCONFDIR") or hardcoded.SYSCONFDIR 614 set_confdirs(cfg, platforms, hardcoded.SYSCONFDIR)
579 local sdir, hdir
580 local name = "config-"..cfg.lua_version..".lua" 615 local name = "config-"..cfg.lua_version..".lua"
581 if platforms.windows then 616 sys_config_file = (cfg.sysconfdir .. "/" .. name):gsub("\\", "/")
582 cfg.home = os.getenv("APPDATA") or "c:" 617 home_config_file = (cfg.homeconfdir .. "/" .. name):gsub("\\", "/")
583 sdir = sysconfdir or (os.getenv("PROGRAMFILES") or "c:") .. "/luarocks"
584 hdir = cfg.home.."/luarocks"
585 cfg.home_tree = cfg.home.."/luarocks"
586 else
587 cfg.home = os.getenv("HOME") or ""
588 sdir = sysconfdir or "/etc/luarocks"
589 hdir = cfg.home.."/.luarocks"
590 cfg.home_tree = (os.getenv("USER") ~= "root") and cfg.home.."/.luarocks/"
591 end
592 sys_config_file = sdir .. "/" .. name
593 home_config_file = hdir .. "/" .. name
594 cfg.sysconfdir = sdir
595
596 sys_config_file = sys_config_file:gsub("\\", "/")
597 home_config_file = home_config_file:gsub("\\", "/")
598
599 if project_dir then 618 if project_dir then
600 project_config_file = project_dir .. "/.luarocks/" .. name 619 project_config_file = project_dir .. "/.luarocks/" .. name
601 end 620 end