Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing extra arguments to eval release command #12292

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/mix/lib/mix/tasks/eval.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ defmodule Mix.Tasks.Eval do
)

case head do
[to_eval] ->
[to_eval | argv] ->
cond do
Mix.Project.get() ->
Mix.Task.run("app.config", ["--no-app-loading" | args])
Expand All @@ -81,8 +81,16 @@ defmodule Mix.Tasks.Eval do
)
end

Code.eval_string(to_eval)
Mix.Task.reenable("eval")
old_argv = System.argv()

try do
System.argv(argv)

Code.eval_string(to_eval)
Mix.Task.reenable("eval")
after
System.argv(old_argv)
end

_ ->
Mix.raise("\"mix eval\" expects a single string to evaluate as argument")
Expand Down
17 changes: 13 additions & 4 deletions lib/mix/lib/mix/tasks/release.init.ex
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,15 @@ defmodule Mix.Tasks.Release.Init do
echo "ERROR: EVAL expects an expression as argument" >&2
exit 1
fi

script="$2"
shift 2
export_release_sys_config
exec "$REL_VSN_DIR/elixir" \
--cookie "$RELEASE_COOKIE" \
--erl-config "$RELEASE_SYS_CONFIG" \
--boot "$REL_VSN_DIR/$RELEASE_BOOT_SCRIPT_CLEAN" \
--boot-var RELEASE_LIB "$RELEASE_ROOT/lib" \
--vm-args "$RELEASE_VM_ARGS" --eval "$2"
--vm-args "$RELEASE_VM_ARGS" --eval "$script" -- "$@"
;;

remote)
Expand Down Expand Up @@ -385,13 +386,21 @@ defmodule Mix.Tasks.Release.Init do
goto end

:eval
set EVAL=%~2
shift
:loop
shift
if not "%1"=="" (
set args=%args% %1
goto :loop
)
"!REL_VSN_DIR!\elixir.bat" ^
--eval "%~2" ^
--eval "!EVAL!" ^
--cookie "!RELEASE_COOKIE!" ^
--erl-config "!RELEASE_SYS_CONFIG!" ^
--boot "!REL_VSN_DIR!\!RELEASE_BOOT_SCRIPT_CLEAN!" ^
--boot-var RELEASE_LIB "!RELEASE_ROOT!\lib" ^
--vm-args "!RELEASE_VM_ARGS!"
--vm-args "!RELEASE_VM_ARGS!" -- %args%
goto end

:remote
Expand Down
24 changes: 24 additions & 0 deletions lib/mix/test/mix/tasks/eval_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@ defmodule Mix.Tasks.EvalTest do
end)
end

test "can accept arguments", context do
josevalim marked this conversation as resolved.
Show resolved Hide resolved
in_tmp(context.test, fn ->
Mix.Tasks.Eval.run(["send self(), {:argv, System.argv()}", "bar", "baz"])
assert_received {:argv, ~w[bar baz]}
end)
end

test "reset argv to previous value", context do
josevalim marked this conversation as resolved.
Show resolved Hide resolved
argv = System.argv()

try do
in_tmp(context.test, fn ->
System.argv(["foo"])

Mix.Tasks.Eval.run(["send self(), {:argv, System.argv()}", "bar", "baz"])
assert_received {:argv, ~w[bar baz]}

assert ["foo"] == System.argv()
end)
after
System.argv(argv)
end
end

test "runs without mix.exs" do
Mix.Project.pop()

Expand Down
6 changes: 6 additions & 0 deletions lib/mix/test/mix/tasks/release_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,12 @@ defmodule Mix.Tasks.ReleaseTest do
assert String.trim_trailing(hello_world) == "hello_world"
refute File.exists?(Path.join(root, "RELEASE_BOOTED"))

{hello_world, 0} =
System.cmd(script, ["eval", "IO.inspect(System.argv( ))", "a", "b", "c"])

assert String.trim_trailing(hello_world) == ~S(["a", "b", "c"])
refute File.exists?(Path.join(root, "RELEASE_BOOTED"))

open_port(script, [~c"eval", ~c"Application.ensure_all_started(:release_test)"])

assert %{
Expand Down