Skip to content

Commit

Permalink
gen.commander task compatible with Phoenix 1.3. Fixed #19
Browse files Browse the repository at this point in the history
  • Loading branch information
grych committed Aug 8, 2017
1 parent 4034b77 commit fc23580
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
36 changes: 28 additions & 8 deletions lib/mix/tasks/drab.gen.commander.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Drab.Mixfile do
use Mix.Project
@version "0.5.2"
@version "0.5.3"

def project do
[app: :drab,
Expand Down
4 changes: 2 additions & 2 deletions priv/templates/drab/drab.gen.commander.ex.eex
Original file line number Diff line number Diff line change
@@ -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
#
Expand Down

0 comments on commit fc23580

Please sign in to comment.