diff options
| author | Hisham Muhammad <hisham@gobolinux.org> | 2018-05-24 16:02:11 -0300 |
|---|---|---|
| committer | Hisham Muhammad <hisham@gobolinux.org> | 2018-05-31 11:16:09 -0300 |
| commit | 9941d6d52c1052d449aec6b77adf95f2d308e320 (patch) | |
| tree | 1d992b855e4ddd291f604bf12ecae1068a338896 | |
| parent | 4b964d2d14882e0593eda548196cd72c0c95f1a9 (diff) | |
| download | luarocks-9941d6d52c1052d449aec6b77adf95f2d308e320.tar.gz luarocks-9941d6d52c1052d449aec6b77adf95f2d308e320.tar.bz2 luarocks-9941d6d52c1052d449aec6b77adf95f2d308e320.zip | |
fs: wrap_script: allow hardcoding arguments, support wrapping luarocks
| -rw-r--r-- | src/luarocks/cmd/path.lua | 13 | ||||
| -rw-r--r-- | src/luarocks/fs/unix.lua | 43 | ||||
| -rw-r--r-- | src/luarocks/fs/win32.lua | 50 | ||||
| -rw-r--r-- | src/luarocks/util.lua | 17 |
4 files changed, 89 insertions, 34 deletions
diff --git a/src/luarocks/cmd/path.lua b/src/luarocks/cmd/path.lua index 9bcd1c81..0f672a91 100644 --- a/src/luarocks/cmd/path.lua +++ b/src/luarocks/cmd/path.lua | |||
| @@ -58,18 +58,7 @@ function path_cmd.command(flags) | |||
| 58 | lr_bin = lr_bin .. path_sep .. os.getenv("PATH") | 58 | lr_bin = lr_bin .. path_sep .. os.getenv("PATH") |
| 59 | end | 59 | end |
| 60 | 60 | ||
| 61 | local lpath_var = "LUA_PATH" | 61 | local lpath_var, lcpath_var = util.lua_path_variables() |
| 62 | local lcpath_var = "LUA_CPATH" | ||
| 63 | |||
| 64 | local lv = cfg.lua_version:gsub("%.", "_") | ||
| 65 | if lv ~= "5_1" then | ||
| 66 | if os.getenv("LUA_PATH_" .. lv) then | ||
| 67 | lpath_var = "LUA_PATH_" .. lv | ||
| 68 | end | ||
| 69 | if os.getenv("LUA_CPATH_" .. lv) then | ||
| 70 | lcpath_var = "LUA_CPATH_" .. lv | ||
| 71 | end | ||
| 72 | end | ||
| 73 | 62 | ||
| 74 | util.printout(fs.export_cmd(lpath_var, util.cleanup_path(lr_path, ';', cfg.lua_version))) | 63 | util.printout(fs.export_cmd(lpath_var, util.cleanup_path(lr_path, ';', cfg.lua_version))) |
| 75 | util.printout(fs.export_cmd(lcpath_var, util.cleanup_path(lr_cpath, ';', cfg.lua_version))) | 64 | util.printout(fs.export_cmd(lcpath_var, util.cleanup_path(lr_cpath, ';', cfg.lua_version))) |
diff --git a/src/luarocks/fs/unix.lua b/src/luarocks/fs/unix.lua index b64a4a38..dbe48cca 100644 --- a/src/luarocks/fs/unix.lua +++ b/src/luarocks/fs/unix.lua | |||
| @@ -64,23 +64,48 @@ end | |||
| 64 | -- @param version string: rock version to be used in loader context. | 64 | -- @param version string: rock version to be used in loader context. |
| 65 | -- @return boolean or (nil, string): True if succeeded, or nil and | 65 | -- @return boolean or (nil, string): True if succeeded, or nil and |
| 66 | -- an error message. | 66 | -- an error message. |
| 67 | function unix.wrap_script(file, dest, name, version) | 67 | function unix.wrap_script(file, dest, name, version, ...) |
| 68 | assert(type(file) == "string") | 68 | assert(type(file) == "string" or not file) |
| 69 | assert(type(dest) == "string") | 69 | assert(type(dest) == "string") |
| 70 | assert(type(name) == "string" or not name) | ||
| 71 | assert(type(version) == "string" or not version) | ||
| 70 | 72 | ||
| 71 | local base = dir.base_name(file) | 73 | local wrapname = fs.is_dir(dest) and dest.."/"..dir.base_name(file) or dest |
| 72 | local wrapname = fs.is_dir(dest) and dest.."/"..base or dest | ||
| 73 | local lpath, lcpath = cfg.package_paths(cfg.root_dir) | ||
| 74 | local wrapper = io.open(wrapname, "w") | 74 | local wrapper = io.open(wrapname, "w") |
| 75 | if not wrapper then | 75 | if not wrapper then |
| 76 | return nil, "Could not open "..wrapname.." for writing." | 76 | return nil, "Could not open "..wrapname.." for writing." |
| 77 | end | 77 | end |
| 78 | |||
| 79 | local lpath, lcpath = cfg.package_paths(cfg.root_dir) | ||
| 80 | lpath = util.cleanup_path(lpath, ";", cfg.lua_version) | ||
| 81 | lcpath = util.cleanup_path(lcpath, ";", cfg.lua_version) | ||
| 82 | |||
| 83 | local luainit = { | ||
| 84 | "package.path="..util.LQ(lpath..";").."..package.path", | ||
| 85 | "package.cpath="..util.LQ(lcpath..";").."..package.cpath", | ||
| 86 | } | ||
| 87 | if dest == "luarocks" then | ||
| 88 | luainit = { | ||
| 89 | "package.path="..util.LQ(package.path), | ||
| 90 | "package.cpath="..util.LQ(package.cpath), | ||
| 91 | } | ||
| 92 | end | ||
| 93 | if name and version then | ||
| 94 | local addctx = "local k,l,_=pcall(require,"..util.LQ("luarocks.loader")..") _=k " .. | ||
| 95 | "and l.add_context("..util.LQ(name)..","..util.LQ(version)..")" | ||
| 96 | table.insert(luainit, addctx) | ||
| 97 | end | ||
| 98 | |||
| 99 | local argv = { | ||
| 100 | fs.Q(dir.path(cfg.variables["LUA_BINDIR"], cfg.lua_interpreter)), | ||
| 101 | file and fs.Q(file) or "", | ||
| 102 | ... | ||
| 103 | } | ||
| 104 | |||
| 78 | wrapper:write("#!/bin/sh\n\n") | 105 | wrapper:write("#!/bin/sh\n\n") |
| 79 | local lua = dir.path(cfg.variables["LUA_BINDIR"], cfg.lua_interpreter) | 106 | wrapper:write("LUA_INIT="..fs.Q(table.concat(luainit, ";")).." exec "..table.concat(argv, " ")..' "$@"\n') |
| 80 | local ppaths = "package.path="..util.LQ(lpath..";").."..package.path; package.cpath="..util.LQ(lcpath..";").."..package.cpath" | ||
| 81 | local addctx = "local k,l,_=pcall(require,"..util.LQ("luarocks.loader")..") _=k and l.add_context("..util.LQ(name)..","..util.LQ(version)..")" | ||
| 82 | wrapper:write('exec '..fs.Q(lua)..' -e '..fs.Q(ppaths)..' -e '..fs.Q(addctx)..' '..fs.Q(file)..' "$@"\n') | ||
| 83 | wrapper:close() | 107 | wrapper:close() |
| 108 | |||
| 84 | if fs.set_permissions(wrapname, "exec", "user") then | 109 | if fs.set_permissions(wrapname, "exec", "user") then |
| 85 | return true | 110 | return true |
| 86 | else | 111 | else |
diff --git a/src/luarocks/fs/win32.lua b/src/luarocks/fs/win32.lua index c2331cad..bf133448 100644 --- a/src/luarocks/fs/win32.lua +++ b/src/luarocks/fs/win32.lua | |||
| @@ -133,26 +133,50 @@ end | |||
| 133 | -- @param version string: rock version to be used in loader context. | 133 | -- @param version string: rock version to be used in loader context. |
| 134 | -- @return boolean or (nil, string): True if succeeded, or nil and | 134 | -- @return boolean or (nil, string): True if succeeded, or nil and |
| 135 | -- an error message. | 135 | -- an error message. |
| 136 | function win32.wrap_script(file, dest, name, version) | 136 | function win32.wrap_script(file, dest, name, version, ...) |
| 137 | assert(type(file) == "string") | 137 | assert(type(file) == "string" or not file) |
| 138 | assert(type(dest) == "string") | 138 | assert(type(dest) == "string") |
| 139 | assert(type(name) == "string" or not name) | ||
| 140 | assert(type(version) == "string" or not version) | ||
| 139 | 141 | ||
| 140 | local base = dir.base_name(file) | 142 | local wrapname = fs.is_dir(dest) and dest.."/"..dir.base_name(file) or dest |
| 141 | local wrapname = fs.is_dir(dest) and dest.."/"..base or dest | ||
| 142 | wrapname = wrapname..".bat" | 143 | wrapname = wrapname..".bat" |
| 143 | local lpath, lcpath = cfg.package_paths() | ||
| 144 | lpath = util.cleanup_path(lpath, ";", cfg.lua_version) | ||
| 145 | lcpath = util.cleanup_path(lcpath, ";", cfg.lua_version) | ||
| 146 | local wrapper = io.open(wrapname, "w") | 144 | local wrapper = io.open(wrapname, "w") |
| 147 | if not wrapper then | 145 | if not wrapper then |
| 148 | return nil, "Could not open "..wrapname.." for writing." | 146 | return nil, "Could not open "..wrapname.." for writing." |
| 149 | end | 147 | end |
| 150 | wrapper:write("@echo off\n") | 148 | |
| 151 | local lua = dir.path(cfg.variables["LUA_BINDIR"], cfg.lua_interpreter) | 149 | local lpath, lcpath = cfg.package_paths(cfg.root_dir) |
| 152 | local ppaths = "package.path="..util.LQ(lpath..";").."..package.path; package.cpath="..util.LQ(lcpath..";").."..package.cpath" | 150 | lpath = util.cleanup_path(lpath, ";", cfg.lua_version) |
| 153 | local addctx = "local k,l,_=pcall(require,"..util.LQ("luarocks.loader")..") _=k and l.add_context("..util.LQ(name)..","..util.LQ(version)..")" | 151 | lcpath = util.cleanup_path(lcpath, ";", cfg.lua_version) |
| 154 | wrapper:write(fs.Qb(lua)..' -e '..fs.Qb(ppaths)..' -e '..fs.Qb(addctx)..' '..fs.Qb(file)..' %*\n') | 152 | |
| 155 | wrapper:write("exit /b %ERRORLEVEL%\n") | 153 | local lpath_var, lcpath_var = util.lua_path_variables() |
| 154 | |||
| 155 | local addctx | ||
| 156 | if name and version then | ||
| 157 | addctx = "local k,l,_=pcall(require,'luarocks.loader') _=k " .. | ||
| 158 | "and l.add_context('"..name.."','"..version.."')" | ||
| 159 | end | ||
| 160 | |||
| 161 | local argv = { | ||
| 162 | fs.Qb(dir.path(cfg.variables["LUA_BINDIR"], cfg.lua_interpreter)), | ||
| 163 | file and fs.Qb(file) or "", | ||
| 164 | ... | ||
| 165 | } | ||
| 166 | |||
| 167 | wrapper:write("@echo off\r\n") | ||
| 168 | if dest == "luarocks" then | ||
| 169 | wrapper:write("set "..fs.Qb(lpath_var.."="..package.path) .. "\r\n") | ||
| 170 | wrapper:write("set "..fs.Qb(lcpath_var.."="..package.cpath) .. "\r\n") | ||
| 171 | else | ||
| 172 | wrapper:write("set "..fs.Qb(lpath_var.."="..lpath..";%"..lpath_var.."%") .. "\r\n") | ||
| 173 | wrapper:write("set "..fs.Qb(lcpath_var.."="..lcpath..";%"..lcpath_var.."%") .. "\r\n") | ||
| 174 | end | ||
| 175 | if addctx then | ||
| 176 | wrapper:write("set "..fs.Qb("LUA_INIT=" .. addctx) .. "\r\n") | ||
| 177 | end | ||
| 178 | wrapper:write(table.concat(argv, " ") .. " %*\r\n") | ||
| 179 | wrapper:write("exit /b %ERRORLEVEL%\r\n") | ||
| 156 | wrapper:close() | 180 | wrapper:close() |
| 157 | return true | 181 | return true |
| 158 | end | 182 | end |
diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index ff36aa50..64765a43 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua | |||
| @@ -318,6 +318,23 @@ function util.lua_versions() | |||
| 318 | end | 318 | end |
| 319 | end | 319 | end |
| 320 | 320 | ||
| 321 | function util.lua_path_variables() | ||
| 322 | local cfg = require("luarocks.core.cfg") | ||
| 323 | local lpath_var = "LUA_PATH" | ||
| 324 | local lcpath_var = "LUA_CPATH" | ||
| 325 | |||
| 326 | local lv = cfg.lua_version:gsub("%.", "_") | ||
| 327 | if lv ~= "5_1" then | ||
| 328 | if os.getenv("LUA_PATH_" .. lv) then | ||
| 329 | lpath_var = "LUA_PATH_" .. lv | ||
| 330 | end | ||
| 331 | if os.getenv("LUA_CPATH_" .. lv) then | ||
| 332 | lcpath_var = "LUA_CPATH_" .. lv | ||
| 333 | end | ||
| 334 | end | ||
| 335 | return lpath_var, lcpath_var | ||
| 336 | end | ||
| 337 | |||
| 321 | function util.starts_with(s, prefix) | 338 | function util.starts_with(s, prefix) |
| 322 | return s:sub(1,#prefix) == prefix | 339 | return s:sub(1,#prefix) == prefix |
| 323 | end | 340 | end |
