Skip to content

Commit fa5f09d

Browse files
committed
Render templates everywhere
1 parent 58a08c0 commit fa5f09d

File tree

3 files changed

+60
-58
lines changed

3 files changed

+60
-58
lines changed

src/grisp_tools_build.erl

+30-26
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ run(Configuration) ->
2929
]},
3030
fun grisp_tools_step:collect/1,
3131
fun download/1,
32-
{prepare, [
32+
{fun prepare/1, [
3333
fun clean/1,
3434
fun patch/1,
3535
{copy, [
@@ -78,6 +78,26 @@ download(#{otp_version := {_,_,_,Ver}} = State0) ->
7878
event(mapz:deep_put([build, download], false, State0), ['_skip'])
7979
end.
8080

81+
prepare(S0) ->
82+
Drivers = maps:to_list(mapz:deep_get([build, overlay, drivers], S0)),
83+
NIFs = maps:to_list(mapz:deep_get([build, overlay, nifs], S0)),
84+
DriverFiles = [#{name => filename:basename(N, ".c")} || {N, _P} <- Drivers],
85+
NIFFiles = [#{name => filename:basename(N, ".c")} || {N, _P} <- NIFs],
86+
87+
DrvPatchLineCount = 10 + length(DriverFiles),
88+
NifPatchLineCount = 9 + length(NIFFiles),
89+
90+
Context = #{
91+
erts_emulator_makefile_in => #{
92+
driver_lines => DrvPatchLineCount,
93+
nif_lines => NifPatchLineCount,
94+
total_lines => DrvPatchLineCount + NifPatchLineCount,
95+
drivers => DriverFiles,
96+
nifs => NIFFiles
97+
}
98+
},
99+
mapz:deep_put([build, context], Context, S0).
100+
81101
clean(#{build := #{flags := #{clean := true}}} = State0) ->
82102
State1 = event(State0, ['_run']),
83103
BuildPath = mapz:deep_get([paths, build], State1),
@@ -89,25 +109,9 @@ clean(#{build := #{flags := #{clean := true}}} = State0) ->
89109
clean(State0) ->
90110
event(State0, ['_skip']).
91111

92-
patch(#{build := #{overlay := #{patches := Patches}}} = State0) ->
112+
patch(#{build := #{overlay := #{patches := Patches}}} = S0) ->
93113
Sorted = lists:sort(maps:to_list(Patches)),
94-
95-
Drivers = maps:to_list(mapz:deep_get([build, overlay, drivers], State0)),
96-
NIFs = maps:to_list(mapz:deep_get([build, overlay, nifs], State0)),
97-
98-
DrvPatchLineCount = 10 + length(Drivers),
99-
NifPatchLineCount = 9 + length(NIFs),
100-
101-
Context = #{
102-
erts_emulator_makefile_in => #{
103-
driver_lines => DrvPatchLineCount,
104-
nif_lines => NifPatchLineCount,
105-
total_lines => DrvPatchLineCount + NifPatchLineCount,
106-
drivers => [#{name => filename:basename(N, ".c")} || {N, _P} <- Drivers],
107-
nifs => [#{name => filename:basename(N, ".c")} || {N, _P} <- NIFs]
108-
}
109-
},
110-
lists:foldl(fun apply_patch/2, mapz:deep_put([build, patch_context], Context, State0), Sorted).
114+
lists:foldl(fun apply_patch/2, S0, Sorted).
111115

112116
files(State0) -> copy_files(State0, files, "").
113117

@@ -222,12 +226,11 @@ build_step(Command, Opts, State0) ->
222226

223227
%--- Internal ------------------------------------------------------------------
224228

225-
apply_patch({Name, #{source := File} = Patch}, State0) ->
229+
apply_patch({Name, Patch}, State0) ->
226230
Dir = mapz:deep_get([paths, build], State0),
227-
Context = mapz:deep_get([build, patch_context], State0),
228-
Rendered = grisp_tools_template:render(File, Context),
229-
ok = file:write_file(filename:join(Dir, Name), Rendered),
230-
State4 = case shell(State0, "git apply " ++ Name ++ " --ignore-whitespace --reverse --check",
231+
Context = mapz:deep_get([build, context], State0),
232+
grisp_tools_util:write_file(Dir, Patch, Context),
233+
State4 = case shell(State0, ["git apply ", Name, " --ignore-whitespace --reverse --check"],
231234
[{cd, Dir}, return_on_error]) of
232235
{{ok, _Output}, State1} ->
233236
event(State1, [{skip, Patch}]);
@@ -249,9 +252,10 @@ sort_files(Apps, Files) ->
249252
end,
250253
lists:sort(SortFiles, [F || {_, F} <- maps:to_list(Files)]).
251254

252-
copy_file(Root, #{target := Target, source := Path} = File, State0) ->
255+
copy_file(Root, File, State0) ->
253256
State1 = event(State0, [File]),
254-
file:copy(Path, filename:join(Root, Target)),
257+
Context = mapz:deep_get([build, context], State0),
258+
grisp_tools_util:write_file(Root, File, Context),
255259
State1.
256260

257261
run_hooks(S0, Type, Opts) ->

src/grisp_tools_deploy.erl

+2-9
Original file line numberDiff line numberDiff line change
@@ -114,21 +114,14 @@ copy_files(#{copy := #{destination := Dest, force := Force}, release := Release}
114114
mapz:deep_get([deploy, overlay, files], State0)
115115
).
116116

117-
write_file(Dest, #{target := Target, source := Source} = File, Force, Context, State0) ->
117+
write_file(Dest, #{target := Target} = File, Force, Context, State0) ->
118118
Path = filename:join(Dest, Target),
119119
State1 = event(State0, [files, {copy, File}]),
120-
Content = load_file(Source, Context),
121120
force_execute(Path, Force, fun(F) ->
122121
grisp_tools_util:ensure_dir(F),
123-
ok = file:write_file(F, Content)
122+
grisp_tools_util:write_file(Dest, File, Context)
124123
end, State1).
125124

126-
load_file({template, Source}, Context) ->
127-
grisp_tools_template:render(Source, Context);
128-
load_file(Source, _Context) ->
129-
{ok, Binary} = file:read_file(Source),
130-
Binary.
131-
132125
force_execute(File, Force, Fun, State0) ->
133126
State1 = case {filelib:is_file(File), Force} of
134127
{true, false} ->

src/grisp_tools_util.erl

+28-23
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
-export([build_hash/1]).
2323
-export([merge_build_config/2]).
2424
-export([source_hash/2]).
25+
-export([write_file/3]).
2526
-export([with_file/3]).
2627
-export([pipe/2]).
2728

@@ -184,8 +185,14 @@ build_hash(#{build := #{overlay := #{hooks := Hooks} = Overlay}}) ->
184185
(_Type, Files, Acc) ->
185186
Acc ++ maps:fold(fun(Name, Info, L) ->
186187
{File, Origin} = case Info of
187-
#{target := Target, source := Source} -> {Target, Source};
188-
#{source := Source} -> {Name, Source}
188+
#{target := Target, source := {template, Source}} ->
189+
{Target, Source};
190+
#{target := Target, source := Source} ->
191+
{Target, Source};
192+
#{source := {template, Source}} ->
193+
{Name, Source};
194+
#{source := Source} ->
195+
{Name, Source}
189196
end,
190197
{ok, Hash} = hash_file(Origin, sha256),
191198
[io_lib:format("~s ~s~n", [File, format_hash(sha256, Hash)])|L]
@@ -206,6 +213,16 @@ source_hash(Apps, Board) ->
206213
Targets2 = maps:merge(Targets, NIFFiles),
207214
hash_files(Targets2).
208215

216+
write_file(Root, #{target := Target} = File, Context) ->
217+
Content = read_file(File, Context),
218+
ok = file:write_file(filename:join(Root, Target), Content).
219+
220+
read_file(#{source := {template, Source}}, Context) ->
221+
grisp_tools_template:render(Source, Context);
222+
read_file(#{source := Source}, _Context) ->
223+
{ok, Binary} = file:read_file(Source),
224+
Binary.
225+
209226
with_file(File, Opts, Fun) ->
210227
Handle = case file:open(File, Opts) of
211228
{ok, H} -> H;
@@ -349,30 +366,18 @@ collect_file_list(App, Dir) -> collect_file_list(App, Dir, "").
349366

350367
collect_file_list(App, Dir, Root) ->
351368
InsertFile = fun(File, A) ->
352-
Info = #{
353-
name => File,
369+
{Source, Target} = check_template(
370+
filename:join(Dir, File),
371+
string:trim(filename:join(Root, File), both, "/")
372+
),
373+
A#{Target => #{
374+
name => Target,
354375
app => App,
355-
source => filename:join(Dir, File),
356-
target => string:trim(filename:join(Root, File), both, "/")
357-
},
358-
A#{File => Info}
376+
source => Source,
377+
target => Target
378+
}}
359379
end,
360380
lists:foldl(InsertFile, #{}, filelib:wildcard("*", binary_to_list(Dir))).
361-
% FIXME: Replace with
362-
% collect_file_list(App, Dir, Root) ->
363-
% InsertFile = fun(File, A) ->
364-
% {Source, Target} = check_template(
365-
% filename:join(Dir, File),
366-
% string:trim(filename:join(Root, File), both, "/")
367-
% ),
368-
% A#{File => #{
369-
% name => File,
370-
% app => App,
371-
% source => Source,
372-
% target => Target
373-
% }}
374-
% end,
375-
% lists:foldl(InsertFile, #{}, filelib:wildcard("*", binary_to_list(Dir))).
376381

377382
collect_file_tree(App, Root) ->
378383
filelib:fold_files(Root, ".*", true, fun(File, T) ->

0 commit comments

Comments
 (0)