feat: Implement tarball caching

This commit is contained in:
Robert Prehn 2023-08-10 09:48:57 +00:00
parent c9b685d9f1
commit 7170010c6b
No known key found for this signature in database
4 changed files with 44 additions and 1 deletions

2
.gitignore vendored
View file

@ -24,3 +24,5 @@ mash-*.tar
# Temporary files, for example, from tests.
/tmp/
.mash-cache.tar.gz

View file

@ -1,5 +1,8 @@
test:
image: "elixir:1.14.4"
cache:
paths:
- .mash-cache.tar.gz
script:
- mix local.hex --force
- mix local.rebar --force

View file

@ -5,11 +5,25 @@ defmodule MashConfig do
def jobs do
[
%{
name: :restore_cache,
run: restore_cache()
},
%{
name: :test,
needs: [:restore_cache],
run: mix("test")
},
%{name: :credo, run: mix("credo", ["--all"])},
%{
name: :credo,
needs: [:restore_cache],
run: mix("credo", ["--all"])
},
%{
name: :save_cache,
needs: [:test],
run: save_cache()
}
]
end
end

View file

@ -17,6 +17,30 @@ defmodule Mash.Helpers do
end
end
def save_cache(name \\ ".mash-cache", files \\ ["deps", "_build"]) do
fn io_pid ->
cmd("tar", ["-czpf", "#{name}.tar.gz" | files], io_pid)
end
end
def restore_cache(name \\ ".mash-cache") do
fn io_pid ->
Process.group_leader(self(), io_pid)
path = "#{name}.tar.gz"
if File.exists?(path) do
cmd("tar", ["-xzf", "#{name}.tar.gz", "--atime-preserve"], io_pid)
else
IO.puts(
IO.ANSI.yellow() <> "Warning: cache file #{path} does not exist." <> IO.ANSI.reset()
)
{[], 0}
end
end
end
def fun(fun) do
fn io_pid ->
Process.group_leader(self(), io_pid)