-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
phx.gen.live.ex
266 lines (204 loc) · 9.3 KB
/
phx.gen.live.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
defmodule Mix.Tasks.Phx.Gen.Live do
@shortdoc "Generates LiveView, templates, and context for a resource"
@moduledoc """
Generates LiveView, templates, and context for a resource.
mix phx.gen.live Accounts User users name:string age:integer
The first argument is the context module. The context is an Elixir module
that serves as an API boundary for the given resource. A context often holds
many related resources. Therefore, if the context already exists, it will be
augmented with functions for the given resource.
The second argument is the schema module. The schema is responsible for
mapping the database fields into an Elixir struct.
The remaining arguments are the schema module plural name (used as the schema
table name), and an optional list of attributes as their respective names and
types. See `mix help phx.gen.schema` for more information on attributes.
When this command is run for the first time, a `Components` module will be
created if it does not exist, along with the resource level LiveViews and
components, including `UserLive.Index`, `UserLive.Show`, and
`UserLive.FormComponent` modules for the new resource.
> Note: A resource may also be split
> over distinct contexts (such as `Accounts.User` and `Payments.User`).
Overall, this generator will add the following files:
* a context module in `lib/app/accounts.ex` for the accounts API
* a schema in `lib/app/accounts/user.ex`, with a `users` table
* a LiveView in `lib/app_web/live/user_live/show.ex`
* a LiveView in `lib/app_web/live/user_live/index.ex`
* a LiveComponent in `lib/app_web/live/user_live/form_component.ex`
* a helpers module in `lib/app_web/live/live_helpers.ex` with a modal
After file generation is complete, there will be output regarding required
updates to the lib/app_web/router.ex file.
Add the live routes to your browser scope in lib/app_web/router.ex:
live "/users", UserLive.Index, :index
live "/users/new", UserLive.Index, :new
live "/users/:id/edit", UserLive.Index, :edit
live "/users/:id", UserLive.Show, :show
live "/users/:id/show/edit", UserLive.Show, :edit
## The context app
A migration file for the repository and test files for the context and
controller features will also be generated.
The location of the web files (LiveView's, views, templates, etc.) in an
umbrella application will vary based on the `:context_app` config located
in your applications `:generators` configuration. When set, the Phoenix
generators will generate web files directly in your lib and test folders
since the application is assumed to be isolated to web specific functionality.
If `:context_app` is not set, the generators will place web related lib
and test files in a `web/` directory since the application is assumed
to be handling both web and domain specific functionality.
Example configuration:
config :my_app_web, :generators, context_app: :my_app
Alternatively, the `--context-app` option may be supplied to the generator:
mix phx.gen.live Accounts User users --context-app warehouse
## Web namespace
By default, the LiveView modules will be namespaced by the web module.
You can customize the web module namespace by passing the `--web` flag with a
module name, for example:
mix phx.gen.live Accounts User users --web Sales
Which would generate the LiveViews in `lib/app_web/live/sales/user_live/`,
namespaced `AppWeb.Sales.UserLive` instead of `AppWeb.UserLive`.
## Customizing the context, schema, tables and migrations
In some cases, you may wish to bootstrap HTML templates, LiveViews,
and tests, but leave internal implementation of the context or schema
to yourself. You can use the `--no-context` and `--no-schema` flags
for file generation control.
You can also change the table name or configure the migrations to
use binary ids for primary keys, see `mix help phx.gen.schema` for more
information.
"""
use Mix.Task
alias Mix.Phoenix.{Context}
alias Mix.Tasks.Phx.Gen
@doc false
def run(args) do
if Mix.Project.umbrella?() do
Mix.raise(
"mix phx.gen.live must be invoked from within your *_web application root directory"
)
end
{context, schema} = Gen.Context.build(args)
Gen.Context.prompt_for_code_injection(context)
binding = [context: context, schema: schema, inputs: Gen.Html.inputs(schema)]
paths = Mix.Phoenix.generator_paths()
prompt_for_conflicts(context)
context
|> copy_new_files(binding, paths)
|> maybe_inject_imports()
|> print_shell_instructions()
end
defp prompt_for_conflicts(context) do
context
|> files_to_be_generated()
|> Kernel.++(context_files(context))
|> Mix.Phoenix.prompt_for_conflicts()
end
defp context_files(%Context{generate?: true} = context) do
Gen.Context.files_to_be_generated(context)
end
defp context_files(%Context{generate?: false}) do
[]
end
defp files_to_be_generated(%Context{schema: schema, context_app: context_app}) do
web_prefix = Mix.Phoenix.web_path(context_app)
test_prefix = Mix.Phoenix.web_test_path(context_app)
web_path = to_string(schema.web_path)
live_subdir = "#{schema.singular}_live"
web_live = Path.join([web_prefix, "live", web_path, live_subdir])
test_live = Path.join([test_prefix, "live", web_path])
[
{:eex, "show.ex", Path.join(web_live, "show.ex")},
{:eex, "index.ex", Path.join(web_live, "index.ex")},
{:eex, "form_component.ex", Path.join(web_live, "form_component.ex")},
{:eex, "index.html.heex", Path.join(web_live, "index.html.heex")},
{:eex, "show.html.heex", Path.join(web_live, "show.html.heex")},
{:eex, "live_test.exs", Path.join(test_live, "#{schema.singular}_live_test.exs")},
{:new_eex, "core_components.ex", Path.join([web_prefix, "components", "core_components.ex"])}
]
end
defp copy_new_files(%Context{} = context, binding, paths) do
files = files_to_be_generated(context)
binding =
Keyword.merge(binding,
assigns: %{
web_namespace: inspect(context.web_module),
gettext: true
}
)
Mix.Phoenix.copy_from(paths, "priv/templates/phx.gen.live", binding, files)
if context.generate?, do: Gen.Context.copy_new_files(context, paths, binding)
context
end
defp maybe_inject_imports(%Context{context_app: ctx_app} = context) do
web_prefix = Mix.Phoenix.web_path(ctx_app)
[lib_prefix, web_dir] = Path.split(web_prefix)
file_path = Path.join(lib_prefix, "#{web_dir}.ex")
file = File.read!(file_path)
inject = "import #{inspect(context.web_module)}.CoreComponents"
if String.contains?(file, inject) do
:ok
else
do_inject_imports(context, file, file_path, inject)
end
context
end
defp do_inject_imports(context, file, file_path, inject) do
Mix.shell().info([:green, "* injecting ", :reset, Path.relative_to_cwd(file_path)])
new_file =
String.replace(
file,
"use Phoenix.Component",
"use Phoenix.Component\n #{inject}"
)
if file != new_file do
File.write!(file_path, new_file)
else
Mix.shell().info("""
Could not find use Phoenix.Component in #{file_path}.
This typically happens because your application was not generated
with the --live flag:
mix phx.new my_app --live
Please make sure LiveView is installed and that #{inspect(context.web_module)}
defines both `live_view/0` and `live_component/0` functions,
and that both functions import #{inspect(context.web_module)}.CoreComponents.
""")
end
end
@doc false
def print_shell_instructions(%Context{schema: schema, context_app: ctx_app} = context) do
prefix = Module.concat(context.web_module, schema.web_namespace)
web_path = Mix.Phoenix.web_path(ctx_app)
if schema.web_namespace do
Mix.shell().info("""
Add the live routes to your #{schema.web_namespace} :browser scope in #{web_path}/router.ex:
scope "/#{schema.web_path}", #{inspect(prefix)}, as: :#{schema.web_path} do
pipe_through :browser
...
#{for line <- live_route_instructions(schema), do: " #{line}"}
end
""")
else
Mix.shell().info("""
Add the live routes to your browser scope in #{Mix.Phoenix.web_path(ctx_app)}/router.ex:
#{for line <- live_route_instructions(schema), do: " #{line}"}
""")
end
if context.generate?, do: Gen.Context.print_shell_instructions(context)
maybe_print_upgrade_info()
end
defp maybe_print_upgrade_info do
unless Code.ensure_loaded?(Phoenix.LiveView.JS) do
Mix.shell().info("""
You must update :phoenix_live_view to v0.18 or later and
:phoenix_live_dashboard to v0.7 or later to use the features
in this generator.
""")
end
end
defp live_route_instructions(schema) do
[
~s|live "/#{schema.plural}", #{inspect(schema.alias)}Live.Index, :index\n|,
~s|live "/#{schema.plural}/new", #{inspect(schema.alias)}Live.Index, :new\n|,
~s|live "/#{schema.plural}/:id/edit", #{inspect(schema.alias)}Live.Index, :edit\n\n|,
~s|live "/#{schema.plural}/:id", #{inspect(schema.alias)}Live.Show, :show\n|,
~s|live "/#{schema.plural}/:id/show/edit", #{inspect(schema.alias)}Live.Show, :edit|
]
end
end