build-dir now exports build_dir envvar

In addition to setting the `~build` hashed directory, also export the
`build_dir` environment variable to enable commands like this:

```
cmake . -B$build_dir
```
This commit is contained in:
Kenneth Benzie 2018-11-07 10:58:04 +00:00
parent 9037d2dd41
commit 256cd06e5a

View File

@ -42,7 +42,7 @@ EOF
fi fi
error() { echo "\e[31merror:\e[0m $1" } error() { echo "\e[31merror:\e[0m $1" }
warning() { echo "\e[33mwarning:\e[0m $1" } warning() { echo "\e[33mwarning:\e[0m $1" }
local build_dir local local_build_dir
if [[ ${#*} -gt 1 ]]; then if [[ ${#*} -gt 1 ]]; then
echo $usage echo $usage
error "unexpected position arguments: ${*[2,${#*}]}"; return 1 error "unexpected position arguments: ${*[2,${#*}]}"; return 1
@ -50,24 +50,24 @@ EOF
if [[ ! -d ${*[1]} ]]; then if [[ ! -d ${*[1]} ]]; then
warning "directory not found: ${*[1]}" warning "directory not found: ${*[1]}"
else else
build_dir=${*[1]} local_build_dir=${*[1]}
fi fi
fi fi
# If <directory> was not set begin selection # If <directory> was not set begin selection
if [[ -z $build_dir ]]; then if [[ -z $local_build_dir ]]; then
# Find build directories # Find build directories
local -a build_dirs local -a local_build_dirs
for entry in `ls -A`; do for entry in `ls -A`; do
[ -d $entry ] && [[ $entry =~ build* ]] && \ [ -d $entry ] && [[ $entry =~ build* ]] && \
build_dirs+=${entry/\//} local_build_dirs+=${entry/\//}
done done
# Interactively select a build directory if more than 1 found # Interactively select a build directory if more than 1 found
integer index=0 integer index=0
if [[ ${#build_dirs} -eq 0 ]]; then if [[ ${#local_build_dirs} -eq 0 ]]; then
error "no build directories found"; return 1 error "no build directories found"; return 1
elif [[ ${#build_dirs} -gt 1 ]]; then elif [[ ${#local_build_dirs} -gt 1 ]]; then
zmodload zsh/curses && { zmodload zsh/curses && {
# Get the size of the terminal # Get the size of the terminal
local size=`stty size` local size=`stty size`
@ -92,13 +92,13 @@ EOF
zcurses string build-dir 'Select a build directory:' zcurses string build-dir 'Select a build directory:'
# Add the selections text # Add the selections text
for (( i = 0; i < ${#build_dirs}; i++ )); do for (( i = 0; i < ${#local_build_dirs}; i++ )); do
integer line=$i+3 integer line=$i+3
zcurses move build-dir $line 1 zcurses move build-dir $line 1
[[ $index -eq $i ]] && [[ $index -eq $i ]] &&
zcurses string build-dir "* " || zcurses string build-dir "* " ||
zcurses string build-dir " " zcurses string build-dir " "
zcurses string build-dir ${build_dirs[$i+1]} zcurses string build-dir ${local_build_dirs[$i+1]}
done done
# Display the text the and wait for input # Display the text the and wait for input
@ -110,7 +110,7 @@ EOF
(UP|k|$'\C-P') (UP|k|$'\C-P')
[[ $index -gt 0 ]] && index=$index-1 ;; [[ $index -gt 0 ]] && index=$index-1 ;;
(DOWN|j|$'\C-N') (DOWN|j|$'\C-N')
[[ $index -lt ${#build_dirs}-1 ]] && index=$index+1 ;; [[ $index -lt ${#local_build_dirs}-1 ]] && index=$index+1 ;;
(ENTER|$'\n') (ENTER|$'\n')
break ;; break ;;
esac esac
@ -127,32 +127,33 @@ EOF
# On success setup the build directory for use # On success setup the build directory for use
if [[ $? -eq 0 ]]; then if [[ $? -eq 0 ]]; then
# Set the build directory from selection if empty # Set the build directory from selection if empty
[[ -z $build_dir ]] && \ [[ -z $local_build_dir ]] && \
build_dir=${build_dirs[$index+1]} local_build_dir=${local_build_dirs[$index+1]}
# If `build.ninja` exists in alias `ninja`, return. # If `build.ninja` exists in alias `ninja`, return.
local build local build
[ -f $build_dir/build.ninja ] && \ [ -f $local_build_dir/build.ninja ] && \
build="ninja -C $build_dir" build="ninja -C $local_build_dir"
# If `Makefile` exists in alias `make`, return. # If `Makefile` exists in alias `make`, return.
if [ -f $build_dir/Makefile ]; then if [ -f $local_build_dir/Makefile ]; then
[ `uname` = Darwin ] && \ [ `uname` = Darwin ] && \
local cpu_count=`sysctl -n hw.ncpu` || local cpu_count=`sysctl -n hw.ncpu` ||
local cpu_count=`grep -c '^processor' /proc/cpuinfo` local cpu_count=`grep -c '^processor' /proc/cpuinfo`
build="make -j $cpu_count -C $build_dir" build="make -j $cpu_count -C $local_build_dir"
fi fi
# If the build variable is not defined the command could not be determined # If the build variable is not defined the command could not be determined
if [ -z $build ]; then if [ -z $build ]; then
warning "build command detection failed: $build_dir" warning "build command detection failed: $local_build_dir"
# Prompt user to enter a build command # Prompt user to enter a build command
vared -p 'enter comand: ' build vared -p 'enter comand: ' build
fi fi
# Redefine the `build` alias and update the `~build` hash directory # Redefine the `build` alias and update the `~build` hash directory
alias build="$build" alias build="$build"
hash -d build=$build_dir hash -d build=$local_build_dir
export build_dir=$local_build_dir
# If `--build` is specified then evaluate the command. # If `--build` is specified then evaluate the command.
if [[ -n $do_build ]]; then if [[ -n $do_build ]]; then