65 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Bash
		
	
	
	
	
	
			
		
		
	
	
			65 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Bash
		
	
	
	
	
	
#! /bin/bash
 | 
						|
#
 | 
						|
# .functions
 | 
						|
# Functions for interactive shells
 | 
						|
#
 | 
						|
 | 
						|
proj() {
 | 
						|
	# Ensure we have an argument
 | 
						|
	if [ -z ${1+x} ]; then
 | 
						|
		echo "proj: requires argument"
 | 
						|
		return 1
 | 
						|
	fi
 | 
						|
	# POSIX mandates this external call to sed
 | 
						|
	projname="$(echo "$1" | sed -re 's/[^ a-zA-Z0-9.-]//g')"
 | 
						|
	projdir="$HOME/Projects/$projname"
 | 
						|
	# Ensure we have a ~/Projects directory
 | 
						|
	mkdir -p "$HOME/Projects" > /dev/null 2>&1
 | 
						|
	# cd into the project or make it if necessary
 | 
						|
	if [ -d "$projdir" ]; then
 | 
						|
		# It exists
 | 
						|
		cd "$projdir" || return 50
 | 
						|
		# Run code if we have it
 | 
						|
		# The fun part is this environment file can access some vars about the proj
 | 
						|
		local envfile="$projdir/.project-env"
 | 
						|
		if [ -r "$envfile" ]; then
 | 
						|
			. "$envfile"
 | 
						|
			echo "Sourced environment file for project"
 | 
						|
			# Also mark an envvar to ensure we can detect this condition
 | 
						|
			export PROJECT="$projname"
 | 
						|
		fi
 | 
						|
	else
 | 
						|
		# It does not exist
 | 
						|
		echo "Creating new project \"$projname\""
 | 
						|
		mkdir -p "$projdir"
 | 
						|
		cd "$projdir" || return 51
 | 
						|
		if command -v git > /dev/null 2>&1; then
 | 
						|
			# Initialize git
 | 
						|
			echo "Initializing git with .gitignore"
 | 
						|
			git init > /dev/null 2>&1
 | 
						|
			echo '*.swp' > .gitignore
 | 
						|
			git add .gitignore > /dev/null 2>&1
 | 
						|
			git commit -am "Create gitignore" > /dev/null 2>&1
 | 
						|
			git status
 | 
						|
		fi
 | 
						|
	fi
 | 
						|
}
 | 
						|
# Autocompletion for bash
 | 
						|
# A note on the shellcheck disable: that's fine, because this is also a test
 | 
						|
# If it fails, we don't even define a completion function
 | 
						|
# shellcheck disable=2039
 | 
						|
complete -F _proj proj > /dev/null 2>&1 && \
 | 
						|
_proj() {
 | 
						|
	[ "${#COMP_WORDS[@]}" != "2" ] && return 0
 | 
						|
	for dir in "$HOME"/Projects/*; do
 | 
						|
		reply="$(basename "$dir")"
 | 
						|
		reply="${reply//[^ \-a-zA-Z0-9.]/}"
 | 
						|
		# shellcheck disable=2179
 | 
						|
		COMPREPLY+=" $reply"
 | 
						|
	done
 | 
						|
	unset reply
 | 
						|
	# shellcheck disable=2178
 | 
						|
	COMPREPLY=($(compgen -W "$COMPREPLY" "${COMP_WORDS[COMP_CWORD]}"))
 | 
						|
	return 0
 | 
						|
}
 |