简体   繁体   English

自定义混合任务在运行时不触发编译

[英]Custom mix task not triggering compile when run

I've got a custom mix task.我有一个自定义混合任务。 its working perfectly, however, it is not triggering a compile when it is called like other mix tasks do.它工作得很好,但是,当它像其他混合任务一样被调用时,它不会触发编译。 I can run the compile manually but this is very frustrating as I almost always forget to do it and have to run the task once or twice before I realize why I'm not seeing my changes.我可以手动运行编译,但这非常令人沮丧,因为我几乎总是忘记这样做并且必须运行一次或两次任务才能意识到为什么我没有看到我的更改。

Here is "my" task, what am i missing?这是“我的”任务,我错过了什么?

defmodule Mix.Tasks.Return do
  @moduledoc """
  Task for processing returns
  """
  use Mix.Task
  require Logger
  alias Returns

  @shortdoc "Check for returns and process"

  def run(args) do
    args
    |> get_return_file_name()
    |> Returns.process()
    |> Kernel.inspect()
    |> Logger.info()
  end

  defp get_file_name(nil), do: get_file_name([])
  defp get_file_name([]), do: get_default_file_name()
  defp get_file_name([filename]), do: filename

  defp get_default_file_name() do
    DateTime.utc_now() 
    |> DateTime.to_string()   
    |> String.split(" ")  
    |> List.first()
    |> (fn date -> "default-#{date}.txt" end).()
  end
end

You need to explicitly say in your task that you want to compile.您需要在任务中明确说明要编译。

@impl true
def run(args) do
  ...
  Mix.Task.run("compile")
  ...

A couple examples are app.tree , app.start and phx.routes .几个例子是app.treeapp.startphx.routes

This is because not all mix tasks require the compilation step.这是因为并非所有混合任务都需要编译步骤。 For example, mix deps.get does not compile the project before fetching dependencies, else you may have lots of errors/warnings about missing modules.例如, mix deps.get在获取依赖项之前不会编译项目,否则您可能会有很多关于缺少模块的错误/警告。 All the dependencies should be called manually, there is no implicit mechanism to call chains of tasks according to some “internal rules” built into mix .所有依赖项都应该手动调用,没有隐式机制根据mix内置的一些“内部规则”调用任务链。

The Mix.Task.run("compile") fix may not actually work. Mix.Task.run("compile")修复可能实际上不起作用。 José Valim has this suggestion: José Valim 有这样的建议:

That's because if they are recompiled, you won't execute the new module version in memory.这是因为如果重新编译它们,您将不会在内存中执行新的模块版本。 A work-around is for you to do this:一种解决方法是让您这样做:

def run(args) do
  Mix.Task.run("compile")
  __MODULE__.compiled_run(args)
end

def compiled_run(args) do
  ...
end

That will force it to pick the latest version of the module.这将迫使它选择模块的最新版本。 Although this is probably something we should tackle in Elixir itself.尽管这可能是我们应该在 Elixir 本身中解决的问题。

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

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