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

Commit 0ef2021

Browse files
author
Aleksi Salmela
committed
Try to improve the scripts.
1 parent b35b803 commit 0ef2021

File tree

5 files changed

+107
-43
lines changed

5 files changed

+107
-43
lines changed

scripts/autocompletion.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ _tmc_opts()
1010
local cur
1111
local sub_command
1212
local main_args
13-
# Pointer to current completion word.
14-
# By convention, it's named "cur" but this isn't strictly necessary.
1513

1614
COMPREPLY=()
1715
cur="\${COMP_WORDS[COMP_CWORD]}"

scripts/stub.sh

Lines changed: 78 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
set -euo pipefail
44

5+
## Embeded binary magic
6+
57
MYSELF=$(which "$0" 2>/dev/null)
68
[ $? -gt 0 ] && [ -f "$0" ] && MYSELF="./$0"
79

10+
## Find the java binary and correct version
11+
812
JAVA_BIN=java
913
JAVA_HOME=${JAVA_HOME-}
1014
if [ -n "$JAVA_HOME" ]; then
@@ -22,21 +26,7 @@ if [ "$JAVA_VERSION" \< "1.7" ]; then
2226
exit 1
2327
fi
2428

25-
AUTOCOMPLETE="$HOME/.tmc-autocomplete.sh"
26-
27-
# this is used in autocompletion file
28-
SCRIPT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
29-
30-
tmc_update_autocomplete() {
31-
cat > "$AUTOCOMPLETE" <<- EOM
32-
TMC_AUTOCOMPLETE_SH
33-
EOM
34-
chmod +x "$AUTOCOMPLETE"
35-
}
36-
37-
tmc_update() {
38-
tmc_update_autocomplete
39-
}
29+
## find the place for running the autocomplete/alias file
4030

4131
tmc_detect_profile() {
4232
local PROFILE_ENV
@@ -89,54 +79,104 @@ tmc_detect_profile() {
8979
fi
9080
}
9181

92-
# create the alias and autocompletion code if tmc alias not set
93-
if ! type tmc &> /dev/null; then
94-
tmc_update_autocomplete
82+
## Bash autocompletion script extraction
83+
84+
# This is used in autocompletion file
85+
SCRIPT_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
86+
87+
tmc_autocomplete_file() {
88+
echo "${TMC_AUTOCOMPLETE_FILE-$HOME/.tmc-autocomplete.sh}"
89+
}
90+
91+
## Create the alias and autocompletion code if tmc alias not set
92+
tmc_update_autocomplete() {
93+
local AUTOCOMPLETE_FILE
94+
local PROFILE_FILE
95+
96+
AUTOCOMPLETE_FILE="$(tmc_autocomplete_file)"
97+
98+
cat > "$AUTOCOMPLETE_FILE" <<- EOM
99+
#EMBED_AUTOCOMPLETE_SH
100+
EOM
101+
chmod +x "$AUTOCOMPLETE_FILE"
95102

96103
PROFILE_FILE=$(tmc_detect_profile)
104+
105+
# get the aliases
106+
set +euo pipefail
107+
source $PROFILE_FILE
108+
set -euo pipefail
109+
110+
if type tmc &> /dev/null; then
111+
exit
112+
fi
113+
97114
if [ -z "$PROFILE_FILE" ]; then
98-
echo "Profile file not found"
99-
echo "Put the \"source $AUTOCOMPLETE\" line in somewhere where"
100-
echo "it's run at terminal initialization."
115+
echo "Profile file not found" >&2
116+
echo "Put the \"source $AUTOCOMPLETE_FILE\" line in somewhere where" >&2
117+
echo "it's run at terminal initialization." >&2
101118
fi
102-
echo "source $AUTOCOMPLETE" >> "$PROFILE_FILE"
103-
fi
119+
echo "source $AUTOCOMPLETE_FILE" >> "$PROFILE_FILE"
104120

105-
if [ "${1-}" == "++internal-update" ]; then
106-
echo "Please report any error messages that may come up below."
121+
echo "To use new autocompletion run the following on command line:" >&2
122+
echo ". ~/.bashrc" >&2
123+
}
124+
125+
## Auto update code
126+
127+
##### If you MODIFY the install script then do the following:
128+
##### Enable the "THE INSTALL SCRIPT DEBUGGING LINE" at [Tmc]CliUpdater.java
129+
##### (It runs the dev script instead of the latest release script)
130+
##### And use the --force-update flag in application.
131+
132+
tmc_update() {
133+
tmc_update_autocomplete
134+
}
135+
136+
tmc_install_update() {
137+
echo "Please report any error messages that may come up below." >&2
107138
if [ ! -f tmc.new ]; then
108-
echo "Could not find the updated file."
139+
echo "Could not find the updated file." >&2
109140
exit 127
110141
fi
111142

112-
echo "Moving the tmc files..."
113-
if mv tmc tmc.orig; then
114-
echo "Failed to backup the original tmc binary"
143+
echo "Moving the tmc files..." >&2
144+
if ! mv tmc tmc.orig; then
145+
echo "Failed to backup the original tmc binary" >&2
115146
exit 128
116147
fi
117-
if mv tmc.new tmc; then
118-
echo "Failed to replace the original binary with new version"
119-
echo "You can replace manually the $PWD/tmc with $PWD/tmc.new"
148+
if ! mv tmc.new tmc; then
149+
echo "Failed to replace the original binary with new version" >&2
150+
echo "You can replace manually the $PWD/tmc with $PWD/tmc.new" >&2
120151
exit 129
121152
fi
122153

123154
rm tmc.orig &> /dev/null
124-
echo "Running the new tmc update script..."
155+
echo "Running the new tmc update script..." >&2
156+
echo "" >&2
125157
tmc_update
126158

127159
echo ""
128160
if [ -f tmc ]; then
129-
echo "Tmc cli installation was successful"
130-
#echo ""
131-
#echo "To use new autocompletion run the following on command line:"
132-
#echo ". ~/.bashrc"
161+
echo "Tmc cli installation was successful" >&2
133162
else
134-
echo "Tmc cli installation failed."
163+
echo "Tmc cli installation failed." >&2
135164
exit 127
136165
fi
137166
exit
167+
}
168+
169+
if [ "${1-}" == "++internal-update" ]; then
170+
tmc_install_update
138171
fi
139172

173+
if [ ! -f "$(tmc_autocomplete_file)" ]; then
174+
tmc_update_autocomplete
175+
exit
176+
fi
177+
178+
#EMBED_UNIT_TESTS_SH
179+
140180
export COLUMNS=$(tput cols)
141181
exec "$JAVA_BIN" -jar "$MYSELF" "$@"
142182

scripts/unit_tests.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#TODO
2+
3+
#backup the profile and the autocompletion if the exist
4+
5+
#try out the functions with different parameters
6+
7+
#restore the files
8+
9+
exit

scripts/wrapper.sh

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,23 @@ set -euo pipefail
55
# find newest jar file
66
jar_file=$(ls -t target/tmc-cli-*.jar | head -1)
77

8-
sed '/TMC_AUTOCOMPLETE_SH/ {
9-
r scripts/autocompletion.sh
10-
d }' scripts/stub.sh > tmc
8+
quotePerlSubst() {
9+
IFS= read -d '' -r < <(sed -e ':a' -e '$!{N;ba' -e '}' -e 's/[$/\]/\\&/g; s/\n/\\&/g' <<<"$1")
10+
printf %s "${REPLY%$'\n'}"
11+
}
12+
13+
content=$(cat scripts/stub.sh)
14+
15+
ac_content=$(quotePerlSubst "$(cat scripts/autocompletion.sh)")
16+
content=$(echo "$content" | sed "s/\#EMBED_AUTOCOMPLETE_SH/$ac_content/g")
17+
18+
if [ "${1-}" = "--with-unit-tests" ] ; then
19+
ac_content=$(quotePerlSubst "$(cat scripts/unit_tests.sh)")
20+
content=$(echo "$content" | \
21+
sed "s/\#EMBED_UNIT_TESTS_SH/$ac_content/g")
22+
fi
23+
24+
echo "$content" > tmc
1125

1226
cat "$jar_file" >> tmc && chmod +x tmc
1327

src/main/java/fi/helsinki/cs/tmc/cli/updater/TmcCliUpdater.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ public boolean run() {
8686
return false;
8787
}
8888

89+
// Below is "THE INSTALL SCRIPT DEBUGGING LINE"
90+
//destination = new File(currentBinLocation + "tmc");
91+
8992
io.println("Running " + destination.getAbsolutePath());
9093
return runNewTmcCliBinary(destination.getAbsolutePath());
9194
}

0 commit comments

Comments
 (0)