PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 10ms RepoModel.GetById 30ms app.codeStats 0ms

/src/mochiweb_skel.erl

http://github.com/basho/mochiweb
Erlang | 84 lines | 70 code | 9 blank | 5 comment | 0 complexity | 48f18eff89f7d381274b4576668e6560 MD5 | raw file
 1-module(mochiweb_skel).
 2-export([skelcopy/2]).
 3
 4-include_lib("kernel/include/file.hrl").
 5
 6%% External API
 7
 8skelcopy(DestDir, Name) ->
 9    ok = ensuredir(DestDir),
10    LDst = case length(filename:dirname(DestDir)) of
11               1 -> %% handle case when dirname returns "/"
12                   0;
13               N ->
14                   N + 1
15           end,
16    skelcopy(src(), DestDir, Name, LDst),
17    ok = file:make_symlink(
18        filename:join(filename:dirname(code:which(?MODULE)), ".."),
19        filename:join([DestDir, Name, "deps", "mochiweb-src"])).
20
21%% Internal API
22
23src() ->
24    Dir = filename:dirname(code:which(?MODULE)),
25    filename:join(Dir, "../priv/skel").
26
27skel() ->
28    "skel".
29
30skelcopy(Src, DestDir, Name, LDst) ->
31    Dest = re:replace(filename:basename(Src), skel(), Name,
32                      [global, {return, list}]),
33    case file:read_file_info(Src) of
34        {ok, #file_info{type=directory, mode=Mode}} ->
35            Dir = DestDir ++ "/" ++ Dest,
36            EDst = lists:nthtail(LDst, Dir),
37            ok = ensuredir(Dir),
38            ok = file:write_file_info(Dir, #file_info{mode=Mode}),
39            case filename:basename(Src) of
40                "ebin" ->
41                    ok;
42                _ ->
43                    {ok, Files} = file:list_dir(Src),
44                    io:format("~s/~n", [EDst]),
45                    lists:foreach(fun ("." ++ _) -> ok;
46                                      (F) ->
47                                          skelcopy(filename:join(Src, F),
48                                                   Dir,
49                                                   Name,
50                                                   LDst)
51                                  end,
52                                  Files),
53                        ok
54            end;
55        {ok, #file_info{type=regular, mode=Mode}} ->
56            OutFile = filename:join(DestDir, Dest),
57            {ok, B} = file:read_file(Src),
58            S = re:replace(binary_to_list(B), skel(), Name,
59                           [{return, list}, global]),
60            ok = file:write_file(OutFile, list_to_binary(S)),
61            ok = file:write_file_info(OutFile, #file_info{mode=Mode}),
62            io:format("    ~s~n", [filename:basename(Src)]),
63            ok;
64        {ok, _} ->
65            io:format("ignored source file: ~p~n", [Src]),
66            ok
67    end.
68
69ensuredir(Dir) ->
70    case file:make_dir(Dir) of
71        ok ->
72            ok;
73        {error, eexist} ->
74            ok;
75        E ->
76            E
77    end.
78
79%%
80%% Tests
81%%
82-include_lib("eunit/include/eunit.hrl").
83-ifdef(TEST).
84-endif.