Permalink
Browse files

make runnable on one machine

  • Loading branch information...
1 parent cb19780 commit dc9fdd8f468a7a67f4e37197b5d67ceebfc462a5 @zgbjgg committed Oct 10, 2012
Showing with 219 additions and 126 deletions.
  1. +28 −3 README
  2. +3 −3 boot
  3. +10 −1 include/icnelia.hrl
  4. +18 −119 src/icnelia.erl
  5. +83 −0 src/icnelia_cmds.erl
  6. +43 −0 src/icnelia_files.erl
  7. +34 −0 src/icnelia_utils.erl
View
31 README
@@ -48,11 +48,36 @@
$ ./icnelia runner daemon
and it will create a run script, to start your application as daemon
- directories Logs and Pipes must be created by icnelia at this point
===== icnelia.config =====
-
- ===== examples =====
+
+ app_deps: here you can define the paths to ebin deps, for example:
+
+ {app_deps, [{"MY_DEP", "my_dep/ebin"}]}.
+
+ where "MY_DEP" is the name that the run script use to recongnize the path to your dep,
+ and "my_dep/ebin" is the path to ebin for the dependencie
+
+ all dependencies must be set on deps directory
+
+ pipes_dir: here you can define the path to your pipes directory, it is used when you want
+ run your application as daemon, for example:
+
+ {pipes_dir, "/usr/local/My_Pipes/"}.
+
+ NOTE: YOU MUST CREATE THE PIPES DIRECTORY MANUALLY
+
+ logs_dir: here you can define the path to logs used by your application, whe it is running as
+ daemon, for example:
+
+ {logs_dir, "/usr/local/My_Logs/"}.
+
+ NOTE: YOU MUST CREATE THE LOGS DIRECTORY MANUALLY
+
+ erl_opts: here you can set the options to compile the source code, the options are all
+ supported by erlang version running, for example:
+
+ {erl_opts, [debug_info]}.
===== LICENSE =====
View
6 boot
@@ -12,14 +12,14 @@
echo "creating dirs ..." && mkdir -p ebin || { echo "error"; exit 1; }
# compiles the src code (erlang)
-echo "compiling ..." && erlc -o ebin src/icnelia.erl || { echo "error"; exit 1; }
+echo "compiling ..." && erlc -o ebin src/*.erl || { echo "error"; exit 1; }
# creates script executable from beam
# copy into script
-echo "copying the files ..." && cp ebin/icnelia.beam icnelia || { echo "error"; exit 1; }
+echo "copying the files ..." && cat ebin/*.beam >> icnelia || { echo "error"; exit 1; }
# set headers for escript, make runnable on any machine
-echo "building script ..." && sed -i '1i#!/usr/bin/env escript' icnelia || { echo "error"; exit 1; }
+echo "building script ..." && sed -i '1i%%! -pa '$PWD'/ebin' icnelia && sed -i '1i#!/usr/bin/env escript' icnelia || { echo "error"; exit 1; }
# set permissions for escript
echo "setting permissions ..." && chmod u+x icnelia || { echo "error"; exit 1; }
View
@@ -35,7 +35,16 @@
-define(msg_d, "cleaning ~s ~n").
% config file (must be called 'icnelia.config').
--define(cfg_file, "icnelia.config").
+-define(config_file, "icnelia.config").
% name for runner script
-define(run, "run").
+
+% u+x
+-define(u_x, "u+x").
+
+% runner option
+-define(runner(Paths, App), "erl -pa ebin/ " ++ Paths ++ " -eval 'application:start(" ++ App ++ ").'").
+
+% runner d option
+-define(runner_d(Paths, App, Pipes, Logs), "run_erl -daemon " ++ Pipes ++ " " ++ Logs ++ " \"exec erl -pa ebin/ " ++ Paths ++ " -eval 'application:start(" ++ App ++ ").'\"").
View
@@ -6,7 +6,6 @@
%
% All rights reserved.
%
-
-module(icnelia).
-export([main/1]).
@@ -15,128 +14,28 @@
% main function, receives the option for a command
main(Opts) ->
- process([list_to_atom(Opt) || Opt <- Opts]).
-
-% process the option, pattern matching with the given option
-process([runner, daemon | _]) ->
- _X = runner_daemon(get_value(app_deps, get_cfg())),
- os:cmd("chmod u+x " ++ ?run);
-process([runner | _]) ->
- _X = runner(get_value(app_deps, get_cfg())),
- os:cmd("chmod u+x " ++ ?run);
-process([clean | _]) ->
- {ok, _ } = clean();
-process([compile | _]) ->
- {ok, _ } = compile(get_value(erl_opts, get_cfg()));
-process(_) ->
- help().
-
-% option compile
-compile(undefined) ->
- compile([]);
-compile(_opts) ->
- Modules = get_files(?src_erl),
- AppSrc = get_files(?src_app_src),
- case AppSrc of
- [] ->
- ok;
- [File] ->
- AppName = get_app_name(),
- ok = file:rename(File, "ebin/" ++ AppName ++ ".app")
- end,
- Beams = [ begin
- case c:c(Module, [{i, ?include_dir}, {outdir, ?ebin_dir}] ++ _opts) of
- error ->
- halt(1);
- R ->
- io:format(?msg_c, [Module]),
- R
- end
- end || Module <- Modules ],
- {ok, Beams}.
-
-% option clean
-clean() ->
- Beams = get_files(?ebin_beam),
- case get_files(?src_app_ebin) of
- [] ->
- ok;
- [App] ->
- ok = file:delete(App)
- end,
- Delete = [ begin
- ok = file:delete(Beam),
- io:format(?msg_d, [Beam])
- end || Beam <- Beams ],
- {ok, Delete}.
-
-% option runner
-runner(undefined) ->
- runner([]);
-runner(_opts) ->
- {ok, IoDev} = file:open(?run, [write, append]),
-% append lines for path to deps (ebin)
- [ io:format(IoDev, "~s=deps/~s~n", [Name, Value]) || {Name, Value} <- _opts ],
-% get name for application
- App = get_app_name(),
-% append line for the runner script
- io:format(IoDev, "erl -pa ebin/ " ++ get_pa_string([ Name || {Name, _} <- _opts ]) ++
- " -eval 'application:start(" ++ App ++ ").'", []).
-
-% option runner daemon
-runner_daemon(undefined) ->
- runner_daemon([]);
-runner_daemon(_opts) ->
- {ok, IoDev} = file:open(?run, [write, append]),
-% append lines for path to deps (ebin)
- [ io:format(IoDev, "~s=deps/~s~n", [Name, Value]) || {Name, Value} <- _opts ],
-% get name for application
- App = get_app_name(),
-% get pipes and logs directories
- Pipes = get_value(pipes_dir, get_cfg()),
- Logs = get_value(logs_dir, get_cfg()),
-% append line for the runner script
- io:format(IoDev, "run_erl -daemon " ++ Pipes ++ " " ++ Logs ++ " exec \"erl -pa ebin/ " ++ get_pa_string([ Name || {Name, _} <- _opts ]) ++
- " -eval 'application:start(" ++ App ++ ").'\"", []).
-
-% get files by wildcard
-get_files(Wildcard) ->
- lists:filter(fun(X) -> filelib:is_file(X) end, filelib:wildcard(Wildcard)).
-
-% get config on meen.config
-get_cfg() ->
- case file:consult(?cfg_file) of
- {error, enoent} ->
- [];
- {error, FileError} ->
- io:format("~s: ~s ~n", [?cfg_file, FileError]),
- halt(2);
- {ok, List} ->
- List
- end.
-
-% get value for key given
-get_value(KeyLookUp, List) ->
- Val = [ Value || {Key, Value} <- List, Key == KeyLookUp ],
- case Val of [] -> undefined; [Value] -> Value end.
-
-% get string from a list with many elements
-get_pa_string([]) ->
- "";
-get_pa_string([H | T]) ->
- "$" ++ H ++ " " ++ get_pa_string(T).
-
-% get application name
-get_app_name() ->
- [File | _] = get_files(?src_app_src),
- {ok, [{application, App, _}]} = file:consult(File),
- atom_to_list(App).
+ try
+ {ok, _} = processing_cmd(icnelia_utils:all_atoms(Opts))
+ catch
+ _E:_R ->
+ help()
+ end.
+
+% process the cmd, pattern matching with the given option
+processing_cmd([ runner, daemon ]) ->
+ icnelia_cmds:cmd(runner_d);
+processing_cmd([ runner ]) ->
+ icnelia_cmds:cmd(runner);
+processing_cmd([ clean ]) ->
+ icnelia_cmds:cmd(clean);
+processing_cmd([ compile ]) ->
+ icnelia_cmds:cmd(compile).
% help or icnelia?
help() ->
io:fwrite(
-"Usage ~s Cmd[Opts] ~n"
-"Valid commands and options~n"
+"Usage ~s cmd ~n"
+"Valid commands~n"
" compile : compiles all in src dir and place under ebin dir~n"
" clean : cleans all in ebin dir (compiled)~n"
" runner : creates a script to run the application~n"
View
@@ -0,0 +1,83 @@
+%
+% ICNELIA : Helper for compile, clean and create run scripts for
+% an erlang OTP application
+%
+% Copyright (C) 2012, Jorge Garrido <jorge.garrido@morelosoft.com>
+%
+% All rights reserved.
+%
+-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_apts, 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 for path to deps (ebin)
+ [ io:format(IoDev, "~s=deps/~s~n", [Name, Path]) || {Name, Path} <- AppDeps ],
+ AppName = icnelia_files:get_app_name(),
+ 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) ->
+ AppDeps = icnelia_utils:get_value(app_deps, Config),
+ {ok, IoDev} = icnelia_files:get_run_script(),
+ [ io:format(IoDev, "~s=deps/~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] ],
+ io:format(IoDev, ?runner_d(icnelia_utils:get_pa_string([ Name || {Name, _} <- AppDeps ]), AppName, Pipes, Logs), []),
+ [] = icnelia_utils:chmod(?run, ?u_x),
+ {ok, ?run}.
View
@@ -0,0 +1,43 @@
+%
+% ICNELIA : Helper for compile, clean and create run scripts for
+% an erlang OTP application
+%
+% Copyright (C) 2012, Jorge Garrido <jorge.garrido@morelosoft.com>
+%
+% All rights reserved.
+%
+-module(icnelia_files).
+
+-export([get_file_config/0, get_files/1, get_app_name/0,
+ get_run_script/0]).
+
+-include("../include/icnelia.hrl").
+
+% get config from icnelia.config file
+get_file_config() ->
+ case file:consult(?config_file) of
+ % use a default configuration
+ {error, enoent} ->
+ [];
+ % error to process file
+ {error, FileError} ->
+ io:format("~s : ~s ~n", [?config_file, FileError]),
+ halt(2);
+ % use the configuration given in icnelia.config
+ {ok, Config} ->
+ Config
+ end.
+
+% get files by wildcard
+get_files(Wildcard) ->
+ lists:filter(fun(X) -> filelib:is_file(X) end, filelib:wildcard(Wildcard)).
+
+% get application name
+get_app_name() ->
+ [ File ] = ?MODULE:get_files(?src_app_src),
+ {ok, [{application, App, _}]} = file:consult(File),
+ atom_to_list(App).
+
+% get run script process to write on it
+get_run_script() ->
+ file:open(?run, [write]).
View
@@ -0,0 +1,34 @@
+%
+% ICNELIA : Helper for compile, clean and create run scripts for
+% an erlang OTP application
+%
+% Copyright (C) 2012, Jorge Garrido <jorge.garrido@morelosoft.com>
+%
+% All rights reserved.
+%
+-module(icnelia_utils).
+
+-export([all_atoms/1, chmod/2, get_value/2, get_pa_string/1]).
+
+% all elements in a list as atoms
+all_atoms(List) ->
+ [ list_to_atom(Elem) || Elem <- List ].
+
+% set permissions for a file/script
+chmod(File, Permissions) ->
+ [] = os:cmd("chmod " ++ Permissions ++ " " ++ File).
+
+% get value with key given
+get_value(KeyLookUp, List) ->
+ case [ Value || {Key, Value} <- List, Key == KeyLookUp ] of
+ [] ->
+ [];
+ [ Value ] ->
+ Value
+ end.
+
+% concat the string for pa runner script option
+get_pa_string([]) ->
+ "";
+get_pa_string([E | Es]) ->
+ "$" ++ E ++ " " ++ get_pa_string(Es).

0 comments on commit dc9fdd8

Please sign in to comment.