/
clean.ex
61 lines (49 loc) · 1.58 KB
/
clean.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
defmodule Mix.Tasks.Clean do
use Mix.Task
@shortdoc "Deletes generated application files"
@recursive true
@moduledoc """
Deletes generated application files.
This command deletes all build artifacts for the current project.
Dependencies' sources and build files are cleaned only if the
`--deps` option is given.
By default this task works across all environments, unless `--only`
is given.
"""
@switches [deps: :boolean, only: :string]
@impl true
def run(args) do
Mix.Project.get!()
loadpaths!()
{opts, _, _} = OptionParser.parse(args, switches: @switches)
# First, we get the tasks. After that, we clean them.
# This is to avoid a task cleaning a compiler module.
tasks =
for compiler <- [:protocols] ++ Mix.Tasks.Compile.compilers(),
module = Mix.Task.get("compile.#{compiler}"),
function_exported?(module, :clean, 0),
do: module
Enum.each(tasks, & &1.clean())
build =
Mix.Project.build_path()
|> Path.dirname()
|> Path.join("*#{opts[:only]}")
if opts[:deps] do
build
|> Path.wildcard()
|> Enum.each(&File.rm_rf/1)
else
build
|> Path.join("lib/#{Mix.Project.config()[:app]}")
|> Path.wildcard()
|> Enum.each(&File.rm_rf/1)
end
end
# Loadpaths without checks because compilers may be defined in deps.
defp loadpaths! do
options = ["--no-elixir-version-check", "--no-deps-check", "--no-archives-check"]
Mix.Task.run("loadpaths", options)
Mix.Task.reenable("loadpaths")
Mix.Task.reenable("deps.loadpaths")
end
end