How to distribute an escript with my library? (using rebar3)

Hi all,

I wrote a library xxx, bootstrapped with rebar3 new lib xxx,
I added an escript to xxx, which I generate with rebar3 escriptize.
The idea is to be able to use the library directly from the command line.

In another project yyy, bootstrapped with rebar3 new app yyy,
I added xxx as a dependency. Is there a way to include the xxx escript
as well?

Cheers!

You can probably do that with overlays. Start here and see if that’s what you’re looking for.

Nice idea, but I thought releases were for applications, not libraries?

Right. You said you had an application yyy. You can add that to the application so it copies the xxx script when the library is built as part of the application build process.

Sorry for my misunderstanding.

I have “published” my lib xxx on a git repository.

yyy is just an example user of xxx. xxx is meant to be used by whatever app.

When I install xxx with {deps, [{xxx, {git, "..."}]} in rebar.config
I don’t find the escript anywhere in the yyy _build folder.

Rebar3 does not automatically expose the escript from a library dependency. Escript is a compiled artifact (a standalone .beam-bundle with a main/1 function) and is not automatically propagated via dependencies and it does not copy or build the escript because escriptize is not part of the standard build lifecycle of a library dependency.

I’ve just tried this with this post hook in the lib:

{post_hooks,
    [{compile, "mkdir -p $REBAR_BUILD_DIR/bin"},
     {compile, "cp src/esc $REBAR_BUILD_DIR/bin/my_script"},
     {compile, "chmod +x $REBAR_BUILD_DIR/bin/my_script"}]}

and it works. Hope that’s what you’re searching for. Here’s more docs about variables in hooks.

EDIT: src/esc is path to escript source

1 Like

Thank you, makes sense.

Maybe I could try with a custom rebar3 plugin that will run rebar3 escriptize inside the dependency folder and then links the escript to the app bin folder.

1 Like

Thank you, this is what I ended up doing:


In the library codebase, I added a Makefile (will be used later in the post)

escript:
	@rebar3 escriptize

and a rebar3 hook to copy the escript to the root, which is ignored by git

{post_hooks, [{escriptize, "cp $REBAR_BUILD_DIR/bin/edm ."}]}.

In the app that uses the library, I added these hooks:

{post_hooks, [{compile, "mkdir -p $REBAR_BUILD_DIR/bin"}                        
             ,{compile, "make -C $REBAR_DEPS_DIR/edm escript"}                  
             ,{compile, "ln -s $REBAR_DEPS_DIR/edm/edm $REBAR_BUILD_DIR/bin/edm"}
             ]}.

Where edm is the name of the library.

This way I end up with the escript in _build/default/bin/edm.

Cheers!

2 Likes