Skip to content

Instantly share code, notes, and snippets.

@grimen
Last active December 24, 2015 20:29
Show Gist options
  • Save grimen/6858545 to your computer and use it in GitHub Desktop.
Save grimen/6858545 to your computer and use it in GitHub Desktop.
Automaticall generate unique/predicatable/consistent $PORT for each project based on fingerprinting of `.powder` and make Pow Proxy aware of it.
# custom ENV variables
#!/usr/bin/env bash
echo "[.powenv]: PORT: $PORT"
echo "[.powenv]: project path: $(pwd)"
# Detect ".env" file path.
if [ -z $ENV_FILE ]
then ENV_FILE="./.env"
fi
echo "[.powenv]: project .env: $ENV_FILE"
# Load ".env" file.
if [ -f $ENV_FILE ]
then source $ENV_FILE
fi
# Detect unique project file to generate default PORT based on.
for project_id_file in "./.powder" "./package.json"
do
if [ -f "$project_id_file" ]
then
PROJECT_ID_FILE=$project_id_file
break
fi
done
echo "[.powenv]: project .env PORT: $PORT"
# Generate default PORT for this project.
if [ -f $PROJECT_ID_FILE ]
then
# PROJECT_BODY=`cat $PROJECT_ID_FILE | grep -v '^$'`
PROJECT_MD5=`cat $PROJECT_ID_FILE | grep -v '^$' | md5sum | cut -f1 -d" "`
PROJECT_NUMBER=`echo $(( 0x${PROJECT_MD5%% *} )) | cut -f2 -d"-"` &>/dev/null
PROJECT_PORT_RANGE_FROM=3000
PROJECT_PORT_RANGE_TO=9999
PROJECT_PORT_DEFAULT=`expr $PROJECT_PORT_RANGE_FROM + $PROJECT_NUMBER % $(expr $PROJECT_PORT_RANGE_TO - $PROJECT_PORT_RANGE_FROM)`
PROJECT_PORT_OVERRIDE=0
echo "[.powenv]: project ID FILE: $PROJECT_ID_FILE"
# echo "[.powenv]: project ID BODY: $PROJECT_BODY"
echo "[.powenv]: project ID MD5: $PROJECT_MD5"
echo "[.powenv]: project ID NUMBER: $PROJECT_NUMBER"
if [ $PROJECT_PORT_OVERRIDE -gt 0 ]
then
echo "[.powenv]: override PORT: $PORT => $PROJECT_PORT_DEFAULT"
PORT=$PROJECT_PORT_DEFAULT
else
if [ -z $PORT ]
then
echo "[.powenv]: undefined PORT: $PORT => $PROJECT_PORT_DEFAULT"
PORT=$PROJECT_PORT_DEFAULT
else
echo "[.powenv]: defined PORT: $PORT"
fi
fi
fi
# Pow environment.
export POW_PROXY_HOST=localhost
export POW_PROXY_PORT=$PORT
# Log
echo "[.powenv]: PROJECT_PORT_DEFAULT: $PROJECT_PORT_DEFAULT"
echo "[.powenv]: POW_PROXY_HOST: $POW_PROXY_HOST"
echo "[.powenv]: POW_PROXY_PORT: $POW_PROXY_PORT"
#!/usr/bin/env bash
if [ -f "$rvm_path/scripts/rvm" ] && [ -f ".rvmrc" ]; then
source "$rvm_path/scripts/rvm"
source ".rvmrc"
fi
#!/usr/bin/env bash
rvm use 1.9.3
#!/usr/bin/env ruby
source 'https://rubygems.org'
# https://github.com/Rodreegez/powder
gem 'powder'
# See: https://github.com/spagalloco/pow_proxy
gem 'pow_proxy'
all: clean install proxy
install:
make clean \
&& npm install \
&& ./node_modules/bower/bin/bower install \
&& gem install bundler \
&& bundle install \
&& powder link
update:
npm update \
&& ./node_modules/bower/bin/bower update
&& bundle update
clean:
if [ -e ./node_modules ]; then rm -rf ./node_modules; fi \
&& if [ -e ./.sass-cache ]; then rm -rf ./.sass-cache; fi \
&& if [ -e ./.tmp ]; then rm -rf ./.tmp; fi \
&& if [ -e ./app/components ]; then rm -rf ./app/components; fi \
&& if [ -e ./dist ]; then rm -rf ./dist; fi \
&& if [ -e ./tmp ]; then rm -rf ./tmp; fi
server:
grunt server
build:
grunt build
open:
powder open
static:
make pow-static
proxy:
make pow-proxy \
&& make server
pow-static:
if [ -e ./public ]; then rm -f ./public; fi \
&& if [ ! -d ./dist ]; then mkdir ./dist; fi \
&& ln -sf ./dist ./public \
&& if [ ! -f ./public/index.html ]; then echo "$(PWD)/public/index.html" > ./public/index.html; fi \
&& if [ -e ./config.ru ]; then rm -f ./config.ru; fi \
&& tree -LC 1 \
&& powder restart
pow-proxy:
if [ -e ./public ]; then rm -f ./public; fi \
&& if [ -f ./config.ru ]; then rm -f ./config.ru; fi \
&& if [ ! -e ./config.ru ]; then ln -sf ./.config.ru ./config.ru; fi \
&& source ./.powenv \
&& if [ ! -e ./.env ]; then touch ./.env; fi \
&& sed -e 's/PORT\=.*//g' ./.env > ./.env.old \
&& echo "PORT=$$POW_PROXY_PORT" > ./.env.new \
&& cat ./.env.old >> ./.env.new \
&& cat ./.env.new | grep -v '^$$' > ./.env \
&& rm ./.env.* \
&& tree -LC 1 \
&& powder restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment