使用Erlang实现游戏服务器-02

使用Erlang实现游戏服务器-02

1、编译脚本的编写

在上一篇文章的最后,我们编译了我们的erl文件,但是工作中我们不可能每次每次写完一个模块就编译一个,因此我们需要编写脚本
1.1、Emakefile
具体介绍请在网上搜索
在script目录创建Emakefile文件
内容如下

{"../src/*", [debug_info, {i, "../include"}, {outdir, "../ebin"}]}.

1.2、编写game_compile

%% API
-export([compile_all/0]).

%%%================================== export func ==================================
%%%---------------------------------------------------------------------------------
%% des:
%%%---------------------------------------------------------------------------------
compile_all() ->
    Terms = read_makefile(),
    transform(Terms).

%%%=================================== local func ==================================
%%%---------------------------------------------------------------------------------
%% Description:读取Emakefile文件
%%%---------------------------------------------------------------------------------
read_makefile() ->
    case file:consult("Emakefile") of
        {ok, Terms} ->
            Terms;
        {error, Reason} ->
            io:format("Error reading Emakefile: ~p~n", [Reason]),
            []
    end.


%%%---------------------------------------------------------------------------------
%% Description:整理Emakefile读取出的数据,拿取所有的模块名
%%%---------------------------------------------------------------------------------
transform([{Dir, OptList} | T]) ->
    spawn(fun() ->
        NewModDirList = expend(Dir),
        [begin
             case c:c(ModDir, OptList) of
                 {ok, ModName} ->
                     io:format("compile file: ~p success, mod name: ~p~n", [ModDir, ModName]);
                 {error, Reason} ->
                     io:format("compile file: ~p error: ~p~n", [ModDir, Reason])
             end
         end || ModDir <- NewModDirList]
          end),
    transform(T);

transform([Other | T]) ->
    io:format("格式错误:~w~n", [Other]),
    transform(T);

transform([]) ->
    ok.

%%%---------------------------------------------------------------------------------
%% Description:拿取所有模块名
%%%---------------------------------------------------------------------------------
expend(Dir) ->
    case lists:member($*, Dir) of
        true ->
            Dirs = filelib:wildcard(Dir),
            find_file(Dirs);
        false ->
            []
    end.

%% ----------------------------------------------------
%% Description: 找到路径下的所有erl文件
%% ----------------------------------------------------
find_file(Dirs) ->
    find_file_(Dirs, []).

find_file_([F | T], FindFiles) ->
    case filelib:is_dir(F) of %%是否是文件夹
        true ->
            {ok, Files} = file:list_dir(F),
            %% 加上路径
            AddFiles = [F ++ "/" ++ File || File <- Files],
            find_file_(AddFiles ++ T, FindFiles);
        false ->
            %% 过滤出erl文件
            case filename:extension(F) of
                ".erl" ->
                    case lists:member(F, FindFiles) of
                        true ->
                            find_file_(T, FindFiles);
                        false ->
                            find_file_(T, [F | FindFiles])
                    end;
                _ ->
                    find_file_(T, FindFiles)
            end
    end;

find_file_([], FindFiles) ->
    FindFiles.

把erl文件编译为beam文件
在这里插入图片描述
1.3 编写脚本
compile_all.bat

@ECHO OFF
chcp 65001

erl -noshell -pa "../deps/ebin" -eval "game_compile:compile_all(),erlang:halt(0)"

echo 编译完成
pause

在这里插入图片描述
执行脚本
在这里插入图片描述
编译成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值