-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
new.ex
446 lines (360 loc) · 11.8 KB
/
new.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
defmodule Mix.Tasks.New do
use Mix.Task
import Mix.Generator
import Mix.Utils, only: [camelize: 1]
@shortdoc "Creates a new Elixir project"
@moduledoc """
Creates a new Elixir project.
It expects the path of the project as argument.
mix new PATH [--sup] [--module MODULE] [--app APP] [--umbrella]
A project at the given PATH will be created. The
application name and module name will be retrieved
from the path, unless `--module` or `--app` is given.
A `--sup` option can be given to generate an OTP application
skeleton including a supervision tree. Normally an app is
generated without a supervisor and without the app callback.
An `--umbrella` option can be given to generate an
umbrella project.
An `--app` option can be given in order to
name the OTP application for the project.
A `--module` option can be given in order
to name the modules in the generated code skeleton.
## Examples
mix new hello_world
Is equivalent to:
mix new hello_world --module HelloWorld
To generate an app with supervisor and application callback:
mix new hello_world --sup
"""
@switches [sup: :boolean, umbrella: :boolean, app: :string, module: :string]
@spec run(OptionParser.argv) :: :ok
def run(argv) do
{opts, argv} = OptionParser.parse!(argv, strict: @switches)
case argv do
[] ->
Mix.raise "Expected PATH to be given, please use \"mix new PATH\""
[path | _] ->
app = opts[:app] || Path.basename(Path.expand(path))
check_application_name!(app, !!opts[:app])
mod = opts[:module] || camelize(app)
check_mod_name_validity!(mod)
check_mod_name_availability!(mod)
File.mkdir_p!(path)
File.cd! path, fn ->
if opts[:umbrella] do
do_generate_umbrella(app, mod, path, opts)
else
do_generate(app, mod, path, opts)
end
end
end
end
defp do_generate(app, mod, path, opts) do
assigns = [app: app, mod: mod, otp_app: otp_app(mod, !!opts[:sup]),
version: get_version(System.version)]
create_file "README.md", readme_template(assigns)
create_file ".gitignore", gitignore_text
if in_umbrella? do
create_file "mix.exs", mixfile_apps_template(assigns)
else
create_file "mix.exs", mixfile_template(assigns)
end
create_directory "config"
create_file "config/config.exs", config_template(assigns)
create_directory "lib"
if opts[:sup] do
create_file "lib/#{app}.ex", lib_sup_template(assigns)
else
create_file "lib/#{app}.ex", lib_template(assigns)
end
create_directory "test"
create_file "test/test_helper.exs", test_helper_template(assigns)
create_file "test/#{app}_test.exs", test_template(assigns)
Mix.shell.info """
Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:
cd #{path}
mix test
Run "mix help" for more commands.
"""
end
defp otp_app(_mod, false) do
" [applications: [:logger]]"
end
defp otp_app(mod, true) do
" [applications: [:logger],\n mod: {#{mod}, []}]"
end
defp do_generate_umbrella(_app, mod, path, _opts) do
assigns = [app: nil, mod: mod]
create_file ".gitignore", gitignore_text
create_file "README.md", readme_template(assigns)
create_file "mix.exs", mixfile_umbrella_template(assigns)
create_directory "apps"
create_directory "config"
create_file "config/config.exs", config_umbrella_template(assigns)
Mix.shell.info """
Your umbrella project was created successfully.
Inside your project, you will find an apps/ directory
where you can create and host many apps:
cd #{path}
cd apps
mix new my_app
Commands like "mix compile" and "mix test" when executed
in the umbrella project root will automatically run
for each application in the apps/ directory.
"""
end
defp check_application_name!(name, from_app_flag) do
unless name =~ ~r/^[a-z][\w_]*$/ do
Mix.raise "Application name must start with a letter and have only lowercase " <>
"letters, numbers and underscore, got: #{inspect name}" <>
(if !from_app_flag do
". The application name is inferred from the path, if you'd like to " <>
"explicitly name the application then use the \"--app APP\" option."
else
""
end)
end
end
defp check_mod_name_validity!(name) do
unless name =~ ~r/^[A-Z]\w*(\.[A-Z]\w*)*$/ do
Mix.raise "Module name must be a valid Elixir alias (for example: Foo.Bar), got: #{inspect name}"
end
end
defp check_mod_name_availability!(name) do
name = Module.concat(Elixir, name)
if Code.ensure_loaded?(name) do
Mix.raise "Module name #{inspect name} is already taken, please choose another name"
end
end
defp get_version(version) do
{:ok, version} = Version.parse(version)
"#{version.major}.#{version.minor}" <>
case version.pre do
[h | _] -> "-#{h}"
[] -> ""
end
end
defp in_umbrella? do
apps = Path.dirname(File.cwd!)
try do
Mix.Project.in_project(:umbrella_check, "../..", fn _ ->
path = Mix.Project.config[:apps_path]
path && Path.expand(path) == apps
end)
catch
_, _ -> false
end
end
embed_template :readme, """
# <%= @mod %>
**TODO: Add description**
<%= if @app do %>
## Installation
If [available in Hex](https://hex.pm/docs/publish), the package can be installed as:
1. Add `<%= @app %>` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[{:<%= @app %>, "~> 0.0.1"}]
end
```
2. Ensure `<%= @app %>` is started before your application:
```elixir
def application do
[applications: [:<%= @app %>]]
end
```
<% end %>
"""
embed_text :gitignore, """
# The directory Mix will write compiled artifacts to.
/_build
# If you run "mix test --cover", coverage assets end up here.
/cover
# The directory Mix downloads your dependencies sources to.
/deps
# Where 3rd-party dependencies like ExDoc output generated docs.
/doc
# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump
# Also ignore archive artifacts (built via "mix archive.build").
*.ez
"""
embed_template :mixfile, """
defmodule <%= @mod %>.Mixfile do
use Mix.Project
def project do
[app: :<%= @app %>,
version: "0.0.1",
elixir: "~> <%= @version %>",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps]
end
# Configuration for the OTP application
#
# Type "mix help compile.app" for more information
def application do
<%= @otp_app %>
end
# Dependencies can be Hex packages:
#
# {:mydep, "~> 0.3.0"}
#
# Or git/path repositories:
#
# {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
#
# Type "mix help deps" for more examples and options
defp deps do
[]
end
end
"""
embed_template :mixfile_apps, """
defmodule <%= @mod %>.Mixfile do
use Mix.Project
def project do
[app: :<%= @app %>,
version: "0.0.1",
build_path: "../../_build",
config_path: "../../config/config.exs",
deps_path: "../../deps",
lockfile: "../../mix.lock",
elixir: "~> <%= @version %>",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps]
end
# Configuration for the OTP application
#
# Type "mix help compile.app" for more information
def application do
<%= @otp_app %>
end
# Dependencies can be Hex packages:
#
# {:mydep, "~> 0.3.0"}
#
# Or git/path repositories:
#
# {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
#
# To depend on another app inside the umbrella:
#
# {:myapp, in_umbrella: true}
#
# Type "mix help deps" for more examples and options
defp deps do
[]
end
end
"""
embed_template :mixfile_umbrella, """
defmodule <%= @mod %>.Mixfile do
use Mix.Project
def project do
[apps_path: "apps",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps]
end
# Dependencies can be Hex packages:
#
# {:mydep, "~> 0.3.0"}
#
# Or git/path repositories:
#
# {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
#
# Type "mix help deps" for more examples and options.
#
# Dependencies listed here are available only for this project
# and cannot be accessed from applications inside the apps folder
defp deps do
[]
end
end
"""
embed_template :config, ~S"""
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config
# This configuration is loaded before any dependency and is restricted
# to this project. If another project depends on this project, this
# file won't be loaded nor affect the parent project. For this reason,
# if you want to provide default values for your application for
# 3rd-party users, it should be done in your "mix.exs" file.
# You can configure for your application as:
#
# config :<%= @app %>, key: :value
#
# And access this configuration in your application as:
#
# Application.get_env(:<%= @app %>, :key)
#
# Or configure a 3rd-party app:
#
# config :logger, level: :info
#
# It is also possible to import configuration files, relative to this
# directory. For example, you can emulate configuration per environment
# by uncommenting the line below and defining dev.exs, test.exs and such.
# Configuration from the imported file will override the ones defined
# here (which is why it is important to import them last).
#
# import_config "#{Mix.env}.exs"
"""
embed_template :config_umbrella, ~S"""
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config
# By default, the umbrella project as well as each child
# application will require this configuration file, ensuring
# they all use the same configuration. While one could
# configure all applications here, we prefer to delegate
# back to each application for organization purposes.
import_config "../apps/*/config/config.exs"
# Sample configuration (overrides the imported configuration above):
#
# config :logger, :console,
# level: :info,
# format: "$date $time [$level] $metadata$message\n",
# metadata: [:user_id]
"""
embed_template :lib, """
defmodule <%= @mod %> do
end
"""
embed_template :lib_sup, """
defmodule <%= @mod %> do
use Application
# See http://elixir-lang.org/docs/stable/elixir/Application.html
# for more information on OTP Applications
def start(_type, _args) do
import Supervisor.Spec, warn: false
# Define workers and child supervisors to be supervised
children = [
# Starts a worker by calling: <%= @mod %>.Worker.start_link(arg1, arg2, arg3)
# worker(<%= @mod %>.Worker, [arg1, arg2, arg3]),
]
# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: <%= @mod %>.Supervisor]
Supervisor.start_link(children, opts)
end
end
"""
embed_template :test, """
defmodule <%= @mod %>Test do
use ExUnit.Case
doctest <%= @mod %>
test "the truth" do
assert 1 + 1 == 2
end
end
"""
embed_template :test_helper, """
ExUnit.start()
"""
end