Skip to content

Commit 3a25a0e

Browse files
committed
Isolate LuaRocks trees
-The environmental variable `%APPDATA%` is now set to `%APPDATA%\LJ4W` to ensure LuaRocks isolation and prevent conflicts with pre-existing LuaRocks trees
1 parent 7f7de5d commit 3a25a0e

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

LuaJIT-For-Windows.cmd

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ rem https://github.com/ScriptTiger/LuaJIT-For-Windows
88
rem =====
99

1010
rem Write lj4w.config if does not exist
11-
if not exist "%APPDATA%\LuaRocks" md "%APPDATA%\LuaRocks"
12-
if not exist "%APPDATA%\LuaRocks\lj4w.txt" (
11+
if not exist "%APPDATA%\LJ4W" md "%APPDATA%\LJ4W"
12+
if not exist "%APPDATA%\LJ4W\lj4w.txt" (
1313
(
1414
echo default.all.interpreter=lj4w
1515
echo default.lua.interpreter=lua
1616
echo default.luajit.interpreter=lj4w
1717
echo lj4w.interpreter=^"%~0^"
18-
)>"%APPDATA%\LuaRocks\lj4w.txt"
18+
) > "%APPDATA%\LJ4W\lj4w.txt"
1919
)
2020

2121
rem If the LJ4W_INTERPRETER_PATH environmental variable is already set, launch directly
@@ -59,24 +59,24 @@ rem Clean up interpreter name
5959
set LJ4W_INTERPRETER=%LJ4W_INTERPRETER:~15%
6060
:launch_comment_header
6161
rem Check if the header is valid, report error and exit if not
62-
findstr /b /i %LJ4W_INTERPRETER%.interpreter "%APPDATA%\LuaRocks\lj4w.txt" > nul || (
62+
findstr /b /i %LJ4W_INTERPRETER%.interpreter "%APPDATA%\LJ4W\lj4w.txt" > nul || (
6363
echo No "%LJ4W_INTERPRETER%" interpreter configured!
6464
exit /b
6565
)
6666
rem Look up path for requested interpreter
67-
for /f "tokens=2 delims==" %%0 in ('findstr /b /i %LJ4W_INTERPRETER%.interpreter "%APPDATA%\LuaRocks\lj4w.txt"') do (
67+
for /f "tokens=2 delims==" %%0 in ('findstr /b /i %LJ4W_INTERPRETER%.interpreter "%APPDATA%\LJ4W\lj4w.txt"') do (
6868
set LJ4W_INTERPRETER_PATH=%%~0
6969
goto launch
7070
)
7171

7272
:default.all.interpreter
7373
rem Look up path for default interpreter and launch
74-
for /f "tokens=2 delims==" %%0 in ('findstr /b /i default.all.interpreter "%APPDATA%\LuaRocks\lj4w.txt"') do (
75-
findstr /b /i %%0.interpreter "%APPDATA%\LuaRocks\lj4w.txt" > nul || (
74+
for /f "tokens=2 delims==" %%0 in ('findstr /b /i default.all.interpreter "%APPDATA%\LJ4W\lj4w.txt"') do (
75+
findstr /b /i %%0.interpreter "%APPDATA%\LJ4W\lj4w.txt" > nul || (
7676
echo The "default.all.interpreter" is misconfigured!
7777
exit /b
7878
)
79-
for /f "tokens=2 delims==" %%a in ('findstr /b /i %%0.interpreter "%APPDATA%\LuaRocks\lj4w.txt"') do (
79+
for /f "tokens=2 delims==" %%a in ('findstr /b /i %%0.interpreter "%APPDATA%\LJ4W\lj4w.txt"') do (
8080
set LJ4W_INTERPRETER=%%0
8181
set LJ4W_INTERPRETER_PATH=%%~a
8282
goto launch
@@ -85,12 +85,12 @@ for /f "tokens=2 delims==" %%0 in ('findstr /b /i default.all.interpreter "%APPD
8585

8686
:default.luajit.interpreter
8787
rem Look up path for default LuaJIT interpreter and launch
88-
for /f "tokens=2 delims==" %%0 in ('findstr /b /i default.luajit.interpreter "%APPDATA%\LuaRocks\lj4w.txt"') do (
89-
findstr /b /i %%0.interpreter "%APPDATA%\LuaRocks\lj4w.txt" > nul || (
88+
for /f "tokens=2 delims==" %%0 in ('findstr /b /i default.luajit.interpreter "%APPDATA%\LJ4W\lj4w.txt"') do (
89+
findstr /b /i %%0.interpreter "%APPDATA%\LJ4W\lj4w.txt" > nul || (
9090
echo The "default.luajit.interpreter" is misconfigured!
9191
exit /b
9292
)
93-
for /f "tokens=2 delims==" %%a in ('findstr /b /i %%0.interpreter "%APPDATA%\LuaRocks\lj4w.txt"') do (
93+
for /f "tokens=2 delims==" %%a in ('findstr /b /i %%0.interpreter "%APPDATA%\LJ4W\lj4w.txt"') do (
9494
set LJ4W_INTERPRETER=%%0
9595
set LJ4W_INTERPRETER_PATH=%%~a
9696
goto launch
@@ -99,12 +99,12 @@ for /f "tokens=2 delims==" %%0 in ('findstr /b /i default.luajit.interpreter "%A
9999

100100
:default.lua.interpreter
101101
rem Look up path for default Lua interpreter and launch
102-
for /f "tokens=2 delims==" %%0 in ('findstr /b /i default.lua.interpreter "%APPDATA%\LuaRocks\lj4w.txt"') do (
103-
findstr /b /i %%0.interpreter "%APPDATA%\LuaRocks\lj4w.txt" > nul || (
102+
for /f "tokens=2 delims==" %%0 in ('findstr /b /i default.lua.interpreter "%APPDATA%\LJ4W\lj4w.txt"') do (
103+
findstr /b /i %%0.interpreter "%APPDATA%\LJ4W\lj4w.txt" > nul || (
104104
echo The "default.lua.interpreter" is misconfigured!
105105
exit /b
106106
)
107-
for /f "tokens=2 delims==" %%a in ('findstr /b /i %%0.interpreter "%APPDATA%\LuaRocks\lj4w.txt"') do (
107+
for /f "tokens=2 delims==" %%a in ('findstr /b /i %%0.interpreter "%APPDATA%\LJ4W\lj4w.txt"') do (
108108
set LJ4W_INTERPRETER=%%0
109109
set LJ4W_INTERPRETER_PATH=%%~a
110110
goto launch
@@ -139,9 +139,9 @@ set LUADIR=%~dp0
139139
set LUADIR=%LUADIR:~,-1%
140140

141141
rem Set paths
142-
set PATH=%LUADIR%\mingw\bin;%LUADIR%\lib;%LUADIR%\bin;%APPDATA%\LuaRocks\bin;%PATH%
143-
set LUA_PATH=%LUADIR%\lua\?.lua;%LUADIR%\lua\?\init.lua;%APPDATA%\LuaRocks\share\lua\5.1\?.lua;%APPDATA%\LuaRocks\share\lua\5.1\?\init.lua;%LUA_PATH%
144-
set LUA_CPATH=%APPDATA%\LuaRocks\lib\lua\5.1\?.dll;%LUA_CPATH%
142+
set PATH=%LUADIR%\mingw\bin;%LUADIR%\lib;%LUADIR%\bin;%APPDATA%\LJ4W\LuaRocks\bin;%PATH%
143+
set LUA_PATH=%LUADIR%\lua\?.lua;%LUADIR%\lua\?\init.lua;%APPDATA%\LJ4W\LuaRocks\share\lua\5.1\?.lua;%APPDATA%\LJ4W\LuaRocks\share\lua\5.1\?\init.lua;%LUA_PATH%
144+
set LUA_CPATH=%APPDATA%\LJ4W\LuaRocks\lib\lua\5.1\?.dll;%LUA_CPATH%
145145

146146
rem If arguments are being sent, pass to LuaJIT and exit
147147
if not "%1"=="" (
@@ -150,8 +150,10 @@ if not "%1"=="" (
150150
)
151151

152152
rem Prepare command prompt environment
153+
set APPDATA=%APPDATA%\LJ4W
154+
if not exist "%APPDATA%\LuaRocks" md "%APPDATA%\LuaRocks"
153155
if not exist "%APPDATA%\LuaRocks\default-lua-version.lua" echo return "5.1">"%APPDATA%\LuaRocks\default-lua-version.lua"
154-
if not exist "%APPDATA%\LuaRocks\config-5.1.lua" luarocks config > "%APPDATA%\LuaRocks\config-5.1.lua"
156+
if not exist "%APPDATA%\LuaRocks\config-5.1.lua" call luarocks config > "%APPDATA%\LuaRocks\config-5.1.lua"
155157

156158
rem Command prompt
157159
echo ***** LuaJIT For Windows *****

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ LuaJIT for Windows is designed with portability and flexibility in mind. Once th
2525
The `LuaJIT-For-Windows.cmd` acts as both a Lua script launcher and development environment all in one and allows you to seamlessly run different implementations of Lua all from the same script. In fact, you can even set `LuaJIT-For-Windows.cmd` as the default application to open any file with a `.lua`, `.luac`, or other Lua file extension and it will automatically launch it using your pre-configured settings. At the same time, because of the ability `LuaJIT-For-Windows.cmd` has to read file headers, there is also no longer any need to have multiple file extensions to maintain Lua scripts for specific Lua implementations, which can oftentimes just cause problems when sharing Lua scripts with others who have different file associations from you on their system.
2626

2727
**`LuaJIT-For-Windows.cmd` as a Lua Script Launcher**
28-
When you first run `LuaJIT-For-Windows.cmd`, the `lj4w.txt` will be created with some default preferences located in the LuaRocks tree in the `%APPDATA%\LuaRocks` directory (i.e. `C:\Users\YourName\AppData\Roaming\LuaRocks`). The three pre-configured keys defined are `default.all.interpreter`, `default.lua.interpreter`, and `default.luajit.interpreter`.
28+
When you first run `LuaJIT-For-Windows.cmd`, the `lj4w.txt` will be created with some default preferences in the `%APPDATA%\LJ4W` directory (i.e. `C:\Users\YourName\AppData\Roaming\LJ4W`). The three pre-configured keys defined are `default.all.interpreter`, `default.lua.interpreter`, and `default.luajit.interpreter`.
2929

3030
While their values can be changed, and further custom key-value pairs entered, these three default keys must always be present. The `default.all.interpreter` is used to determine which Lua implementation to use by default for any script without a header designating which implementation to use. The `default.lua.interpreter` is used to determine which Lua implementation to use when executing Lua bytecode generated from Luac. And `default.luajit.interpreter` is used to determine which LuaJIT implementation to use when executing LuaJIT bytecode. While both the `default.all.interpreter` and `default.luajit.interpreter` start off with the `lj4w` value and associated `lj4w.interpreter` key defining the full path to the `LuaJIT-For-Windows.cmd`, `default.lua.interpreter` is pre-configured with the `lua` value but no associated `lua.interpreter` defining its path. Therefore, you must add the full path of your `lua.exe` to the `lua.interpreter` value, as an example, in order to use this feature. Otherwise, if `lua.interpreter` is left undefined, or whatever value you choose to associate with `default.lua.interpreter` (i.e. `foo` value and associated `foo.interpreter` key), you will encounter an error when attempting to execute Luac bytecode.
3131

@@ -38,6 +38,8 @@ For maximum flexibility, the `LJ4W_INTERPRETER` and/or `LJ4W_INTERPRETER_PATH` e
3838
**`LuaJIT-For-Windows.cmd` as a Development Environment**
3939
When launching `LuaJIT-For-Windows.cmd` as a stand-alone script without any arguments, it will open a development environment where you will have access to all of the tools available within the LuaJIT For Windows package. So, whether you want to access LuaRocks to manage packages, access LuaJIT's REPL interface, access gcc or any of the other tools within the LLVM MinGW suite, you can do so immediately all from the same environment.
4040

41+
An important note for the development environment is that the `APPDATA` environmental variable is changed to `%APPDATA%\LJ4W` during a session in order to ensure that LuaRocks and all of its components remain isolated from any pre-existing LuaRocks trees which may be present. This change is only in effect while using the development environment and does not affect scripts which are directly launched via the script launcher functionality.
42+
4143
For more ScriptTiger scripts and goodies, check out ScriptTiger's GitHub Pages website:
4244
https://scripttiger.github.io/
4345

0 commit comments

Comments
 (0)