Skip to content

Instantly share code, notes, and snippets.

@liquidgenius
Last active November 10, 2021 12:22
Show Gist options
  • Save liquidgenius/56167cfedc7270752d207377cf618fcf to your computer and use it in GitHub Desktop.
Save liquidgenius/56167cfedc7270752d207377cf618fcf to your computer and use it in GitHub Desktop.
A Bash based one liner for setting up Masonite 3 projects if using pyenv and pipenv. Also makes Pycharm project setup much simpler. Just select the interpreter from the in project .venv directory after opening the project directory in Pycharm. Tested on OSX Big Sur.
#! /bin/bash
# License: MIT; https://opensource.org/licenses/MIT
# Version: 0.0.1
# Maintainer: https://gist.github.com/liquidgenius
# Gist: https://gist.github.com/liquidgenius/56167cfedc7270752d207377cf618fcf
#*******************************************************************************
# Masonite 3 Project Scaffolding
#*******************************************************************************
# Creates a new pipenv masonite python project with a specific
# version of Python from the masonite cookie cutter repository for
# use with Pycharm. Use Pycharm to open the project. Assumes you are
# using pyenv to manage python versions. Interpreter will always
# be in the .venv folder at the root of the project.
# USAGE
# Create Masonite project with masonite-validation, pandas and numpy
# installing everything into a src subfolder with the .venv interpreter
# at the project root.
# sh scaffolder.sh --validation true --mods "pandas numpy"
# Create Masonite project named test, with numpy installing everything into
# the project root with the .venv interpreter at the project root.
# sh scaffolder.sh --name test --pyver "3.9.1" --src false --mods "numpy"
# FUTURE IMPROVEMENTS
# Check if pyenv is installed "which pyenv"
# If pyenv is installed check if version of python is installed "pyenv versions"
# If pyenv does not have that version of python, install it
# Check if pipenv is installed "which pipenv"
# Fail gracefully if pyenv or pipenv are not installed provide install links
# Arguments
name=${name:-""}
pyver=${pyver:-""}
orm=${orm:-true}
logging=${logging:-true}
events=${events:-true}
scheduler=${scheduler:-false}
validation=${validation:-false}
src=${src:-true}
mods=${mods:-""}
# Assert for required apps
has_pyenv="$(which pyenv)"
if [ "$has_pyenv" == "" ] ; then
echo "\nPyenv not found\nPlease install pyenv"
echo "\nPyenv: https://github.com/pyenv/pyenv\n\
Install: https://github.com/pyenv/pyenv-installer"
echo "\nQuitting\n"
exit 1 ; fi
has_pipenv="$(which pipenv)"
if [ "$has_pipenv" == "" ] ; then
echo "\nPipenv not found\nPlease install pipenv"
echo "\nPipenv: https://pipenv.pypa.io/en/latest/"
echo "\nQuitting\n"
exit 1 ; fi
# Catch required fields if empty
if [ $name = ""] ; then
echo "\nWhat is the project's name?"
read name
fi
if [ $pyver = ""] ; then
echo "\nInstalled Pyenv Python Versions:"
pyenv versions
echo "Use which pyenv python version?"
read pyver
fi
# Variables
cwd=$(pwd)
core="masonite masonite-validation"
install=""
verbosity=-1
proj_venv=1
divider="**********************************************************************"
# Parse arguements
while [ $# -gt 0 ]; do
if [[ $1 == *"--"* ]]; then
param="${1/--/}"
declare $param="$2"
fi
shift
done
# Install path based on src flag
if [ $src = true ] ; then
projpath="$cwd/$name/src"
else
projpath="$cwd/$name" ; fi
# Module string manipulations
install="$core"
if [ $orm = true ] ; then
install="$install masonite-orm" ; fi
if [ $logging = true ] ; then
install="$install masonite-logging" ; fi
if [ $events = true ] ; then
install="$install masonite-events" ; fi
if [ $scheduler = true ] ; then
install="$install masonite-scheduler" ; fi
install="$install $mods"
# Console output
echo "\n$divider"
echo "Scaffolding Masonite 3 Project: $name"
echo "$divider"
echo "\nRequires pyenv and pipenv installed. PyCharm compatible, simply open \
the created \ndirectory and assign the in-project .venv folder as the interpreter."
echo "\nThe following modules will be installed:\n$install"
# Setup directories
if [ $src = true ] ; then
echo "\nUse src subfolder"
echo "\nMaking directory: $cwd/$name\nMaking directory: $projpath"
mkdir "$cwd/$name" "$projpath"
echo "Changing to: $cwd/$name"
cd "$cwd/$name"
echo "Pyenv setting local (and subfolder's) python: $pyver"
pyenv local $pyver
echo "Changing to: $projpath"
cd "$projpath"
else
echo "\nUse root project folder (no src subfolder)"
echo "\nMaking directory: $cwd/$name"
mkdir "$cwd/$name"
echo "Changing to: $projpath"
cd "$projpath"
echo "Pyenv setting local (and subfolder's) python: $pyver\n"
pyenv local $pyver ; fi
# Create Pipenv environment
PIPENV_VERBOSITY=$verbosity PIPENV_VENV_IN_PROJECT=$proj_venv \
pipenv install --python $pyver
# Install required modules
pipenv install $install
# GIT Pull the masonite cookie-cutter project
echo "\nInstalling Masonite Cookie Cutter base project"
pipenv run craft new
# Add additional directories
extra_dirs="app/services app/helpers app/observers databases/seed_sources"
extra_dir_arr=($extra_dirs)
echo "\nAdding extra directories:"
for extra_dir in ${extra_dir_arr[@]}
do
echo " $extra_dir"
mkdir "$projpath/$extra_dir"
done
# Publish Masonite Logging Provider
if [ $logging = true ] ; then
echo "\nPublishing LoggingProvider"
pipenv run craft publish LoggingProvider ; fi
# Move key files and interpreter to project root if using src
if [ $src = true ] ; then
file1=".venv"
file2=".gitignore"
mv "$file1" "$cwd/$name/"
mv "$file2" "$cwd/$name/"
mv Pipfile "$cwd/$name/"
mv Pipfile.lock "$cwd/$name/" ; fi
# Final instructions
echo "\n$divider"
echo "Removing scaffolding, $name ready"
echo "$divider"
echo "\nProject: $cwd/$name"
echo "Interpreter: $cwd/$name/.venv/bin/python"
echo "Delete project: rm -fr $cwd/$name"
printf "\ncd to the project directory, where you may run commands:\n\
pipenv run craft\n\
pipenv run craft migrate\n\
pipenv run craft seed:run\n\
pipenv run craft migrate:refresh\n\
pipenv run craft serve"
echo "\n\nMasonite Docs: https://docs.masoniteproject.com/"
echo "Masonite ORM Docs: https://orm.masoniteproject.com/"
echo "\nHappy Crafting!\n"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment