erlang:基于cowboy,一键创建erlang…

  基于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!"
}

未完待续...
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值