Skip to content

Commit 656bd64

Browse files
author
José Valim
committed
Load config/config.exs into escript
Closes #2643
1 parent bc2cb15 commit 656bd64

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

lib/mix/lib/mix/tasks/escript.build.ex

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,29 +191,47 @@ defmodule Mix.Tasks.Escript.Build do
191191
end
192192

193193
defp gen_main(name, module, app) do
194+
config =
195+
if File.regular?("config/config.exs") do
196+
Mix.Config.read!("config/config.exs")
197+
else
198+
[]
199+
end
200+
194201
{:module, ^name, binary, _} =
195202
defmodule name do
196203
@module module
204+
@config config
197205
@app app
198206

207+
# We need to use Erlang modules at this point
208+
# because we are not sure Elixir is available.
199209
def main(args) do
200-
case Application.ensure_all_started(:elixir) do
210+
case :application.ensure_all_started(:elixir) do
201211
{:ok, _} ->
212+
load_config(@config)
202213
start_app(@app)
203214
args = Enum.map(args, &List.to_string(&1))
204215
Kernel.CLI.run fn _ -> @module.main(args) end, true
205216
_ ->
206-
io_error "Elixir is not available, aborting."
207-
System.halt(1)
217+
:io.put_chars :standard_error, "Elixir is not available, aborting.\n"
218+
:erlang.halt(1)
208219
end
209220
end
210221

222+
defp load_config(config) do
223+
for {app, kw} <- config, {k, v} <- kw do
224+
:application.set_env(app, k, v, persistent: true)
225+
end
226+
:ok
227+
end
228+
211229
defp start_app(nil) do
212230
:ok
213231
end
214232

215233
defp start_app(app) do
216-
case Application.ensure_all_started(app) do
234+
case :application.ensure_all_started(app) do
217235
{:ok, _} -> :ok
218236
{:error, {app, reason}} ->
219237
io_error "Could not start application #{app}: " <>

lib/mix/test/fixtures/escripttest/lib/escripttest.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ defmodule Escripttest do
44
end
55

66
def main(_args) do
7-
IO.puts "TEST"
7+
IO.puts Application.get_env(:foobar, :value, "TEST")
88
end
99
end

lib/mix/test/mix/tasks/escript_test.exs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ defmodule Mix.Tasks.EscriptTest do
4242
end
4343
end
4444

45-
test "generate simple escript" do
45+
test "generate escript" do
4646
Mix.Project.push Escript
4747

4848
in_fixture "escripttest", fn ->
@@ -55,7 +55,20 @@ defmodule Mix.Tasks.EscriptTest do
5555
end
5656
end
5757

58-
test "generate simple escript with path" do
58+
test "generate escript with config" do
59+
Mix.Project.push Escript
60+
61+
in_fixture "escripttest", fn ->
62+
File.write! "config/config.exs", """
63+
[foobar: [value: "FROM CONFIG"]]
64+
"""
65+
Mix.Tasks.Escript.Build.run []
66+
assert_received {:mix_shell, :info, ["Generated escript escriptest with MIX_ENV=dev"]}
67+
assert System.cmd("escript", ["escriptest"]) == {"FROM CONFIG\n", 0}
68+
end
69+
end
70+
71+
test "generate escript with path" do
5972
Mix.Project.push EscriptWithPath
6073

6174
in_fixture "escripttest", fn ->

0 commit comments

Comments
 (0)