-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrr_impl.bat
More file actions
88 lines (69 loc) · 2.15 KB
/
Copy pathrr_impl.bat
File metadata and controls
88 lines (69 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
@echo off
setlocal
setlocal enabledelayedexpansion
:: We use the for-loop to tokenize the args in a way to preserve the
:: string contents of the args. 'Standard' arg syntax will remove
:: certain delimiter characters inside args (e.g. '=').
for /f "tokens=1* delims= " %%A in ("%*") do (
set COMMAND=%%A
set OPTIONS=%%B
)
:: Check for missing args
if not defined COMMAND (
echo Root Resolve
echo Usage: rr ^<command^> [options]
exit /b 1
)
:: Locate the root file
call :FindRootFilePath
if not defined ROOT_FILE_PATH (
echo ERROR: Can't find `rr.cfg`
exit /b 1
)
:: Directory for the root file
for %%a in (%ROOT_FILE_PATH%) do set ROOT_FILE_DIR=%%~dpa
:: List of executable extensions
:: First entry empty to allow for explicit extension specification
set EXTENSIONS[0]=
set EXTENSIONS[1]=.bat
set EXTENSIONS[2]=.cmd
set EXTENSIONS[3]=.exe
:: Parse the root file looking for paths
for /f %%a in (%ROOT_FILE_PATH%) do (
set COMMAND_DIR=%ROOT_FILE_DIR%%%a
:: Search for a valid path/command/extension mix
for /f "tokens=2 delims==" %%s in ('set EXTENSIONS[') do (
set COMMAND_PATH=!COMMAND_DIR!\%COMMAND%%%s
if exist !COMMAND_PATH! (
:: Launch and return the error code
if defined USE_START (
start !COMMAND_PATH! %OPTIONS%
) else (
call !COMMAND_PATH! %OPTIONS%
)
exit /b %ERRORLEVEL%
)
)
)
echo Command %COMMAND% not found
exit /b 1
:FindRootFilePath
:: Walk up parent directories lookup for root file
set SEARCH_PATH=%cd%
:WhileTrue
:: Exit search at drive root
if %SEARCH_PATH:~0,3% == %SEARCH_PATH% (
set ROOT_FILE_PATH=
exit /b
)
:: Check to see if the root file can be found at this level
set ROOT_FILE_PATH=%SEARCH_PATH%\rr.cfg
if exist %ROOT_FILE_PATH% (
exit /b
)
:: Hack to get drive and directory
for %%a in (%SEARCH_PATH%) do set SEARCH_PATH=%%~dpa
:: Remove any trailing slashes
if %SEARCH_PATH:~-1%==\ SET SEARCH_PATH=%SEARCH_PATH:~0,-1%
goto :WhileTrue
exit /b