feat: Improve environment handling

This commit is contained in:
Robert Prehn 2023-08-10 10:55:27 +00:00
parent 65a976b54f
commit dbb6241c40
No known key found for this signature in database
5 changed files with 30 additions and 13 deletions

View file

@ -4,6 +4,7 @@ test:
paths:
- .mash-cache.tar.gz
script:
- if [ -e ".mash-cache.tar.gz" ]; then tar -xzf .mash-cache.tar.gz --atime-preserve; fi
- mix local.hex --force
- mix local.rebar --force
- mix deps.get

View file

@ -9,6 +9,10 @@ defmodule MashConfig do
name: :restore_cache,
run: restore_cache()
},
%{
name: :compile_dev,
run: mix("compile", [], env: [{"MIX_ENV", "dev"}])
},
%{
name: :test,
needs: [:restore_cache],
@ -21,8 +25,14 @@ defmodule MashConfig do
},
%{
name: :save_cache,
needs: [:test],
needs: [:test, :compile_dev],
run: save_cache()
},
%{
name: :function_test,
run: fn _io_pid ->
IO.puts("This line should be captured.")
end
}
]
end

View file

@ -5,11 +5,17 @@ defmodule Mash.Helpers do
@doc "Create a run function for a job that executes a mix task with name `task_name` and the arguments `args`"
@spec mix(String.t(), [String.t()]) :: function()
def mix(task_name, args \\ []) do
preferred_mix_env = Mix.Task.preferred_cli_env(task_name) || Mix.env()
def mix(task_name, args \\ [], opts \\ []) do
env = Keyword.get(opts, :env, [])
mix_env =
Enum.find_value(env, fn {key, value} -> key == "MIX_ENV" && value end) ||
Atom.to_string(Mix.Task.preferred_cli_env(task_name)) ||
Atom.to_string(Mix.env()) ||
:dev
fn io_pid ->
cmd("mix", [task_name | args], io_pid, [{"MIX_ENV", Atom.to_string(preferred_mix_env)}])
cmd("mix", [task_name | args], io_pid, [{"MIX_ENV", mix_env} | env])
end
end
@ -17,9 +23,11 @@ defmodule Mash.Helpers do
Create a run function which executes the script/binary named `command` with the arguments `args`.
"""
@spec shell(String.t(), [String.t()]) :: function()
def shell(command, args \\ []) do
def shell(command, args \\ [], opts \\ []) do
env = Keyword.get(opts, :env, [])
fn io_pid ->
cmd(command, args, io_pid)
cmd(command, args, io_pid, env)
end
end
@ -33,7 +41,7 @@ defmodule Mash.Helpers do
@spec save_cache(String.t(), [String.t()]) :: function()
def save_cache(name \\ ".mash-cache", files \\ ["deps", "_build"]) do
fn io_pid ->
cmd("tar", ["-czpf", "#{name}.tar.gz" | files], io_pid)
cmd("tar", ["-czpf", "#{name}.tar.gz" | files], io_pid, [])
end
end
@ -48,18 +56,16 @@ defmodule Mash.Helpers do
path = "#{name}.tar.gz"
if File.exists?(path) do
cmd("tar", ["-xzf", "#{name}.tar.gz", "--atime-preserve"], io_pid)
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
defp cmd(command, args, io_pid, env \\ []) do
defp cmd(command, args, io_pid, env) do
exec_string = "-c #{command} #{Enum.join(args, " ")}"
System.cmd("script", ["-eqf", exec_string, "/dev/null"],

View file

@ -3,7 +3,7 @@ defmodule Mash.Job do
Structure that represents the configuration and status of one Mash job.
"""
defstruct name: nil, needs: [], run: nil, state: nil, task: nil, io_pid: nil
defstruct name: nil, needs: [], run: nil, env: [], state: nil, task: nil, io_pid: nil
def from_map(map) do
struct(__MODULE__, map)

View file

@ -11,7 +11,7 @@ defmodule Mix.Tasks.Mash do
receive do
{:result, failure_count} when failure_count > 0 ->
Mix.raise("#{failure_count} jobs failed.", exit_status: 1)
Mix.raise("#{failure_count} job(s) failed.", exit_status: 1)
{:result, 0} ->
:ok