Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make use of build_per_environment

  • Loading branch information...
commit 4051ccd37d4331a53d57bcac7a465397e8d6e81c 1 parent bdbb606
José Valim josevalim authored
2  .gitignore
View
@@ -1,4 +1,4 @@
-/ebin
+/_build
/deps
/erl_crash.dump
/tmp
70 lib/dynamo.ex
View
@@ -127,6 +127,7 @@ defmodule Dynamo do
@before_compile { unquote(__MODULE__), :define_endpoint }
@before_compile { unquote(__MODULE__), :define_filters }
@before_compile { unquote(__MODULE__), :define_templates_paths }
+ @before_compile { unquote(__MODULE__), :define_source_paths }
@before_compile { unquote(__MODULE__), :define_static }
@before_compile { unquote(__MODULE__), :define_root }
@@ -169,17 +170,7 @@ defmodule Dynamo do
dynamo = config[:dynamo]
if dynamo[:compile_on_demand] do
- callback = fn
- path, acc when is_binary(path) ->
- (path |> Path.expand(root) |> Path.wildcard) ++ acc
- _, acc ->
- acc
- end
-
- source = Enum.reduce dynamo[:source_paths], [], callback
- templates = Enum.reduce dynamo[:templates_paths], [], callback
-
- Dynamo.Loader.append_paths(source -- templates)
+ Dynamo.Loader.append_paths(source_paths -- templates_paths)
Dynamo.Loader.enable
if Code.ensure_loaded?(IEx) and IEx.started? do
@@ -313,10 +304,9 @@ defmodule Dynamo do
end
templates_server = dynamo[:supervisor].TemplatesServer
-
templates_paths = lc path inlist templates_paths do
if is_binary(path) do
- quote do: Path.expand(unquote(path), root)
+ Path.expand(path)
else
Macro.escape(path)
end
@@ -339,6 +329,18 @@ defmodule Dynamo do
end
end
+ defmacro define_source_paths(env) do
+ dynamo = Module.get_attribute(env.module, :config)[:dynamo]
+ source = Enum.flat_map(dynamo[:source_paths], &Path.wildcard/1) |> Enum.map(&Path.expand/1)
+
+ quote location: :keep do
+ @doc """
+ Returns source paths after expansion.
+ """
+ def source_paths, do: unquote(source)
+ end
+ end
+
@doc false
defmacro define_static(env) do
module = env.module
@@ -364,48 +366,24 @@ defmodule Dynamo do
@doc false
defmacro define_root(env) do
- module = env.module
- dynamo = Module.get_attribute(module, :config)[:dynamo]
- root =
- cond do
- dynamo[:root] -> dynamo[:root]
- nil?(dynamo[:otp_app]) -> File.cwd!
- true -> nil
- end
+ dynamo = Module.get_attribute(env.module, :config)[:dynamo]
- if root do
+ if app = dynamo[:otp_app] do
quote location: :keep do
@doc """
- Returns the root path for this Dynamo.
+ Returns the root path for this Dynamo
+ based on the OTP app directory.
"""
- def root, do: unquote(root)
+ def root do
+ :code.lib_dir(unquote(app)) |> String.from_char_list!
+ end
end
else
- app = dynamo[:otp_app]
- tmp = "#{app}/tmp/#{dynamo[:env]}/#{app}"
-
quote location: :keep do
@doc """
- Returns the root path for this Dynamo
- based on the OTP app directory.
+ Returns the root path for this Dynamo.
"""
- def root do
- case :code.lib_dir(unquote(app)) do
- list when is_list(list) ->
- bin = String.from_char_list!(list)
- size = size(bin)
-
- if size > unquote(size(tmp)) do
- :binary.replace bin, unquote(tmp),
- unquote(atom_to_binary(app)), scope: { size, unquote(-size(tmp)) }
- else
- bin
- end
- _ ->
- raise "could not find OTP app #{unquote(dynamo[:otp_app])} for #{inspect __MODULE__}. " <>
- "This may happen if the directory name is different than the application name."
- end
- end
+ def root, do: unquote(dynamo[:root] || File.cwd!)
end
end
end
2  lib/mix/tasks/compile.dynamo.ex
View
@@ -58,7 +58,7 @@ defmodule Mix.Tasks.Compile.Dynamo do
project = Mix.project
dynamo = mod.config[:dynamo]
- compile_path = project[:compile_path]
+ compile_path = Mix.Project.compile_path
compile_exts = project[:elixirc_exts]
watch_exts = project[:elixirc_watch_exts]
source_paths = dynamo[:source_paths]
5 lib/mix/tasks/dynamo.ex
View
@@ -136,10 +136,9 @@ defmodule Mix.Tasks.Dynamo do
def project do
[ app: :<%= @app %>,
version: "0.0.1",
+ build_per_environment: true,
dynamos: [<%= @mod %>.Dynamo],
compilers: [:elixir, :dynamo, :app],
- env: [prod: [compile_path: "ebin"]],
- compile_path: "tmp/#{Mix.env}/<%= @app %>/ebin",
deps: deps ]
end
@@ -151,7 +150,7 @@ defmodule Mix.Tasks.Dynamo do
defp deps do
[ { :cowboy, github: "extend/cowboy" },
- { :dynamo, "<%= @version %>", <%= @dynamo %> } ]
+ { :dynamo, "~> <%= @version %>", <%= @dynamo %> } ]
end
end
"""
2  mix.exs
View
@@ -3,7 +3,7 @@ defmodule Dynamo.Mixfile do
def project do
[ app: :dynamo,
- elixir: "~> 0.11.0",
+ elixir: "~> 0.11.2-dev",
version: "0.1.0-dev",
name: "Dynamo",
source_url: "https://github.com/elixir-lang/dynamo",
4 test/dynamo/static_test.exs
View
@@ -10,7 +10,7 @@ defmodule Dynamo.StaticTest do
config :dynamo,
otp_app: :dynamo,
- static_root: "test/fixtures/static",
+ static_root: "../../../../test/fixtures/static",
static_route: "/static",
cache_static: true
end
@@ -24,7 +24,7 @@ defmodule Dynamo.StaticTest do
config :dynamo,
otp_app: :dynamo,
- static_root: "test/fixtures/static",
+ static_root: "../../../../test/fixtures/static",
static_route: "/static",
cache_static: false
end
2  test/dynamo_test.exs
View
@@ -37,7 +37,7 @@ defmodule DynamoTest do
## Config
test "defines root based on otp app" do
- assert ReloadApp.root == Path.expand("../..", __FILE__)
+ assert ReloadApp.root == String.from_char_list! :code.lib_dir(:dynamo)
end
test "defines root based on user config" do
53 test/mix/tasks_test.exs
View
@@ -4,7 +4,7 @@ defmodule Mix.TasksTest do
test "compiles an application" do
in_tmp "my_compiled_app", fn ->
- app_with_dynamo_deps_path
+ app_with_dynamo_deps_path(:prod)
output = System.cmd "MIX_ENV=prod mix compile"
assert output =~ %r(Compiled web/routers/application_router.ex)
@@ -12,61 +12,38 @@ defmodule Mix.TasksTest do
assert output =~ %r(Compiled lib/my_app/dynamo.ex)
assert output =~ %r(Generated my_compiled_app.app)
assert output =~ %r(Generated MyApp.Dynamo.CompiledTemplates)
- assert File.regular?("ebin/Elixir.MyApp.Dynamo.CompiledTemplates.beam")
+ assert File.regular?("_build/prod/lib/my_compiled_app/ebin/Elixir.MyApp.Dynamo.CompiledTemplates.beam")
# Can recompile after changes
File.touch!("web/routers/application_router.ex", { { 2030, 1, 1 }, { 0, 0, 0 } })
output = System.cmd "MIX_ENV=prod mix compile"
assert output =~ %r(Compiled web/routers/application_router.ex)
-
- # TODO: Get rid of this
- File.rm_rf "ebin"
-
- # Can compile for other environments too
- output = System.cmd "MIX_ENV=dev mix compile"
- assert output =~ %r(Generated my_compiled_app.app)
end
end
test "prints application filters" do
in_tmp "my_filters_app", fn ->
- app_with_dynamo_deps_path
+ app_with_dynamo_deps_path(:dev)
output = System.cmd "mix dynamo.filters"
assert output =~ %r(filter Dynamo.Filters.Head)
assert output =~ %r(filter \{Dynamo.Filters.Loader, *true, *true\})
assert output =~ %r(ApplicationRouter.service/1)
-
- # Check it works with first compilation in prod
- output = System.cmd "MIX_ENV=prod mix do compile, dynamo.filters"
- refute output =~ %r(Dynamo.Filters.Loader)
- assert output =~ %r(ApplicationRouter.service/1)
-
- # Check that noop compile also works
- output = System.cmd "MIX_ENV=prod mix do compile, dynamo.filters"
- refute output =~ %r(Dynamo.Filters.Loader)
- assert output =~ %r(ApplicationRouter.service/1)
end
end
test "runs application code" do
in_tmp "my_run_app", fn ->
- app_with_dynamo_deps_path
+ app_with_dynamo_deps_path(:dev)
output = System.cmd %s{mix run -e "IO.inspect HelloRouter.__info__(:module)"}
assert output =~ %r(HelloRouter)
-
- # TODO: Get rid of this
- File.rm_rf "ebin"
-
- output = System.cmd %s{MIX_ENV=prod mix do compile, run "IO.inspect HelloRouter.__info__(:module)"}
- assert output =~ %r(HelloRouter)
end
end
test "tests application code" do
in_tmp "my_test_app", fn ->
- app_with_dynamo_deps_path
+ app_with_dynamo_deps_path(:test)
File.write! "test/routers/hello_router_test.exs", """
defmodule HelloRouterTest do
@@ -78,28 +55,18 @@ defmodule Mix.TasksTest do
end
"""
- output = System.cmd %s{mix test}
+ output = System.cmd %s{unset MIX_ENV; mix test}
assert output =~ %r(3 tests, 0 failures)
end
end
- test "warns on missing dependencies" do
- in_tmp "missing_deps", fn ->
- Mix.Tasks.Dynamo.run [".", "--dev"]
- error = %r(Can't continue due to errors on dependencies)
-
- output = System.cmd "mix server"
- assert output =~ error
-
- output = System.cmd "MIX_ENV=prod mix server"
- assert output =~ error
- end
- end
-
- defp app_with_dynamo_deps_path do
+ defp app_with_dynamo_deps_path(env) do
Mix.Tasks.Dynamo.run [".", "--module", "MyApp", "--dev"]
File.cp! "../../mix.lock", "mix.lock"
+ File.mkdir_p!("_build/#{env}")
+ File.cp_r!("../../_build/shared/.", "_build/#{env}")
+
File.write! "mix.exs",
Regex.replace(%r"deps: deps", File.read!("mix.exs"), %s(deps: deps, deps_path: "../../deps"))
Please sign in to comment.
Something went wrong with that request. Please try again.