PageRenderTime 36ms CodeModel.GetById 24ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/deps/mochiweb/src/mochiweb_skel.erl

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