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