-
Notifications
You must be signed in to change notification settings - Fork 5
/
touch.bat
122 lines (101 loc) · 3.21 KB
/
touch.bat
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
@echo off
setlocal enabledelayedexpansion
@rem only for interactive debugging !
set _DEBUG=0
@rem #########################################################################
@rem ## Environment setup
set _EXITCODE=0
call :env
if not %_EXITCODE%==0 goto end
@rem #########################################################################
@rem ## Main
for %%i in (%*) do (
set __FILE=%%i
if not exist "!__FILE!" (
for %%i in ("!__FILE!") do set "__PARENT_DIR=%%~dpi"
if %_DEBUG%==1 echo %_DEBUG_LABEL% __PARENT_DIR=!__PARENT_DIR! 1>&2
echo.> !__PARENT_DIR!!__FILE!
echo created file !__PARENT_DIR!!__FILE! 1>&2
)
call :touch_file "!__FILE!"
if not !_EXITCODE!==0 goto end
)
goto end
@rem #########################################################################
@rem ## Subroutines
:env
set _BASENAME=%~n0
set "_ROOT_DIR=%~dp0"
call :env_colors
set _DEBUG_LABEL=%_NORMAL_BG_CYAN%[%_BASENAME%]%_RESET%
set _ERROR_LABEL=%_STRONG_FG_RED%Error%_RESET%:
set _WARNING_LABEL=%_STRONG_FG_YELLOW%Warning%_RESET%:
@rem we use the newer PowerShell version if available
where /q pwsh.exe
if %ERRORLEVEL%==0 ( set _PWSH_CMD=pwsh.exe
) else ( set _PWSH_CMD=powershell.exe
)
goto :eof
:env_colors
@rem ANSI colors in standard Windows 10 shell
@rem see https://gist.github.com/mlocati/#file-win10colors-cmd
@rem normal foreground colors
set _NORMAL_FG_BLACK=[30m
set _NORMAL_FG_RED=[31m
set _NORMAL_FG_GREEN=[32m
set _NORMAL_FG_YELLOW=[33m
set _NORMAL_FG_BLUE=[34m
set _NORMAL_FG_MAGENTA=[35m
set _NORMAL_FG_CYAN=[36m
set _NORMAL_FG_WHITE=[37m
@rem normal background colors
set _NORMAL_BG_BLACK=[40m
set _NORMAL_BG_RED=[41m
set _NORMAL_BG_GREEN=[42m
set _NORMAL_BG_YELLOW=[43m
set _NORMAL_BG_BLUE=[44m
set _NORMAL_BG_MAGENTA=[45m
set _NORMAL_BG_CYAN=[46m
set _NORMAL_BG_WHITE=[47m
@rem strong foreground colors
set _STRONG_FG_BLACK=[90m
set _STRONG_FG_RED=[91m
set _STRONG_FG_GREEN=[92m
set _STRONG_FG_YELLOW=[93m
set _STRONG_FG_BLUE=[94m
set _STRONG_FG_MAGENTA=[95m
set _STRONG_FG_CYAN=[96m
set _STRONG_FG_WHITE=[97m
@rem strong background colors
set _STRONG_BG_BLACK=[100m
set _STRONG_BG_RED=[101m
set _STRONG_BG_GREEN=[102m
set _STRONG_BG_YELLOW=[103m
set _STRONG_BG_BLUE=[104m
@rem we define _RESET in last position to avoid crazy console output with type command
set _BOLD=[1m
set _UNDERSCORE=[4m
set _INVERSE=[7m
set _RESET=[0m
goto :eof
rem input parameter: %1=input file
:touch_file
set "__FILE=%~1"
set __PS1_SCRIPT= ^
if (%_DEBUG% -eq 1) { Get-ItemProperty '%__FILE%' ^| Select LastWriteTime } ^
[System.IO.Directory]::SetLastWriteTime('%__FILE%', (Get-Date)); ^
if (%_DEBUG% -eq 1) { Get-ItemProperty '%__FILE%' ^| Select LastWriteTime }
if %_DEBUG%==1 echo %_DEBUG_LABEL% call "%_PWSH_CMD%" -c "..." 1>&2
call "%_PWSH_CMD%" -c "%__PS1_SCRIPT%"
if not %ERRORLEVEL%==0 (
echo %_ERROR_LABEL% Failed to execute ps1 cmdlet 1>&2
set _EXITCODE=1
goto :eof
)
goto :eof
@rem #########################################################################
@rem ## Cleanups
:end
if %_DEBUG%==1 echo %_DEBUG_LABEL% _EXITCODE=%_EXITCODE% 1>&2
exit /b %_EXITCODE%
endlocal