-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscalafmt.sh
executable file
·174 lines (153 loc) · 3.37 KB
/
scalafmt.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env bash
set -e
BOOTSTRAP_DIR="$HOME/.scalafmt"
SCALA_VERSION="2.13"
SCALAFMT_VERSION="2.6.3"
SCALAFMT_ORG="org.scalameta"
SERVER=1
SERVER_PORT="${NAILGUN_PORT:-2113}"
CONFIG=".scalafmt.conf"
TEST=1
OPTIND=1
while getopts "c:d:o:p:stv:S:" opt; do
case $opt in
c)
CONFIG=$OPTARG
;;
d)
BOOTSTRAP_DIR=$OPTARG
;;
o)
SCALAFMT_ORG=$OPTARG
;;
p)
SERVER=0
SERVER_PORT=$OPTARG
;;
s)
SERVER=0
;;
S)
SCALA_VERSION=$OPTARG
;;
t)
TEST=0
;;
v)
SCALAFMT_VERSION=$OPTARG
;;
\?)
exit 1
;;
esac
done
shift $((OPTIND - 1))
function cmd_not_exists() {
if [[ $(command -v $1) ]]; then
return 1
else
return 0
fi
}
function version_neq() {
if [[ $($1 --version | cut -d' ' -f2) != "$SCALAFMT_VERSION" ]]; then
return 0
else
return 1
fi
}
function is_server() {
return $SERVER
}
function should_bootstrap() {
if is_server; then
(cmd_not_exists "scalafmt_ng" \
&& cmd_not_exists "$BOOTSTRAP_DIR/$SCALAFMT_VERSION/scalafmt_ng") \
|| version_neq "ng --nailgun-port $SERVER_PORT scalafmt"
else
(cmd_not_exists "scalafmt" || version_neq "scalafmt") \
&& (cmd_not_exists "$BOOTSTRAP_DIR/$SCALAFMT_VERSION/scalafmt" \
|| version_neq "$BOOTSTRAP_DIR/$SCALAFMT_VERSION/scalafmt")
fi
}
function test_only() {
return $TEST
}
function server_running() {
lsof -ti tcp:$SERVER_PORT -sTCP:LISTEN >/dev/null 2>&1
if [ $? -eq 0 ]; then
return 0
else
return 1
fi
}
function wait_for_server() {
end=$((SECONDS + 3))
while [ $SECONDS -lt $end ]; do
if server_running; then
break
fi
sleep 0.25
done
}
function kill_server() {
local lsof_res
if lsof_res=$(lsof -ti tcp:$SERVER_PORT -sTCP:LISTEN); then
kill -9 $lsof_res
fi
}
function start_server() {
$1 $SERVER_PORT >/dev/null 2>&1 &
wait_for_server
ng --nailgun-port $SERVER_PORT ng-alias scalafmt org.scalafmt.cli.Cli
}
# bootstrap scalafmt if needed
if should_bootstrap; then
mkdir -p "$BOOTSTRAP_DIR/$SCALAFMT_VERSION"
if cmd_not_exists "coursier" && cmd_not_exists "$BOOTSTRAP_DIR/coursier"; then
curl -L -o "$BOOTSTRAP_DIR/coursier" https://git.io/coursier-cli && chmod +x "$BOOTSTRAP_DIR/coursier" >/dev/null
fi
if cmd_not_exists "coursier"; then
COURSIER_CMD="$BOOTSTRAP_DIR/coursier"
else
COURSIER_CMD="coursier"
fi
if is_server; then
$COURSIER_CMD bootstrap -f \
--standalone "$SCALAFMT_ORG:scalafmt-cli_$SCALA_VERSION:$SCALAFMT_VERSION" \
--main com.martiansoftware.nailgun.NGServer \
-o "$BOOTSTRAP_DIR/$SCALAFMT_VERSION/scalafmt_ng" >/dev/null
else
$COURSIER_CMD bootstrap -f \
--standalone "$SCALAFMT_ORG:scalafmt-cli_$SCALA_VERSION:$SCALAFMT_VERSION" \
--main org.scalafmt.cli.Cli \
-o "$BOOTSTRAP_DIR/$SCALAFMT_VERSION/scalafmt" >/dev/null
fi
fi
# set proper command and start server if in server mode
if is_server; then
SCALAFMT_CMD="ng --nailgun-port $SERVER_PORT scalafmt"
if cmd_not_exists "scalafmt_ng"; then
SERVER_CMD="$BOOTSTRAP_DIR/$SCALAFMT_VERSION/scalafmt_ng"
else
SERVER_CMD="scalafmt_ng"
fi
if version_neq "$SCALAFMT_CMD"; then
kill_server
fi
if ! server_running; then
start_server $SERVER_CMD >/dev/null
fi
else
if cmd_not_exists "scalafmt"; then
SCALAFMT_CMD="$BOOTSTRAP_DIR/$SCALAFMT_VERSION/scalafmt"
else
SCALAFMT_CMD="scalafmt"
fi
fi
# format passed files
if test_only; then
$SCALAFMT_CMD --test -c $CONFIG "$@" >/dev/null
else
$SCALAFMT_CMD -c $CONFIG "$@" >/dev/null
fi