Skip to content

Instantly share code, notes, and snippets.

@DonBattery
Last active July 8, 2020 14:52
Show Gist options
  • Save DonBattery/2ff33e60453513dd891d05df4efc6f84 to your computer and use it in GitHub Desktop.
Save DonBattery/2ff33e60453513dd891d05df4efc6f84 to your computer and use it in GitHub Desktop.
Fast POC bootstrap with BASH
#!/usr/bin/env bash
set -eou pipefail
GIMME_DIR="${HOME}/dev/gimme"
LEGAL_PROJ_TYPES=("go" "bash" "js" "py")
function usage {
echo "
Gimme is a fast project bootstrapping script.
It accepts one or two arguments regarding the project type and project name.
Unnamed projects will be given a random name (this can be changed later)
and untyped projects will be given the'etc' type
Examples:
Creating a named Go project
$ gimme go <projectname>
Creating a BASH project with a random name
$ gimme bash
Creating a named etc project
$ gimme <projectname>
List all the Gimme projects (this command depends on the 'tree' apt package)
$ gimme ls
Rename a Gimme project (issue this command inside the folder you want to rename)
$ gimme rm <another-projectname>
Print usage (this very meassage)
$ gimme help
"
exit 0
}
function is_legal {
for elem in "${LEGAL_PROJ_TYPES[@]}"; do
if [[ "$elem" == "${1}" ]]; then
return 0
fi
done
return 1
}
function is_gimme_dir {
if [[ "${1}" == "${GIMME_DIR}"* ]]; then
return 0
fi
return 1
}
function get_random_name {
local prefixes=("big" "golden" "epic" "good" "red" "hot" "new" "legacy" "inmortal" "first")
local names=("shit" "project" "app" "software" "core" "framework" "link" "engin" "thingy")
local suffixes=("v0.0.1" "final" "beta" "alpha" "dev" "prod" "staging" "version-911" "trash" "mock" "poc" "test")
local random_pre=${prefixes[$RANDOM % ${#prefixes[@]} ]}
local random_name=${names[$RANDOM % ${#names[@]} ]}
local random_suf=${suffixes[$RANDOM % ${#suffixes[@]} ]}
echo -n "${random_pre}-${random_name}-${random_suf}"
}
# Create the project by type arg #1 and name arg #2
# if it is already exists just start VS Code in it
function create_project {
PROJ_DIR="${GIMME_DIR}/${1}/${2}"
if [[ -d "${GIMME_DIR}/${1}/${2}" ]]; then
echo "${PROJ_DIR} already exists"
cd "${PROJ_DIR}"
code .
else
echo "Creating ${PROJ_DIR}"
mkdir -p "${PROJ_DIR}"
cd "${PROJ_DIR}"
prepare ${1}
code .
fi
}
# prepare is a switch function of the actual prepare functions
function prepare {
case "$1" in
"go") prepare_go
;;
"bash") prepare_bash
;;
"js") prepare_js
;;
"py") prepare_py
;;
*) prepare_etc
;;
esac
}
# Prepare Go project
function prepare_go {
echo "Processing Go ..."
cat >main.go <<EOL
package main
import (
"fmt"
)
func main() {
fmt.Println("asdf")
}
EOL
echo "main.go created ..."
}
# Prepare BASH project
function prepare_bash {
echo "Processing BASH ..."
cat >main.sh <<EOL
#!/usr/bin/env bash
echo "yolo"
EOL
echo "main.sh created ..."
chmod +x main.sh
echo "main.sh made executable ..."
}
# Prepare JavaScript project
function prepare_js {
echo "Processing JavaScript ..."
cat >main.js <<EOL
#!/usr/bin/env node
"use strict";
console.log("yolo");
EOL
echo "main.js created ..."
chmod +x main.js
echo "main.js made executable ..."
}
# Prepare Python project
function prepare_py {
echo "Processing Python ..."
cat >main.py <<EOL
#!/usr/bin/env python3
print("yolo")
EOL
echo "main.py created ..."
chmod +x main.py
echo "main.py made executable ..."
}
# Prepare etc project
function prepare_etc {
echo "Processing etc ..."
cat >notes <<EOL
yolo
EOL
echo "notes created ..."
}
function main {
# Set the project type to 'etc' and the project name to a random name
PROJ_TYPE="etc"
PROJ_NAME="$(get_random_name)"
# If Gimme was called without any argument
if [[ $# -eq 0 ]]; then
usage
fi
# If there is only one argument
if [[ $# -eq 1 ]]; then
# And it is "ls" list the gimme folders
if [[ "${1}" == "ls" ]]; then
# Directories only, 2 level deep
tree -d -L 2 "${GIMME_DIR}"
exit 0
fi
# If it is -h --help or help print usage and exit with 0
if [[ "${1}" == "help" || "${1}" == "-h" || "${1}" == "--help" ]]; then
usage
fi
# If it is a legal project type set it as PROJ_TYPE
if is_legal "${1}"; then
PROJ_TYPE="${1}"
# Else set it as PROJ_NAME
else
PROJ_NAME="${1}"
fi
# If there are two or more arguments (only consider the first two)
elif [[ $# -ge 2 ]]; then
# If the first is "rm": rename the current dir
if [[ "${1}" == "rm" ]]; then
# If the pwd is a gimme dir, rename it to the second argument
if is_gimme_dir "${PWD}"; then
newpwd="${PWD%/*}/${2}"
echo "Renaming ${PWD} to ${newpwd}"
mv "${PWD}" "${newpwd}"
exit 0
else
echo "Cannot rename PWD: ${PWD} as it is not a Gimme dir"
exit 1
fi
fi
# If the first argument is a legal project type set PROJ_TYPE to it, else leave it on "etc"
if is_legal "${1}"; then
PROJ_TYPE="${1}"
fi
# Set the PROJ_NAME to the second arg
PROJ_NAME="${2}"
fi
# Create the project
create_project ${PROJ_TYPE} ${PROJ_NAME}
}
# Call main with all the arguments of this script
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment