Permalink
Browse files

initial

  • Loading branch information...
0 parents commit e707417e2c3378d03424e91cded78f23ece54710 Jorge Garrido committed Oct 9, 2012
Showing with 282 additions and 0 deletions.
  1. +8 −0 LICENSE.txt
  2. +59 −0 README
  3. +29 −0 boot
  4. +41 −0 include/icnelia.hrl
  5. +145 −0 src/icnelia.erl
@@ -0,0 +1,8 @@
+==========================================================================================================
+ 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.
+==========================================================================================================
59 README
@@ -0,0 +1,59 @@
+ ===== icnelia =====
+
+ icnelia: is a word in nahuatl languaje that means "help"
+
+ Helper for compile, clean and create run scripts for your erlang OTP application
+
+ ===== how to start =====
+
+ clone icnelia repository from github:
+
+ $ git clone https://github.com/jorgegarrido/icnelia.git
+
+ move into icnelia directory:
+
+ $ cd icnelia
+
+ build the project and script:
+
+ $ ./boot
+
+ now you can use the icnelia script in your OTP applications:
+
+ ===== how to use =====
+
+ just place the icnelia script into your main directory of your application
+
+ to compile:
+
+ $ ./icnelia compile
+
+ and it will compile all the code in src directory, also set the compiled files (.beam)
+ under ebin directory
+
+ to clean:
+
+ $ ./icnelia clean
+
+ and it will clean all the compiled code in ebin directoy
+
+ to create a run script:
+
+ $ ./icnelia runner
+
+ and it will create a run script, to start your application (only start and load an erlang shell)
+
+ to create a run daemon script:
+
+ $ ./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 =====
+
+ ===== LICENSE =====
+
+ see LICENSE.txt for more info
29 boot
@@ -0,0 +1,29 @@
+#!/bin/sh
+#
+# 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.
+#
+
+# creates dir ebin if not exists
+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; }
+
+# creates script executable from beam
+# copy into script
+echo "copying the files ..." && cp ebin/icnelia.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; }
+
+# set permissions for escript
+echo "setting permissions ..." && chmod u+x icnelia || { echo "error"; exit 1; }
+
+# send a help message
+echo "Icnelia has been compiled, now you can use the icnelia script to"
+echo " compile, clean and create run scripts for your erlang otp application"
@@ -0,0 +1,41 @@
+%
+% 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.
+%
+
+% include dir
+-define(include_dir, "include/").
+
+% source dir
+-define(source_dir, "src/").
+
+% ebin (compiled) dir
+-define(ebin_dir, "ebin/").
+
+% wildcard for .erl on source dir
+-define(src_erl, "src/*.erl").
+
+% wildcard for .beam on ebin dir
+-define(ebin_beam, "ebin/*.beam").
+
+% wildcard for .app on ebin dir
+-define(src_app_ebin, "ebin/*.app").
+
+% wildcard for .app.src on src dir
+-define(src_app_src, "src/*.app.src").
+
+% message when compiling
+-define(msg_c, "compiling ~s ~n").
+
+% message when cleaning
+-define(msg_d, "cleaning ~s ~n").
+
+% config file (must be called 'icnelia.config').
+-define(cfg_file, "icnelia.config").
+
+% name for runner script
+-define(run, "run").
@@ -0,0 +1,145 @@
+%
+% 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).
+
+-export([main/1]).
+
+-include("../include/icnelia.hrl").
+
+% 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).
+
+% help or icnelia?
+help() ->
+ io:fwrite(
+"Usage ~s Cmd[Opts] ~n"
+"Valid commands and options~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"
+" runner daemon : creates a script to run the application as daemon~n",
+[filename:basename(escript:script_name())]),
+ halt(1).

0 comments on commit e707417

Please sign in to comment.