proj/proj

234 lines
5.1 KiB
Plaintext
Raw Normal View History

#! /bin/bash
#
# proj
# A program for creating and managing individual projects and related files
# Copyright (C) 2021 Vintage Salt <rehashedsalt@cock.li>
#
# Distributed under terms of the MIT license.
#
set -e
# Read-only set-once variables
declare -r _name="$(basename -- "$0")"
# Options
declare -a _config=(
2021-03-06 05:41:53 -06:00
[hook_pre_create]=""
[hook_post_create]=""
2021-03-06 05:41:53 -06:00
[hook_pre_spawn]=""
2021-03-06 06:00:27 -06:00
[hook_env]=".projenv"
2021-03-06 05:41:53 -06:00
[project_dir]="$HOME/Projects"
)
declare _optconfigfile="${XDG_CONFIG_HOME:-$HOME/.config}/${_name}.conf"
declare -i _opthelp
declare -i _optverbose
# Working variables
declare -a _args
declare _return
# Helper functions
log() {
# Print a line to the terminal if _optverbose is greater than $2
# $2 defaults to 0
# loglevel 0: Daily-use messages
# loglevel 1: Detailed but not quite debugging
# loglevel 2: Definitely debugging
[ -z "$1" ] && return 1
if (( _optverbose >= ${2:-0} )); then
printf "%s\\n" "$1"
fi
}
warn() {
# Print a yellow line to the terminal, respecting _optverbose
[ -z "$1" ] && return 1
if (( _optverbose >= ${2:-0} )); then
if [ -t 1 ]; then
printf "\\e[33m%s\\e[0m\\n" "$1"
else
printf "WARN: %s\\n" "$1"
fi
fi
}
error() {
# Print a red line to the terminal, exit if $2 is specified
[ -z "$1" ] && return 1
if [ -t 2 ]; then
printf "\\e[31m%s\\e[0m\\n" "$1" 1>&2
else
printf "ERROR: %s\\n" "$1" 1>&2
fi
[ -z "$2" ] && return
exit "${2:-1}"
}
has() {
# Parse out all arguments and try to find them in path
# If an argument cannot be found, set _return and fail
for prog in "$@"; do
if ! command -v "$prog" > /dev/null 2>&1; then
_return="$prog"
return 1
fi
done
return 0
}
2021-03-06 06:00:27 -06:00
validateline() {
# Takes a line and errors if it's just whitespace or a comment
local linenows=${1//[[:space:]]}
if ! [ "${1#\#}" = "$1" ] || [ -z "$linenows" ]; then
return 1
fi
return 0
}
# Core program functions
printhelp() {
cat << EOF
Usage: $_name [OPTION]...
-c [FILE] Load the given file in place of the usual config file
-h Print this help text
-v Print more status messages. Stacks
Copyright (c) 2021 rehashedsalt@cock.li
Licensed under the MIT license
EOF
}
hook() {
# Execute a hook, if it exists
# If it fails for whatever reason, continue anyway silently
# $1 path to executable
# $2 name of hook
[ -z "$1" ] && return 1
[ -z "$2" ] && return 2
log "Attempting to execute hook \"$2\": $1"
if ! [ -e "$1" ]; then
warn "Hook \"$2\" does not exist: $1"
return
fi
if ! [ -x "$1" ]; then
warn "Hook \"$2\" cannot be executed: $1"
return
fi
if [ -d "$1" ]; then
warn "Hook \"$2\" is a directory: $1"
return
fi
"$1" || warn "Hook \"$2\" failed: $?"
}
2021-03-06 06:00:27 -06:00
proj() {
# Create/find a project directory and get in there
[ -z "$1" ] && return 1
# Get to project_dir
local projdir="${_config[project_dir]}"
log "Using project_dir: $projdir" 2
if ! [ -d "$projdir" ]; then
if [ -e "$projdir" ]; then
error "Project directory exists as another file type: $projdir" 61
fi
mkdir -p "$projdir"
log "Created project directory: $projdir" 1
fi
log "Entering directory: $projdir" 2
cd "$projdir"
# Get to THE project directory
local dir="$1"
local dirnew
if ! [ -d "$1" ]; then
if [ -e "$1" ]; then
error "File is in the way of creating a directory: $dir" 62
fi
# Execute the pre-create hook
[ -n "${_config[hook_pre_create]}" ] && hook "${_config[hook_pre_create]}" "pre-create"
mkdir -p "$dir"
dirnew=1
fi
log "Entering directory: $dir" 2
cd "$dir"
# Execute a post-create hook if necessary
if [ -n "$dirnew" ]; then
[ -n "${_config[hook_post_create]}" ] && hook "${_config[hook_post_create]}" "post-create"
fi
# Execute the pre-spawn hook if necesary
[ -n "${_config[hook_pre_spawn]}" ] && hook "${_config[hook_pre_spawn]}" "pre-spawn"
# Get in there
(
2021-03-06 06:00:27 -06:00
:
) || :
unset dirnew precreatehook postcreatehook
2021-03-06 06:00:27 -06:00
}
# Main
main() {
# Parse out arguments
while [ -n "$1" ]; do
# Parse out flags
while getopts ":c:hv" opt; do
case $opt in
c)
_optconfigfile="$OPTARG"
;;
h)
_opthelp=1
;;
v)
_optverbose+=1
;;
:)
error "Option requires argument: -$OPTARG" 2
;;
*)
error "Invalid option: -$OPTARG" 2
;;
esac
done
# Store arguments
shift $((OPTIND - 1))
if [ -n "$1" ]; then
_args+=("$1")
shift
fi
unset OPTIND
done
# Early hook for help
[ -n "$_opthelp" ] && printhelp && exit 0
# Parse out a config file if it exists
if [ -f "$_optconfigfile" ]; then
log "Loading config file: $_optconfigfile" 2
while read line; do
# If the line has an equals sign and isn't a comment
if [ "$line" != "${line#*=}" ] && validateline "$line"; then
local key="${line%=*}"
local value="${line#*=}"
_config[$key]="$value"
log "Setting $key to $value" 2
fi
done < "$_optconfigfile"
else
warn "Could not find configuration file" 2
fi
# Validate critical options
# TODO: That
2021-03-06 06:00:27 -06:00
# Validate arguments
if [ "${#_args[@]}" -ne "1" ]; then
error "Required exactly 1 argument, got ${#_args[@]}" 50
fi
# Validate core program dependencies
log "Validating dependencies" 2
if ! has basename; then
error "Failed to find program: $_return" 1
fi
# Do the do
proj "${_args[0]}"
exit 0
}
main "$@"