-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfix-file-style.sh
More file actions
executable file
·99 lines (81 loc) · 3.72 KB
/
Copy pathfix-file-style.sh
File metadata and controls
executable file
·99 lines (81 loc) · 3.72 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
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/bash
#==================================================================
# Check and fix file style.
# Latest version in: https://github.com/qualiu/msrTools
#==================================================================
ThisDir="$( cd "$( dirname "$0" )" && pwd )"
SYS_TYPE=$(uname -s | sed 's/_.*//g' | awk '{print tolower($0)}')
which msr 2>/dev/null 1>&2
if [ $? -ne 0 ]; then
sh $ThisDir/check-download-tools.sh
if [ $? -ne 0 ]; then
echo "Failed to call $ThisDir/check-download-tools.sh" >&2
exit -1
fi
fi
msr -z "LostArg$1" -t "^LostArg(|-h|--help|/\?)$" > /dev/null
if [ $? -ne 0 ]; then
echo "Usage : $0 Files-or-Directories [options]"
echo "Example: $0 my.cpp"
echo "Example: $0 \"my.cpp,my.ps1,my.bat\""
echo "Example: $0 directory-1"
echo "Example: $0 \"directory-1,file2,directory-3\""
echo "Example: $0 \"directory-1,file2,directory-3\" -r"
echo "Example: $0 \$PWD -r"
echo "Example: $0 . -r --nf \"\.(log|md|exe|cygwin|gcc\w*|txt)$\""
echo "Example: $0 . -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+(-\S+)|\w+"
exit -1
fi
PathToDo=$1
msrOptions=${@:2}
for ((k = 2; k <= $#; k++)) ; do
if [ "${!k}" = "-f" ]; then
hasFileFilter=1
fi
done
if [ -z "${msrOptions[@]}" ]; then
msrOptions=("--nd" "^\.git$")
else
msrOptions=(${msrOptions[@]} "--np" "[\\\\/]*(\.git)[\\\\/]")
fi
# if path has one directory, add file filter
if [ ! -f "$PathToDo" ]; then
if [ "$hasFileFilter" != "1" ]; then
FileFilter=("-f" "\.(c|cpp|cxx|h|hpp|cs|java|scala|py|bat|cmd|ps1|sh)$")
else
FileFilter=("--pp" "\.(c|cpp|cxx|h|hpp|cs|java|scala|py|bat|cmd|ps1|sh)$")
fi
fi
echo "## Remove all white spaces if it is a white space line" | msr -PA -e .+
msr ${msrOptions[@]} -p $PathToDo ${FileFilter[@]} -t "^\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[@]} -t "(\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 ${FileFilter[@]} -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.
echo "## Convert tab at head of each lines in a file, util all tabs are replaced." | msr -aPA -e .+
function ConvertTabTo4Spaces() {
if [ -d $PathToDo ]; then
msr ${msrOptions[@]} -p $PathToDo ${FileFilter[@]} -it "^(\s*)\t" -o '$1 ' -R -c Covert TAB to 4 spaces.
else
msr ${msrOptions[@]} -p $PathToDo ${FileFilter[@]} -it "^(\s*)\t" -o '$1 ' -R -c Covert TAB to 4 spaces.
fi
if (($? > 0)); then
ConvertTabTo4Spaces
fi
}
ConvertTabTo4Spaces
echo "## Convert line ending style from CR LF to LF for Linux files" | msr -PA -e .+
FileFilterForLinuxLineEnding=("-f" "^makefile$|\.sh$|\.mak\w*$")
if [ "$hasFileFilter" = "1" ]; then
FileFilterForLinuxLineEnding=("--pp" "[\\\\/]*makefile$|\.sh$|\.mak\w*$")
fi
msr ${msrOptions[@]} -p $PathToDo ${FileFilterForLinuxLineEnding[@]} -l -PICc | msr -t ".+" -o 'dos2unix \"$0\"' -XA
echo "## Convert line ending style from LF to CR LF for Windows files" | msr -PA -e .+
FileFilterForWindowsLineEnding=("-f" "\.(bat|cmd|ps1)$")
if [ "$hasFileFilter" = "1" ]; then
FileFilterForWindowsLineEnding=("--pp" "\.(bat|cmd|ps1)$")
fi
msr ${msrOptions[@]} -p $PathToDo ${FileFilterForWindowsLineEnding[@]} -l -PICc | msr -t ".+" -o 'unix2dos \"$0\"' -XA