在Rake中,可以指定任务之间的依赖关系.然后,引擎构建依赖关系树,并按依赖关系的顺序执行这些任务,并且每个任务只执行一次.
在elixir/mix中是否有类似的机制?
task seed_users: [:seed_companies] do
# actions
end
task :seed_companies do
# actions
end
Run Code Online (Sandbox Code Playgroud) 我希望能够在正在运行的进程中多次运行ExUnit测试,例如iex.
为此,我的代码看起来有点像这样:
def test do
# Unload test files
["test"]
|> Mix.Utils.extract_files("*")
|> Enum.map(&Path.expand/1)
|> Code.unload_files
# Reenable tasks
~w(loadpaths deps.loadpaths test)
|> Enum.map(&Mix.Task.reenable/1)
# Run the test suite
Mix.Task.run("test", args)
# Cleanup
:elixir_config.put(:at_exit, [])
end
Run Code Online (Sandbox Code Playgroud)
这可以,但打印test/my_app/foo_test.exs:1 warning: redefining module FooTest我的测试文件中定义的每个模块.
我认为,因为我从:elixir_code_server这些警告中卸载这些文件不会被提出,但事实并非如此.
如果不诉诸沉默stderr等方法,我怎么能沉默或避免这些警告呢?
似乎有一个编译器标志可以用来抑制这些警告,但是没有明确的公共API来设置这个标志.我们似乎可以禁用这些警告消息,没有明确的API可以这样做.
请参阅elixir_compiler:get_opt/1
https://github.com/elixir-lang/elixir/blob/master/lib/elixir/src/elixir_compiler.erl#L8-L13
查看elixir_module:check_module_availability/3它检查的位置elixir_compiler:get_opt(ignore_module_conflict)
https://github.com/elixir-lang/elixir/blob/master/lib/elixir/src/elixir_module.erl#L408-L418
当我尝试运行mix deps.get或mix deps.compile收到此错误时:
== Compilation error on file lib/phoenix_ecto/html.ex ==
** (CompileError) lib/phoenix_ecto/html.ex:3: unknown key :model for struct Ecto.Changeset
(stdlib) lists.erl:1354: :lists.mapfoldl/3
Run Code Online (Sandbox Code Playgroud)
为什么会发生这种情况,我该如何解决?
如何将应用程序配置为仅在特定环境中在运行时加载?我知道我只能为测试环境配置依赖项。
有没有办法将应用程序配置mix.exs为仅在测试环境中加载?
例如:
def application do
[mod: {MyApp, []},
applications: [:phoenix]] end
defp deps do
[{:phoenix, "~> 1.2.1"}] end
Run Code Online (Sandbox Code Playgroud)
我可以只为测试环境配置凤凰应用程序吗?
我有一个 Python 脚本,作为我正在编写的 Elixir 库的一部分运行 - 也就是说,在启动应用程序时,我System.cmd("python", [path/to/script], [])在我的 start_link 函数之一中运行。
我将该脚本安装在用户的 deps/mylibrary/lib 文件夹中。
这path/to/script是我不知道该怎么做的部分 - 我可以通过评估System.cwd()和查看结果来推断脚本所在的位置,但这似乎是错误的。
我认为必须有一种方法可以使用 Mix 来做到这一点。
我现在浏览Elixir官方网站,并了解Mix。我遵循他们的榜样。
他们在文档中提到:
编译源代码时,Elixir会将工件编译到_build目录。但是,在许多情况下,为了避免不必要的复制,Elixir会创建从_build到实际源文件的文件系统链接。为true时,:build_embedded禁用此行为,因为它旨在提供在_build中运行应用程序所需的一切。
关于此摘录,我有几个问题:
_build文件夹中构建工件,而是在其他地方?为什么将所有工件放在_build文件夹中比其他地方更好?显然是因为最好将所有工件放在一个文件夹中吗?
最后一个问题 :start_permanent:
:start_permanent option starts your application in permanent mode, which means the Erlang VM will crash if your application’s supervision tree shuts down.。当监控树关闭时,使VM崩溃总是更好吗?这背后的动机是什么?我正在尝试运行两个不同的脚本,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"]
Run Code Online (Sandbox Code Playgroud)
只运行第一个文件.我试图重新启用run但我无法逃避文件名.
"run 'priv/repo/v1_to_v2_migrator.exs'; run -e 'Mix.Task.reenable(:run)'"
给出了这个错误:
** (Mix) No such file: priv/repo/v1_to_v2_migrator.exs;
文件结尾处包含分号的位置.
有没有办法挂钩 Elixir 的 Mix 内置任务以在另一个任务完成后执行任务?
我知道你可以做类似的事情。
defmodule Mix.Tasks.Other.Get
use Mix.Task
@shortdoc "Other dependencies?"
def run(_) do
Mix.Task.run("deps.get")
end
end
Run Code Online (Sandbox Code Playgroud)
但我有点希望在mix deps.get考虑使用make包装最有意义的命令后立即运行任务。(即make deps这将同时运行mix deps.get然后mix other.get)
通过混合在iex会话中运行凤凰服务器,如:
iex -S mix phx.server
Run Code Online (Sandbox Code Playgroud)
有时候会列出一些警告:
Compiling 1 file (.ex)
warning: variable "user" is unused
lib/app_web/controllers/user_controller.ex:37
Run Code Online (Sandbox Code Playgroud)
但似乎只是第一次运行服务器,之后我认为它是缓存的,所以它没有显示出来.我怎么能在以后看到这些警告来修复它们?谢谢.
当我在mix.exsand run 中定义别名时mix help,它只会将其描述显示为“在 mix.exs 中定义的别名”。
假设,例如,我有这个mix.exs:
defp aliases do
[
play: "run --no-halt"
]
end
Run Code Online (Sandbox Code Playgroud)
然后,该mix help命令显示如下任务列表:
...
mix local.rebar # Installs Rebar locally
mix new # Creates a new Elixir project
mix play # Alias defined in mix.exs
mix profile.cprof # Profiles the given file or expression with cprof
mix profile.eprof # Profiles the given file or expression with eprof
...
Run Code Online (Sandbox Code Playgroud)
如何描述play任务?