diff options
Diffstat (limited to 'install.bat')
-rw-r--r-- | install.bat | 166 |
1 files changed, 126 insertions, 40 deletions
diff --git a/install.bat b/install.bat index a4257be6..43a2b34f 100644 --- a/install.bat +++ b/install.bat | |||
@@ -6,8 +6,8 @@ local vars = {} | |||
6 | 6 | ||
7 | vars.PREFIX = [[C:\LuaRocks]] | 7 | vars.PREFIX = [[C:\LuaRocks]] |
8 | vars.VERSION = "2.1" | 8 | vars.VERSION = "2.1" |
9 | vars.SYSCONFDIR = [[C:\LuaRocks]] | 9 | vars.SYSCONFDIR = nil |
10 | vars.ROCKS_TREE = [[C:\LuaRocks]] | 10 | vars.ROCKS_TREE = nil |
11 | vars.SCRIPTS_DIR = nil | 11 | vars.SCRIPTS_DIR = nil |
12 | vars.LUA_INTERPRETER = nil | 12 | vars.LUA_INTERPRETER = nil |
13 | vars.LUA_PREFIX = nil | 13 | vars.LUA_PREFIX = nil |
@@ -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,25 @@ 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 | -- rename file (full path) to backup (name only), appending number if required | ||
92 | -- returns the new name (name only) | ||
93 | local function backup(filename, backupname) | ||
94 | local path = filename:match("(.+)%\\.-$").."\\" | ||
95 | local nname = backupname | ||
96 | local i = 0 | ||
97 | while exists(path..nname) do | ||
98 | i = i + 1 | ||
99 | nname = backupname..tostring(i) | ||
100 | end | ||
101 | exec([[REN "]]..filename..[[" "]]..nname..[[" > NUL]]) | ||
102 | return nname | ||
103 | end | ||
104 | |||
85 | -- interpolate string with values from 'vars' table | 105 | -- interpolate string with values from 'vars' table |
86 | local function S (tmpl) | 106 | local function S (tmpl) |
87 | return (tmpl:gsub('%$([%a_][%w_]*)', vars)) | 107 | return (tmpl:gsub('%$([%a_][%w_]*)', vars)) |
@@ -136,6 +156,11 @@ Other options: | |||
136 | /F Remove installation directory if it already exists. | 156 | /F Remove installation directory if it already exists. |
137 | /R Load registry information to register '.rockspec' | 157 | /R Load registry information to register '.rockspec' |
138 | extension with LuaRocks commands (right-click). | 158 | extension with LuaRocks commands (right-click). |
159 | /NOADMIN The installer requires admin priviledges. If not | ||
160 | available it will elevate a new process. Use this | ||
161 | switch to prevent elevation, but make sure the | ||
162 | destination paths are all accessible for the current | ||
163 | user. | ||
139 | 164 | ||
140 | ]]) | 165 | ]]) |
141 | end | 166 | end |
@@ -151,8 +176,6 @@ local function parse_options(args) | |||
151 | os.exit(0) | 176 | os.exit(0) |
152 | elseif name == "/P" then | 177 | elseif name == "/P" then |
153 | vars.PREFIX = option.value | 178 | vars.PREFIX = option.value |
154 | vars.SYSCONFDIR = option.value | ||
155 | vars.ROCKS_TREE = option.value | ||
156 | P_SET = true | 179 | P_SET = true |
157 | elseif name == "/CONFIG" then | 180 | elseif name == "/CONFIG" then |
158 | vars.SYSCONFDIR = option.value | 181 | vars.SYSCONFDIR = option.value |
@@ -180,6 +203,8 @@ local function parse_options(args) | |||
180 | FORCE = true | 203 | FORCE = true |
181 | elseif name == "/R" then | 204 | elseif name == "/R" then |
182 | REGISTRY = true | 205 | REGISTRY = true |
206 | elseif name == "/NOADMIN" then | ||
207 | NOADMIN = true | ||
183 | else | 208 | else |
184 | die("Unrecognized option: " .. name) | 209 | die("Unrecognized option: " .. name) |
185 | end | 210 | end |
@@ -376,7 +401,7 @@ local function look_for_lua_install () | |||
376 | then | 401 | then |
377 | if get_runtime() then | 402 | if get_runtime() then |
378 | print("Runtime check completed, now testing interpreter...") | 403 | print("Runtime check completed, now testing interpreter...") |
379 | if exec(S[[$LUA_BINDIR\$LUA_INTERPRETER -v 2>NUL]]) then | 404 | if exec(S[["$LUA_BINDIR\$LUA_INTERPRETER" -v 2>NUL]]) then |
380 | print(" Ok") | 405 | print(" Ok") |
381 | return true | 406 | return true |
382 | end | 407 | end |
@@ -397,7 +422,7 @@ local function look_for_lua_install () | |||
397 | print("Headers found, checking runtime to use...") | 422 | print("Headers found, checking runtime to use...") |
398 | if get_runtime() then | 423 | if get_runtime() then |
399 | print("Runtime check completed, now testing interpreter...") | 424 | print("Runtime check completed, now testing interpreter...") |
400 | if exec(S[[$LUA_BINDIR\$LUA_INTERPRETER -v 2>NUL]]) then | 425 | if exec(S[["$LUA_BINDIR\$LUA_INTERPRETER" -v 2>NUL]]) then |
401 | print(" Ok") | 426 | print(" Ok") |
402 | return true | 427 | return true |
403 | end | 428 | end |
@@ -411,7 +436,11 @@ local function look_for_lua_install () | |||
411 | return false | 436 | return false |
412 | end | 437 | end |
413 | 438 | ||
414 | --- | 439 | |
440 | -- *********************************************************** | ||
441 | -- Installer script start | ||
442 | -- *********************************************************** | ||
443 | |||
415 | -- Poor man's command-line parsing | 444 | -- Poor man's command-line parsing |
416 | local config = {} | 445 | local config = {} |
417 | local with_arg = { -- options followed by an argument, others are flags | 446 | local with_arg = { -- options followed by an argument, others are flags |
@@ -427,6 +456,7 @@ local with_arg = { -- options followed by an argument, others are flags | |||
427 | } | 456 | } |
428 | -- reconstruct argument values with spaces and double quotes | 457 | -- reconstruct argument values with spaces and double quotes |
429 | -- this will be damaged by the batch construction at the start of this file | 458 | -- this will be damaged by the batch construction at the start of this file |
459 | local oarg = arg -- retain old table | ||
430 | if #arg > 0 then | 460 | if #arg > 0 then |
431 | farg = table.concat(arg, " ") .. " " | 461 | farg = table.concat(arg, " ") .. " " |
432 | arg = {} | 462 | arg = {} |
@@ -448,7 +478,10 @@ if #arg > 0 then | |||
448 | while farg:sub(1,1) == " " do farg = farg:sub(2,-1) end -- remove prefix spaces | 478 | while farg:sub(1,1) == " " do farg = farg:sub(2,-1) end -- remove prefix spaces |
449 | end | 479 | end |
450 | end | 480 | end |
481 | for k,v in pairs(oarg) do if k < 1 then arg[k] = v end end -- copy 0 and negative indexes | ||
482 | oarg = nil | ||
451 | 483 | ||
484 | -- build config option table with name and value elements | ||
452 | local i = 1 | 485 | local i = 1 |
453 | while i <= #arg do | 486 | while i <= #arg do |
454 | local opt = arg[i] | 487 | local opt = arg[i] |
@@ -470,6 +503,36 @@ print(S"LuaRocks $VERSION.x installer.\n") | |||
470 | parse_options(config) | 503 | parse_options(config) |
471 | check_flags() | 504 | check_flags() |
472 | 505 | ||
506 | if not permission() then | ||
507 | if not NOADMIN then | ||
508 | -- must elevate the process with admin priviledges | ||
509 | if not exec("PowerShell /? >NUL 2>&1") then | ||
510 | -- powershell is not available, so error out | ||
511 | die("No administrative priviledges detected and cannot auto-elevate. Please run with admin priviledges or use the /NOADMIN switch") | ||
512 | end | ||
513 | print("Need admin priviledges, now elevating a new process to continue installing...") | ||
514 | local runner = os.getenv("TEMP").."\\".."LuaRocks_Installer.bat" | ||
515 | local f = io.open(runner, "w") | ||
516 | f:write("@echo off\n") | ||
517 | f:write("CHDIR /D "..arg[0]:match("(.+)%\\.-$").."\n") -- return to current dir, elevation changes current path | ||
518 | f:write('"'..arg[-1]..'" "'..table.concat(arg, '" "', 0)..'"\n') | ||
519 | f:write("ECHO Press any key to close this window...\n") | ||
520 | f:write("PAUSE > NUL\n") | ||
521 | f:write('DEL "'..runner..'"') -- temp batch file deletes itself | ||
522 | f:close() | ||
523 | -- run the created temp batch file in elevated mode | ||
524 | exec("PowerShell -Command (New-Object -com 'Shell.Application').ShellExecute('"..runner.."', '', '', 'runas')\n") | ||
525 | print("Now exiting unpriviledged installer") | ||
526 | os.exit() -- exit here, the newly created elevated process will do the installing | ||
527 | else | ||
528 | print("Attempting to install without admin priviledges...") | ||
529 | end | ||
530 | else | ||
531 | print("Admin priviledges available for installing") | ||
532 | end | ||
533 | |||
534 | vars.SYSCONFDIR = vars.SYSCONFDIR or vars.PREFIX | ||
535 | vars.ROCKS_TREE = vars.ROCKS_TREE or vars.PREFIX | ||
473 | vars.FULL_PREFIX = S"$PREFIX\\$VERSION" | 536 | vars.FULL_PREFIX = S"$PREFIX\\$VERSION" |
474 | vars.BINDIR = vars.FULL_PREFIX | 537 | vars.BINDIR = vars.FULL_PREFIX |
475 | vars.LIBDIR = vars.FULL_PREFIX | 538 | vars.LIBDIR = vars.FULL_PREFIX |
@@ -599,8 +662,13 @@ else | |||
599 | end | 662 | end |
600 | 663 | ||
601 | 664 | ||
665 | -- *********************************************************** | ||
666 | -- Configure LuaRocks | ||
667 | -- *********************************************************** | ||
668 | |||
602 | print() | 669 | print() |
603 | print("Configuring LuaRocks...") | 670 | print("Configuring LuaRocks...") |
671 | |||
604 | -- Create a site-config file | 672 | -- Create a site-config file |
605 | if exists(S[[$LUADIR\luarocks\site_config.lua]]) then | 673 | if exists(S[[$LUADIR\luarocks\site_config.lua]]) then |
606 | exec(S[[RENAME "$LUADIR\luarocks\site_config.lua" site_config.lua.bak]]) | 674 | exec(S[[RENAME "$LUADIR\luarocks\site_config.lua" site_config.lua.bak]]) |
@@ -644,50 +712,56 @@ vars.CONFIG_FILE = vars.SYSCONFDIR.."\\config.lua" | |||
644 | if not exists(vars.SYSCONFDIR) then | 712 | if not exists(vars.SYSCONFDIR) then |
645 | mkdir(vars.SYSCONFDIR) | 713 | mkdir(vars.SYSCONFDIR) |
646 | end | 714 | end |
647 | if not exists(vars.CONFIG_FILE) then | 715 | if exists(vars.CONFIG_FILE) then |
648 | local f = io.open(vars.CONFIG_FILE, "w") | 716 | local nname = backup(vars.CONFIG_FILE, "config.bak") |
649 | f:write([=[ | 717 | print("***************") |
718 | print(S"*** WARNING *** LuaRocks config file already exists: '$CONFIG_FILE'. The old file has been renamed to '"..nname.."'") | ||
719 | print("***************") | ||
720 | end | ||
721 | local f = io.open(vars.CONFIG_FILE, "w") | ||
722 | f:write([=[ | ||
650 | rocks_servers = { | 723 | rocks_servers = { |
651 | [[http://luarocks.org/repositories/rocks]] | 724 | [[http://luarocks.org/repositories/rocks]] |
652 | } | 725 | } |
653 | rocks_trees = { | 726 | rocks_trees = { |
654 | ]=]) | 727 | ]=]) |
655 | if FORCE_CONFIG then | 728 | if FORCE_CONFIG then |
656 | f:write(" home..[[/luarocks]],\n") | 729 | f:write(" home..[[/luarocks]],\n") |
657 | end | 730 | end |
658 | f:write(S" [[$ROCKS_TREE]]\n") | 731 | f:write(S" [[$ROCKS_TREE]]\n") |
659 | f:write("}\n") | 732 | f:write("}\n") |
660 | if vars.SCRIPTS_DIR then | 733 | if vars.SCRIPTS_DIR then |
661 | f:write(S"scripts_dir=[[$SCRIPTS_DIR]]\n") | 734 | f:write(S"scripts_dir=[[$SCRIPTS_DIR]]\n") |
662 | end | 735 | end |
663 | f:write("variables = {\n") | 736 | f:write("variables = {\n") |
664 | if USE_MINGW and vars.LUA_RUNTIME == "MSVCRT" then | 737 | if USE_MINGW and vars.LUA_RUNTIME == "MSVCRT" then |
665 | f:write(" MSVCRT = 'm', -- make MinGW use MSVCRT.DLL as runtime\n") | 738 | f:write(" MSVCRT = 'm', -- make MinGW use MSVCRT.DLL as runtime\n") |
666 | else | ||
667 | f:write(" MSVCRT = '"..vars.LUA_RUNTIME.."',\n") | ||
668 | end | ||
669 | f:write(S" LUALIB = '$LUA_LIBNAME'\n") | ||
670 | f:write("}\n") | ||
671 | f:close() | ||
672 | print(S"Created LuaRocks config file: $CONFIG_FILE") | ||
673 | else | 739 | else |
674 | print(S"LuaRocks config file already exists: $CONFIG_FILE") | 740 | f:write(" MSVCRT = '"..vars.LUA_RUNTIME.."',\n") |
675 | end | 741 | end |
742 | f:write(S" LUALIB = '$LUA_LIBNAME'\n") | ||
743 | f:write("}\n") | ||
744 | f:write("verbose = false -- set to 'true' to enable verbose output\n") | ||
745 | f:close() | ||
746 | |||
747 | print(S"Created LuaRocks config file: $CONFIG_FILE") | ||
748 | |||
676 | 749 | ||
677 | print() | 750 | print() |
678 | print("Creating rocktrees...") | 751 | print("Creating rocktrees...") |
679 | if not exists(vars.ROCKS_TREE) then | 752 | if not exists(vars.ROCKS_TREE) then |
680 | mkdir(vars.ROCKS_TREE) | 753 | mkdir(vars.ROCKS_TREE) |
681 | print(S[[Created rocktree: "$ROCKS_TREE"]]) | 754 | print(S[[Created system rocktree : "$ROCKS_TREE"]]) |
682 | else | 755 | else |
683 | print(S[[Rocktree exists: "$ROCKS_TREE"]]) | 756 | print(S[[System rocktree exists : "$ROCKS_TREE"]]) |
684 | end | 757 | end |
685 | local APPDATA = os.getenv("APPDATA") | 758 | |
686 | if not exists(APPDATA.."\\luarocks") then | 759 | vars.LOCAL_TREE = os.getenv("APPDATA")..[[\LuaRocks]] |
687 | mkdir(APPDATA.."\\luarocks") | 760 | if not exists(vars.LOCAL_TREE) then |
688 | print([[Created rocktree: "]]..APPDATA..[[\luarocks"]]) | 761 | mkdir(vars.LOCAL_TREE) |
762 | print(S[[Created local user rocktree: "$LOCAL_TREE"]]) | ||
689 | else | 763 | else |
690 | print([[Rocktree exists: "]]..APPDATA..[[\luarocks"]]) | 764 | print(S[[Local user rocktree exists : "$LOCAL_TREE"]]) |
691 | end | 765 | end |
692 | 766 | ||
693 | -- Load registry information | 767 | -- Load registry information |
@@ -712,12 +786,24 @@ exec( S[[del "$FULL_PREFIX\pe-parser.lua" > nul]] ) | |||
712 | -- *********************************************************** | 786 | -- *********************************************************** |
713 | 787 | ||
714 | print(S[[ | 788 | print(S[[ |
789 | |||
715 | *** LuaRocks is installed! *** | 790 | *** LuaRocks is installed! *** |
716 | 791 | ||
717 | You may want to add the following elements to your paths; | 792 | You may want to add the following elements to your paths; |
718 | PATH : $LUA_BINDIR;$FULL_PREFIX | 793 | Lua interpreter; |
719 | LUA_PATH : $ROCKS_TREE\share\lua\$LUA_VERSION\?.lua;$ROCKS_TREE\share\lua\$LUA_VERSION\?\init.lua | 794 | PATH : $LUA_BINDIR |
720 | LUA_CPATH: $ROCKS_TREE\lib\lua\$LUA_VERSION\?.dll | 795 | PATHEXT : .LUA |
796 | LuaRocks; | ||
797 | PATH : $FULL_PREFIX | ||
798 | LUA_PATH : $FULL_PREFIX\lua\?.lua;$FULL_PREFIX\lua\?\init.lua | ||
799 | Local user rocktree (Note: %APPDATA% is user dependent); | ||
800 | PATH : %APPDATA%\LuaRocks\bin | ||
801 | LUA_PATH : %APPDATA%\LuaRocks\share\lua\$LUA_VERSION\?.lua;%APPDATA%\LuaRocks\share\lua\$LUA_VERSION\?\init.lua | ||
802 | LUA_CPATH: %APPDATA%\LuaRocks\lib\lua\$LUA_VERSION\?.dll | ||
803 | System rocktree | ||
804 | PATH : $ROCKS_TREE\bin | ||
805 | LUA_PATH : $ROCKS_TREE\share\lua\$LUA_VERSION\?.lua;$ROCKS_TREE\share\lua\$LUA_VERSION\?\init.lua | ||
806 | LUA_CPATH: $ROCKS_TREE\lib\lua\$LUA_VERSION\?.dll | ||
721 | 807 | ||
722 | ]]) | 808 | ]]) |
723 | os.exit(0) | 809 | os.exit(0) |