简体   繁体   English

如何在mix.exs中运行两次别名?

[英]How to alias run twice in mix.exs?

I'm trying to run two different scripts, v1_to_v2_migrator.exs and update_images.exs 我正在尝试运行两个不同的脚本,v1_to_v2_migrator.exs和update_images.exs

defp aliases do
  ["ecto.reset": ["ecto.drop", "ecto.create", "ecto.migrate", "run priv/repo/v1_to_v2_migrator.exs", "run priv/repo/update_images.exs"]

Only the first file runs. 只运行第一个文件。 I've tried to reenable run but I can't escape the filename. 我试图重新启用run但我无法逃避文件名。

"run 'priv/repo/v1_to_v2_migrator.exs'; run -e 'Mix.Task.reenable(:run)'"

gives this error: 给出了这个错误:

** (Mix) No such file: priv/repo/v1_to_v2_migrator.exs;

Where the file ending includes the semicolon. 文件结尾处包含分号的位置。

You can use Mix.Task.rerun/2 to invoke mix run twice like this: 您可以使用Mix.Task.rerun/2来调用mix run两次,如下所示:

["ecto.reset": [
  "ecto.drop",
  "ecto.create",
  "ecto.migrate",
  ~s|run -e 'Mix.Task.rerun("run", ["priv/repo/v1_to_v2_migrator.exs"]); Mix.Task.rerun("run", ["priv/repo/update_images.exs"])'|]]

For your particular example, you can pass multiple files to run: 对于您的特定示例,您可以传递多个文件来运行:

mix run -r priv/repo/foo.exs -r priv/repo/bar.exs

But if the question is how to generally reenable tasks, then @Dogbert's and @mudasobwa's approaches are correct. 但如果问题是如何重新启用任务,那么@ Dogbert和@ mudasobwa的方法是正确的。

While answer by @Dogbert would work, I'd suggest you take a different approach. 虽然@Dogbert的回答可行,但我建议你采取不同的方法。 When you find yourself stuck with the tool's provided functionality it usually means the paradigm change is required. 当您发现自己坚持使用该工具提供的功能时,通常意味着需要更改范例。

Unlike many other build tools, mix welcomes task creation. 与许多其他构建工具不同, mix欢迎创建任务。 It's easy, pretty straightforward and more idiomatic than executing multiple scripts. 它比执行多个脚本更简单,更直接,更惯用。 Just create a file, say, my_awesome_task.ex in your lib/mix/tasks directory (create the directory unless it already exists,) using the following scaffold: 只需使用以下脚手架在lib/mix/tasks目录中创建一个文件,比如my_awesome_task.ex (创建目录,除非它已经存在):

defmodule Mix.Tasks.MyAwesomeTask do
  use Mix.Task

  @shortdoc "Migrates v1 to v2 and updates images"

  @doc false
  def run(args \\ []) do
    # it’s better to implement stuff explicitly,
    #   but this is also fine
    Mix.Tasks.Run.run(["priv/repo/v1_to_v2_migrator.exs"])
    Mix.Tasks.Run.rerun(["priv/repo/update_images.exs"])
  end
end

Now all you need is to call this task in your mix.exs : 现在您只需要在mix.exs调用此任务:

["ecto.reset": ~w|ecto.drop ecto.create ecto.migrate my_awesome_task|]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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