@@ -113,6 +113,28 @@ defmodule Mix.Tasks.Phx.New do
113
113
114
114
You can read more about umbrella projects using the
115
115
official [Elixir guide](https://hexdocs.pm/elixir/dependencies-and-umbrella-projects.html#umbrella-projects)
116
+
117
+ ## PHX_NEW_CACHE_DIR
118
+ In rare cases, it may be useful to copy the build from a previously
119
+ cached build. To do this, set the `PHX_NEW_CACHE_DIR` environment
120
+ variable before running `mix phx.new`. For example, you could generate a
121
+ cache by running:
122
+
123
+ ```shell
124
+ mix phx.new mycache --no-install && cd mycache \
125
+ && mix deps.get && mix deps.compile && mix assets.setup \
126
+ && rm -rf assets config lib priv test mix.exs README.md
127
+ ```
128
+
129
+ Your cached build directory
130
+ should contain:
131
+
132
+ _build
133
+ deps
134
+ mix.lock
135
+
136
+ The entire cache directory will be copied to the new project, replacing
137
+ any existing files where conflicts exist.
116
138
"""
117
139
use Mix.Task
118
140
alias Phx.New . { Generator , Project , Single , Umbrella , Web , Ecto }
@@ -185,7 +207,8 @@ defmodule Mix.Tasks.Phx.New do
185
207
|> Generator . put_binding ( )
186
208
|> validate_project ( path )
187
209
|> generator . generate ( )
188
- |> prompt_to_install_deps ( generator , path )
210
+ |> maybe_copy_cached_build ( path )
211
+ |> maybe_prompt_to_install_deps ( generator , path )
189
212
end
190
213
191
214
defp validate_project ( % Project { opts: opts } = project , path ) do
@@ -197,6 +220,15 @@ defmodule Mix.Tasks.Phx.New do
197
220
project
198
221
end
199
222
223
+ defp maybe_prompt_to_install_deps ( % Project { } = project , generator , path_key ) do
224
+ # we can skip the install deps setup, even with --install, because we already copied deps
225
+ if project . cached_build_path do
226
+ project
227
+ else
228
+ prompt_to_install_deps ( project , generator , path_key )
229
+ end
230
+ end
231
+
200
232
defp prompt_to_install_deps ( % Project { } = project , generator , path_key ) do
201
233
path = Map . fetch! ( project , path_key )
202
234
@@ -404,4 +436,24 @@ defmodule Mix.Tasks.Phx.New do
404
436
)
405
437
end
406
438
end
439
+
440
+ defp maybe_copy_cached_build ( % Project { } = project , path_key ) do
441
+ project_path = Map . fetch! ( project , path_key )
442
+
443
+ case System . fetch_env ( "PHX_NEW_CACHE_DIR" ) do
444
+ { :ok , cache_dir } ->
445
+ copy_cached_build ( % { project_path: project_path , cache_dir: cache_dir } )
446
+ % Project { project | cached_build_path: cache_dir }
447
+
448
+ :error ->
449
+ project
450
+ end
451
+ end
452
+
453
+ defp copy_cached_build ( % { project_path: project_path , cache_dir: cache_dir } ) do
454
+ if File . exists? ( cache_dir ) do
455
+ Mix . shell ( ) . info ( "Copying cached build from #{ cache_dir } " )
456
+ System . cmd ( "cp" , [ "-Rp" , Path . join ( cache_dir , "." ) , project_path ] )
457
+ end
458
+ end
407
459
end
0 commit comments