-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathchecker.sh
executable file
·144 lines (129 loc) · 5.46 KB
/
checker.sh
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env bash
clear
########################################################################################################################
#
# Utility function to fix known troubles with production server
#
# You can run this command with parameters
#
########################################################################################################################
# install pre-push hook if it does not exists
hook='.git/hooks/pre-push'
if [ ! -f ${hook} ]; then
echo 'Install pre-push hook'
touch ${hook}
echo '#!/bin/bash' >> ${hook}
echo 'if [ $(git symbolic-ref HEAD | sed -e "s,.*/\(.*\),\1,") = "develop" ] && [[ ! $(git log -1 --pretty=%B) =~ "#without-check" ]] ' >> ${hook}
echo 'then . ./checker.sh; fi' >> ${hook}
chmod +x ${hook}
echo 'done'
fi
########################################################################################################################
# Define default variables
########################################################################################################################
CURRENT_BRANCH=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/');
# IGNORE files
declare -a IGNOREFILES=(
"./Maps/Map_Projections.md",
"./Basic_Charts/Sunburst_Chart.md",
"./Quick_Start/Quick_Start.md",
"./Working_with_Data/Data_Adapter/Parsing_HTML_Table.md",
"./samples/AS_Fonts_01.html"
"./samples/BCT_Sunburst_Chart_04.html"
"./samples/CS_Labels_10.html"
"./samples/CS_Preloader_01.html"
"./samples/Maps_Connectors_11.html"
"./samples/WD_Data_Adapter_HTML_Table_02.html"
"./samples/quick_start_pie.html"
)
# filter only files that diff with origin/develop
FILESLIST=$(git diff --name-only origin/develop | grep -e .html -e .md )
FILESLIST_COUNT=$(git diff --name-only origin/develop | grep -e .html -e .md | wc -l)
# function for "fix" file.
function heal_file(){
filename=$1
# replace all STG urls to COM and non-{{branch-name}}
perl -pi -e 's,src="../anychart,src="https://cdn.anychart.com/releases/d/js/anychart,g' ${filename}
perl -pi -e 's/\.stg/\.com/g' ${filename}
perl -pi -e 's,(releases)/([^/])+/,\1/\{\{branch-name\}\}/,g' ${filename}
if [[ ! " ${IGNOREFILES[*]} " == *"$filename"* ]]; then
# match all non-ascii symbols
# Search charcode by symbol here https://www.compart.com/en/unicode/search?q=
# Search symbol by hex code here https://utf8-chartable.de/unicode-utf8-table.pl?start=8192&number=128&utf8=string-literal
match=$(cat ${filename} | tr -d '\r' | tr -d '\n' | sed 's/\xC2\xA0/+/g' | \
sed -e "s/'//g;s/[\s\t]/ /g" | sed -e 's/[0-9A-z"*+-=()/&!?.,:;$<>#{}%~|@ ]//g' | \
awk '{$1=$1}1' )
if [ ! ${#match} -eq 0 ]; then
res=""
for i in $(seq 1 ${#match});do
char=${match:i-1:1}
res="$res $char($(echo ${char} | tr -d '\n' | xxd -u -p | sed 's/\(..\)/\\x\1/g' ))"
done
echo "[Non-ASCII] $filename: $res"
# echo ${match:0:1} | tr -d '\n' | xxd -ps -c 200
fi
fi
}
# default mode
FILE_MODIFYER="heal_file"
# sugar function
function broke_file(){
perl -pi -e "s,(releases)/(\{\{branch-name\}\})+/,\1/$CURRENT_BRANCH/,g" $1
}
########################################################################################################################
# Main functionality
########################################################################################################################
# read command arguments
for ARGUMENT in "$@"
do
case "$ARGUMENT" in
replace|r|"-r") FILE_MODIFYER="broke_file" ;;
all|a|"-a")
FILESLIST=$(find . -type f | grep -e .md -e .html)
FILESLIST_COUNT=$(find . -type f | grep -e .md -e .html | wc -l)
;;
"-h"|"--help"|help|h|"-help") printf "parameters: \
\n 'replace (-r)' - to rename all {{branch-name}} to current branch\
\n 'all (-a)' - modify all files (by default False, modify only diff with origin/develop)\
\n 'links (-l)' - get links to pg and github.com for changed samples\
\n" && exit 1 ;;
links|link|l|"-l")
for filename in ${FILESLIST}; do
fileext=${filename:${#filename}-4}
if [[ "$fileext" = "html" ]]; then
pglink="http://playground.anychart.stg/docs/$CURRENT_BRANCH/${filename:0:${#filename}-5}\n\t"
fi
printf "\n*$filename*\n\t${pglink}https://github.com/AnyChart/docs.anychart.com/blob/$CURRENT_BRANCH/$filename\n"
done
exit 0
;;
*)
esac
done
echo 'Starting check...'
echo
echo "Items to check: "
echo $FILESLIST
echo "Modifier: ${FILE_MODIFYER}"
echo "COUNT: ${FILESLIST_COUNT}"
echo
nr=0
# for each files in tree of folders do (like python walk)
for filename in ${FILESLIST}; do
(( ++nr ))
# in diff mode file may be marked as deleted
if [ -f $filename ];then
echo -ne "${nr} / ${FILESLIST_COUNT} > ${filename} \r"
${FILE_MODIFYER} ${filename}
fi
done
# cause echo -ne previous
echo
CHANGES=$(git diff --name-only)
if [ "$CHANGES" ] && [[ "${FILE_MODIFYER}"=~"heal_file" ]]; then
echo
echo 'ABORTED! Files were modified (autofixed). Check them before doing the commit, please.'
echo ' git status'
exit 1
fi
exit 0