22
33set -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
87126tmc_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
92131tmc_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
100143EOM
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
133175tmc_update () {
134176 tmc_update_autocomplete
135177}
136178
137179tmc_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