-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfix-file-style.bat
103 lines (87 loc) · 4.48 KB
/
fix-file-style.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
::=======================================================
:: Check and fix file style.
:: Latest version in: https://github.com/qualiu/msrTools/
::=======================================================
@if "%PATH:~-1%" == "\" set "PATH=%PATH:~0,-1%"
@echo off
SetLocal EnableExtensions EnableDelayedExpansion
where msr.exe 2>nul >nul || if not exist %~dp0\msr.exe powershell -Command "$ProgressPreference = 'SilentlyContinue'; [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -Uri https://github.com/qualiu/msr/blob/master/tools/msr.exe?raw=true -OutFile %~dp0\msr.exe"
where msr.exe 2>nul >nul || set "PATH=%PATH%;%~dp0"
msr -z "X%~1" -it "^X(|-h|--help|/\?)$" >nul
if !ERRORLEVEL! EQU 1 (
echo Usage : %~n0 Files-or-Directories [options]
echo Example: %~n0 my.cpp
echo Example: %~n0 "my.cpp,my.ps1,my.bat"
echo Example: %~n0 directory-1
echo Example: %~n0 "directory-1,file2,directory-3"
echo Example: %~n0 "directory-1,file2,directory-3" -r
echo Example: %~n0 %%CD%% -r
echo Example: %~n0 . -r --nf "\.(log|md|exe|cygwin|gcc\w*|txt)$"
echo Example: %~n0 . -r -f "\.(bat|cmd|ps1|sh)$" --nd "^(target|bin)$"
echo Should not use --np and --pp as used by this; and -p also used. | msr -PA -t "(-\S+)|\w+"
exit /b -1
)
set PathToDo=%1
shift
:: Begin to get other arguments, save to msrOptions
set /a hasFileFilter=0
:CheckArgs
if "%~1" == "" goto CheckArgsCompleted
if "%~1" == "-f" set /a hasFileFilter=1
set msrOptions=!msrOptions! %1
shift
goto CheckArgs
:CheckArgsCompleted
:: if not defined msrOptions set set msrOptions=--nd "^\.git$" -f "\.(hp*|cp*|cx*|cs|ps1|bat|cmd)$"
if not defined msrOptions (
set msrOptions=--nd "^\.git$"
) else (
set msrOptions=!msrOptions! --np "[\\\\/]*(\.git)[\\\\/]"
)
:: if path has one directory, add file filter
if exist %PathToDo%\* (
if !hasFileFilter! NEQ 0 (
set FileFilter=--pp "\.(c|cpp|cxx|h|hpp|cs|java|scala|py|bat|cmd|ps1|sh)$"
) else (
set FileFilter=-f "\.(c|cpp|cxx|h|hpp|cs|java|scala|py|bat|cmd|ps1|sh)$"
)
)
@echo ## Remove all white spaces if it is a white space line | msr -PA -e .+
msr !msrOptions! -p %PathToDo% !FileFilter! -it "^\s+$" -o "" -R -c Remove all white spaces if it is a white space line.
@echo ## Remove white spaces at each line end | msr -PA -e .+
msr !msrOptions! -p %PathToDo% !FileFilter! -it "(\S+)\s+$" -o "$1" -R -c Remove white spaces at each line end.
::@echo ## Add a tail new line to files | msr -PA -e .+
::msr !msrOptions! -p %PathToDo% -S -t "(\S+)$" -o "$1\n" -R -c Add a tail new line to files.
@echo ## Add/Delete to have only one tail new line in files | msr -PA -e .+
msr !msrOptions! -p %PathToDo% !FileFilter! -S -t "(\S+)\s*$" -o "$1\n" -R -c Add a tail new line to files.
:: Convert tab at head of each lines in a file, util all tabs are replaced.
echo !FileFilter! | msr -t "(^|\s+)--nf\s+" >nul
if !ERRORLEVEL! EQU 0 (
set SkipConvert4TabForMakeFile=--nf "^makefile$|\.mak\w*$"
) else (
echo !FileFilter! | msr -t "(^|\s+)--np\s+" >nul
if !ERRORLEVEL! EQU 0 (
set SkipConvert4TabForMakeFile=--np "(^|[\\/])(makefile|\.mak\w*)$"
)
)
:ConvertTabTo4Spaces
if exist %PathToDo%\* (
msr !msrOptions! -p %PathToDo% !FileFilter! !SkipConvert4TabForMakeFile! -it "^^(\s*)\t" -o "$1 " -R -g -1 -c Covert TAB to 4 spaces.
) else (
msr !msrOptions! -p %PathToDo% !SkipConvert4TabForMakeFile! -it "^^(\s*)\t" -o "$1 " -R -g -1 -c Covert TAB to 4 spaces.
)
REM if !ERRORLEVEL! GTR 0 goto :ConvertTabTo4Spaces else exit /b 0
where dos2unix >nul 2>nul
if !ERRORLEVEL! EQU 0 (
@echo ## Convert line ending style from CR LF to LF for Linux files | msr -PA -e .+
set FileFilterForLinuxLineEnding=-f "^makefile$|\.sh$|\.mak\w*$"
if !hasFileFilter! NEQ 0 set FileFilterForLinuxLineEnding=--pp "[\\\\/]*makefile$|\.sh$|\.mak\w*$"
msr !msrOptions! -p %PathToDo% !FileFilterForLinuxLineEnding! -l -PICc | msr -t ".+" -o "dos2unix \"$0\"" -XA
)
where unix2dos >nul 2>nul
if !ERRORLEVEL! EQU 0 (
@echo ## Convert line ending style from LF to CR LF for Windows files | msr -PA -e .+
set FileFilterForWindowsLineEnding=-f "\.(bat|cmd|ps1)$"
if !hasFileFilter! NEQ 0 set FileFilterForWindowsLineEnding=--pp "\.(bat|cmd|ps1)$"
msr !msrOptions! -p %PathToDo% !FileFilterForWindowsLineEnding! -l -PICc | msr -t ".+" -o "unix2dos \"$0\"" -XA
)