Skip to content

Commit

Permalink
Calculate build hash when collecting files
Browse files Browse the repository at this point in the history
  • Loading branch information
eproxus committed Jul 6, 2021
1 parent 2ed45cb commit 61d58a8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 31 deletions.
27 changes: 9 additions & 18 deletions src/grisp_tools_build.erl
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,9 @@ install(S0) ->

run_hooks(S2, post_install, [{cd, InstallPath}]).

post(#{build := #{overlay := Overlay}} = S0) ->
% info("Computing file hashes"),
{Hash, HashIndex} = grisp_tools_util:overlay_hash(Overlay),


% info("Writing hashes to file. Hash: ~p", [Hash]),
post(#{build := #{hash := #{index := Index}}} = S0) ->
PackageListing = filename:join(mapz:deep_get([paths, install], S0), "GRISP_PACKAGE_FILES"),
ok = file:write_file(PackageListing, HashIndex),
ok = file:write_file(PackageListing, Index),

% info("Copying revision string into install dir"),
ToolchainRoot = mapz:deep_get([paths, toolchain], S0),
Expand All @@ -209,19 +204,15 @@ post(#{build := #{overlay := Overlay}} = S0) ->
{ok, _} -> ok;
_ -> error({missing_toolchain_revision, RevSource})
end,
mapz:deep_put([build, hash], Hash, S0).
S0.

tar(#{build := #{flags := #{tar := true}}} = S0) ->
#{
build := #{hash := Hash},
otp_version := Version,
paths := #{package := PackagePath, install := InstallPath}
} = S0,
CacheFile = grisp_tools_util:package_name(#{otp_version => Version, hash => Hash}),
Filename = filename:join(PackagePath, CacheFile),
grisp_tools_util:ensure_dir(Filename),
shell_ok(S0, ["tar -zcf ", Filename, " ."], [{cd, InstallPath}]),
event(S0, [{file, relative(Filename)}]);
#{paths := #{package := PackagePath, install := InstallPath}} = S0,
Name = grisp_tools_util:package_name(S0),
Package = filename:join(PackagePath, Name),
grisp_tools_util:ensure_dir(Package),
shell_ok(S0, ["tar -zcf ", Package, " ."], [{cd, InstallPath}]),
event(S0, [{file, relative(Package)}]);
tar(S0) ->
event(S0, ['_skip']).

Expand Down
12 changes: 3 additions & 9 deletions src/grisp_tools_deploy.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ run(State) ->
fun grisp_tools_step:version/1
]},
fun grisp_tools_step:collect/1,
fun calculate_hash/1,
fun package/1,
fun release/1,
fun copy/1
Expand All @@ -29,14 +28,9 @@ run(State) ->

%--- Tasks ---------------------------------------------------------------------

calculate_hash(#{build := #{overlay := Overlay}} = S0) ->
% FIXME: Move to function in _steps module and use in build task
{Hash, _HashIndex} = grisp_tools_util:overlay_hash(Overlay),
S0#{hash => Hash}.

package(#{custom_build := true, hash := Hash} = State0) ->
package(#{custom_build := true, build := #{hash := #{value := Hash}}} = State0) ->
event(State0, [{type, {custom_build, Hash}}]);
package(#{hash := Hash} = State0) ->
package(#{build := #{hash := #{value := Hash}}} = State0) ->
State1 = event(State0, [{type, {package, Hash}}]),
grisp_tools_util:weave(State1, [
fun meta/1,
Expand Down Expand Up @@ -200,7 +194,7 @@ download_loop({ReqID, RequestPid}, Handle, State0, Bytes) ->
State1 = event(State0, ['_skip']),
mapz:deep_merge([State1, #{package => #{state => not_modified}}]);
{http, {ReqID, {{_HTTPVer, 404, "Not Found"}, _Headers, _Body}}} ->
#{otp_version := OTPVersion, hash := Hash} = State0,
#{otp_version := OTPVersion, build := #{hash := #{value := Hash}}} = State0,
error({package, {not_found, OTPVersion, Hash}});
{http, Other} ->
State1 = event(State0, [{error, Other}]),
Expand Down
4 changes: 3 additions & 1 deletion src/grisp_tools_step.erl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ apps(#{apps := Apps} = State0) ->

collect(#{platform := Platform} = State0) ->
Platforms = [default, Platform],
lists:foldl(fun collect_platform_files/2, State0, Platforms).
State1 = lists:foldl(fun collect_platform_files/2, State0, Platforms),
{Hash, HashIndex} = grisp_tools_util:build_hash(State1),
mapz:deep_put([build, hash], #{value => Hash, index => HashIndex}, State1).

%--- Internal ------------------------------------------------------------------

Expand Down
6 changes: 3 additions & 3 deletions src/grisp_tools_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
-export([rm/1]).
-export([build_overlay/4]).
-export([deploy_overlay/4]).
-export([overlay_hash/1]).
-export([build_hash/1]).
-export([merge_build_config/2]).
-export([source_hash/2]).
-export([with_file/3]).
Expand Down Expand Up @@ -98,7 +98,7 @@ paths(Root, Platform, Version) ->
otp_dir(Root, Platform, {_Components, _Pre, _Build, Ver}) ->
filename:join([Root, "_grisp", Platform, "otp", Ver]).

package_name(#{otp_version := {_, _, _, OTPVersion}, hash := Hash}) ->
package_name(#{otp_version := {_, _, _, OTPVersion}, build := #{hash := #{value := Hash}}}) ->
iolist_to_binary(["grisp_otp_build_", OTPVersion, "_", Hash, ".tar.gz"]).

package_cache_temp(State) ->
Expand Down Expand Up @@ -178,7 +178,7 @@ collect_overlay(Dir, Platform, Versions, Init, CollectFun) ->
merge_build_config(C1, C2) ->
mapz:deep_merge(C1, C2).

overlay_hash(#{hooks := Hooks} = Overlay) ->
build_hash(#{build := #{overlay := #{hooks := Hooks} = Overlay}}) ->
AllHooks = maps:from_list([{N, I} || {_T, F} <- maps:to_list(Hooks), {N, I} <- maps:to_list(F)]),
HashIndex = lists:sort(maps:fold(fun
(_Type, Files, Acc) ->
Expand Down

0 comments on commit 61d58a8

Please sign in to comment.