-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a156431
commit f1e2336
Showing
4 changed files
with
60 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Writing Generators | ||
|
||
In `Igniter`, generators are done as a wrapper around `Mix.Task`, allowing them to be called individually or composed as part of a task. Since an example is worth a thousand words, lets take a look at an example that generates a file and ensures a configuration is set in the user's `config.exs`. | ||
|
||
```elixir | ||
# lib/mix/tasks/your_lib.gen.your_thing.ex | ||
defmodule Mix.Tasks.YourLib.Gen.YourThing do | ||
use Igniter.Mix.Task | ||
|
||
def igniter(igniter, [module_name | _ ] = argv) do | ||
module_name = Igniter.Module.parse(module_name) | ||
path = Igniter.Module.proper_location(module_name) | ||
app_name = Igniter.Application.app_name() | ||
|
||
igniter | ||
|> Igniter.create_new_elixir_file(igniter, path, """ | ||
defmodule #{inspect(module_name)} do | ||
use YourLib.Thing | ||
...some_code | ||
end | ||
""") | ||
|> Igniter.Config.configure( | ||
"config.exs", | ||
app_name, | ||
[:list_of_things], | ||
[module_name], | ||
&Igniter.Code.List.prepend_new_to_list(&1, module_name) | ||
) | ||
end | ||
end | ||
``` | ||
|
||
Now, your users can run | ||
|
||
`mix your_lib.gen.your_thing MyApp.MyModuleName` | ||
|
||
and it will present them with a diff, creating a new file and updating their `config.exs`. | ||
|
||
Additionally, other generators can "compose" this generator using `Igniter.compose_task/3` | ||
|
||
```elixir | ||
igniter | ||
|> Igniter.compose_task(Mix.Tasks.YourLib.Gen.YourThing, ["MyApp.MyModuleName"]) | ||
|> Igniter.compose_task(Mix.Tasks.YourLib.Gen.YourThing, ["MyApp.SomeOtherName"]) | ||
``` | ||
|
||
## Writing a library installer | ||
|
||
Igniter will look for a mix task called `your_library.install` when a user runs `mix igniter.install your_library`. As long as it has the correct name, it will be run automatically as part of installation! |
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
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