Created
October 27, 2016 11:23
-
-
Save eproxus/8529262fdef8254c5ed404eed7ef4fd1 to your computer and use it in GitHub Desktop.
mix_rel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Mix.Tasks.Rel do | |
@moduledoc """ | |
Creates a release from a Relx configuration file. | |
mix rel RELEASE_NAME | |
Finished releases end up in the `_rel` folder, both as folders and as | |
tarballs. | |
# Command line options | |
* `--dev` - create dev release (links applications) | |
""" | |
@dialyzer [:no_return] | |
use Mix.Task | |
@options [ | |
dev: :boolean, | |
clean: :boolean, | |
] | |
def run(args) do | |
{opts, argv, errors} = OptionParser.parse(args, strict: @options) | |
if errors != [] do | |
error(hd(errors)) | |
end | |
Mix.Task.run(:compile) | |
name = case argv do | |
[name] -> name | |
_else -> error("missing argument: RELEASE_NAME") | |
end | |
target = Path.join(["_rel", "#{Mix.env}"]) | |
if Keyword.get(opts, :clean, false) do | |
File.rm_rf!(Path.join([target, name])) | |
end | |
config = "config/config.exs" | |
sys_config = :io_lib.format('~72.p.~n', [Mix.Config.read!(config)]) | |
sys_config_path = '#{System.tmp_dir!}/#{name}-sys.config' | |
File.write!(sys_config_path, List.to_string(sys_config)) | |
result = :relx.do([ | |
output_dir: String.to_charlist(target), | |
lib_dir: '_build/#{Mix.env}/lib', | |
lib_dir: String.to_char_list( | |
Path.dirname(Path.expand(:code.lib_dir(:elixir))) | |
), | |
log_level: 2, | |
goals: [:iex, :sasl], | |
config: 'releases/#{name}.config', | |
sys_config: sys_config_path, | |
dev_mode: Keyword.get(opts, :dev, false), | |
], ['release', 'tar']) | |
case result do | |
{:ok, _state} -> | |
:ok | |
{:error, _reason} = error -> | |
error({:relx, error}) | |
end | |
end | |
defp error(reason), do: error(reason, 1) | |
defp error({:relx, error}, code) do | |
error(List.to_string(:relx.format_error(error)), code) | |
end | |
defp error({flag, nil}, code) do | |
error("unknown flag: #{flag}", code) | |
end | |
defp error(msg, code) when is_binary(msg) do | |
Mix.Shell.IO.error(msg) | |
exit({:shutdown, code}) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment