简体   繁体   中英

Custom Mix Task to generate ex files

I'm currently trying to write a mix task that automates the scaffolding of an Elm app in a phoenix (v1.3) project. It's generating all the elm files / js script to add, installing node and elm modules correctly. I'm also generating an elm_view.ex and elm_controller.ex that takes the name of the current otp_app , and it outputs the files correctly like below:

defmodule PipeDream.Web.ElmController do
  use PipeDream.Web, :controller

  def index(conn, _params) do
    render conn, "index.html"
  end
end

However, when I start the server with mix phx.server I get an error: "module PipeDream.Web.ElmController is not available" . I've found that when I go to these files and save them the module gets picked up correctly (similar error with the elm_view.ex ).

Could this be something to do with how I'm rendering the controller and view templates? The templates look like this:

defmodule <%= @app_name %>.Web.ElmController do
  use <%= @app_name %>.Web, :controller

  def index(conn, _params) do
    render conn, "index.html"
  end
end

and I'm using EEx.eval_string(template_string, assigns: [app_name: app]) to add the app module name and then writing the file with Mix.Generator.create_file

Would be super grateful for any help or suggestions!

Since there are many comments above, I decided to post a [hacky but yet working] solution as an answer for the future visitors.

There is some kind of file creation time issue when Elixir/Mix try to cache/check the dependencies. While the sledgehammer solution proposed by Steve Pallen ( rm -rf deps && rm -rf _build && mix do deps.get, deps.compile, compile ) should work always , there might be a more handy hack: call File.touch/2 on newly generated files to enforce Mix to recompile them.

Also, as a sidenote, I would suggest to use Mix.Generator.embed_template/2 to compile and embed template, rather than generic EEx.eval_string to clarity the intent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM