-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
phx.gen.presence.ex
61 lines (48 loc) · 1.64 KB
/
phx.gen.presence.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.Phx.Gen.Presence do
@shortdoc "Generates a Presence tracker"
@moduledoc """
Generates a Presence tracker.
$ mix phx.gen.presence
$ mix phx.gen.presence MyPresence
The argument, which defaults to `Presence`, defines the module name of the
Presence tracker.
Generates a new file, `lib/my_app_web/channels/my_presence.ex`, where
`my_presence` is the snake-cased version of the provided module name.
"""
use Mix.Task
@doc false
def run([]) do
run(["Presence"])
end
def run([alias_name]) do
if Mix.Project.umbrella?() do
Mix.raise "mix phx.gen.presence must be invoked from within your *_web application root directory"
end
context_app = Mix.Phoenix.context_app()
otp_app = Mix.Phoenix.otp_app()
web_prefix = Mix.Phoenix.web_path(context_app)
inflections = Mix.Phoenix.inflect(alias_name)
inflections = Keyword.put(inflections, :module, "#{inflections[:web_module]}.#{inflections[:scoped]}")
binding = inflections ++ [
otp_app: otp_app,
pubsub_server: Module.concat(inflections[:base], "PubSub")
]
files = [
{:eex, "presence.ex", Path.join(web_prefix, "channels/#{binding[:path]}.ex")},
]
Mix.Phoenix.copy_from paths(), "priv/templates/phx.gen.presence", binding, files
Mix.shell().info """
Add your new module to your supervision tree,
in lib/#{otp_app}/application.ex:
children = [
...
#{binding[:module]}
]
You're all set! See the Phoenix.Presence docs for more details:
https://hexdocs.pm/phoenix/Phoenix.Presence.html
"""
end
defp paths do
[".", :phoenix]
end
end