-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspace.config
More file actions
170 lines (149 loc) · 4.77 KB
/
Copy pathworkspace.config
File metadata and controls
170 lines (149 loc) · 4.77 KB
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
# Configure environment per workspace
# The configuration is defined in a file called .workspace.config
# that is contained in any folder. If such a file is found
# in the current folder or any of its parents, this file
# is sourced.
# Functions defined in module files contained in the subdirectory
# called "modules" are used to activate and deactivate the current
# workspace.
# `${module}_activate_workspace and `${module}_deactivate_workspace`
# can be defined to run actions that are executed when a workspace
# is activated (entering) or deactivated (leaving).
# Search for project config file in current directory and
# recursively in parent directories until root directory is found.
# Use `WORKSPACE_CONFIG_LOOK_UP` environment variable to avoid
# calling the function recursively when changing folder inside
# the folder configuration file.
function _function_lock {
export WORKSPACE_CONFIG_LOOK_UP=1
$@
unset WORKSPACE_CONFIG_LOOK_UP
}
# Deactivate workspace
# Deactivate virtual environment, unload packages, remove
# environment variables.
function _deactivate_workspace {
if [[ -f "$CURRENT_WORKSPACE/$config_filename" ]]; then
source "$CURRENT_WORKSPACE/$config_filename"
for func in ${list_deactivate_functions[@]}
do
$func
done
fi
unset CURRENT_WORKSPACE
}
# Manually deactivate a workspace
function deactivate_workspace {
local current_path=$1
_function_lock _deactivate_workspace
}
# Activate workspace
# Activate virtual environment, load packages.
function _activate_workspace {
source "$current_path/$config_filename"
export CURRENT_WORKSPACE=$current_path
for func in ${list_activate_functions[@]}
do
$func
done
}
# Returns 0 if workspace is activated and print workspace
# name if verbose.
function is_workspace_on {
if [[ $# -gt 1 ]] || [[ $# -eq 1 ]] && [[ $1 != "-v" ]]; then
# $funcstack[1] is used in zsh
# ${FUNCNAME[0]} is used in bash
echo "Usage: ${FUNCNAME[0]}$funcstack[1] [-v]"
fi
local _current=$CURRENT_WORKSPACE
local ret=0
if [[ -z "$CURRENT_WORKSPACE" ]]; then
local _current="None"
local ret=1
fi
if [[ $1 == "-v" ]]; then
echo "Current workspace: $_current"
fi
return $ret
}
# Manually activate a workspace
function activate_workspace {
config_filename=".workspace.config"
if [[ $# -ne 1 ]] || [[ ! -f $1/$config_filename ]]; then
echo "Function requires one argument: valid workspace directory"
return 1
fi
local current_path=$1
# First deactivate previous workspace
_function_lock _deactivate_workspace
# Then activate new workspace
_function_lock _activate_workspace
}
# Function called when one changes folder.
function chpwd {
if [[ -n $WORKSPACE_CONFIG_LOOK_UP ]]; then
return 0
fi
local current_path=`pwd`
config_filename=".workspace.config"
while [[ $current_path != / ]];
do
res=`find "$current_path" -maxdepth 1 -mindepth 1 -name $config_filename`
if [[ -f $res ]]
then
# If same workspace as before, do nothing to save time
if [[ `dirname $res` == "$CURRENT_WORKSPACE" ]]; then
return 0
fi
# First deactivate previous workspace
_function_lock _deactivate_workspace
# Then activate new workspace
_function_lock _activate_workspace
return 0
fi
current_path="$(readlink -f "$current_path"/..)"
done
_function_lock _deactivate_workspace
unset WORKSPACE_CONFIG_LOOK_UP
}
# Zsh has `chpwd` hook, but Bash does not have it builtin.
# Both shells have the function `cd` builtin.
function cd {
builtin cd "$1"
chpwd
}
function _add_function_to_list {
# Check if functions exist and add them to list of available functions
function_name=$1$2
$(type $function_name)
if [[ $? == 0 ]]; then
echo $function_name
return
fi
}
# Get this script directory
if [ -n "$ZSH_VERSION" ]; then
# assume Zsh
DIR="$(dirname ${(%):-%N})"
else
# assume Bash
# Do not check "$BASH_VERSION"
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)"
fi
#Reset array variables
unset list_activate_functions
unset list_deactivate_functions
#Expected functions
activate_function_suffix="_activate_workspace"
deactivate_function_suffix="_deactivate_workspace"
# Load all modules
for name in $(find $DIR/modules/* -type f | sort -z | tr -d '\00');
do
# Get module name
source $name
module=$(echo $name | sed -n 's;[^-]*-\(.*\);\1;p')
list_activate_functions+=($(_add_function_to_list $module $activate_function_suffix))
list_deactivate_functions+=($(_add_function_to_list $module $deactivate_function_suffix))
done
# Force call chpwd when sourcing this file.
chpwd