Skip to content

Commit

Permalink
Make the base compiler accept target file names
Browse files Browse the repository at this point in the history
The rebar_base_compiler:run/5 is an alternative to run/8, where
instead of source/target dirs and file name extensions, it takes
a list of tuples: [{SourceFileName, TargetFileName}], after the
idea by Tuncer Ayaz in discussion with Luis Rascão and me.

This is useful for situations where the target file name cannot be
expressed as only a new suffix or extension.  For example the gpb
protocol buffer compiler can take an option to add a file name prefix
as well as a suffix.

As a result, the values passed around inside the internal functions in
rebar_base_compiler are either source file names (eg. when the run/4
function has been called), or items, which are two-tuples
{SourceFileName,TargetFileName} (when the run/7,8 or /5 functions have
been called).
  • Loading branch information
tomas-abrahamsson committed Jan 24, 2015
1 parent 135e629 commit 2e9706f
Showing 1 changed file with 40 additions and 22 deletions.
62 changes: 40 additions & 22 deletions src/rebar_base_compiler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
-export([run/4,
run/7,
run/8,
run/5,
ok_tuple/3,
error_tuple/5]).

Expand Down Expand Up @@ -62,23 +63,31 @@ run(Config, FirstFiles, SourceDir, SourceExt, TargetDir, TargetExt,

run(Config, FirstFiles, SourceDir, SourceExt, TargetDir, TargetExt,
Compile3Fn, Opts) ->
%% Convert simple extension to proper regex
SourceExtRe = "^[^._].*\\" ++ SourceExt ++ [$$],

Recursive = proplists:get_value(recursive, Opts, true),
%% Find all possible source files
FoundFiles = rebar_utils:find_files(SourceDir, SourceExtRe, Recursive),
Recursive = proplists:get_value(recursive, Opts, true),
FoundFiles = rebar_utils:find_files_by_ext(SourceDir, SourceExt, Recursive),

%% Remove first files from found files
RestFiles = [Source || Source <- FoundFiles,
not lists:member(Source, FirstFiles)],

FirstUnits = source_to_unit_each(FirstFiles,
SourceDir, SourceExt,
TargetDir, TargetExt),
RestUnits = source_to_unit_each(RestFiles,
SourceDir, SourceExt,
TargetDir, TargetExt),
run(Config, FirstUnits, RestUnits, Compile3Fn, Opts).

%% FirstUnits and RestUnits are lists of tuples: {Source,Target}
run(Config, FirstUnits, RestUnits, Compile3Fn, Opts) ->

%% Check opts for flag indicating that compile should check lastmod
CheckLastMod = proplists:get_bool(check_last_mod, Opts),

run(Config, FirstFiles, RestFiles,
fun(S, C) ->
Target = target_file(S, SourceDir, SourceExt,
TargetDir, TargetExt),
run(Config, FirstUnits, RestUnits,
fun({S, Target}, C) ->
simple_compile_wrapper(S, Target, Compile3Fn, C, CheckLastMod)
end).

Expand All @@ -103,6 +112,10 @@ simple_compile_wrapper(Source, Target, Compile3Fn, Config, true) ->
skipped
end.

source_to_unit_each(Files, SourceDir, SourceExt, TargetDir, TargetExt) ->
[{File, target_file(File, SourceDir, SourceExt, TargetDir, TargetExt)}
|| File <- Files].

target_file(SourceFile, SourceDir, SourceExt, TargetDir, TargetExt) ->
BaseFile = remove_common_path(SourceFile, SourceDir),
filename:join([TargetDir, filename:basename(BaseFile, SourceExt) ++ TargetExt]).
Expand All @@ -117,8 +130,8 @@ remove_common_path1(FilenameParts, _) ->
filename:join(FilenameParts).


compile(Source, Config, CompileFn) ->
case CompileFn(Source, Config) of
compile(Unit, Config, CompileFn) ->
case CompileFn(Unit, Config) of
ok ->
ok;
skipped ->
Expand All @@ -129,24 +142,29 @@ compile(Source, Config, CompileFn) ->

compile_each([], _Config, _CompileFn) ->
ok;
compile_each([Source | Rest], Config, CompileFn) ->
case compile(Source, Config, CompileFn) of
compile_each([Unit | Rest], Config, CompileFn) ->
case compile(Unit, Config, CompileFn) of
ok ->
?CONSOLE("Compiled ~s\n", [Source]);
?CONSOLE("Compiled ~s\n", [unit_source(Unit)]);
{ok, Warnings} ->
report(Warnings),
?CONSOLE("Compiled ~s\n", [Source]);
?CONSOLE("Compiled ~s\n", [unit_source(Unit)]);
skipped ->
?INFO("Skipped ~s\n", [Source]);
?INFO("Skipped ~s\n", [unit_source(Unit)]);
Error ->
?CONSOLE("Compiling ~s failed:\n",
[maybe_absname(Config, Source)]),
[maybe_absname(Config, unit_source(Unit))]),
maybe_report(Error),
?DEBUG("Compilation failed: ~p\n", [Error]),
?FAIL
end,
compile_each(Rest, Config, CompileFn).

unit_source({Source, _Target}) ->
Source;
unit_source(Source) ->
Source.

compile_queue(_Config, [], []) ->
ok;
compile_queue(Config, Pids, Targets) ->
Expand All @@ -168,17 +186,17 @@ compile_queue(Config, Pids, Targets) ->
?DEBUG("Worker compilation failed: ~p\n", [Error]),
?FAIL;

{compiled, Source, Warnings} ->
{compiled, Unit, Warnings} ->
report(Warnings),
?CONSOLE("Compiled ~s\n", [Source]),
?CONSOLE("Compiled ~s\n", [unit_source(Unit)]),
compile_queue(Config, Pids, Targets);

{compiled, Source} ->
?CONSOLE("Compiled ~s\n", [Source]),
{compiled, Unit} ->
?CONSOLE("Compiled ~s\n", [unit_source(Unit)]),
compile_queue(Config, Pids, Targets);

{skipped, Source} ->
?INFO("Skipped ~s\n", [Source]),
{skipped, Unit} ->
?INFO("Skipped ~s\n", [unit_source(Unit)]),
compile_queue(Config, Pids, Targets);

{'DOWN', Mref, _, Pid, normal} ->
Expand Down

0 comments on commit 2e9706f

Please sign in to comment.