Skip to content

Commit 52ac57c

Browse files
committed
Added support for ksh with refactor of the shell functions. PATH to the tools also added when missing
1 parent 0474363 commit 52ac57c

File tree

5 files changed

+123
-89
lines changed

5 files changed

+123
-89
lines changed

etc/profile.d/tykky.sh

Lines changed: 32 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,44 @@
11
# Tykky shell functions to activate/deactivate environments
22

3-
__tykky_get_env_path() {
4-
# Get and validate installation path of a tykky environment.
5-
# If a name is passed as an argument, try to find it in colon-separated
6-
# list TYKKY_PATH
7-
8-
__candidate=""
9-
case "$1" in
10-
*/*)
11-
__candidate="${1%/}"
12-
;;
13-
*)
14-
oldIFS=$IFS
15-
IFS=:
16-
for __tykky_path in ${TYKKY_PATH:-""}; do
17-
IFS=$oldIFS
18-
if [ -d "$__tykky_path/$1" ]; then
19-
__candidate="${__tykky_path%/}/${1%/}"
20-
break
21-
fi
22-
done
23-
24-
;;
25-
esac
26-
27-
# Validation of genunine tykky installation
28-
if [ -f "$__candidate/common.sh" ] && [ -d "$__candidate/bin" ]; then
29-
echo "$__candidate"
30-
unset __candidate __tykky_path
31-
else
32-
unset __candidate __tykky_path
33-
echo "ERROR: $1 is not a valid tykky environment" >&2
34-
false
35-
fi
36-
37-
}
38-
39-
__tykky_activate() {
40-
# Activate a Tykky environment. Change PS1 if interactive by default,
41-
# unless TYKKY_CHANGE_PS1 is set to 0
42-
43-
if [ -z "${2:-}" ]; then
44-
echo "ERROR: You must specify a valid tykky environment to activate" >&2
45-
false
46-
return
47-
fi
48-
__candidate=$(__tykky_get_env_path $2)
49-
if [ "$?" -ne 0 ]; then
50-
false
51-
return
52-
fi
53-
if [ -n "$__candidate" ]; then
54-
__tykky_deactivate
55-
export TYKKY_PREFIX="$(realpath $__candidate)"
56-
export PATH=$TYKKY_PREFIX/bin:$PATH
57-
if [ -n "${PS1:-}" ] && [ "${TYKKY_CHANGE_PS1:-}" != "0" ]; then
58-
PS1="($(basename $(echo $TYKKY_PREFIX))) $PS1"
59-
fi
60-
fi
61-
unset __candidate
62-
}
3+
__tykky_dir=""
4+
if [ -n "${BASH_SOURCE:-}" ]; then
5+
__tykky_dir="$(dirname "${BASH_SOURCE[0]}")"
6+
elif [ -n "${ZSH_VERSION:-}" ]; then
7+
__tykky_dir="$(dirname "${(%):-%N}")"
8+
elif [ -n "${KSH_VERSION:-}" ]; then
9+
__tykky_dir="$(dirname "${.sh.file}")"
10+
else
11+
# Generic POSIX shell case or dash
12+
__tykky_dir="$(dirname "$0")"
13+
fi
6314

15+
__tykky_dir="$(realpath $(dirname $(dirname "$__tykky_dir")))"
6416

65-
__tykky_deactivate() {
66-
# Deactivate a Tykky environment. Change PS1 if interactive by default,
17+
# Add the tykky tools to PATH if not present
18+
if [ "${PATH#*$__tykky_dir/bin:}" = "$PATH" ]; then
19+
echo "export PATH"
20+
export PATH="$__tykky_dir/bin:$PATH"
21+
fi
6722

68-
if [ -n "${TYKKY_PREFIX:-}" ]; then
69-
export PATH=$(echo $PATH | sed -e "s|$TYKKY_PREFIX/bin:||g")
70-
if [ -n "${PS1:-}" ]; then
71-
PS1="$(echo "$PS1" | sed -e "s|^($(basename $TYKKY_PREFIX)) ||")"
72-
fi
73-
unset TYKKY_PREFIX
23+
# Make available tykky functions for this shell and subshells
24+
for __function in $__tykky_dir/share/sh_functions/*; do
25+
source $__function
26+
if [ -z "$KSH_VERSION" ]; then
27+
export -f $(basename $__function)
7428
fi
75-
}
29+
done
30+
unset __function
7631

77-
78-
tykky() {
79-
# Top level shell function to activate and deactivate Tykky environments
80-
81-
case "${1:-}" in
82-
activate)
83-
__tykky_activate "$@"
84-
;;
85-
deactivate)
86-
__tykky_deactivate "$@"
87-
;;
88-
*)
89-
echo "Usage: tykky activate <env_name_or_dir>"
90-
echo " tykky deactivate"
91-
;;
92-
esac
93-
}
94-
export -f tykky
32+
# KSH does not support exporting functions
33+
if [ "${FPATH#*$__tykky_dir/share/sh_functions:}" = "$FPATH" ]; then
34+
echo "export FPATH"
35+
export FPATH="$__tykky_dir/share/sh_functions:$FPATH"
36+
fi
9537

9638
# Enable BASH autocompletion
97-
if [ -n "$BASH_VERSION" ] && [ -n "$PS1" ]; then
98-
__tykky_bash_completion_file="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../bash_completion.d/tykky_completion"
39+
if [ -n "${BASH_VERSIONi:-}" ] && [ -n "${PS1:-}" ]; then
40+
__tykky_bash_completion_file="$_tykky_dir/etc/bash_completion.d/tykky_completion"
9941
[ -f "$__tykky_bash_completion_file" ] && . "$__tykky_bash_completion_file"
10042
unset __tykky_bash_completion_file
10143
fi
44+
unset __tykky_dir

share/sh_functions/__tykky_activate

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Tykky shell functions to activate/deactivate environments
2+
__tykky_activate() {
3+
# Activate a Tykky environment. Change PS1 if interactive by default,
4+
# unless TYKKY_CHANGE_PS1 is set to 0
5+
6+
if [ -z "${2:-}" ]; then
7+
echo "ERROR: You must specify a valid tykky environment to activate" >&2
8+
false
9+
return
10+
fi
11+
__candidate=$(__tykky_get_env_path $2)
12+
if [ "$?" -ne 0 ]; then
13+
false
14+
return
15+
fi
16+
if [ -n "$__candidate" ]; then
17+
__tykky_deactivate
18+
export TYKKY_PREFIX="$(realpath $__candidate)"
19+
export PATH=$TYKKY_PREFIX/bin:$PATH
20+
if [ -n "${PS1:-}" ] && [ "${TYKKY_CHANGE_PS1:-}" != "0" ]; then
21+
PS1="($(basename $(echo $TYKKY_PREFIX))) $PS1"
22+
fi
23+
fi
24+
unset __candidate
25+
}

share/sh_functions/__tykky_deactivate

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Tykky shell functions to activate/deactivate environments
2+
__tykky_deactivate() {
3+
# Deactivate a Tykky environment. Change PS1 if interactive by default,
4+
5+
if [ -n "${TYKKY_PREFIX:-}" ]; then
6+
export PATH=$(echo $PATH | sed -e "s|$TYKKY_PREFIX/bin:||g")
7+
if [ -n "${PS1:-}" ]; then
8+
PS1="$(echo "$PS1" | sed -e "s|^($(basename $TYKKY_PREFIX)) ||")"
9+
fi
10+
unset TYKKY_PREFIX
11+
fi
12+
}
13+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Tykky shell functions to activate/deactivate environments
2+
__tykky_get_env_path() {
3+
# Get and validate installation path of a tykky environment.
4+
# If a name is passed as an argument, try to find it in colon-separated
5+
# list TYKKY_PATH
6+
7+
__candidate=""
8+
case "$1" in
9+
*/*)
10+
__candidate="${1%/}"
11+
;;
12+
*)
13+
oldIFS=$IFS
14+
IFS=:
15+
for __tykky_path in ${TYKKY_PATH:-""}; do
16+
IFS=$oldIFS
17+
if [ -d "$__tykky_path/$1" ]; then
18+
__candidate="${__tykky_path%/}/${1%/}"
19+
break
20+
fi
21+
done
22+
23+
;;
24+
esac
25+
26+
# Validation of genunine tykky installation
27+
if [ -f "$__candidate/common.sh" ] && [ -d "$__candidate/bin" ]; then
28+
echo "$__candidate"
29+
unset __candidate __tykky_path
30+
else
31+
unset __candidate __tykky_path
32+
echo "ERROR: $1 is not a valid tykky environment" >&2
33+
false
34+
fi
35+
36+
}

share/sh_functions/tykky

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Tykky shell functions to activate/deactivate environments
2+
tykky() {
3+
# Top level shell function to activate and deactivate Tykky environments
4+
5+
case "${1:-}" in
6+
activate)
7+
__tykky_activate "$@"
8+
;;
9+
deactivate)
10+
__tykky_deactivate "$@"
11+
;;
12+
*)
13+
echo "Usage: tykky activate <env_name_or_dir>"
14+
echo " tykky deactivate"
15+
;;
16+
esac
17+
}

0 commit comments

Comments
 (0)