forked from dynamo/dynamo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dynamo.ex
77 lines (65 loc) · 1.73 KB
/
dynamo.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
defmodule Mix.Dynamo do
@moduledoc false
@doc """
Returns the app file location.
"""
def app_file do
Mix.project[:dynamo_app] || "config/app.ex"
end
@doc """
Check if the app beam file is stale compared to the app file,
env files and the mix file.
"""
def stale_app?(app) do
mix_file = Mix.Utils.source(Mix.Project.get!)
env_file = "config/environments/#{Dynamo.env}.exs"
app_beam = File.join(Mix.project[:compile_path], "#{app}.beam")
Mix.Utils.stale?([app_file(), mix_file, env_file], [app_beam])
end
@doc """
Returns the lock path.
"""
def lock_path do
File.join Mix.project[:compile_path], "env.lock"
end
@doc """
Reads the lock file. It returns a list with each line
in the lock or nil otherwise.
"""
def read_lock do
case File.iterator(lock_path) do
{ :ok, iterator } -> Enum.map(iterator, fn(x) -> x end)
{ :error, _ } -> nil
end
end
@doc """
Remove the lock (if it exists) and associated artifacts.
"""
def remove_lock do
if lock = read_lock do
File.rm!(lock_path)
path = Mix.project[:compile_path]
files = tl(lock) # Remove env
Enum.each files, fn(file) ->
File.rm_rf! File.join(path, "#{file}.beam")
end
end
end
@doc """
Takes a lock snapshot.
"""
def lock_snapshot(fun) do
remove_lock
query = File.join Mix.project[:compile_path], "*.beam"
pre = File.wildcard(query)
res = fun.()
pos = File.wildcard(query)
app = Dynamo.app /> atom_to_binary
new = Enum.map pos -- pre, &1 /> File.basename /> File.rootname
new = List.delete(new, app)
File.open! lock_path, [:write], fn(f) ->
Enum.each [Mix.env,app|new], IO.puts(f, &1)
end
res
end
end