-
Notifications
You must be signed in to change notification settings - Fork 4
/
vendor.sh
executable file
·134 lines (115 loc) · 4.39 KB
/
vendor.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
#!/bin/bash
# Script to update tree-sitter and grammars
set -e
sitter_version=0.15.14
grammars=(
"bash;v0.16.0;parser.c;scanner.cc"
"c-sharp;v0.15.0;parser.c"
"c;v0.15.3;parser.c"
"cpp;v0.15.1;parser.c;scanner.cc"
"go;v0.15.1;parser.c"
"java;v0.15.0;parser.c"
"javascript;v0.15.1;parser.c;scanner.c"
"php;v0.13.1;parser.c;scanner.cc"
"python;v0.15.1;parser.c;scanner.cc"
"ruby;v0.15.3;parser.c;scanner.cc"
"rust;v0.15.2;parser.c;scanner.c"
"typescript;v0.15.1"
)
function download_sitter() {
rm -rf vendor
git clone -b $1 https://github.com/tree-sitter/tree-sitter.git vendor
sed -i.bak 's/"tree_sitter\//"/g' vendor/lib/src/*.c vendor/lib/src/*.h
sed -i.bak 's/"unicode\//"/g' vendor/lib/src/unicode/*.h vendor/lib/src/*.h
# tree-sitter 0.15.13 misses include, might be fixed in newer version
echo "#include \"unicode.h\"" | cat - vendor/lib/src/query.c > /tmp/out && mv /tmp/out vendor/lib/src/query.c
cp vendor/lib/include/tree_sitter/*.h ./
cp vendor/lib/src/*.c ./
cp vendor/lib/src/*.h ./
cp vendor/lib/src/unicode/*.h ./
rm -rf vendor
# avoid "duplicate symbols" errors as go compiles all c files separately
rm ./lib.c
}
function download_grammar() {
lang=$1; shift
version=$1; shift
files=$@
target=$lang
if [ "$lang" == "go" ]; then
target="golang"
fi
if [ "$lang" == "c-sharp" ]; then
target="csharp"
fi
mkdir -p "$target"
echo "downloading $lang $version"
curl -s -f -S "https://raw.githubusercontent.com/tree-sitter/tree-sitter-$lang/$version/src/tree_sitter/parser.h" -o "$target/parser.h"
for file in $files; do
curl -s -f -S "https://raw.githubusercontent.com/tree-sitter/tree-sitter-$lang/$version/src/$file" -o "$target/$file"
sed -i.bak 's/<tree_sitter\/parser\.h>/"parser\.h"/g' "$target/$file"
rm "$target/$file.bak"
done
}
# typescript is special as it contains 2 different grammars
function download_typescript() {
version=$1; shift
langs="typescript tsx"
files="parser.c scanner.c"
echo "downloading typescript $version"
for lang in $langs; do
curl -s -f -S "https://raw.githubusercontent.com/tree-sitter/tree-sitter-typescript/$version/common/scanner.h" -o "typescript/$lang/scanner.h"
curl -s -f -S "https://raw.githubusercontent.com/tree-sitter/tree-sitter-typescript/$version/$lang/src/tree_sitter/parser.h" -o "typescript/$lang/parser.h"
for file in $files; do
curl -s -f -S "https://raw.githubusercontent.com/tree-sitter/tree-sitter-typescript/$version/$lang/src/$file" -o "typescript/$lang/$file"
sed -i.bak 's/"\.\.\/\.\.\/common\/scanner\.h"/"scanner\.h"/g' "typescript/$lang/$file"
sed -i.bak 's/<tree_sitter\/parser\.h>/"parser\.h"/g' "typescript/$lang/$file"
done
sed -i.bak 's/<tree_sitter\/parser\.h>/"parser\.h"/g' "typescript/$lang/scanner.h"
rm typescript/$lang/*.bak
done
}
function download() {
download_sitter $sitter_version
for grammar in ${grammars[@]}; do
if [[ "$grammar" == typescript* ]]; then
download_typescript `echo $grammar | cut -d';' -f2`
else
download_grammar `echo $grammar | tr ';' ' '`
fi
done
}
function print_grammar_version() {
lang=$1
version=$2
remote_version=`git ls-remote --tags --refs --sort='-v:refname' "https://github.com/tree-sitter/tree-sitter-$lang.git" v\* | head -n 1 | cut -f2 | cut -d'/' -f3`
outdated=""
if [ "$version" != "$remote_version" ]; then
outdated="outdated"
fi
echo -e "$lang\t\tvendored: $version\tremote: $remote_version\t$outdated"
}
function check-updates() {
remote_version=`git ls-remote --tags --refs --sort='-v:refname' "https://github.com/tree-sitter/tree-sitter.git" | head -n 1 | cut -f2 | cut -d'/' -f3`
outdated=""
if [ "$sitter_version" != "$remote_version" ]; then
outdated="outdated"
fi
echo -e "tree-sitter\tvendored: $sitter_version\tremote: $remote_version\t$outdated"
for grammar in ${grammars[@]}; do
print_grammar_version `echo $grammar | tr ';' ' '`
done
}
function help() {
echo "this script supports 2 subcommands:"
echo "* check-updates - compares vendored versions with remote"
echo "* download - re-downloads vendored files"
}
case $1 in
check-updates) check-updates
;;
download) download
;;
*) help
;;
esac