【问题标题】:Building an `escript` in an Elixir umbrella project在 Elixir 伞形项目中构建“escript”
【发布时间】:2018-08-14 04:49:48
【问题描述】:

我希望在 Elixir Umbrella project 中构建一个 escript。 我有两个同级应用 CompressorPrinter

Compressor 依赖于 snappyer package,它是 google 快速压缩算法的 nif 包装器。

# apps/compressor/mix.exs

defmodule Compressor.MixProject do
  # ..
  defp deps do
    [
      {:snappyer, "~> 1.2.4"},
    ]
  end
end

# apps/compressor/lib/compressor.ex

defmodule Compressor do
  def compress(message) do
    :snappyer.compress(message)
  end
end

打印机需要Compressor,压缩一些数据并打印结果。

# apps/printer/mix.exs

defmodule Printer.MixProject do
  # ..
  def project do
    [
      app: :printer,
      version: "0.1.0",
      build_path: "../../_build",
      config_path: "../../config/config.exs",
      deps_path: "../../deps",
      lockfile: "../../mix.lock",
      elixir: "~> 1.7",
      escript: escript(),
      start_permanent: Mix.env() == :prod,
      deps: deps()
    ]
  end

  defp escript do
    [main_module: Printer.CLI]
  end

  defp deps do
    [
      {:compressor, in_umbrella: true},
    ]
  end
end

# apps/printer/lib/printer/cli.ex

defmodule Printer.CLI do
  def main(args \\ []) do
    IO.inspect Compressor.compress(<<1, 2, 3>>)
  end
end

当我通过 mix 运行 Printer.CLI.main([]) 时,它会按预期打印结果

$ mix run -e "Printer.CLI.main([])"
{:ok, <<3, 8, 1, 2, 3>>}

但是,当我通过 escript 运行它时,它会失败:

$  cd apps/printer && mix escript.build && ./printer
Generated escript printer with MIX_ENV=dev
** (UndefinedFunctionError) function :snappyer.compress/1 is undefined (module :snappyer is not available)
    (snappyer) :snappyer.compress(<<1, 2, 3>>)
    (printer) lib/printer/cli.ex:3: Printer.CLI.main/1
    (elixir) lib/kernel/cli.ex:105: anonymous fn/3 in Kernel.CLI.exec_fun/2

兄弟伞应用程序中是否允许使用 escript?如果没有,是否有任何已知的解决方法?

这是 minimal, complete, and verifiable example

【问题讨论】:

    标签: elixir


    【解决方案1】:

    我不确定这是否有帮助,但我认为这个问题可能与伞形应用无关。我认为问题的发生是因为您尝试使用 nif

    来自escriptdocs

    注意:escripts 不支持需要从 priv 目录存储或读取工件的项目和依赖项。

    另外,请参阅elixir issue 的答案:

    不幸的是,这是 escripts 的限制。他们无法访问 priv 中的任何内容,因此他们无法访问嵌入的 .so 文件。换句话说,目前无法构建包含 NIF 的 escript。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-20
      • 2016-07-24
      • 2015-01-13
      • 2016-10-14
      • 2017-09-23
      • 2015-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多