Skip to content

Instantly share code, notes, and snippets.

@dch
Last active August 29, 2015 14:15
Show Gist options
  • Save dch/b54fc88f727324a16c67 to your computer and use it in GitHub Desktop.
Save dch/b54fc88f727324a16c67 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
display_usage(){
echo "Usage: $0 PROJECT_NAME [ cowboy ]"
exit 1
}
[[ $# -eq 0 || $# == "--help" || $# == "-h" ]] && display_usage
red='\033[0;31m'
blue='\033[0;34m'
green='\033[0;32m'
yellow='\033[0;33m'
normal='\033[0m'
#echo -ne "${green}Hello${normal}"
APPNAME=$1
mkdir $APPNAME 2>/dev/null
[ $? -ne 0 ] && {
echo ""
echo -e "${red}Failed to create a directory named $APPNAME ${normal}"
echo ""
exit 1
}
cd $APPNAME
echo -ne "Getting erlank.mk... "
wget -q https://raw.githubusercontent.com/ninenines/erlang.mk/1.2.0/erlang.mk
[ $? -ne 0 ] && {
echo ""
echo -e "${red}Failed to reach GitHub. Check the inter-web-tubes. ${normal}"
echo ""
exit 1
}
echo -e "${green}done${normal}"
echo -ne "Bootstrapping... "
make -f erlang.mk bootstrap
[ $? -ne 0 ] && {
echo ""
echo -e "${red}Failed to make bootstrap :( ${normal}"
echo ""
exit 1
}
cd src
# rtrace
wget -qO rtrace.hrl https://gist.githubusercontent.com/ahmadster/003bcc8b970d3030205c/raw/rtrace.hrl
[ $? -ne 0 ] && {
echo ""
echo -e "${red}Failed to reach Gist. Check the inter-web-tubes. ${normal}"
echo ""
exit 1
}
# inject rtrace
sed -i '' "s/-behaviour(application)./-behaviour(application)."$'\\\n'$'\\\n'"-include(\"rtrace.hrl\")./" ${APPNAME}_app.erl
sed -i '' "s/-behaviour(supervisor)./-behaviour(supervisor)."$'\\\n'$'\\\n'"-include(\"rtrace.hrl\")./" ${APPNAME}_sup.erl
sed -i '' "s/, 1, 5/, 100, 10/" ${APPNAME}_sup.erl
# gen_server
wget -qO $APPNAME.erl https://gist.githubusercontent.com/ahmadster/003bcc8b970d3030205c/raw/gen_server.erl
[ $? -ne 0 ] && {
echo ""
echo -e "${red}Failed to reach Gist. Check the series of inter-web-tubes. ${normal}"
echo ""
exit 1
}
# inject gen_server
sed -i '' "s/-module(gen_server)./-module($APPNAME)./" $APPNAME.erl
sed -i '' "s/Procs = \[\],/Procs = \[\{$APPNAME, \{$APPNAME, start_link, \[\]}, permanent, 1000, worker,\[$APPNAME\]\}\],/" ${APPNAME}_sup.erl
cd ..
echo -e "${green}done${normal}"
echo -ne "Initial build... "
make > /dev/null && [ $? -ne 0 ] && echo "Something went wrong. Do you have erlang installed?" && exit 1
echo -e "${green}nice!${normal}"
echo -e "Relxing with JSON... "
#make -f erlang.mk bootstrap-rel
mkdir config
echo "
% This specifies the release name and version and optionally any dependencies
{release, {$APPNAME, \"0.1.0\"}, [$APPNAME]}.
% The file containing the application configuration (this will be copied into the release)
{sys_config, \"$APPNAME.config\"}.
% Use a complete start script
{extended_start_script, true}.
% Don't include the source
{include_src, false}.
" > relx.config
echo "[{$APPNAME, []}]." > $APPNAME.config
# inject json
sed -i '' "s/${APPNAME}/${APPNAME}"$'\\\n'$'\\\n'"DEPS = json jsonpointer jsx"$'\\\n'"dep_jsonpointer = git https:\/\/github.com\/talentdeficit\/jsonpointer.git v1.1.0"$'\\\n'"dep_jsx = git https:\/\/github.com\/talentdeficit\/jsx.git v2.4.0"$'\\\n'"dep_json = git https:\/\/github.com\/ahmadster\/json.git 1.0.0"$'\\\n'"/" Makefile
sed -i '' "s/stdlib/stdlib,"$'\\\n'" jsx,"$'\\\n'" jsonpointer,"$'\\\n'" json/" src/${APPNAME}.app.src
make
[ $? -ne 0 ] && {
echo ""
echo -e "${red}Somethnig went wrong dude(tte). ${normal}"
echo ""
exit 1
}
echo ""
echo ""
echo -e " ${green}Looking Good :)${normal}"
echo ""
[ "$2" == "cowboy" ] && {
echo ""
echo -e " ${yellow}So, you wanna ride'm cows slick?"
echo -e " OK, giddy up!${normal}"
echo ""
echo ""
sed -i '' "s/ jsx/ jsx cowboy"$'\\\n'"dep_cowboy = git https:\/\/github.com\/ninenines\/cowboy.git 1.0.1/" Makefile
sed -i '' "s/stdlib/stdlib,"$'\\\n'" cowboy/" src/${APPNAME}.app.src
make
echo ""
echo -e " ${green}All set, good luck!${normal}"
echo ""
cd src
}
echo ""
echo -e " ${green}Edit the code with sublime:${normal}"
echo ""
echo -e " ${yellow}subl $APPNAME ${normal}"
echo ""
echo -e " ${green}Or try running it now:${normal}"
echo ""
echo -e " ${yellow}$APPNAME/_rel/$APPNAME/bin/$APPNAME console${normal}"
echo ""
echo ""
# Black 0;30 Dark Gray 1;30
# Blue 0;34 Light Blue 1;34
# Green 0;32 Light Green 1;32
# Cyan 0;36 Light Cyan 1;36
# Red 0;31 Light Red 1;31
# Purple 0;35 Light Purple 1;35
# Brown/Orange 0;33 Yellow 1;33
# Light Gray 0;37 White 1;37
%%%-------------------------------------------------------------------
%%% @author erlang.dev
%%% @copyright (C) 3000, <COMPANY>
%%% Created : 1. Feb 3000 12:00 AM
%%%-------------------------------------------------------------------
-module(gen_server).
-author("ahmad").
-include("rtrace.hrl").
-behaviour(gen_server).
-define(SERVER, ?MODULE).
%% gen_server callbacks
-export([init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2,
code_change/3]).
%% API
-export([start_link/0]).
%%--------------------------------------------------------------------
%% Starts the server
%% This gets called by a supervisor to start this server.
%% So it would be running under the caller's PID.
%%--------------------------------------------------------------------
-spec(start_link() ->
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
%%%===================================================================
%%% API
%%%===================================================================
% Sample blocking call API, see handle_call below.
% do_blocking_work(Param1, Param2) ->
% gen_server:call(?SERVER, {blocking_work, Param1, Param2}).
% Sample non-blocking call API, see handle_cast below.
% do_non_blocking_work(Param1, Param2) ->
% gen_server:cast(?SERVER, {non_blocking_work, Param1, Param2}).
%%%===================================================================
%%% gen_server callbacks
%%%===================================================================
%%--------------------------------------------------------------------
%% Initializes the server
%% This gets called by start_link above under our own new server PID.
%% **Note:
%% This is a blocking call, so try not to fail here. The supervisor
%% won't restart a child that hasn't started at least once.
%%
%% A good trick is to quickly return:
%% {ok, #{}, 0} <--- timeout of 0
%% This will allow the supervisor to consider the child started and
%% assume proper supervision.
%%
%% It will also cause an immediate (and parallel) call to
%% handle_info(timeout, State)
%% Which is where you can perform long-running-error-prone
%% initialization.
%%--------------------------------------------------------------------
-spec(init(Args :: term()) ->
{ok, State :: #{}} | {ok, State :: #{}, timeout() | hibernate} |
{stop, Reason :: term()} | ignore).
init([]) -> {ok, #{}, 0}.
%%--------------------------------------------------------------------
%% Handling all non call/cast messages
%% This includes messages from the system, or messages we send to
%% ourselves. Basically any message that did not come from a caller.
%%--------------------------------------------------------------------
-spec(handle_info(Info :: timeout() | term(), State :: #{}) ->
{noreply, NewState :: #{}} |
{noreply, NewState :: #{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #{}}).
%% Our init timeout handler
handle_info(timeout, State) ->
%%----------------------------------------------------------------
%% Do real init work here
%%----------------------------------------------------------------
?green("<yellow>....</yellow>~n~s initialized and ready!~n"
"<d>to shutdown, enter q().</d>",[?SERVER]),
{noreply, State};
handle_info(_Info, State) ->
{noreply, State}.
%%--------------------------------------------------------------------
%% Handling call (synchronous, blocking) messages
%%--------------------------------------------------------------------
-spec(handle_call(Request :: term(), From :: {pid(), Tag :: term()},
State :: #{}) ->
{reply, Reply :: term(), NewState :: #{}} |
{reply, Reply :: term(), NewState :: #{}, timeout() | hibernate} |
{noreply, NewState :: #{}} |
{noreply, NewState :: #{}, timeout() | hibernate} |
{stop, Reason :: term(), Reply :: term(), NewState :: #{}} |
{stop, Reason :: term(), NewState :: #{}}).
% handle_call({blocking_work, Param1, Param2}, _From, State) ->
% {reply, ok, State};
handle_call(_Request, _From, State) ->
{reply, ok, State}.
%%--------------------------------------------------------------------
%% Handling cast (asynchronous, non-blocking) messages
%%--------------------------------------------------------------------
-spec(handle_cast(Request :: term(), State :: #{}) ->
{noreply, NewState :: #{}} |
{noreply, NewState :: #{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #{}}).
% handle_cast({non_blocking_work, Param1, Param2}, State) ->
% {noreply, State};
handle_cast(_Request, State) ->
{noreply, State}.
%%--------------------------------------------------------------------
%% This function is called by a gen_server when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any
%% necessary cleaning up. When it returns, the gen_server terminates
%% with Reason. The return value is ignored.
%%--------------------------------------------------------------------
-spec(terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()),
State :: #{}) -> term()).
terminate(_Reason, _State) ->
ok.
%%--------------------------------------------------------------------
%% Convert process state when code is changed
%%--------------------------------------------------------------------
-spec(code_change(OldVsn :: term() | {down, term()}, State :: #{},
Extra :: term()) ->
{ok, NewState :: #{}} | {error, Reason :: term()}).
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%%===================================================================
%%% Internal functions
%%%===================================================================
% erlang-bitleaf rtrace
% output colors
-define(_red, "\e[01;31m").
-define(_green, "\e[01;32m").
-define(_blue , "\e[38;5;27m").
-define(_yellow, "\e[01;33m").
-define(_orange, "\e[38;5;208m").
-define(_magenta, "\e[01;35m").
-define(_cyan, "\e[01;36m").
-define(_gray, "\e[01;30m").
-define(_white, "\e[01;37m").
-define(_bold, "\e[1m").
-define(_dim, "\e[2m").
-define(_underline, "\e[4m").
-define(_blink, "\e[5m").
-define(_inverted, "\e[7m").
-define(_normal, "\e[00m").
-define(_self_color, "\e[38;5;217m").
-define(_module_color, "\e[38;5;76m").
-define(_func_color, "\e[38;5;117m").
-define(_arity_color, "\e[38;5;86m").
-define(FUNCTION(), element(2, element(2, process_info(self(), current_function))) ).
-define(ARITY(), element(3, element(2, process_info(self(), current_function))) ).
-define(debug, true).
-ifdef(debug).
-define(rtrace(C__r, F__r, P__r) , io:format(?_dim ++ ?_self_color ++ "~p : "++ ?_module_color ++ "~p : ~p : "++ ?_func_color++"~p/"++?_arity_color++"~p~n" ++ ?_normal++ ?C__r ++ F__r ++ "~n" ++ ?_normal, lists:append([self(), ?MODULE, ?LINE, ?FUNCTION(), ?ARITY()], P__r))).
-else.
-define(rtrace(C__r, F__r, P__r) , true).
-endif.
-define(colorize(T__r, N__r),
re:replace(
re:replace(
re:replace(
re:replace(
re:replace(
re:replace(
re:replace(
re:replace(
re:replace(
re:replace(
re:replace(
re:replace(
re:replace(
re:replace(
re:replace(
re:replace(T__r, "<n>", ?_normal, [{return, list},global]),
"<white>", ?_white, [{return, list},global]),
"<i>", ?_inverted, [{return, list},global]),
"<gray>", ?_gray, [{return, list},global]),
"<cyan>", ?_cyan, [{return, list},global]),
"<magenta>", ?_magenta, [{return, list},global]),
"<orange>", ?_orange, [{return, list},global]),
"<yellow>", ?_yellow, [{return, list},global]),
"<blue>", ?_blue, [{return, list},global]),
"<green>", ?_green, [{return, list},global]),
"<red>", ?_red, [{return, list},global]),
"<d>", ?_dim, [{return, list},global]),
"<b>", ?_blink, [{return, list},global]),
"<u>", ?_underline, [{return, list},global]),
"<e>", ?_bold, [{return, list},global]),
"</(b|u|e|d|i|n|red|green|blue|yellow|orange|magenta|cyan|gray|white)>", ?_normal ++ N__r, [{return, list},global])
).
%
-define(red(P__r) , ?rtrace(_red, ?colorize(P__r,?_red), [] ) ).
-define(red(F__r, P__r) , ?rtrace(_red, ?colorize(F__r,?_red), P__r) ).
-define(green(P__r) , ?rtrace(_green, ?colorize(P__r, ?_green), [] ) ).
-define(green(F__r, P__r) , ?rtrace(_green, ?colorize(F__r,?_green), P__r) ).
-define(blue(P__r) , ?rtrace(_blue, ?colorize(P__r,?_blue), [] ) ).
-define(blue(F__r, P__r) , ?rtrace(_blue, ?colorize(F__r,?_blue), P__r) ).
-define(yellow(P__r) , ?rtrace(_yellow, ?colorize(P__r,?_yellow), [] ) ).
-define(yellow(F__r, P__r) , ?rtrace(_yellow, ?colorize(F__r,?_yellow), P__r) ).
-define(orange(P__r) , ?rtrace(_orange, ?colorize(P__r,?_orange), [] ) ).
-define(orange(F__r, P__r) , ?rtrace(_orange, ?colorize(F__r,?_orange), P__r) ).
-define(magenta(P__r) , ?rtrace(_magenta, ?colorize(P__r,?_magenta), [] ) ).
-define(magenta(F__r, P__r) , ?rtrace(_magenta, ?colorize(F__r,?_magenta), P__r) ).
-define(cyan(P__r) , ?rtrace(_cyan, ?colorize(P__r,?_cyan), [] ) ).
-define(cyan(F__r, P__r) , ?rtrace(_cyan, ?colorize(F__r,?_cyan), P__r) ).
-define(gray(P__r) , ?rtrace(_gray, ?colorize(P__r,?_gray), [] ) ).
-define(gray(F__r, P__r) , ?rtrace(_gray, ?colorize(F__r,?_gray), P__r) ).
-define(white(P__r) , ?rtrace(_white, ?colorize(P__r,?_white), [] ) ).
-define(white(F__r, P__r) , ?rtrace(_white, ?colorize(F__r,?_white), P__r) ).
-define(bold(P__r) , ?rtrace(_bold, ?colorize(P__r,?_bold), [] ) ).
-define(bold(F__r, P__r) , ?rtrace(_bold, ?colorize(F__r,?_bold), P__r) ).
-define(dim(P__r) , ?rtrace(_dim, ?colorize(P__r,?_dim), [] ) ).
-define(dim(F__r, P__r) , ?rtrace(_dim, ?colorize(F__r,?_dim), P__r) ).
-define(underline(P__r) , ?rtrace(_underline, ?colorize(P__r,?_underline), [] ) ).
-define(underline(F__r, P__r) , ?rtrace(_underline, ?colorize(F__r,?_underline), P__r) ).
-define(blink(P__r) , ?rtrace(_blink, ?colorize(P__r,?_blink), [] ) ).
-define(blink(F__r, P__r) , ?rtrace(_blink, ?colorize(F__r,?_blink), P__r) ).
-define(inverted(P__r) , ?rtrace(_inverted, ?colorize(P__r,?_inverted), [] ) ).
-define(inverted(F__r, P__r) , ?rtrace(_inverted, ?colorize(F__r,?_inverted), P__r) ).
-define(normal(P__r) , ?rtrace(_normal, ?colorize(P__r,?_normal), [] ) ).
-define(normal(F__r, P__r) , ?rtrace(_normal, ?colorize(F__r,?_normal), P__r) ).
-define(rtrace_test(), ?gray("Hello. This rtrace!~nIt is a multi-color tracing macro set that support html-style output. For example, this line has <red>red</red>, <green>green</green>, <blue>blue</blue>, <yellow>yellow</yellow>, <orange>orange</orange>, <magenta>magenta</magenta>, <cyan>cyan</cyan>, <white>white</white>, and <gray>gray</gray> colored text.~nSome text can also be <e>bold</e> <u>underline</u> <d>dim</d> <b>blink</b> <i>inverted</i> or <n>normal</n>. You can't, however, <red>you can <b>embed</b> colors</red>... yet.")).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment