snippetbashMinor
Script to generate templated PHP code
Viewed 0 times
scripttemplatedphpgeneratecode
Problem
I've created a script to create a module in our MVC framework. The structure of the module is like this (for this example, the module is called
Here's what I have for the script. If can have an optional flag of -o to allow the script to remove and override the folder is it already exists.
Is there anything bad/suboptimal, or that could b
test):test >
Test.php
controllers >
IndexController.php
models >
TestModel.php
open >
css >
js >
views >Here's what I have for the script. If can have an optional flag of -o to allow the script to remove and override the folder is it already exists.
#!/bin/bash
OVERRIDE=false
while getopts ":o" opt; do
case $opt in
o ) OVERRIDE=true
esac
done
# Check we're in the right directory.
if [ ${PWD##*/} != "modules" ]; then
echo "Make sure you're in the modules directory"
exit 1
fi
echo "Enter the module name you want:"
# Get the name, and format it into the formats we need.
read moduleName
lowercaseModuleName=${moduleName,,}
uppercaseFirstModuleName=${lowercaseModuleName^}
# Check if the directory exists already.
if [ -d $lowercaseModuleName ]
then
if [ "$OVERRIDE" = true ]; then
echo "Directory already exists, overridding it because of the -o flag"
rm -rf $lowercaseModuleName
else
echo "Directory '$lowercaseModuleName' already exists, please remove if and try again. To have it overridden please use the -o flag"
exit 1;
fi
fi
# Make dir.
echo "Adding module $lowercaseModuleName..."
mkdir $lowercaseModuleName
cd $lowercaseModuleName
# Make root class
echo "Adding Root class..."
cat > $uppercaseFirstModuleName.php IndexController.php $uppercaseFirstModuleName"Model.php" << EOF
<?php
class ${uppercaseFirstModuleName}Model extends Layer_Model {
}
EOF
# Add open folder
echo "Adding open folder..."
cd ../
mkdir open
cd open
mkdir css js
cd ../
# Add view folder
echo "Adding view folder..."
mkdir views
cd ../
echo "Done! Module $uppercaseFirstModuleName created."Is there anything bad/suboptimal, or that could b
Solution
just a few ideas from the top of my head.
I would define a variable with your start directory at the top of the script, so to that you can always refer to absolute paths in contrast to using
Now the directory creation can be simplified:
As for the structure, I'd probaly create all the directories in one 'paragraph' and add the templates in a second step, just so that you one place where you can clearly see which directories are being created. But that might just be my personal tast.
In the template creation I'd use absolute paths here as well, using the new
Hope this helps!
I would define a variable with your start directory at the top of the script, so to that you can always refer to absolute paths in contrast to using
cd ../ often. In case you ever change the structure of your script, this will ensure that everything keeps working.MODULE_DIR=$(readlink -f $lowercaseModuleName)Now the directory creation can be simplified:
# Add open folder
echo "Adding open folder..."
mkdir -p ${MODULE_DIR}/open/{css,js}/
# Add views folder
echo "Adding view folder..."
mkdir ${MODULE_DIR}/viewsAs for the structure, I'd probaly create all the directories in one 'paragraph' and add the templates in a second step, just so that you one place where you can clearly see which directories are being created. But that might just be my personal tast.
In the template creation I'd use absolute paths here as well, using the new
MODULE_DIR variable. Keeps you from having to navigate around a lot using cd which might cause confusion later on. Hope this helps!
Code Snippets
MODULE_DIR=$(readlink -f $lowercaseModuleName)# Add open folder
echo "Adding open folder..."
mkdir -p ${MODULE_DIR}/open/{css,js}/
# Add views folder
echo "Adding view folder..."
mkdir ${MODULE_DIR}/viewsContext
StackExchange Code Review Q#69729, answer score: 3
Revisions (0)
No revisions yet.