-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ecto.gen.repo.ex
75 lines (55 loc) · 1.74 KB
/
ecto.gen.repo.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
defmodule Mix.Tasks.Ecto.Gen.Repo do
use Mix.Task
import Mix.Ecto
import Mix.Generator
@shortdoc "Generates a new repository"
@moduledoc """
Generates a new repository.
The repository will be placed in the `lib` directory.
## Examples
mix ecto.gen.repo
mix ecto.gen.repo -r Custom.Repo
## Command line options
* `-r`, `--repo` - the repo to generate (defaults to `YourApp.Repo`)
"""
@doc false
def run(args) do
no_umbrella!("ecto.gen.repo")
repo = parse_repo(args)
config = Mix.Project.config
underscored = Mix.Utils.underscore(inspect(repo))
base = Path.basename(underscored)
file = Path.join("lib", underscored) <> ".ex"
app = config[:app] || :YOUR_APP_NAME
opts = [mod: repo, app: app, base: base]
create_directory Path.dirname(file)
create_file file, repo_template(opts)
case File.read "config/config.exs" do
{:ok, contents} ->
Mix.shell.info [:green, "* updating ", :reset, "config/config.exs"]
File.write! "config/config.exs", contents <> config_template(opts)
{:error, _} ->
create_file "config/config.exs", "use Mix.Config\n" <> config_template(opts)
end
open?("config/config.exs")
Mix.shell.info """
Don't forget to add your new repo to your supervision tree
(typically in lib/#{app}.ex):
worker(#{inspect repo}, [])
"""
end
embed_template :repo, """
defmodule <%= inspect @mod %> do
use Ecto.Repo,
adapter: Ecto.Adapters.Postgres,
otp_app: <%= inspect @app %>
end
"""
embed_template :config, """
config <%= inspect @app %>, <%= inspect @mod %>,
database: "<%= @app %>_<%= @base %>",
username: "user",
password: "pass",
hostname: "localhost"
"""
end