aboutsummaryrefslogtreecommitdiff
path: root/install.bat
diff options
context:
space:
mode:
Diffstat (limited to 'install.bat')
-rw-r--r--install.bat166
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
7vars.PREFIX = [[C:\LuaRocks]] 7vars.PREFIX = [[C:\LuaRocks]]
8vars.VERSION = "2.1" 8vars.VERSION = "2.1"
9vars.SYSCONFDIR = [[C:\LuaRocks]] 9vars.SYSCONFDIR = nil
10vars.ROCKS_TREE = [[C:\LuaRocks]] 10vars.ROCKS_TREE = nil
11vars.SCRIPTS_DIR = nil 11vars.SCRIPTS_DIR = nil
12vars.LUA_INTERPRETER = nil 12vars.LUA_INTERPRETER = nil
13vars.LUA_PREFIX = nil 13vars.LUA_PREFIX = nil
@@ -27,6 +27,7 @@ local FORCE_CONFIG = false
27local INSTALL_LUA = false 27local INSTALL_LUA = false
28local USE_MINGW = false 28local USE_MINGW = false
29local REGISTRY = false 29local REGISTRY = false
30local 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]])
83end 84end
84 85
86-- does the current user have admin priviledges ( = elevated)
87local function permission()
88 return exec("net session >nul 2>&1") -- fails if not admin
89end
90
91-- rename file (full path) to backup (name only), appending number if required
92-- returns the new name (name only)
93local 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
103end
104
85-- interpolate string with values from 'vars' table 105-- interpolate string with values from 'vars' table
86local function S (tmpl) 106local 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]])
141end 166end
@@ -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
412end 437end
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
416local config = {} 445local config = {}
417local with_arg = { -- options followed by an argument, others are flags 446local 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
459local oarg = arg -- retain old table
430if #arg > 0 then 460if #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
450end 480end
481for k,v in pairs(oarg) do if k < 1 then arg[k] = v end end -- copy 0 and negative indexes
482oarg = nil
451 483
484-- build config option table with name and value elements
452local i = 1 485local i = 1
453while i <= #arg do 486while i <= #arg do
454 local opt = arg[i] 487 local opt = arg[i]
@@ -470,6 +503,36 @@ print(S"LuaRocks $VERSION.x installer.\n")
470parse_options(config) 503parse_options(config)
471check_flags() 504check_flags()
472 505
506if 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
530else
531 print("Admin priviledges available for installing")
532end
533
534vars.SYSCONFDIR = vars.SYSCONFDIR or vars.PREFIX
535vars.ROCKS_TREE = vars.ROCKS_TREE or vars.PREFIX
473vars.FULL_PREFIX = S"$PREFIX\\$VERSION" 536vars.FULL_PREFIX = S"$PREFIX\\$VERSION"
474vars.BINDIR = vars.FULL_PREFIX 537vars.BINDIR = vars.FULL_PREFIX
475vars.LIBDIR = vars.FULL_PREFIX 538vars.LIBDIR = vars.FULL_PREFIX
@@ -599,8 +662,13 @@ else
599end 662end
600 663
601 664
665-- ***********************************************************
666-- Configure LuaRocks
667-- ***********************************************************
668
602print() 669print()
603print("Configuring LuaRocks...") 670print("Configuring LuaRocks...")
671
604-- Create a site-config file 672-- Create a site-config file
605if exists(S[[$LUADIR\luarocks\site_config.lua]]) then 673if 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"
644if not exists(vars.SYSCONFDIR) then 712if not exists(vars.SYSCONFDIR) then
645 mkdir(vars.SYSCONFDIR) 713 mkdir(vars.SYSCONFDIR)
646end 714end
647if not exists(vars.CONFIG_FILE) then 715if 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("***************")
720end
721local f = io.open(vars.CONFIG_FILE, "w")
722f:write([=[
650rocks_servers = { 723rocks_servers = {
651 [[http://luarocks.org/repositories/rocks]] 724 [[http://luarocks.org/repositories/rocks]]
652} 725}
653rocks_trees = { 726rocks_trees = {
654]=]) 727]=])
655 if FORCE_CONFIG then 728if FORCE_CONFIG then
656 f:write(" home..[[/luarocks]],\n") 729 f:write(" home..[[/luarocks]],\n")
657 end 730end
658 f:write(S" [[$ROCKS_TREE]]\n") 731f:write(S" [[$ROCKS_TREE]]\n")
659 f:write("}\n") 732f:write("}\n")
660 if vars.SCRIPTS_DIR then 733if vars.SCRIPTS_DIR then
661 f:write(S"scripts_dir=[[$SCRIPTS_DIR]]\n") 734 f:write(S"scripts_dir=[[$SCRIPTS_DIR]]\n")
662 end 735end
663 f:write("variables = {\n") 736f:write("variables = {\n")
664 if USE_MINGW and vars.LUA_RUNTIME == "MSVCRT" then 737if 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")
673else 739else
674 print(S"LuaRocks config file already exists: $CONFIG_FILE") 740 f:write(" MSVCRT = '"..vars.LUA_RUNTIME.."',\n")
675end 741end
742f:write(S" LUALIB = '$LUA_LIBNAME'\n")
743f:write("}\n")
744f:write("verbose = false -- set to 'true' to enable verbose output\n")
745f:close()
746
747print(S"Created LuaRocks config file: $CONFIG_FILE")
748
676 749
677print() 750print()
678print("Creating rocktrees...") 751print("Creating rocktrees...")
679if not exists(vars.ROCKS_TREE) then 752if 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"]])
682else 755else
683 print(S[[Rocktree exists: "$ROCKS_TREE"]]) 756 print(S[[System rocktree exists : "$ROCKS_TREE"]])
684end 757end
685local APPDATA = os.getenv("APPDATA") 758
686if not exists(APPDATA.."\\luarocks") then 759vars.LOCAL_TREE = os.getenv("APPDATA")..[[\LuaRocks]]
687 mkdir(APPDATA.."\\luarocks") 760if 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"]])
689else 763else
690 print([[Rocktree exists: "]]..APPDATA..[[\luarocks"]]) 764 print(S[[Local user rocktree exists : "$LOCAL_TREE"]])
691end 765end
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
714print(S[[ 788print(S[[
789
715*** LuaRocks is installed! *** 790*** LuaRocks is installed! ***
716 791
717 You may want to add the following elements to your paths; 792You may want to add the following elements to your paths;
718PATH : $LUA_BINDIR;$FULL_PREFIX 793Lua interpreter;
719LUA_PATH : $ROCKS_TREE\share\lua\$LUA_VERSION\?.lua;$ROCKS_TREE\share\lua\$LUA_VERSION\?\init.lua 794 PATH : $LUA_BINDIR
720LUA_CPATH: $ROCKS_TREE\lib\lua\$LUA_VERSION\?.dll 795 PATHEXT : .LUA
796LuaRocks;
797 PATH : $FULL_PREFIX
798 LUA_PATH : $FULL_PREFIX\lua\?.lua;$FULL_PREFIX\lua\?\init.lua
799Local 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
803System 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]])
723os.exit(0) 809os.exit(0)