Created
January 30, 2011 17:49
-
-
Save vaclavbohac/803051 to your computer and use it in GitHub Desktop.
Stupid model scaffolding for Nette Framework.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Generate model class and SQL. | |
# This bash script gives you some functionality similiar to rails scaffolding. | |
# It creates: | |
# - a file with model class | |
# - a file with SQL to create table with that model | |
# - a rollback file with SQL to drop that table | |
# Created by Vaclav Bohac (c) 2011. | |
# usage: scaf.sh generate model Article title:string body:text | |
MODELS_DIR="`pwd`/models" | |
MIGRATIONS_DIR="`pwd`/migrations" | |
help () { | |
echo "usage: scaf.sh generate model 'ModelName column:type column:type'" | |
echo "For more information check the man page." | |
} | |
testEnvironment () { | |
echo "Running tests in `pwd`:" | |
# Test if model folder exists. | |
if [ -d "$MODELS_DIR" ]; then | |
echo "OK: Models directory exists." | |
else | |
echo "FAIL: Models directory does not exist." | |
fi | |
# Test if script has priviledges to write to the model folder. | |
if [ -w "$MODELS_DIR" ]; then | |
echo "OK: Models directory has sufficient priviledges." | |
else | |
echo "FAIL: Models directory does not have sufficient priviledges." | |
fi | |
# Test if migrations folder exists. | |
if [ -d "$MIGRATIONS_DIR" ]; then | |
echo "OK: Migrations directory exists." | |
else | |
echo "FAIL: Migrations directory does not exist." | |
fi | |
# Test if script has priviledges to write to the migrations folder. | |
if [ -w "$MIGRATIONS_DIR" ]; then | |
echo "OK: Migrations directory has sufficient priviledges." | |
else | |
echo "FAIL: Migrations directory does not have sufficient priviledges." | |
fi | |
} | |
if [ ! -n "$1" ] || [ "$1" = "--help" ]; then | |
help | |
exit 0 | |
fi | |
# Generate some stuff. | |
if [ "$1" = "generate" ] || [ "$1" = "g" ]; then | |
shift # get next argument | |
if [ ! "$1" = "model" ]; then | |
echo "You cannot generate anything else then 'model' at the moment." | |
help | |
exit 1 | |
else | |
shift | |
# Create model file. | |
modelname="$1" | |
modelfile="$MODELS_DIR/${modelname}sModel.php" | |
echo "[X] Creating model $modelname" | |
touch $modelfile | |
echo -e "<?php\n\n/**\n * @author ${USER}\n * @table $modelname\n **/\nclass ${modelname} extends BaseModel\n{\n\t// More is up to you.\n}" > $modelfile | |
# Create SQL file. | |
migrationfile="$MIGRATIONS_DIR/$modelname.sql" | |
echo "[X] Creating SQL file $modelname" | |
touch $migrationfile | |
# Open create table. | |
echo -e "CREATE TABLE ${modelname,,}s (" > $migrationfile | |
# Create primary key. | |
echo -e "\t\`id\` int NOT NULL AUTO_INCREMENT PRIMARY KEY," >> $migrationfile | |
# Create user defined columns. | |
until shift && [ "$1" = "" ] | |
do | |
columnname="\t\`${1%%:*}\`" | |
columntype="${1##*:} NOT NULL" | |
echo -e "$columnname $columntype," >> $migrationfile | |
done | |
# Create time related columns. | |
echo -e "\t\`created\` datetime NOT NULL," >> $migrationfile | |
echo -e "\t\`updated\` timestamp NULL" >> $migrationfile | |
# Close create table. | |
echo -e ") ENGINE='InnoDB' COLLATE='utf8_czech_ci'" >> $migrationfile | |
# Substitute string for VARCHAR(512). | |
echo "[X] Translating into MySQL syntax." | |
sed -i "s/string/VARCHAR\(512\)/g" $migrationfile | |
# Create rollback SQL. | |
rollbackfile="$MIGRATIONS_DIR/${modelname}-rollback.sql" | |
echo "[X] Creating rollback SQL file." | |
touch $rollbackfile | |
echo -e "DROP TABLE \`${modelname,,}\`" > $rollbackfile | |
fi | |
# Test folder and priviledges. | |
elif [ "$1" = "test" ]; then | |
testEnvironment | |
else | |
echo "There is something wrong!" | |
help | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment