Last active
November 21, 2017 17:36
-
-
Save jessedp/f1faa66696b88fe1da555877170bcfab to your computer and use it in GitHub Desktop.
Bash script to help get a Slim Framework clean or skeleton app installed.
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 | |
# Some sanity checks... | |
if [ "$#" == 0 ] || [ "$#" -gt 1 ] || [ -d $1 ]; then | |
echo "Please supply a unique project (directory) name!" | |
exit -1 | |
fi | |
if [ ! $(which php) ]; then | |
echo "php cli binary must be installed!" | |
exit -1 | |
fi | |
projectType="" | |
projectDir="$1" | |
projectPath="$(pwd)/$projectDir" | |
# globalComposer is ugly. "which" gives us a path or nothing... then we overwrite it with 0 (yes, use it) or 1 (go local) later | |
globalComposer=$(which composer) | |
# You guessed it, function to gather y/n input | |
function ynInput() { | |
read -p " | |
$1 " choice | |
case "$choice" in | |
y|Y) return 0;; | |
n|N) return 1;; | |
* ) echo "Invalid input..." | |
return $(ynInput "$1") | |
;; | |
esac | |
} | |
# Function to encase the composer setup options | |
function composerInit() { | |
local composer_cmd="" | |
if [ $globalComposer ]; then | |
ynInput "Global composer install found. Use that (y) or install locally (n)?" | |
globalComposer=$? | |
fi | |
setProjectType | |
if [ "$globalComposer" -eq "0" ]; then | |
echo "Using globally installed composer..." | |
composer_cmd="composer" | |
else | |
# straight from the composer install instuctions at: | |
# https://getcomposer.org/download/ | |
echo "Installing composer for local use..." | |
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" | |
if [ ! -f "composer-setup.php" ]; then | |
echo "composer download failed, see above" | |
exit -1 | |
fi | |
php -r " | |
if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { | |
echo 'Installer verified'; | |
} else { | |
echo 'Installer corrupt'; | |
unlink('composer-setup.php'); } echo PHP_EOL;" | |
if [ ! -f "composer-setup.php" ]; then | |
echo "composer download failed, see above" | |
exit -1 | |
fi | |
php composer-setup.php | |
php -r "unlink('composer-setup.php');" | |
composer_cmd="php composer.phar" | |
fi | |
if [ "$projectType" == "skel" ]; then | |
$composer_cmd create-project slim/slim-skeleton $projectDir | |
moveComposer | |
else | |
echo "Creating project dir: $projectDir" | |
mkdir -p "$projectDir/src/public" | |
moveComposer | |
cd "$projectDir" | |
$composer_cmd require slim/slim | |
fi | |
} | |
function moveComposer() { | |
if [ "$globalComposer" -ne "0" ]; then | |
echo " | |
** Moved composer.phar to $projectDir/ ... you can use it with 'php composer.phar' there going forward | |
" | |
mv composer.phar $projectDir/ | |
fi | |
} | |
# very basic git init stuffs | |
function gitInit() { | |
if [ $(which git) ]; then | |
ynInput "Init git things (y/n)?" | |
if [ "$?" -eq "0" ]; then | |
cd "$projectDir" | |
git init | |
echo "vendor/*" > .gitignore | |
git add -A | |
git commit -m "Initial commit of $projectDir" | |
fi | |
fi | |
} | |
function setProjectType() { | |
ynInput "Install skeleton project (y) or start from scratch (n)? " | |
if [ "$?" == "0" ]; then | |
projectType="skel" | |
else | |
projectType="clean" | |
fi | |
} | |
function apacheConfig() { | |
if [ $(which httpd) ] || [ $(which apache2) ]; then | |
local path="" | |
if [ "$projectType" == "skel" ]; then | |
path="$projectPath/public" | |
else | |
path="$projectPath/src/public" | |
fi | |
ynInput "FOUND Apache! | |
- install .htaccess to $path/ | |
- and see sample vhost config? (y/n) " | |
if [ "$?" -eq "0" ]; then | |
echo "** Adding .htaccess in '$path'" | |
echo "<IfModule mod_rewrite.c> | |
RewriteEngine On | |
# Some hosts may require you to use the `RewriteBase` directive. | |
# Determine the RewriteBase automatically and set it as environment variable. | |
# If you are using Apache aliases to do mass virtual hosting or installed the | |
# project in a subdirectory, the base path will be prepended to allow proper | |
# resolution of the index.php file and to redirect to the correct URI. It will | |
# work in environments without path prefix as well, providing a safe, one-size | |
# fits all solution. But as you do not need it in this case, you can comment | |
# the following 2 lines to eliminate the overhead. | |
RewriteCond %{REQUEST_URI}::\$1 ^(/.+)/(.*)::\2\$ | |
RewriteRule ^(.*) - [E=BASE:%1] | |
# If the above doesn't work you might need to set the `RewriteBase` directive manually, it should be the | |
# absolute physical path to the directory that contains this htaccess file. | |
# RewriteBase / | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteRule ^ index.php [QSA,L] | |
</IfModule> | |
" > "$path/.htaccess" | |
echo "** Sample vhost config | |
----------------------------------------------------------------- | |
<VirtualHost $projectDir:80> | |
ServerName $projectDir | |
DocumentRoot $path/ | |
ServerSignature on | |
<Directory $path/> | |
Options Indexes FollowSymLinks MultiViews | |
AllowOverride All | |
Require all granted | |
</Directory> | |
</VirtualHost> | |
----------------------------------------------------------------- | |
" | |
vhostNote | |
fi | |
fi | |
} | |
function nginxConfig() { | |
if [ $(which nginx) ]; then | |
local path="" | |
if [ "$projectType" == "skel" ]; then | |
path="$projectPath/pubic" | |
else | |
path="$projectPath/src/public" | |
fi | |
ynInput "FOUND Nginx! | |
- see sample vhost config? (y/n) " | |
if [ "$?" -eq "0" ]; then | |
echo "** Sample vhost config | |
----------------------------------------------------------------- | |
server { | |
listen 80; | |
listen [::]:80; | |
root $path/ | |
index.php; | |
server_name $projectDir; | |
location / { | |
if (!-e \$request_filename){ | |
rewrite ^(.*)$ /index.php break; | |
} | |
try_files $uri $uri/ =404; | |
} | |
} | |
----------------------------------------------------------------- | |
" | |
vhostNote | |
fi | |
fi | |
} | |
function vhostNote() { | |
echo "note: '$projectDir' is simply the base directory you used. It can be anything hostname compliant AND you may need to configure it in your /etc/hosts file." | |
} | |
# set necessary file/dir permissions | |
function setPermissions() { | |
ynInput "Set permissions - files to 664, dirs to 755, logs/ to 777 (y/n)? " | |
if [ "$?" -eq "0" ]; then | |
#could try to set web group here, but fraught with platform difficulties :( | |
#echo "Ensure '$USER' is in www-data group" | |
#sudo usermod -a -G www-data $USER | |
cd "$projectDir" | |
echo "Set all file permissions to 664" | |
find . -type f -exec chmod 664 {} \; | |
echo "Set all directory permissions to 775" | |
find . -type d -exec chmod 755 {} \; | |
mkdir -p logs | |
chmod 777 logs/ | |
fi | |
} | |
# "main" - make all the things happen | |
composerInit | |
apacheConfig | |
nginxConfig | |
setPermissions | |
gitInit | |
echo " | |
Slim app initialized! | |
now 'cd $projectPath' to get started. | |
" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment