From fc23580b8b07c763bfccdf598183426ffa0f2635 Mon Sep 17 00:00:00 2001 From: Tomek Gryszkiewicz Date: Tue, 8 Aug 2017 21:09:25 +0200 Subject: [PATCH] gen.commander task compatible with Phoenix 1.3. Fixed #19 --- lib/mix/tasks/drab.gen.commander.ex | 36 ++++++++++++++----- mix.exs | 2 +- priv/templates/drab/drab.gen.commander.ex.eex | 4 +-- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/lib/mix/tasks/drab.gen.commander.ex b/lib/mix/tasks/drab.gen.commander.ex index 1057127..2b0ce97 100644 --- a/lib/mix/tasks/drab.gen.commander.ex +++ b/lib/mix/tasks/drab.gen.commander.ex @@ -14,26 +14,27 @@ defmodule Mix.Tasks.Drab.Gen.Commander do def run(args) do [module] = validate_args!(args) - binding = Mix.Phoenix.inflect(module) - path = binding[:path] + inf = Mix.Phoenix.inflect(module) + module = web_module(inf) + path = inf[:path] # Drab requires Phoenix, so I can use its brilliant helpers - Mix.Phoenix.check_module_name_availability!(binding[:module] <> "Commander") - check_controller_existence!(path, binding[:module]) + Mix.Phoenix.check_module_name_availability!(module <> "Commander") + check_controller_existence!(path, module) - copy_from paths(), "priv/templates/drab/", binding, [ - {:eex, "drab.gen.commander.ex.eex", "web/commanders/#{path}_commander.ex"} + copy_from paths(), "priv/templates/drab/", [module: module], [ + {:eex, "drab.gen.commander.ex.eex", "#{web_path()}/commanders/#{path}_commander.ex"} ] Mix.shell.info """ - Add the following line to your #{binding[:module]}Controller: + Add the following line to your #{module}Controller: use Drab.Controller """ end defp check_controller_existence!(path, module) do - controller_file = "web/controllers/#{path}_controller.ex" + controller_file = "#{web_path()}/controllers/#{path}_controller.ex" unless File.exists?(controller_file) do unless Mix.shell.yes?("Can't find corresponding #{module}Controller in #{controller_file}. Proceed? ") do Mix.raise "Aborted" @@ -55,6 +56,25 @@ defmodule Mix.Tasks.Drab.Gen.Commander do [".", :drab] end + defp web_module(inflected) do + if phoenix12?() do + inflected[:module] + else + "#{inflected[:web_module]}.#{inflected[:alias]}" + end + end + + defp web_path() do + if phoenix12?() do + "web" + else + #TODO: read web path from Phoenix View :root + "lib/#{Drab.Config.app_name()}_web" + end + end + + defp phoenix12?(), do: Regex.match?(~r/^1.2/, Application.spec(:phoenix, :vsn) |> to_string()) + if Regex.match?(~r/^1.2/, Application.spec(:phoenix, :vsn) |> to_string()) do defp copy_from(paths, source_path, binding, mapping) do Mix.Phoenix.copy_from paths, source_path, "", binding, mapping diff --git a/mix.exs b/mix.exs index ad7d87c..8bc61ac 100644 --- a/mix.exs +++ b/mix.exs @@ -1,6 +1,6 @@ defmodule Drab.Mixfile do use Mix.Project - @version "0.5.2" + @version "0.5.3" def project do [app: :drab, diff --git a/priv/templates/drab/drab.gen.commander.ex.eex b/priv/templates/drab/drab.gen.commander.ex.eex index b86d929..f0dba9f 100644 --- a/priv/templates/drab/drab.gen.commander.ex.eex +++ b/priv/templates/drab/drab.gen.commander.ex.eex @@ -1,12 +1,12 @@ defmodule <%= module %>Commander do use Drab.Commander - # place your event handlers here + # Place your event handlers here # # def button_clicked(socket, sender) do # set_prop socket, "#output_div", innerHTML: "Clicked the button!" # end # - # place you callbacks here + # Place you callbacks here # # onload :page_loaded #