基于cowboy一键创建erlang工程的前提是:已经安装了ets和rebar,
1、shell script如下:
#!/bin/bash
projectName=releaseErlangProject
rebarfile=rebar.config
projectPath=`pwd`/${projectName}
runScript=run.sh
#获取rebar工具
get_rebar(){
git clone git://github.com/rebar/rebar.git
cd rebar
./bootstrap
sudo mv rebar /usr/local/bin
}
#创建src目录
mk_app(){
goProjectPath
#rebar create-app appid=releaseErlangProject
rebar create template=simpleapp appid=${projectName}
echo "---SUCCESS mk_app!"
}
change_erlang_app(){
goProjectPath
cd src
accessPath="Dispatch=cowboy_router:compile([{'_',[{\"/\",${projectName}_handler,[]}]}]),cowboy:start_http(my_http_listener,100,[{port,8080}],[{env,[{dispatch,Dispatch}]}]),"
line=$(grep -n "${projectName}_sup" ${projectName}_app.erl | cut -d ":" -f 1)
sed -i ${line}'i '$accessPath''
${projectName}_app.erl
}
bootFile(){
goProjectPath
cd src
bootFileName=${projectName}_boot
bootFile=${bootFileName}.erl
echo "-module($bootFileName)." >> $bootFile
echo "-export([start/0, stop/0])." >> $bootFile
echo " "
echo " "
echo "start() ->" >> $bootFile
echo "application:start(ranch)," >> $bootFile
echo "application:start(crypto)," >> $bootFile
echo "application:start(cowlib)," >> $bootFile
echo "application:start(cowboy)," >> $bootFile
echo "application:start(${projectName})." >> $bootFile
echo "stop() ->" >> $bootFile
echo "application:stop(${projectName})," >> $bootFile
echo "application:stop(cowboy)," >> $bootFile
echo "application:stop(cowlib)," >> $bootFile
echo "application:stop(crypto)," >> $bootFile
echo "application:stop(ranch)." >> $bootFile
echo "---SUCCESS mk bootFile!"
}
#创建include目录
includeDir(){
goProjectPath
if [ ! -d "include" ] ; then
mkdir include
fi
echo "---SUCCESS mk includeDir!"
}
#创建include目录
privDir(){
goProjectPath
if [ ! -d "priv" ] ; then
mkdir priv
fi
echo "---SUCCESS mk privDir!"
}
#创建测试目录
testDir(){
goProjectPath
if [ ! -d "test" ] ; then
mkdir test
echo "{erl_opts, [debug_info,{i, \"test\"},{src_dirs, [\"src\"]}]}." >> ${rebarfile}
echo "{eunit_opts, [verbose, {report, {eunit_surefire, [{dir, \".\"}]}}]}." >> ${rebarfile}
echo "{cover_enabled, true}." >> ${rebarfile}
#testfile
fi
echo "---SUCCESS mk testDir!"
}
testfile(){
goProjectPath
testfile="demo.erl"
echo "-ifdef(TEST).
-include(\"${testfile}\").
-endif." >> ${testfile}
}
# Erlang/OTP 行为模式的模板
genTemplate(){
goProjectPath
rebar create template=simplesrv srvid=test_gen_server
rebar create template=simplefsm fsmid=test_gen_fsm
echo "---SUCCESS mk genTemplate!"
}
#创建工程依赖
depsDir(){
goProjectPath
if [ ! -d "deps" ] ; then
cowboy="{cowboy, \".*\", {git, \"https://github.com/ninenines/cowboy.git\"}}"
jiffy="{jiffy, \".*\", {git, \"https://github.com/davisp/jiffy.git\"}}"
emysql="{emysql, \".*\", {git, \"https://github.com/Eonblast/Emysql.git\"}}"
split=","
objects=${cowboy}
#objects=${cowboy}${split} ${jiffy}${split}${emysql}
echo "{deps, [${objects}]}." >> ${rebarfile}
# get deps
rebar get-deps
fi
echo "---SUCCESS mk depsDir!"
}
goProjectPath(){
if [ ${projectPath} != `pwd`} ]; then
cd ${projectPath}
fi
}
mkHandler(){
goProjectPath
cd src
echo "-module(${projectName}_handler).
-author(\"wxm\").
%% API
-behaviour(cowboy_handler).
-export([init/2]).
-export([terminate/3]).
init(Req, _Opts) ->
io:format(\"Req: ~p~n\", [Req]),
cowboy_req:reply(200, Req),
{ok, Req, _Opts}.
terminate(_Reason, _Req, _State) ->
ok." >> ${projectName}_handler.erl
echo "---SUCCESS
mkHandler!"
}
未完待续...