[erlang-questions] mini rebar recipe, (Was Re: escript and archives )

Angel J. Alvarez Miguel clist@REDACTED
Wed Oct 19 10:16:27 CEST 2011


as nobody care about this question, i post this just for newbies like me.

I ended using rebar...

i created a .src rebar file for my setup.

walker.app.src

{application, walker,
 [
  {description, ""},
  {vsn, "1"},
  {registered, []},
  {applications, [
                  kernel,
                  stdlib
                 ]},
  {mod, { walker, []}},
  {env, []}
 ]}.

and then having my walker.erl and the getopt.erl filesin my src directory

i did "rebar compile" and then "rebar escriptize", rebar cares of generating a walker.app file 
and then packages all the beam files and the app making a fresh escript file.

So you have to make a app file to allow escript manage the zip file..

This is the result app file:


{application,walker,
             [{description,[]},
              {vsn,"1"},
              {registered,[]},
              {applications,[kernel,stdlib]},
              {mod,{walker,[]}},
              {env,[]},
              {modules,[getopt,walker]}]}.

Just for the record the walker.erl (a rip of ex1.erl from getopt) is :

-module(walker).

-export([main/1]).

%% as of getopt examples
option_spec_list() ->
	CurrentUser = case os:getenv("USER") of
		false -> "user";
		User  ->  User
	end,
	[
	{recursion,   $r,        "recurse",     {boolean,false},             "Recurse over directory contents"},
	{directory,   undefined, undefined,     string,                "Target directory"}
	].

main([]) ->
	getopt:usage(option_spec_list(), escript:script_name());
main(Args) ->
	OptSpecList = option_spec_list(),
	io:format("Args (~p) ~ngetopt:parse/2 returns:~n~n", [Args]),
	case getopt:parse(OptSpecList, Args) of
		{ok, {Options, NonOptArgs}} -> 
			io:format("Options:~n  ~p~n~nNon-option arguments:~n  ~p~n", [Options, NonOptArgs]),
			case proplists:is_defined(directory,Options) of
				true -> do_walk(Options);
				false -> getopt:usage(OptSpecList, escript:script_name())  %% No target directory?
			end;
		{error, {Reason, Data}} ->
			io:format("Error: ~s ~p~n~n", [Reason, Data]),
			getopt:usage(OptSpecList, escript:script_name())
	end.

do_walk(Args) ->
	Files = filelib:fold_files(proplists:get_value(directory,Args),
		".*",
		proplists:get_bool(recursion,Args),
		fun(Path, Acc) ->
			io:format("Procesando fichero ~s \n",[Path]),
			{ok, Bin} = file:read_file(Path),
			[{Path, Bin}|Acc] end,
		[]).


(A simple escript exercise for un cmdline tool that im planning to do)


/Angel

On Martes, 18 de Octubre de 2011 10:47:31 Angel J. Alvarez Miguel escribió:
> Hi,
> 
> I want to packe some beams on a .ez archive and then insert the escript
> header to get a selfcontaines script ala rebar..
> 
> i made a script witth a zip archive the usual way..
> 
> 	Files = filelib:fold_files("./ebin",
> 				".*",
> 				true,
> 				fun(Path, Acc) ->
> 					io:format("Procesando fichero ~s \n",[Path]),
> 					{ok, Bin} = file:read_file(Path),
> 					[{Path, Bin}|Acc] end,
> 				[]),
> 	{ok, {"mem", ZipBin}} = zip:create("mem", Files, [memory]),
> 	Script = <<"#!/usr/bin/env escript\n%%! -noshell -noinput\n",
> ZipBin/binary>>,
> 
> 	ok = file:write_file("walker", Script),
> 	os:cmd("chmod u+x walker"),
> 
> 
> i have my ebin dir with my two test files, the getopt.erl from jcomellas
> git and a simple walker.erl file:
> 
> -module(walker).
> 
> -export([main/1]).
> 
> main([String]) ->
> 	io:format("Up and running!! mayormente... (args:~s )\n",[String]),
> 	halt(0);
> 
> main(_) ->
>   io:format("Uso: ~s <args> \n",[escript:script_name()]),
>   halt(1).
> 
> 
> but then escript doesnt find the walker file:
> 
> ./walker
> escript: exception error: undefined function walker:main/1
>   in function  escript:run/2
>   in call from escript:start/1
>   in call from init:start_it/1
>   in call from init:start_em/1
> 
> if I place the escript into the ebin files it obviouosly find the waker
> module but is the .beam that is there!.
> 
> 
> Do need I to include a "walker.app"  to allow script to find where my main
> function is from the zip archive?
> 
> 
> Regards,  Angel
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list