Skip to content

Instantly share code, notes, and snippets.

@jhyland87
Created May 16, 2017 17:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhyland87/a177f9a930102373819bf54c2e503169 to your computer and use it in GitHub Desktop.
Save jhyland87/a177f9a930102373819bf54c2e503169 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/bash4
function stderr {
echo $@ 1>&2
}
function abspath {
[[ -d $1 ]] && { cd "$1"; echo "$(pwd -P)"; } ||
{ cd "$(dirname "$1")"; echo "$(pwd -P)/$(basename "$1")"; }
}
# Function to create a new script, make it executable, add the shebang and other
# docblock comments, then open it in the preferred or specified editor
#
# Examples:
# # Create a test.sh, add the shebang for bash, open in atom
# shebang test.sh bash atom
# # Create a test.sh, add the shebang for bash, open in $EDITOR (or $VISUAL)
# shebang test.sh bash4
#
# Todos:
# 1) Should be able to determine the executable by the file extension
#
function shebang {
declare -A editors
# The editors should be the full command used to open a file
# Replacement Strings:
# %script% - Replaced with scripts absolute path
# %wc-l% - Replaced with the scripts line count
editors['subl']="/Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl --add %script%:%wc-l%"
editors['atom']="/Applications/Atom.app/Contents/MacOS/Atom --add %script%:%wc-l%"
editors['vim']="/usr/bin/vim +%wc-l% %script%"
editors['vi']="/usr/bin/vi +%wc-l% %script%"
editors['nano']="/usr/bin/nano +%wc-l% %script%"
# Make sure a script name is provided
if [[ -z $1 ]]; then
stderr "No script name specified"
return 1
fi
declare -A script
script['arg']="${1}"
script['abspath']=$(abspath "${script['arg']}")
script['dirname']=$(dirname "${script['abspath']}")
script['basename']=$(basename "${script['abspath']}")
if [[ -a "${script['abspath']}" ]]
then
stderr "The script \"${script['abspath']}\" already exists"
return 1
fi
# Create the file
touch "${script['abspath']}"
if [[ $? -ne 0 ]]
then
stderr "Failed to create a script at ${script['abspath']}"
return 1
fi
# Set the permissions
chmod +x "${script['abspath']}"
if [[ $? -ne 0 ]]
then
stderr "Failed to add executable permissions to ${script['abspath']}"
return 1
fi
# If an executable was provided (for the shebang line), then verify it exists, and create the shebang
if [[ -n $2 ]]
then
bin=$(which "${2}")
if [[ $? != 0 ]]
then
stderr "Error: Unable to find a \"${2}\" executable in your \$PATH (${PATH})"
return 1
else
# Make the script
echo -e "#! ${bin}\n" >> "${script['abspath']}"
fi
fi
docblock_format="# %-10s : %s\n"
printf "${docblock_format}" "Author" $(whoami) >> "${script['abspath']}"
printf "${docblock_format}" "Created" $(date +%Y-%m-%d) >> "${script['abspath']}"
printf "${docblock_format}" "Name" "${script['basename']}" >> "${script['abspath']}"
printf "${docblock_format}" "Directory" "${script['dirname']}" >> "${script['abspath']}"
[[ -n $bin ]] && printf "${docblock_format}" "Executable" "${bin}" >> "${script['abspath']}"
echo -e "\n\n" >> "${script['abspath']}"
script_wc=$(wc -l "${script['abspath']}" | awk '{print $1}')
# If a custom editor was specified
if [[ -n $3 ]]
then
# Custom editor specified
if test "${editors[$3]}"
then
#printf "Executing: %s %s\n" "${editors[$3]}" "${script['abspath']}"
# Specified editor IS in scripts array
cmd=$(echo "${editors[$3]}" | sed "s|%script%|${script['abspath']}|g")
cmd=$(echo "${cmd}" | sed "s|%wc-l%|${script_wc}|g")
#echo "Executing: ${cmd}"
eval ${cmd}
if [[ $? -ne 0 ]]; then
stderr "There was an error while opening ${script['abspath']} using $3"
return 1
fi
echo "Successfully opened ${script['abspath']} using $3"
return 0
else
# Specified editor is NOT in scripts array - Check if its an executable
edit_bin=$(which ${3})
if [[ $? -ne 0 ]]; then
stderr "The custom text editor specified \"${3}\" does not exist"
return 1
fi
${edit_bin} "${script['abspath']}"
if [[ $? -ne 0 ]]; then
stderr "There was an error while opening ${script['abspath']} using ${edit_bin}"
return 1
fi
echo "Successfully opened ${script['abspath']} using ${edit_bin}"
return 0
fi
elif [[ -n ${VISUAL} ]] || [[ -n ${FCEDIT} ]] || [[ -n ${EDITOR} ]]
then
env_editor=${FCEDIT:-${VISUAL:-${EDITOR:-vi}}}
${env_editor} "${script['abspath']}"
if [[ $? -ne 0 ]]; then
stderr "There was an error while opening ${script['abspath']} using ${env_editor}"
return 1
fi
echo "Successfully opened ${script['abspath']} using ${env_editor}"
return 0
else
vim "${script['abspath']}"
fi
}
# /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl --add "${script['abspath']}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment