简体   繁体   English

混合运行任意命令

[英]Running arbitrary commands in mix

I've got some protocol buffers definitions, in proto/*.proto .我在proto/*.proto有一些协议缓冲区定义。 My current Makefile-based build system compiles each of them into an Erlang module and header, and into a self-descriptor (as a .pb file) and then embeds the descriptor as a BEAM module:我当前基于 Makefile 的构建系统将它们中的每一个编译成一个 Erlang 模块和头文件,然后编译成一个自描述符(作为.pb文件),然后将描述符作为一个 BEAM 模块嵌入:

foo.proto -> foo_pb.erl, foo_pb.hrl
          -> foo.pb -> foo_pb_desc.beam

I've implemented a mix_protobuffs task to do the first step, and the foo.proto -> foo.pb part-step.我已经实现了一个mix_protobuffs任务来完成第一步,以及foo.proto -> foo.pb部分步骤。

I've also got the relevant code for embedding an arbitrary file into an Erlang module -- inspired by this answer -- but I'm wondering about the recommended way of integrating it into my mix-based build process.我还获得了将任意文件嵌入 Erlang 模块的相关代码——受此答案的启发——但我想知道将它集成到我的基于混合的构建过程中的推荐方法。

Similarly, I've got some other files that need to be processed and then embedded, which suggests that I shouldn't make the mix_protobuffs task know about embedding.同样,我还有一些其他文件需要处理然后嵌入,这表明我不应该让mix_protobuffs任务知道嵌入。

So I guess I'm asking: is there a way to have mix run arbitrary commands pre/post another task?所以我想我在问:有没有办法在另一个任务之前/之后混合运行任意命令?

The common way would be to define your own compiler.常见的方法是定义自己的编译器。 For that, you are to create a module in Mix.Task.Compiler namespace and implement Mix.Task.Compiler behaviour.为此,您将在Mix.Task.Compiler命名空间中创建一个模块实现Mix.Task.Compiler行为。 Also the project/0 function of your mix.exs should be updated to return your new compiler.您的mix.exsproject/0函数mix.exs应该更新以返回您的新编译器。 Somewhat like below would do.有点像下面会做。

defmodule Mix.Tasks.Compile.Protobufs do
  use Mix.Task.Compiler

  @recursive true

  @impl Mix.Task.Compiler
  def run(argv) do
    # do your compilation, possibly with System.cmd/3
    :ok
  end
end

And in your project file:在您的项目文件中:

def project do
  [
    compilers: Mix.compilers() ++ [:protobufs],
    # ...
  ]
end

You also can call any existing mix task from your own task, like Mix.Tasks.Run.run(args) .您还可以从您自己的任务中调用任何现有的mix任务,例如Mix.Tasks.Run.run(args)

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

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