Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit 34261e1

Browse files
authored
Merge pull request #392 from testmycode/script-changes
Script changes
2 parents 45ab76e + cd2b080 commit 34261e1

File tree

6 files changed

+175
-97
lines changed

6 files changed

+175
-97
lines changed

appveyor.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ install:
1515
- cmd: SET PATH=C:\maven\apache-maven-3.2.5\bin;%JAVA_HOME%\bin;%PATH%
1616
- cmd: SET MAVEN_OPTS=-XX:MaxPermSize=2g -Xmx4g
1717
- cmd: SET JAVA_OPTS=-XX:MaxPermSize=2g -Xmx4g
18-
build_script:
19-
- mvn clean install --batch-mode
18+
build: off
19+
test_script:
20+
- mvn -Dsurefire.rerunFailingTestsCount=3 test
21+
- mvn checkstyle:check
2022
cache:
2123
- C:\maven\
2224
- C:\Users\appveyor\.m2

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>fi.helsinki.cs.tmc.cli</groupId>
55
<artifactId>tmc-cli</artifactId>
6-
<version>0.7.6</version>
6+
<version>0.8.0</version>
77
<packaging>jar</packaging>
88
<properties>
99
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -85,6 +85,11 @@
8585

8686
<build>
8787
<plugins>
88+
<plugin>
89+
<groupId>org.apache.maven.plugins</groupId>
90+
<artifactId>maven-surefire-plugin</artifactId>
91+
<version>2.18.1</version>
92+
</plugin>
8893
<plugin>
8994
<groupId>org.apache.maven.plugins</groupId>
9095
<artifactId>maven-shade-plugin</artifactId>

scripts/autocompletion.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22

33
# SCRIPT_PATH will be filled by stub script
4-
alias tmc="$SCRIPT_PATH/tmc"
4+
alias tmc="$SCRIPT_PATH"
55

66
TMC_COMMANDS=( "\$(tmc -d shell-helper -c)" )
77

scripts/install.sh

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,34 @@
11
#!/bin/bash
22

3-
RCFILE=".bashrc"
4-
RCPATH="$HOME/$RCFILE"
3+
set -euo pipefail
54

65
echo "~ Installing TMC-CLI ~"
7-
echo "This install script assumes you are using Bash."
8-
echo "If you are using another shell, please add 'source $HOME/.tmc-autocomplete.sh' in your shell's rc file after TMC-CLI has been installed."
96
echo ""
107

11-
if [ ! -f $RCPATH ]; then
12-
echo -e "$RCPATH not found, creating"
13-
echo ""
14-
touch $RCPATH
15-
fi
16-
178
if [ -f $HOME/.tmc-autocomplete.sh ]; then
18-
# If .tmc-autocomplete.sh is already in user's home dir, don't download it
19-
if ! ((grep -Fxq "source $HOME/.tmc-autocomplete.sh" $RCPATH) \
20-
|| (grep -Fxq "source \$HOME/.tmc-autocomplete.sh" $RCPATH) \
21-
|| (grep -Fxq "source ~/.tmc-autocomplete.sh" $RCPATH)); then
22-
echo -e ".tmc-autocomplete already exists in $HOME, but is not sourced in $RCFILE"
23-
echo -e "Adding to $RCPATH"
24-
echo "" >> $RCPATH
25-
echo "source $HOME/.tmc-autocomplete.sh" >> $RCPATH
26-
exit
27-
else
28-
echo "TMC-CLI is already installed. Did you forget to source your rc file?"
29-
echo -e "Try 'source $RCPATH'."
30-
exit
31-
fi
9+
echo -e ".tmc-autocomplete is already installed try adding the following line to your bashrc file."
10+
echo "source $HOME/.tmc-autocomplete.sh"
11+
exit
3212
fi
13+
3314
echo "Fetching latest release URL"
34-
URL=$(curl -s https://api.github.com/repos/testmycode/tmc-cli/releases/latest | grep '"browser_download_url"' | grep '/tmc"' | head -n 1 | cut -d '"' -f 4)
35-
echo "Downloading TMC-CLI"
36-
curl -LO $URL > ./tmc
15+
if ! PAGE=$(curl -s https://api.github.com/repos/testmycode/tmc-cli/releases/latest); then
16+
echo "Failed to fetch latest release from github api." >&2
17+
fi
18+
URL=$(echo "$PAGE" | grep '"browser_download_url"' | grep '/tmc"' | head -n 1 | cut -d '"' -f 4)
19+
20+
echo "Downloading TMC-CLI from following address"
21+
echo "$URL"
22+
echo
23+
24+
curl -LO "$URL" > ./tmc || true
25+
3726
if [ ! -f ./tmc ]; then
3827
echo "Error downloading TMC-CLI"
3928
exit 1
4029
fi
30+
4131
chmod u+x ./tmc
4232
./tmc > /dev/null
33+
4334
echo "Installation complete"
44-
exit

scripts/stub.sh

Lines changed: 145 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,83 @@
22

33
set -euo pipefail
44

5-
## Embeded binary magic
5+
if [[ ${TMC_DEBUG-} == 1 ]]; then
6+
tmc_debug() {
7+
(>&2 echo "$*")
8+
}
9+
else
10+
tmc_debug() {
11+
:
12+
}
13+
fi
614

7-
MYSELF=$(which "$0" 2>/dev/null)
8-
[ $? -gt 0 ] && [ -f "$0" ] && MYSELF="./$0"
15+
tmc_is_native() {
16+
# get the script directory
17+
local DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
918

10-
## Find the java binary and correct version
19+
TMC_NATIVE_PACKAGE=0
1120

12-
JAVA_BIN=java
13-
JAVA_HOME=${JAVA_HOME-}
14-
if [ -n "$JAVA_HOME" ]; then
15-
JAVA_BIN="$JAVA_HOME/bin/java"
16-
fi
21+
# check if the tmc binary is in read-only directory
22+
if [[ ! -w $DIR ]]; then
23+
TMC_NATIVE_PACKAGE=1
24+
fi
1725

18-
if ! hash "$JAVA_BIN" 2>/dev/null; then
19-
echo "Java not installed. If you have installed it in another directory then set the \$JAVA_HOME variable."
20-
exit 1
21-
fi
26+
if [[ "$TMC_NATIVE_PACKAGE" == "1" ]]; then
27+
tmc_debug "Tmc is installed through package manager"
28+
fi
2229

23-
JAVA_VERSION=$("$JAVA_BIN" -version 2>&1 | awk -F '"' '/version/ {print $2}')
24-
if [ "$JAVA_VERSION" \< "1.7" ]; then
25-
echo "You must have at least Java 1.7 installed."
26-
exit 1
27-
fi
30+
echo "$TMC_NATIVE_PACKAGE"
31+
}
2832

29-
## find the place for running the autocomplete/alias file
33+
TMC_NATIVE_PACKAGE=$(tmc_is_native)
3034

31-
tmc_detect_profile() {
32-
local PROFILE_ENV
33-
local SHELL_ENV
34-
local HOME_ENV
35+
tmc_get_binary() {
36+
tmc_debug "Embeded binary magic"
37+
38+
local MYSELF=$(which "$0" 2>/dev/null)
39+
[ $? -gt 0 ] && [ -f "$0" ] && MYSELF="./$0"
40+
41+
echo "$MYSELF"
42+
}
43+
44+
tmc_find_java_binary() {
45+
tmc_debug "Find the java binary and correct version"
46+
47+
local JAVA_BIN=java
48+
local JAVA_HOME=${JAVA_HOME-}
49+
50+
if [ -n "$JAVA_HOME" ]; then
51+
JAVA_BIN="$JAVA_HOME/bin/java"
52+
fi
53+
54+
if ! hash "$JAVA_BIN" 2>/dev/null; then
55+
echo "Java not installed. If you have installed it" >&2
56+
echo "in another directory then set the \$JAVA_HOME" >&2
57+
echo "variable." >&2
58+
exit 1
59+
fi
60+
61+
local JAVA_VERSION=$("$JAVA_BIN" -version 2>&1 | awk -F '"' '/version/ {print $2}')
62+
63+
if [ "$JAVA_VERSION" \< "1.7" ]; then
64+
echo "You must have at least Java 1.7 installed." >&2
65+
exit 1
66+
fi
67+
68+
echo $JAVA_BIN
69+
}
70+
JAVA_BIN=$(tmc_find_java_binary)
71+
72+
#####
73+
tmc_debug "Find the place for running the autocomplete/alias file"
3574

36-
PROFILE_ENV=${PROFILE-}
37-
SHELL_ENV=${SHELL-}
38-
HOME_ENV=${HOME-}
75+
tmc_detect_profile() {
76+
local PROFILE_ENV=${PROFILE-}
77+
local SHELL_ENV=${SHELL-}
78+
local HOME_ENV=${HOME-}
3979

4080
if [ -n "$HOME_ENV" ] && [ -f "$PROFILE_ENV" ]; then
41-
(>&2 echo "Home environment variable is not set")
81+
echo "Home environment variable is not set" >&2
4282
return
4383
fi
4484

@@ -47,10 +87,8 @@ tmc_detect_profile() {
4787
return
4888
fi
4989

50-
local DETECTED_PROFILE
51-
DETECTED_PROFILE=''
52-
local SHELLTYPE
53-
SHELLTYPE="$(basename "/$SHELL_ENV")"
90+
local DETECTED_PROFILE=''
91+
local SHELLTYPE="$(basename "/$SHELL_ENV")"
5492

5593
if [ "$SHELLTYPE" = "bash" ]; then
5694
if [ -f "$HOME_ENV/.bashrc" ]; then
@@ -81,60 +119,69 @@ tmc_detect_profile() {
81119

82120
## Bash autocompletion script extraction
83121

84-
# This is used in autocompletion file
85-
SCRIPT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
122+
tmc_binary_file() {
123+
echo "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"
124+
}
86125

87126
tmc_autocomplete_file() {
88127
echo "${TMC_AUTOCOMPLETE_FILE-$HOME/.tmc-autocomplete.sh}"
89128
}
90129

91130
## Create the alias and autocompletion code if tmc alias not set
92131
tmc_update_autocomplete() {
93-
local AUTOCOMPLETE_FILE
94-
local PROFILE_FILE
132+
local AUTOCOMPLETE_FILE="$(tmc_autocomplete_file)"
133+
local INSTALLED=0
95134

96-
AUTOCOMPLETE_FILE="$(tmc_autocomplete_file)"
135+
# This variable is defined for the embeded autocompletion file
136+
SCRIPT_PATH="$(tmc_binary_file)"
137+
if [[ -e "$AUTOCOMPLETE_FILE" ]]; then
138+
INSTALLED=1
139+
fi
97140

98141
cat > "$AUTOCOMPLETE_FILE" <<- EOM
99142
#EMBED_AUTOCOMPLETE_SH
100143
EOM
101144
chmod +x "$AUTOCOMPLETE_FILE"
102145

103-
PROFILE_FILE=$(tmc_detect_profile)
104-
105-
# get the aliases
106-
#TODO remove this and use global variables
107-
set +euo pipefail
108-
source $PROFILE_FILE
109-
set -euo pipefail
110-
111-
if type tmc &> /dev/null; then
112-
exit
146+
if [[ $INSTALLED == 0 ]]; then
147+
tmc_install_hook "$AUTOCOMPLETE_FILE"
113148
fi
149+
}
150+
151+
tmc_install_hook() {
152+
local AUTOCOMPLETE_FILE="$1"
153+
local PROFILE_FILE=$(tmc_detect_profile)
114154

115155
if [ -z "$PROFILE_FILE" ]; then
116156
echo "Profile file not found" >&2
117-
echo "Put the \"source $AUTOCOMPLETE_FILE\" line in somewhere where" >&2
118-
echo "it's run at terminal initialization." >&2
157+
echo "Put the \"source $AUTOCOMPLETE_FILE\" line in" >&2
158+
echo "your shell's rc file." >&2
119159
fi
120-
echo "source $AUTOCOMPLETE_FILE" >> "$PROFILE_FILE"
160+
# The `|| true` structure is used just in case that the rc file
161+
# has strict mode and user has manually removed tmc script
162+
echo "source $AUTOCOMPLETE_FILE || true" >> "$PROFILE_FILE"
121163

122-
echo "To use new autocompletion run the following on command line:" >&2
123-
echo ". ~/.bashrc" >&2
164+
echo "To use new autocompletion run the following on command line:" >&1
165+
echo ". $PROFILE_FILE" >&1
124166
}
125167

126168
## Auto update code
127169

128170
##### If you MODIFY the install script then do the following:
129-
##### Enable the "THE INSTALL SCRIPT DEBUGGING LINE" at [Tmc]CliUpdater.java
130-
##### (It runs the dev script instead of the latest release script)
171+
##### Enable the "THE INSTALL SCRIPT DEBUGGING LINE" at AutoUpdater.java
172+
##### (It runs your script instead of the script from latest github release)
131173
##### And use the --force-update flag in application.
132174

133175
tmc_update() {
134176
tmc_update_autocomplete
135177
}
136178

137179
tmc_install_update() {
180+
if [[ $TMC_NATIVE_PACKAGE == 1 ]]; then
181+
echo "Tmc should be updated by your package manager" >&2
182+
exit 126
183+
fi
184+
138185
echo "Please report any error messages that may come up below." >&2
139186
if [ ! -f tmc.new ]; then
140187
echo "Could not find the updated file." >&2
@@ -155,9 +202,9 @@ tmc_install_update() {
155202
rm tmc.orig &> /dev/null
156203
echo "Running the new tmc update script..." >&2
157204
echo "" >&2
205+
158206
tmc_update
159207

160-
echo ""
161208
if [ -f tmc ]; then
162209
echo "Tmc cli installation was successful" >&2
163210
else
@@ -167,18 +214,51 @@ tmc_install_update() {
167214
exit
168215
}
169216

170-
if [ "${1-}" == "++internal-update" ]; then
171-
tmc_install_update
172-
fi
217+
tmc_uninstall() {
218+
local AUTOCOMPLETE_FILE="$(tmc_autocomplete_file)"
219+
local PROFILE_FILE=$(tmc_detect_profile)
220+
local TMC_FILE="$(tmc_binary_file)"
173221

174-
if [ ! -f "$(tmc_autocomplete_file)" ]; then
175-
tmc_update_autocomplete
176-
exit
177-
fi
222+
#remove the include line from rc file
178223

179-
#EMBED_UNIT_TESTS_SH
224+
grep -v "source $AUTOCOMPLETE_FILE || true" "$PROFILE_FILE" > "${PROFILE_FILE}2"
225+
if [[ "$?" == 0 ]]; then
226+
mv "${PROFILE_FILE}2" "$PROFILE_FILE"
227+
fi
180228

181-
export COLUMNS=$(tput cols)
182-
exec "$JAVA_BIN" -jar "$MYSELF" "$@"
229+
rm "$AUTOCOMPLETE_FILE"
230+
rm "$TMC_FILE"
231+
}
232+
233+
tmc_main() {
234+
if [ "${1-}" == "++internal-update" ]; then
235+
tmc_install_update
236+
fi
237+
238+
if [ "${1-}" == "--uninstall" ]; then
239+
tmc_uninstall
240+
exit
241+
fi
242+
243+
# check if this is first time running the tmc
244+
if [ ! -e "$(tmc_autocomplete_file)" ]; then
245+
tmc_update_autocomplete
246+
exit
247+
fi
248+
249+
local TMC_FLAGS=
250+
251+
# disable auto updates if tmc is from native package
252+
if [[ $TMC_NATIVE_PACKAGE == 1 ]]; then
253+
TMC_FLAGS="-d"
254+
fi
255+
256+
#EMBED_UNIT_TESTS_SH
257+
258+
export COLUMNS=$(tput cols)
259+
exec "$JAVA_BIN" -jar "$(tmc_get_binary)" $TMC_FLAGS "$@"
260+
261+
exit 0
262+
}
183263

184-
exit 0
264+
tmc_main $*

0 commit comments

Comments
 (0)