Skip to content

Commit 2bafa0b

Browse files
mobileoverlordJosé Valim
authored andcommitted
Add preferred_cli_target (#9118)
1 parent c953de0 commit 2bafa0b

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

lib/mix/lib/mix/cli.ex

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ defmodule Mix.CLI do
2929
load_mix_exs()
3030
{task, args} = get_task(args)
3131
ensure_hex(task)
32-
change_env(task)
32+
maybe_change_env_and_target(task)
3333
run_task(task, args)
3434
end
3535

@@ -104,26 +104,39 @@ defmodule Mix.CLI do
104104
end
105105
end
106106

107-
defp change_env(task) do
108-
if env = preferred_cli_env(task) do
109-
Mix.env(env)
107+
defp maybe_change_env_and_target(task) do
108+
task = String.to_atom(task)
109+
config = Mix.Project.config()
110110

111-
if project = Mix.Project.pop() do
112-
%{name: name, file: file} = project
113-
Mix.Project.push(name, file)
114-
end
111+
env = preferred_cli_env(task, config)
112+
target = preferred_cli_target(task, config)
113+
env && Mix.env(env)
114+
target && Mix.target(target)
115+
116+
if env || target do
117+
reload_project()
118+
end
119+
end
120+
121+
defp reload_project() do
122+
if project = Mix.Project.pop() do
123+
%{name: name, file: file} = project
124+
Mix.Project.push(name, file)
115125
end
116126
end
117127

118-
defp preferred_cli_env(task) do
128+
defp preferred_cli_env(task, config) do
119129
if System.get_env("MIX_ENV") do
120130
nil
121131
else
122-
task = String.to_atom(task)
123-
Mix.Project.config()[:preferred_cli_env][task] || Mix.Task.preferred_cli_env(task)
132+
config[:preferred_cli_env][task] || Mix.Task.preferred_cli_env(task)
124133
end
125134
end
126135

136+
defp preferred_cli_target(task, config) do
137+
config[:preferred_cli_target][task]
138+
end
139+
127140
defp load_dot_config do
128141
path = Path.join(Mix.Utils.mix_config(), "config.exs")
129142

lib/mix/lib/mix/project.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,13 @@ defmodule Mix.Project do
6767
* `:preferred_cli_env` - a keyword list of `{task, env}` tuples where `task`
6868
is the task name as an atom (for example, `:"deps.get"`) and `env` is the
6969
preferred environment (for example, `:test`). This option overrides what
70-
specified by the tasks with the `@preferred_cli_env` attribute (see the
70+
is specified by the tasks with the `@preferred_cli_env` attribute (see the
7171
docs for `Mix.Task`). Defaults to `[]`.
7272
73+
* `:preferred_cli_target` - a keyword list of `{task, target}` tuples where
74+
`task` is the task name as an atom (for example, `:test`) and `target`
75+
is the preferred target (for example, `:host`). Defaults to `[]`.
76+
7377
For more options, keep an eye on the documentation for single Mix tasks; good
7478
examples are the `Mix.Tasks.Compile` task and all the specific compiler tasks
7579
(such as `Mix.Tasks.Compile.Elixir` or `Mix.Tasks.Compile.Erlang`).

lib/mix/test/mix/cli_test.exs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,32 @@ defmodule Mix.CLITest do
143143
System.delete_env("MIX_EXS")
144144
end
145145

146+
test "target config defaults to the user's preferred cli target", context do
147+
in_tmp(context.test, fn ->
148+
File.write!("custom.exs", """
149+
defmodule P do
150+
use Mix.Project
151+
def project, do: [app: :p, version: "0.1.0", preferred_cli_target: [test_task: :other]]
152+
end
153+
154+
defmodule Mix.Tasks.TestTask do
155+
use Mix.Task
156+
157+
def run(args) do
158+
IO.inspect {Mix.target, args}
159+
end
160+
end
161+
""")
162+
163+
System.put_env("MIX_EXS", "custom.exs")
164+
165+
output = mix(["test_task", "a", "b", "c"])
166+
assert output =~ ~s({:other, ["a", "b", "c"]})
167+
end)
168+
after
169+
System.delete_env("MIX_EXS")
170+
end
171+
146172
test "new with tests and cover" do
147173
in_tmp("new_with_tests", fn ->
148174
output = mix(~w[new .])

0 commit comments

Comments
 (0)