Permalink
Browse files

Generate the ewgi_examples.app file (VSN from the Makefile; MODULES c…

…onstructed using the names of the compiled modules - all under the ebin folder).
  • Loading branch information...
1 parent e089f8f commit 4de07b2c185d94210d1caeb0483e9606d07513ed @davide davide committed Oct 13, 2009
Showing with 89 additions and 12 deletions.
  1. +3 −4 Makefile
  2. +2 −8 src/ewgi.app
  3. +44 −0 support/compile.erl
  4. +40 −0 support/gen_app_file.erl
View
@@ -6,8 +6,7 @@ APP := ewgi
all: erl ebin/$(APP).app
erl: ebin lib
- @$(ERL) -pa $(EBIN_DIRS) -pa ebin -noinput +B \
- -eval 'case make:all() of up_to_date -> halt(0); error -> halt(1) end.'
+ @./support/compile.erl ebin src/$(APP).app $(EBIN_DIRS)
docs:
@erl -noshell -run edoc_run application '$(APP)' '"."' '[]'
@@ -16,8 +15,8 @@ clean:
@echo "removing:"
@rm -fv ebin/*.beam ebin/*.app
-ebin/$(APP).app: src/$(APP).app
- @cp -v src/$(APP).app $@
+ebin/$(APP).app: src/$(APP).app Makefile
+ @./support/gen_app_file.erl src/$(APP).app $@ $(VSN)
ebin:
@mkdir ebin
View
@@ -1,13 +1,7 @@
{application, ewgi,
[{description, "Erlang Webserver Gateway Interface"}
- ,{vsn, "0.2"}
+ ,{vsn, "%VSN%"}
,{applications, [kernel, stdlib]}
,{env, []}
- ,{modules, [ewgi_api
- ,ewgi_application
- ,ewgi_inets
- ,ewgi_mochiweb
- ,ewgi_testapp
- ,ewgi_yaws
- ]}
+ ,{modules, [%MODULES%]}
]}.
View
@@ -0,0 +1,44 @@
+#!/usr/bin/env escript
+%% -*- erlang -*-
+%%! -noinput +B
+
+%% @author Davide Marquês <nesrait@gmail.com>
+%% @copyright 2009 Davide Marquês <nesrait@gmail.com>
+%%
+%% @doc Wrapper script for building a project using an Emakefile.
+%% If new modules are detected we do a touch("src/$(APP).app") to trigger (in the
+%% Makefile) the recreation of the ebin/$(APP).app file with the new modules.
+%% @end
+%%
+%% Licensed under the MIT license:
+%% http://www.opensource.org/licenses/mit-license.php
+
+main([EBinFolder, AppFileSrc | Others]) ->
+ [code:add_patha(Other) || Other <- Others],
+ code:add_patha(EBinFolder),
+ M1 = filelib:wildcard(EBinFolder ++ "/*.beam"),
+ case make:all() of
+ up_to_date ->
+ %% If there are new/missing files regenerate the .app file
+ M2 = filelib:wildcard(EBinFolder ++ "/*.beam"),
+ if (M1 =/= M2) ->
+ touch(AppFileSrc);
+ true -> ok
+ end,
+ halt(0);
+ error ->
+ halt(1)
+ end;
+main(_) ->
+ io:format("Invalid arguments to compile.erl!").
+
+-include_lib("kernel/include/file.hrl").
+
+%% Opening/closing file because just calling file:write_file_info/2
+%% wasn't getting the job done.
+touch(FileName) ->
+ {ok, IoDevice} = file:open(FileName, [read, write]),
+ {ok, FileInfo} = file:read_file_info(FileName),
+ Now = calendar:now_to_local_time(erlang:now()),
+ ok = file:write_file_info(FileName, FileInfo#file_info{mtime=Now}),
+ file:close(IoDevice).
@@ -0,0 +1,40 @@
+#!/usr/bin/env escript
+%% -*- erlang -*-
+
+%% @author Davide Marquês <nesrait@gmail.com>
+%% @copyright 2009 Davide Marquês <nesrait@gmail.com>
+%%
+%% @doc .app file generator: copies AppSrc to AppTarget replacing %VSN% by the
+%% Version passed as a parameter and %MODULES% by the names of the modules
+%% for which we can find .beam files under the AppTargets directory.
+%% @end
+%%
+%% Licensed under the MIT license:
+%% http://www.opensource.org/licenses/mit-license.php
+
+main([AppSrc, AppTarget, Version]) ->
+ EBinFolder = filename:dirname(AppTarget) ++ "/",
+ Modules = get_app_modules(EBinFolder),
+ generate_app_file(AppSrc, AppTarget, Version, Modules),
+ ok;
+main(_) ->
+ io:format("Invalid arguments to gen_app.erl!").
+
+get_app_modules(EBinFolder) ->
+ BeamFiles = filelib:wildcard(EBinFolder ++ "*.beam"),
+ Names = [extract_module(beam_lib:info(X)) || X <- BeamFiles],
+ string:join(Names, ", ").
+
+extract_module([]) ->
+ "undefined";
+extract_module([{module, Mod}|_]) ->
+ atom_to_list(Mod);
+extract_module([_|R]) ->
+ extract_module(R).
+
+generate_app_file(AppSrc, AppTarget, Version, Modules) ->
+ {ok, AppFile} = file:read_file(AppSrc),
+ App1 = re:replace(AppFile, "%VSN%", Version),
+ App2 = re:replace(App1, "%MODULES%", Modules),
+ ok = file:write_file(AppTarget, App2).
+

0 comments on commit 4de07b2

Please sign in to comment.