-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for generating services (#4)
* (feat): add support for Create services * (refactor): put common functions for generators in their own module * (feat): add update & delete services. Bump version. Update docs.
- Loading branch information
Showing
8 changed files
with
168 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
defmodule Mix.Tasks.Phx.Gen.Solid.Service do | ||
@shortdoc "Generates C~~R~~UD services for a resource" | ||
|
||
@moduledoc """ | ||
Generates C~~R~~UD Services for a resource. | ||
mix phx.gen.solid.value Accounts User users | ||
The first argument is the context module followed by the schema module and its | ||
plural name. | ||
This creates the following services: | ||
- `MyApp.Accounts.Service.CreateUser` | ||
- `MyApp.Accounts.Service.UpdateUser` | ||
- `MyApp.Accounts.Service.DeleteUser` | ||
For more information about the generated Services, see the [Overview](overview.html). | ||
""" | ||
|
||
use Mix.Task | ||
|
||
alias Mix.Phoenix.Context | ||
alias Mix.Tasks.Phx.Gen | ||
alias PhxGenSolid.Generator | ||
|
||
@switches [] | ||
|
||
@impl true | ||
def run(args) do | ||
if Mix.Project.umbrella?() do | ||
Mix.raise("mix phx.gen.solid can only be run inside an application directory") | ||
end | ||
|
||
{opts, parsed} = OptionParser.parse!(args, strict: @switches) | ||
|
||
# Don't pass along the opts | ||
{context, schema} = Gen.Context.build(parsed, __MODULE__) | ||
|
||
binding = [ | ||
context: context, | ||
opts: opts, | ||
schema: schema, | ||
web_app_name: Generator.web_app_name(context), | ||
service_create_module: build_cud_module_name(context, schema, "Create"), | ||
service_update_module: build_cud_module_name(context, schema, "Update"), | ||
service_delete_module: build_cud_module_name(context, schema, "Delete") | ||
] | ||
|
||
paths = Generator.paths() | ||
Generator.prompt_for_conflicts(context, &files_to_be_generated/1) | ||
Generator.copy_new_files(context, binding, paths, &files_to_be_generated/1) | ||
end | ||
|
||
defp files_to_be_generated(%Context{schema: schema} = context) do | ||
[ | ||
{:eex, "service_create.ex", | ||
Path.join([context.dir, "services", "create_#{schema.singular}.ex"])}, | ||
{:eex, "service_update.ex", | ||
Path.join([context.dir, "services", "update_#{schema.singular}.ex"])}, | ||
{:eex, "service_delete.ex", | ||
Path.join([context.dir, "services", "delete_#{schema.singular}.ex"])} | ||
] | ||
end | ||
|
||
defp build_cud_module_name(context, schema, action) do | ||
Module.concat([ | ||
context.base_module, | ||
"#{inspect(context.alias)}", | ||
"Service", | ||
"#{action}#{inspect(schema.alias)}" | ||
]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
defmodule PhxGenSolid.Generator do | ||
alias Mix.Phoenix.Context | ||
|
||
# The paths to look for template files for generators. | ||
# | ||
# Defaults to checking the current app's `priv` directory and falls back to | ||
# phx_gen_solid's `priv` directory. | ||
def paths do | ||
[".", :phx_gen_solid, :phoenix] | ||
end | ||
|
||
def web_app_name(%Context{} = context) do | ||
context.web_module | ||
|> inspect() | ||
|> Phoenix.Naming.underscore() | ||
end | ||
|
||
def copy_new_files(%Context{} = context, binding, paths, files_fn) do | ||
files = files_fn.(context) | ||
Mix.Phoenix.copy_from(paths, "priv/templates/phx.gen.solid", binding, files) | ||
|
||
context | ||
end | ||
|
||
def copy_new_files(%Context{} = context, binding, paths, files_fn, opts) do | ||
files = files_fn.(context, opts) | ||
Mix.Phoenix.copy_from(paths, "priv/templates/phx.gen.solid", binding, files) | ||
|
||
context | ||
end | ||
|
||
def prompt_for_conflicts(context, files_fn) do | ||
context | ||
|> files_fn.() | ||
|> Mix.Phoenix.prompt_for_conflicts() | ||
end | ||
|
||
def prompt_for_conflicts(context, files_fn, opts) do | ||
context | ||
|> files_fn.(opts) | ||
|> Mix.Phoenix.prompt_for_conflicts() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
defmodule PhxGenSolid.MixProject do | ||
use Mix.Project | ||
|
||
@version "0.1.0" | ||
@version "0.2.0" | ||
|
||
def project do | ||
[ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule <%= inspect service_create_module %> do | ||
@moduledoc """ | ||
Creates a <%= schema.singular %>. | ||
""" | ||
|
||
alias <%= inspect context.module %> | ||
|
||
def call(params) do | ||
params | ||
|> <%= inspect context.alias %>.create_<%= schema.singular %>(params) | ||
|> handle_result() | ||
end | ||
|
||
defp handle_result(result), do: result | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule <%= inspect service_delete_module %> do | ||
@moduledoc """ | ||
Deletes a <%= schema.singular %>. | ||
""" | ||
|
||
alias <%= inspect context.module %> | ||
|
||
def call(params) do | ||
params | ||
|> <%= inspect context.alias %>.delete_<%= schema.singular %>(params) | ||
|> handle_result() | ||
end | ||
|
||
defp handle_result(result), do: result | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule <%= inspect service_update_module %> do | ||
@moduledoc """ | ||
Updates a <%= schema.singular %>. | ||
""" | ||
|
||
alias <%= inspect context.module %> | ||
|
||
def call(params) do | ||
params | ||
|> <%= inspect context.alias %>.update_<%= schema.singular %>(params) | ||
|> handle_result() | ||
end | ||
|
||
defp handle_result(result), do: result | ||
end |