-
Notifications
You must be signed in to change notification settings - Fork 0
/
bashrc_build_dir
64 lines (59 loc) · 2.1 KB
/
bashrc_build_dir
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
# -*- shell-script -*-
### Manage out of source build directories ###########################
#
# Keeps tracks of all your out of source builds by creating dedicated
# directories per repository AND branch.
#
# Before all things, two global variables must be set:
# ----------------------------------------------------
#
# - MY_SOURCE_BUILD_DIR: the desired target build directory.
#
# - MY_SOURCE_HOME_DIR: the root path of all your repositories,
# usually $HOME/src or $HOME if you have multiple global source
# directories (src, work, ...)
#
# Two commands are provided:
# --------------------------
#
# - The "cdb" command provides the following feature:
#
# # Create a build directory in "MY_SOURCE_BUILD_DIR" that reflects
# the repository and the branch selected. The created directory
# path is exported in the variable "pwd_build".
#
# # Create a link in the repository root that targets the compile
# commands in the build directory (useful for Language Server
# Protocol).
#
# # Export the variable "pwd_source" that points to the directory
# where 'cdb' command is called.
#
# # Change the current directory to "$pwd_build"
#
# The "cds" command comes back to the pwd_source directory.
#
get_build_dir() {
pwd_source_repo=$(git rev-parse --show-toplevel)
repo=$(basename $pwd_source_repo)
path_in_repo=$(realpath --relative-to="${pwd_source_repo}" .)
branch=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/^* \(.*\)/\1/')
dest_path=${MY_SOURCE_BUILD_DIR}${pwd_source_repo#${MY_SOURCE_HOME_DIR}}/${branch}/${path_in_repo}
echo ${dest_path}
}
link_compile_command() {
build_dir=$(realpath $1)
pwd_source_repo=$(git rev-parse --show-toplevel)
rm -f ${pwd_source_repo}/compile_commands.json
ln -s $(echo "${build_dir}/compile_commands.json") $(realpath ${pwd_source_repo})/compile_commands.json
}
cdb() {
if [[ "$(pwd)" != "${pwd_build}" ]]; then
export pwd_source=$(pwd -L)
export pwd_build=$(get_build_dir);
mkdir -p ${pwd_build}
link_compile_command ${pwd_build}
cd ${pwd_build}
fi
}
alias cds='cd ${pwd_source}'