feat: Improve environment handling
This commit is contained in:
		
							parent
							
								
									65a976b54f
								
							
						
					
					
						commit
						dbb6241c40
					
				
					 5 changed files with 30 additions and 13 deletions
				
			
		|  | @ -4,6 +4,7 @@ test: | ||||||
|     paths: |     paths: | ||||||
|     - .mash-cache.tar.gz |     - .mash-cache.tar.gz | ||||||
|   script: |   script: | ||||||
|  |     - if [ -e ".mash-cache.tar.gz" ]; then tar -xzf .mash-cache.tar.gz --atime-preserve; fi | ||||||
|     - mix local.hex --force |     - mix local.hex --force | ||||||
|     - mix local.rebar --force |     - mix local.rebar --force | ||||||
|     - mix deps.get |     - mix deps.get | ||||||
|  |  | ||||||
							
								
								
									
										12
									
								
								.mash.exs
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								.mash.exs
									
									
									
									
									
								
							|  | @ -9,6 +9,10 @@ defmodule MashConfig do | ||||||
|         name: :restore_cache, |         name: :restore_cache, | ||||||
|         run: restore_cache() |         run: restore_cache() | ||||||
|       }, |       }, | ||||||
|  |       %{ | ||||||
|  |         name: :compile_dev, | ||||||
|  |         run: mix("compile", [], env: [{"MIX_ENV", "dev"}]) | ||||||
|  |       }, | ||||||
|       %{ |       %{ | ||||||
|         name: :test, |         name: :test, | ||||||
|         needs: [:restore_cache], |         needs: [:restore_cache], | ||||||
|  | @ -21,8 +25,14 @@ defmodule MashConfig do | ||||||
|       }, |       }, | ||||||
|       %{ |       %{ | ||||||
|         name: :save_cache, |         name: :save_cache, | ||||||
|         needs: [:test], |         needs: [:test, :compile_dev], | ||||||
|         run: save_cache() |         run: save_cache() | ||||||
|  |       }, | ||||||
|  |       %{ | ||||||
|  |         name: :function_test, | ||||||
|  |         run: fn _io_pid -> | ||||||
|  |           IO.puts("This line should be captured.") | ||||||
|  |         end | ||||||
|       } |       } | ||||||
|     ] |     ] | ||||||
|   end |   end | ||||||
|  |  | ||||||
|  | @ -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`" |   @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() |   @spec mix(String.t(), [String.t()]) :: function() | ||||||
|   def mix(task_name, args \\ []) do |   def mix(task_name, args \\ [], opts \\ []) do | ||||||
|     preferred_mix_env = Mix.Task.preferred_cli_env(task_name) || Mix.env() |     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 -> |     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 | ||||||
|   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`. |   Create a run function which executes the script/binary named `command` with the arguments `args`. | ||||||
|   """ |   """ | ||||||
|   @spec shell(String.t(), [String.t()]) :: function() |   @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 -> |     fn io_pid -> | ||||||
|       cmd(command, args, io_pid) |       cmd(command, args, io_pid, env) | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|  | @ -33,7 +41,7 @@ defmodule Mash.Helpers do | ||||||
|   @spec save_cache(String.t(), [String.t()]) :: function() |   @spec save_cache(String.t(), [String.t()]) :: function() | ||||||
|   def save_cache(name \\ ".mash-cache", files \\ ["deps", "_build"]) do |   def save_cache(name \\ ".mash-cache", files \\ ["deps", "_build"]) do | ||||||
|     fn io_pid -> |     fn io_pid -> | ||||||
|       cmd("tar", ["-czpf", "#{name}.tar.gz" | files], io_pid) |       cmd("tar", ["-czpf", "#{name}.tar.gz" | files], io_pid, []) | ||||||
|     end |     end | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|  | @ -48,18 +56,16 @@ defmodule Mash.Helpers do | ||||||
|       path = "#{name}.tar.gz" |       path = "#{name}.tar.gz" | ||||||
| 
 | 
 | ||||||
|       if File.exists?(path) do |       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 |       else | ||||||
|         IO.puts( |         IO.puts( | ||||||
|           IO.ANSI.yellow() <> "Warning: cache file #{path} does not exist." <> IO.ANSI.reset() |           IO.ANSI.yellow() <> "Warning: cache file #{path} does not exist." <> IO.ANSI.reset() | ||||||
|         ) |         ) | ||||||
| 
 |  | ||||||
|         {[], 0} |  | ||||||
|       end |       end | ||||||
|     end |     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, " ")}" |     exec_string = "-c #{command} #{Enum.join(args, " ")}" | ||||||
| 
 | 
 | ||||||
|     System.cmd("script", ["-eqf", exec_string, "/dev/null"], |     System.cmd("script", ["-eqf", exec_string, "/dev/null"], | ||||||
|  |  | ||||||
|  | @ -3,7 +3,7 @@ defmodule Mash.Job do | ||||||
|   Structure that represents the configuration and status of one Mash job. |   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 |   def from_map(map) do | ||||||
|     struct(__MODULE__, map) |     struct(__MODULE__, map) | ||||||
|  |  | ||||||
|  | @ -11,7 +11,7 @@ defmodule Mix.Tasks.Mash do | ||||||
| 
 | 
 | ||||||
|     receive do |     receive do | ||||||
|       {:result, failure_count} when failure_count > 0 -> |       {: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} -> |       {:result, 0} -> | ||||||
|         :ok |         :ok | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue
	
	 Robert Prehn
						Robert Prehn