diff options
| author | Thijs Schreijer <thijs@thijsschreijer.nl> | 2013-11-29 23:44:59 +0100 |
|---|---|---|
| committer | Thijs Schreijer <thijs@thijsschreijer.nl> | 2013-11-29 23:44:59 +0100 |
| commit | aeed3edc86962fe21fb2b2553e91fadbec878a3d (patch) | |
| tree | a3019e9bc53977d8500fa2452a474ab8f71b4f63 | |
| parent | 50e6d37d900cd4fbdc69cfc35011418e7771562b (diff) | |
| download | luarocks-aeed3edc86962fe21fb2b2553e91fadbec878a3d.tar.gz luarocks-aeed3edc86962fe21fb2b2553e91fadbec878a3d.tar.bz2 luarocks-aeed3edc86962fe21fb2b2553e91fadbec878a3d.zip | |
The installer will automatically request admin priviledges to allow proper installation in protected locations. A new switch /NOADMIN is added to explicitly prevent this, and allow installation in unprotected locations when the user doesn't have admin rights.
| -rw-r--r-- | install.bat | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/install.bat b/install.bat index a4257be6..26255502 100644 --- a/install.bat +++ b/install.bat | |||
| @@ -27,6 +27,7 @@ local FORCE_CONFIG = false | |||
| 27 | local INSTALL_LUA = false | 27 | local INSTALL_LUA = false |
| 28 | local USE_MINGW = false | 28 | local USE_MINGW = false |
| 29 | local REGISTRY = false | 29 | local REGISTRY = false |
| 30 | local NOADMIN = false | ||
| 30 | 31 | ||
| 31 | --- | 32 | --- |
| 32 | -- Some helpers | 33 | -- Some helpers |
| @@ -82,6 +83,12 @@ local function mkdir (dir) | |||
| 82 | return exec([[.\bin\bin\mkdir -p "]]..dir..[[" >NUL]]) | 83 | return exec([[.\bin\bin\mkdir -p "]]..dir..[[" >NUL]]) |
| 83 | end | 84 | end |
| 84 | 85 | ||
| 86 | -- does the current user have admin priviledges ( = elevated) | ||
| 87 | local function permission() | ||
| 88 | return exec("net session >nul 2>&1") -- fails if not admin | ||
| 89 | end | ||
| 90 | |||
| 91 | |||
| 85 | -- interpolate string with values from 'vars' table | 92 | -- interpolate string with values from 'vars' table |
| 86 | local function S (tmpl) | 93 | local function S (tmpl) |
| 87 | return (tmpl:gsub('%$([%a_][%w_]*)', vars)) | 94 | return (tmpl:gsub('%$([%a_][%w_]*)', vars)) |
| @@ -136,6 +143,11 @@ Other options: | |||
| 136 | /F Remove installation directory if it already exists. | 143 | /F Remove installation directory if it already exists. |
| 137 | /R Load registry information to register '.rockspec' | 144 | /R Load registry information to register '.rockspec' |
| 138 | extension with LuaRocks commands (right-click). | 145 | extension with LuaRocks commands (right-click). |
| 146 | /NOADMIN The installer requires admin priviledges. If not | ||
| 147 | available it will elevate a new process. Use this | ||
| 148 | switch to prevent elevation, but make sure the | ||
| 149 | destination paths are all accessible for the current | ||
| 150 | user. | ||
| 139 | 151 | ||
| 140 | ]]) | 152 | ]]) |
| 141 | end | 153 | end |
| @@ -180,6 +192,8 @@ local function parse_options(args) | |||
| 180 | FORCE = true | 192 | FORCE = true |
| 181 | elseif name == "/R" then | 193 | elseif name == "/R" then |
| 182 | REGISTRY = true | 194 | REGISTRY = true |
| 195 | elseif name == "/NOADMIN" then | ||
| 196 | NOADMIN = true | ||
| 183 | else | 197 | else |
| 184 | die("Unrecognized option: " .. name) | 198 | die("Unrecognized option: " .. name) |
| 185 | end | 199 | end |
| @@ -427,6 +441,7 @@ local with_arg = { -- options followed by an argument, others are flags | |||
| 427 | } | 441 | } |
| 428 | -- reconstruct argument values with spaces and double quotes | 442 | -- reconstruct argument values with spaces and double quotes |
| 429 | -- this will be damaged by the batch construction at the start of this file | 443 | -- this will be damaged by the batch construction at the start of this file |
| 444 | local oarg = arg -- retain old table | ||
| 430 | if #arg > 0 then | 445 | if #arg > 0 then |
| 431 | farg = table.concat(arg, " ") .. " " | 446 | farg = table.concat(arg, " ") .. " " |
| 432 | arg = {} | 447 | arg = {} |
| @@ -448,6 +463,9 @@ if #arg > 0 then | |||
| 448 | while farg:sub(1,1) == " " do farg = farg:sub(2,-1) end -- remove prefix spaces | 463 | while farg:sub(1,1) == " " do farg = farg:sub(2,-1) end -- remove prefix spaces |
| 449 | end | 464 | end |
| 450 | end | 465 | end |
| 466 | for k,v in pairs(oarg) do if k < 1 then arg[k] = v end end -- copy 0 and negative indexes | ||
| 467 | oarg = nil | ||
| 468 | |||
| 451 | 469 | ||
| 452 | local i = 1 | 470 | local i = 1 |
| 453 | while i <= #arg do | 471 | while i <= #arg do |
| @@ -470,6 +488,29 @@ print(S"LuaRocks $VERSION.x installer.\n") | |||
| 470 | parse_options(config) | 488 | parse_options(config) |
| 471 | check_flags() | 489 | check_flags() |
| 472 | 490 | ||
| 491 | if not permission() then | ||
| 492 | if not NOADMIN then | ||
| 493 | -- must elevate the process with admin priviledges | ||
| 494 | print("Need admin priviledges, now elevating a new process to continue installing...") | ||
| 495 | local runner = os.getenv("TEMP").."\\".."LuaRocks_Installer.bat" | ||
| 496 | local f = io.open(runner, "w") | ||
| 497 | f:write("@echo off\n") | ||
| 498 | f:write("CHDIR /D "..arg[0]:match("(.+)%\\.-$").."\n") -- return to current die, elevation changes current path | ||
| 499 | f:write('"'..arg[-1]..'" "'..table.concat(arg, '" "', 0)..'"\n') | ||
| 500 | f:write("ECHO Press any key to close this window...\n") | ||
| 501 | f:write("PAUSE > NUL\n") | ||
| 502 | f:close() | ||
| 503 | -- run the created temp batch file in elevated mode | ||
| 504 | exec("PowerShell -Command (New-Object -com 'Shell.Application').ShellExecute('"..runner.."', '', '', 'runas')\n") | ||
| 505 | print("Now exiting unpriviledged installer") | ||
| 506 | os.exit() -- exit here, the newly created elevated process will do the installing | ||
| 507 | else | ||
| 508 | print("Attempting to install without admin priviledges...") | ||
| 509 | end | ||
| 510 | else | ||
| 511 | print("Admin priviledges available for installing") | ||
| 512 | end | ||
| 513 | |||
| 473 | vars.FULL_PREFIX = S"$PREFIX\\$VERSION" | 514 | vars.FULL_PREFIX = S"$PREFIX\\$VERSION" |
| 474 | vars.BINDIR = vars.FULL_PREFIX | 515 | vars.BINDIR = vars.FULL_PREFIX |
| 475 | vars.LIBDIR = vars.FULL_PREFIX | 516 | vars.LIBDIR = vars.FULL_PREFIX |
