diff --git a/.gitignore b/.gitignore index 1269f14..703420d 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,5 @@ mash-*.tar # Temporary files, for example, from tests. /tmp/ + +.mash-cache.tar.gz diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f1bd7de..c2e9e00 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -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 diff --git a/.mash.exs b/.mash.exs index a7fc6a4..5ee9cd7 100644 --- a/.mash.exs +++ b/.mash.exs @@ -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 diff --git a/lib/mash/helpers.ex b/lib/mash/helpers.ex index b1cf76b..2397538 100644 --- a/lib/mash/helpers.ex +++ b/lib/mash/helpers.ex @@ -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)