Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 7 additions & 21 deletions mirai.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,18 @@
rem Lets PowerShell and cmd find ./mirai: runs the bash launcher with Git Bash. The path goes with
rem forward slashes because the launcher finds the engine root from the slashes of $0.
setlocal
set "bash=%ProgramFiles%\Git\bin\bash.exe"
if exist "%bash%" goto run
set "bash=%ProgramFiles(x86)%\Git\bin\bash.exe"
if exist "%bash%" goto run
set "bash=%LocalAppData%\Programs\Git\bin\bash.exe"
if exist "%bash%" goto run
if defined GIT_INSTALL_ROOT set "bash=%GIT_INSTALL_ROOT%\bin\bash.exe"
if exist "%bash%" goto run
rem Git for Windows installed elsewhere: git.exe in PATH names its root.
rem Git for Windows is found by its git, as tools/mirai finds it: git --exec-path names
rem <root>\mingw64\libexec\git-core, and its bash is <root>\bin\bash.exe.
set "bash="
for /f "delims=" %%i in ('git --exec-path 2^>nul') do call :from_exec "%%i"
if defined bash goto run
goto missing

:from_exec
for %%j in ("%~1\..\..\..") do (
if exist "%%~fj\bin\bash.exe" set "bash=%%~fj\bin\bash.exe"
if not defined bash if exist "%%~fj\usr\bin\bash.exe" set "bash=%%~fj\usr\bin\bash.exe" & set "bashopt=-l"
)
goto :eof
for /f "delims=" %%i in ('git --exec-path 2^>nul') do set "bash=%%~fi\..\..\..\bin\bash.exe"
if not defined bash goto missing
if not exist "%bash%" goto missing

:run
set "launcher=%~dp0mirai"
"%bash%" %bashopt% "%launcher:\=/%" %*
"%bash%" "%launcher:\=/%" %*
exit /b %errorlevel%

:missing
echo mirai: Git Bash is missing; install Git for Windows or run ./mirai from bash 1>&2
echo mirai: Git Bash is missing; install Git for Windows with git on the PATH, or run ./mirai from bash 1>&2
exit /b 1
16 changes: 11 additions & 5 deletions tools/mirai/shell.odin
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@ package main
import "core:fmt"
import "core:os"
import "core:strings"
import "mirai:std/subprocess"

// The shell that runs the scripts of the repository: Git Bash on Windows, by the path mirai.cmd
// uses (the bash of the PATH is usually the one of WSL, which has no odin), sh elsewhere.
// The shell that runs the scripts of the repository: Git Bash on Windows, found as mirai.cmd finds
// it, through git --exec-path (the bash of the PATH is usually the one of WSL, which has no odin);
// sh elsewhere.
shell_executable :: proc() -> (path:string,found:bool) {
when ODIN_OS==.Windows {
program_files,_:=os.lookup_env("ProgramFiles",context.allocator)
bash:=join(program_files,"Git","bin","bash.exe")
state,exec,_,error:=subprocess.exec({command={"git","--exec-path"}},context.allocator)
if error!=nil || state.exit_code!=0 {return "git --exec-path",false}
bash:=git_bash(strings.trim_space(string(exec)))
return bash,os.is_file(bash)
} else {
return "sh",true
}
}

// The bash of the Git for Windows whose git --exec-path is exec: <root>/mingw64/libexec/git-core.
git_bash :: proc(exec:string) -> string {return join(exec,"..","..","..","bin","bash.exe")}

// command with the script it starts run by shell_executable, where the system cannot start one
// itself: on Windows, a file named with a path that begins with #! (./mirai).
// ok is false, after saying why, when the script needs a shell that is not there.
Expand All @@ -24,7 +30,7 @@ script_command :: proc(command:[]string) -> (resolved:[]string,ok:bool) {
if len(command)==0 || !strings.contains_any(command[0],"/\\") || !shebang(command[0]) {return command,true}
shell,found:=shell_executable()
if !found {
fmt.eprintfln("mirai: %s is an sh script and Git Bash is missing in %s; install Git for Windows",command[0],shell)
fmt.eprintfln("mirai: %s is an sh script and no Git Bash was found by %s; install Git for Windows with git on the PATH",command[0],shell)
return nil,false
}
list:=make([dynamic]string,0,len(command)+1)
Expand Down
12 changes: 12 additions & 0 deletions tools/mirai/shell_test.odin
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,15 @@ a_content_tool_runs_with_its_collections_and_its_arguments :: proc(t:^testing.T)
testing.expect(t,!slice.contains(alone[:],"--"),"nothing after it")
testing.expect_value(t,alone[2],"C:/game/tools/content")
}

// Git Bash is found from the root git --exec-path names, also through a shim such as Scoop's,
// whose git reports the folder it was installed in.
@(test)
git_bash_is_found_from_the_exec_path_of_git :: proc(t:^testing.T) {
context.allocator=context.temp_allocator
cases:=[?]struct{exec:string,root:string}{
{"C:/Program Files/Git/mingw64/libexec/git-core","C:/Program Files/Git"},
{"C:/Users/u/scoop/apps/git/current/mingw64/libexec/git-core","C:/Users/u/scoop/apps/git/current"},
}
for c in cases {testing.expect_value(t,git_bash(c.exec),join(c.root,"bin","bash.exe"))}
}
Loading