Save Routes for when the build breaks

One thing that I miss from Ruby is that when the code fails to compile, you can’t run “mix phx.routes” to see what the valid route helpers are.
It’s particularly a problem when you are trying to change routes and your guess breaks the compile.

So here is a mix task people might find helpful.
It dumps the current routes after every successful test run.
That way if the compile breaks you can look into routes.txt to see what they used to be.

#  lib/mix/tasks/routes.ex
defmodule Mix.Tasks.Routes do
 @shortdoc "Dump Routes to a File" 
 use Mix.Task
 import ExUnit.CaptureIO
 def run(args) do
   {_opts, _, _ } = OptionParser.parse(args, aliases: [])
   execute_main = fn ->
     Mix.Tasks.Phx.Routes.run(args)
   end
   output = capture_io(execute_main)
   File.open("routes.txt", [:write], fn(file) ->
     String.split( output, "\n")
     |> Enum.map( fn k -> IO.puts(file, k) end)
   end)
 end
end
# And then change the alias in mix.exs
#
defp aliases do
  # ...  
  "test": ["ecto.create --quiet", "ecto.migrate", "test", "routes"],
                                                          ^^^^^^^^
2 Likes