Last active
January 3, 2016 20:29
-
-
Save mfdj/8515732 to your computer and use it in GitHub Desktop.
Challenge: create a one-liner to install a custom "hello world" app using symfony2 standard, composer, git and standardish bash tools. Try it out — copy, paste, run: $ wget -O symfony2_hello_world.sh https://gist.github.com/mfdj/8515732/raw/87dc7c489fe6a8b1d632a2b21e411e6596116152/symfony2_hello_world.sh && chmod +x symfony2_hello_world.sh && ./…
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
#!/usr/bin/env bash | |
# Works partially with Symofny 2.5 | |
# Works fully with Symfony 2.3 — 2.4 | |
# Tested against Symfony: 2.5.0, 2.4.1, 2.3.9 | |
# Usage: | |
# symfony2_hello_world.sh <optional: project folder> <optional: symfony version> <optional: domain name> | |
# Examples: | |
# symfony2_hello_world.sh | |
# symfony2_hello_world.sh myprojectfolder | |
# symfony2_hello_world.sh nested/deeper/project 2.4.1 | |
# symfony2_hello_world.sh anotherproject 2.3.9 testdomain.local | |
### ~~~~~~~~~~~~ TODO ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
# • handle incorrect version numbers | |
# • detect asboulte paths and handle them | |
# • if bundle addition fails then offer source code to screen | |
### ~~~~~~~~~~~~ SETUP ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
### Arguments | |
ROOT=$PWD | |
if [[ -z "$1" ]]; then | |
PATH_TO=$ROOT/hello_symfony_project | |
else | |
PATH_TO=$ROOT/$1 | |
fi | |
TEMP_PATH="$ROOT/.hello_symfony_temp" | |
VER=$2 | |
DOMAIN=$3 | |
### Env | |
mkdir -p $PATH_TO | |
mkdir -p $TEMP_PATH | |
CNSL='php app/console' | |
if [ $(which composer) ]; then | |
COMPOSER='composer' | |
else | |
if [[ ! -e 'composer.phar' ]]; then | |
echo "downloading composer.phar" | |
curl -sS https://getcomposer.org/installer | php | |
fi | |
COMPOSER='php composer.phar' | |
fi | |
### ~~~~~~~~~~~~ FUNCTIONS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
getTempFile() { | |
local file=$TEMP_PATH/`date +%s` | |
echo $file | |
} | |
outputDiff_1() | |
{ | |
local name=$1 | |
cat >$name <<"EOL" | |
diff --git a/app/AppKernel.php b/app/AppKernel.php | |
index c5c9eb6..2680df4 100644 | |
--- a/app/AppKernel.php | |
+++ b/app/AppKernel.php | |
@@ -19,9 +19,7 @@ class AppKernel extends Kernel | |
); | |
if (in_array($this->getEnvironment(), array('dev', 'test'))) { | |
- $bundles[] = new Acme\DemoBundle\AcmeDemoBundle(); | |
$bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle(); | |
- $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle(); | |
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle(); | |
} | |
-- | |
1.8.4 | |
EOL | |
} | |
outputDiff_2() | |
{ | |
local name=$1 | |
cat >$name <<"EOL" | |
diff --git a/app/AppKernel.php b/app/AppKernel.php | |
index 2680df4..e839e67 100644 | |
--- a/app/AppKernel.php | |
+++ b/app/AppKernel.php | |
@@ -16,6 +16,7 @@ class AppKernel extends Kernel | |
new Symfony\Bundle\AsseticBundle\AsseticBundle(), | |
new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(), | |
new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(), | |
+ new HelloWorld\HiBundle\HiBundle(), | |
); | |
if (in_array($this->getEnvironment(), array('dev', 'test'))) { | |
-- | |
1.8.4 | |
EOL | |
} | |
outputDiff_3() | |
{ | |
local name=$1 | |
cat >$name <<"EOL" | |
diff --git a/composer.json b/composer.json | |
index dad465b..4e4b929 100644 | |
--- a/composer.json | |
+++ b/composer.json | |
@@ -15,11 +15,13 @@ | |
"symfony/assetic-bundle": "~2.3", | |
"symfony/swiftmailer-bundle": "~2.3", | |
"symfony/monolog-bundle": "~2.4", | |
- "sensio/distribution-bundle": "~2.3", | |
"sensio/framework-extra-bundle": "~3.0", | |
"sensio/generator-bundle": "~2.3", | |
"incenteev/composer-parameter-handler": "~2.0" | |
}, | |
+ "require-dev": { | |
+ "sensio/distribution-bundle": "~2.3" | |
+ }, | |
"scripts": { | |
"post-install-cmd": [ | |
"Incenteev\\ParameterHandler\\ScriptHandler::buildParameters", | |
@@ -47,6 +49,8 @@ | |
}, | |
"branch-alias": { | |
"dev-master": "2.4-dev" | |
- } | |
- } | |
+ }, | |
+ "symfony-assets-install": "symlink" | |
+ }, | |
+ "preferred-install": "dist" | |
} | |
-- | |
1.8.4 | |
EOL | |
} | |
clobberRoutingDevYaml() | |
{ | |
cat >app/config/routing_dev.yml <<EOL | |
_wdt: | |
resource: "@WebProfilerBundle/Resources/config/routing/wdt.xml" | |
prefix: /_wdt | |
_profiler: | |
resource: "@WebProfilerBundle/Resources/config/routing/profiler.xml" | |
prefix: /_profiler | |
_main: | |
resource: routing.yml | |
EOL | |
} | |
clobberSecurityYaml() | |
{ | |
cat >app/config/security.yml <<EOL | |
security: | |
# encoders: | |
# Symfony\Component\Security\Core\User\User: | |
# algorithm: bcrypt | |
# cost: 13 | |
providers: | |
in_memory: | |
memory: ~ | |
# providers: | |
# chain_provider: | |
# chain: | |
# providers: [in_memory, user_db] | |
# in_memory: | |
# memory: | |
# users: | |
# user: { password: pass } | |
# user_db: | |
# entity: { class: Symfony\Component\Security\Core\User\User, property: username } | |
firewalls: | |
secured_area: | |
anonymous: ~ | |
# firewalls: | |
# secured: | |
# pattern: ^/secured | |
# form_login: | |
# check_path: _security_check | |
# insecured: | |
# pattern: ^/(css|images|js) | |
# security: false | |
access_control: ~ | |
EOL | |
} | |
addHelloWorld() | |
{ | |
mkdir -p src/HelloWorld/HiBundle/Controller | |
mkdir -p src/HelloWorld/HiBundle/Resources/views/Default | |
cat >src/HelloWorld/HiBundle/Controller/DefaultController.php <<"EOL" | |
<?php | |
namespace HelloWorld\HiBundle\Controller; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; | |
class DefaultController extends Controller | |
{ | |
/** | |
* @Route("/", name="root") | |
* @Route("/{name}", name="named") | |
* @Template() | |
*/ | |
public function indexAction($name = '') | |
{ | |
return array('name' => $name); | |
} | |
} | |
EOL | |
# addHelloWorld | |
cat >src/HelloWorld/HiBundle/HiBundle.php <<EOL | |
<?php | |
namespace HelloWorld\HiBundle; | |
use Symfony\Component\HttpKernel\Bundle\Bundle; | |
class HiBundle extends Bundle | |
{ | |
} | |
EOL | |
# addHelloWorld | |
cat >src/HelloWorld/HiBundle/Resources/views/Default/index.html.twig <<EOL | |
{% extends '::base.html.twig' %} | |
{% block body %} | |
<style type="text/css"> | |
header { border:20px solid #eeccee; margin: 0; font-family: Avenir, Helvetic, sans-serif; } | |
header h1, header p { text-align: center; } | |
header p span { font-family: monospace; background: #cceecc; padding: 5px; } | |
</style> | |
<header> | |
<h1>Hello World!</h1> | |
{% if name != ' ' and name != '' %} | |
<p>You're on: <span>/{{ name }}</span></p> | |
{% endif %} | |
</header> | |
{% endblock %} | |
EOL | |
# END addHelloWorld | |
cat >app/config/routing.yml <<EOL | |
hi: | |
resource: "@HiBundle/Controller/" | |
type: annotation | |
prefix: / | |
EOL | |
} | |
cleanup() { | |
_e "remove temp files: '$TEMP_PATH/'?" | |
echo -n " : yes / no [y] " | |
input_e='' | |
read input_e | |
if [ "$input_e" == 'no' -o "$input_e" == 'n' ]; then | |
echo " : skipped removal" | |
else | |
echo " : removing" | |
rm -rf $TEMP_PATH | |
fi | |
} | |
_e() { | |
echo -e "\n:~~~: $1" | |
} | |
_gc() { | |
git add . --all | |
git commit -m "$1" | |
} | |
### ~~~~~~~~~~~~ WORK ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
echo -e ":~~~: running:\n : composer create-project symfony/framework-standard-edition\n : (can take a minute)" | |
$COMPOSER create-project symfony/framework-standard-edition --prefer-dist --profile $PATH_TO $VER --no-interaction | |
# echo ":~~~: just joking, copying local" | |
# cp -R ./2.4source/ $PATH_TO | |
_e "gitinit fresh symfony" | |
cd $PATH_TO | |
git init | |
_gc "Fresh Symfony Standard $VER (via composer)" | |
_e "removing upgrade, license, readme, web/config" | |
rm UPGRADE* LICENSE README.md web/config.php && _gc 'removed UPGRADE*, LICENSE, README, web/config' | |
_e "removing web: apple-touch-icon, favicon" | |
rm web/apple-touch-icon.png web/favicon.ico && _gc 'removed web: apple-touch-icon, favicon' | |
_e "removing src: AcmeDemoBundle; web/bundles: acmedemo, sensiodistribution" | |
rm -rf src/Acme web/bundles/acmedemo web/bundles/sensiodistribution && _gc 'removed src: AcmeDemoBundle' | |
_e "outputting diff patch 1 and unlpugging Acme and SensioDist" | |
DFF=`getTempFile`.diff | |
outputDiff_1 $DFF | |
git apply $DFF && _gc 'unplugged Acme and SensioDist from kernel' | |
_e "unlpugging Acme and SensioDist from routing" | |
clobberRoutingDevYaml && _gc 'unplugged Acme and SensioDist from routing_dev' | |
_e "adding helloworld bundle" | |
addHelloWorld && _gc 'added helloworld bundle' | |
_e "outputting diff patch 2 and plugging hibundle in appkernel" | |
DFF=`getTempFile`.diff | |
outputDiff_2 $DFF | |
git apply $DFF && _gc 'plugged hibundle in appkernel' | |
_e "more useful security.yml" | |
clobberSecurityYaml && _gc 'simplified security.yml' | |
_e 'tweaking composer.json: symlinks, dist, require-dev' | |
DFF=`getTempFile`.diff | |
outputDiff_3 $DFF | |
git apply $DFF && _gc 'tweaked composer.json: symlinks, dist, require-dev' | |
cleanup | |
if [[ ! -z "$DOMAIN" ]]; then | |
echo -e " | |
============================================ | |
some sample configurations for local | |
============================================ | |
# …/httpd.conf or …/extra/httpd-vhosts.conf | |
<VirtualHost *:80> | |
ServerName $DOMAIN | |
DocumentRoot '$PATH_TO/web' | |
<Directory '$PATH_TO/web'> | |
AllowOverride All | |
</Directory> | |
</VirtualHost> | |
# /etc/hosts | |
127.0.0.1 $DOMAIN | |
============================================ | |
" | |
sleep 1 | |
echo -en "Open http://$DOMAIN/you-are-here in your browser?\nyes / no [y] " | |
read input_e | |
if [ "$input_e" = '' -o "$input_e" == 'yes' -o "$input_e" == 'y' ]; then | |
_e "first, clearing cache" | |
$CNSL cache:clear --env=dev | |
$CNSL cache:clear --env=prod | |
$CNSL router:debug --env=prod | |
open "http://$DOMAIN/you-are-here" | |
_e "updating composer.lock (can take a 30-60 seconds)" | |
$COMPOSER update --lock && git add composer.lock && git commit --amend --no-edit | |
else | |
echo -en "Update composer.lock\nyes / no [y] " | |
read input_e | |
if [ "$input_e" = '' -o "$input_e" == 'yes' -o "$input_e" == 'y' ]; then | |
_e "updating composer.lock (can take a 30-60 seconds)" | |
$COMPOSER update --lock && git add composer.lock && git commit --amend --no-edit | |
fi | |
fi | |
fi | |
_e "get learning:" | |
echo " : open https://github.com/symfony/symfony-standard" | |
echo " : open http://symfony.com/doc/current/cookbook/index.html" | |
echo " : open https://github.com/mfdj/symfony2-on-pagodabox" | |
_e "get working:" | |
if [ -z "$EDITOR" ]; then | |
if hash pbcopy 2>/dev/null; then | |
echo -n "cd $PATH_TO" | pbcopy | |
echo " : cd $PATH_TO <--- copied to clipboard" | |
else | |
echo " : cd $PATH_TO" | |
fi | |
else | |
FILEA=app/Resources/views/base.html.twig | |
FILEB=src/HelloWorld/HiBundle/Resources/views/Default/index.html.twig | |
FILEC=app/config/routing.yml | |
FILED=src/HelloWorld/HiBundle/Controller/DefaultController.php | |
echo " : cd $PATH_TO" | |
echo " : $EDITOR $FILEA" | |
echo " : $EDITOR $FILEB" | |
echo " : $EDITOR $FILEC" | |
echo " : $EDITOR $FILED" | |
if hash pbcopy 2>/dev/null; then | |
echo -n "cd $PATH_TO && $EDITOR $FILEA $FILEB $FILEC $FILED" | pbcopy | |
echo " : ~~~~~~~~ above copied to clipboard as a one-liner ~~~~~~~" | |
echo " : ~~~~~ (paste+run if you're into that kind of thing) ~~~~~" | |
fi | |
fi | |
_e "tip: changes in configuration or routing generally require cache:clear:" | |
echo " : php app/console cache:clear -e dev" | |
if [[ ! -z "$DOMAIN" ]]; then | |
echo " : open http://$DOMAIN/app_dev.php/" | |
fi | |
echo " : php app/console cache:clear -e prod" | |
if [[ ! -z "$DOMAIN" ]]; then | |
echo " : open http://$DOMAIN/" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment