/
icnelia_cmds.erl
113 lines (102 loc) · 3.79 KB
/
icnelia_cmds.erl
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
%
% ICNELIA : Helper for compile, clean and create run scripts for
% an erlang OTP application
%
% Copyright (C) 2012-2013, Jorge Garrido <zgbjgg@gmail.com> All rights reserved.
%
% This Source Code Form is subject to the terms of the Mozilla Public
% License, v. 2.0. If a copy of the MPL was not distributed with this
% file, You can obtain one at http://mozilla.org/MPL/2.0/.
%
-module(icnelia_cmds).
-export([cmd/1]).
-include("../include/icnelia.hrl").
% command selector
cmd(Cmd) ->
Config = icnelia_files:get_file_config(),
case Cmd of
compile -> compile(Config);
clean -> clean(Config);
runner -> runner(Config);
runner_d -> runner_d(Config)
end.
% compile cmd
compile(Config) ->
ErlOpts = icnelia_utils:get_value(erl_opts, Config),
Modules = icnelia_files:get_files(?src_erl),
case icnelia_files:get_files(?src_app_src) of
[] ->
ok;
[ File ] ->
AppName = icnelia_files:get_app_name(),
{ok, Binary} = file:read_file(File),
ok = file:write_file(?ebin_dir ++ AppName ++ ".app", Binary)
end,
Beams = [ begin
case c:c(Module, [{i, ?include_dir}, {outdir, ?ebin_dir}] ++ ErlOpts) of
error ->
halt(1);
C ->
io:format(?msg_c, [Module]),
C
end
end || Module <- Modules ],
{ok, Beams}.
% clean cmd
clean(_Config) ->
Beams = icnelia_files:get_files(?ebin_beam),
case icnelia_files:get_files(?src_app_ebin) of
[] ->
ok;
[ AppFile ] ->
ok = file:delete(AppFile)
end,
Removed = [ begin
ok = file:delete(Beam),
io:format(?msg_d, [Beam])
end || Beam <- Beams ],
{ok, Removed}.
% runner cmd
runner(Config) ->
AppDeps = icnelia_utils:get_value(app_deps, Config),
{ok, IoDev} = icnelia_files:get_run_script(),
%% append lines used as commentaries in script
a_comments(deps, IoDev),
% append lines for path to deps (ebin)
[ io:format(IoDev, "~s=~s~n", [Name, Path]) || {Name, Path} <- AppDeps ],
AppName = icnelia_files:get_app_name(),
%% append lines used as commentaries in script
a_comments(startup, IoDev),
io:format(IoDev, ?runner(icnelia_utils:get_pa_string([ Name || {Name, _} <- AppDeps ]), AppName), []),
[] = icnelia_utils:chmod(?run, ?u_x),
{ok, ?run}.
% runner daemon cmd
runner_d(Config) ->
runner_d(Config, os:type()).
runner_d(Config, {unix, _}) ->
AppDeps = icnelia_utils:get_value(app_deps, Config),
{ok, IoDev} = icnelia_files:get_run_script(),
%% append lines used as commentaries in script
a_comments(deps, IoDev),
[ io:format(IoDev, "~s=~s~n", [Name, Path]) || {Name, Path} <- AppDeps ],
AppName = icnelia_files:get_app_name(),
[Pipes, Logs] = [ icnelia_utils:get_value(Key, Config) || Key <- [pipes_dir, logs_dir] ],
%% append lines used as commentaries in script
a_comments(startup, IoDev),
io:format(IoDev, ?runner_d(icnelia_utils:get_pa_string([ Name || {Name, _} <- AppDeps ]), AppName, Pipes, Logs), []),
[] = icnelia_utils:chmod(?run, ?u_x),
{ok, ?run};
runner_d(_Config, {win32, _}) ->
io:format("Cannot create a daemon script on WIN32 SYSTEM\n"),
{ok, ""}.
% append comments
a_comments(Type, IoDev) ->
a_comments(Type, IoDev, os:type()).
a_comments(deps, IoDev, {unix, _}) ->
io:format(IoDev, "# Deployment Application Settings\n#-------------------------------------------------\n", []);
a_comments(deps, IoDev, {win32, _}) ->
io:format(IoDev, "@echo off~nREM Deployment Application Settings~nREM -------------------------------------------------~n", []);
a_comments(startup, IoDev, {unix, _}) ->
io:format(IoDev, "\n\n# Startup\n#-------------------------------------------------\n", []);
a_comments(startup, IoDev, {win32, _}) ->
io:format(IoDev, "~n~nREM Startup~nREM -------------------------------------------------~n", []).