Skip to content

Commit

Permalink
improvement: draw the rest of the owl
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Jun 12, 2024
1 parent 2a43fc4 commit 55d0b06
Show file tree
Hide file tree
Showing 23 changed files with 1,776 additions and 1,274 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
erlang 26.0.2
elixir 1.16.2
elixir 1.17.0
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
![Logo](https://github.com/ash-project/igniter/blob/main/logos/igniter-logo.png?raw=true#gh-light-mode-only)
![Logo](https://github.com/ash-project/igniter/blob/main/logos/igniter-logo.png?raw=true#gh-dark-mode-only)
![Logo](https://github.com/ash-project/igniter/blob/main/logos/igniter-logo-small.png?raw=true#gh-light-mode-only)
![Logo](https://github.com/ash-project/igniter/blob/main/logos/igniter-logo-small.png?raw=true#gh-dark-mode-only)

![Elixir CI](https://github.com/ash-project/igniter/workflows/Ash%20CI/badge.svg)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
Expand Down Expand Up @@ -44,9 +44,9 @@ defmodule Mix.Tasks.MyApp.Gen.Resource do
use Igniter.Mix.Task

def igniter(igniter, [resource | _] = argv) do
resource = Igniter.Module.parse(resource)
resource = Igniter.Code.Module.parse(resource)
my_special_thing = Module.concat([resource, SpecialThing])
location = Igniter.Module.proper_location(my_special_thing)
location = Igniter.Code.Module.proper_location(my_special_thing)

igniter
|> Igniter.compose_task("ash.gen.resource", argv)
Expand Down
47 changes: 28 additions & 19 deletions lib/application.ex
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
defmodule Igniter.Application do
@moduledoc "Codemods and tools for working with Application modules."

require Igniter.Common
require Igniter.Code.Common
require Igniter.Code.Function

alias Igniter.Common
alias Igniter.Code.Common
alias Sourceror.Zipper

@doc "Returns the name of the current application."
@spec app_name() :: atom()
def app_name do
Mix.Project.config()[:app]
end

def add_child(igniter, to_supervise) do
@doc "Adds a new child to the `children` list in the application file"
@spec add_new_child(Igniter.t(), module() | {module, term()}) :: Igniter.t()
def add_new_child(igniter, to_supervise) do
project = Mix.Project.get!()

# TODO: Would be better to check the source and parse the app module out
# as something else may have set an app module
to_perform =
case project.application()[:mod] do
nil -> {:create_an_app, Igniter.Module.module_name("Application")}
nil -> {:create_an_app, Igniter.Code.Module.module_name("Application")}
{mod, _} -> {:modify, mod}
mod -> {:modify, mod}
end
Expand All @@ -40,40 +45,44 @@ defmodule Igniter.Application do
end

def do_add_child(igniter, application, to_supervise) do
path = Igniter.Module.proper_location(application)
path = Igniter.Code.Module.proper_location(application)

diff_checker =
case to_supervise do
v when is_atom(v) ->
&Common.equal_modules?/2
&Common.nodes_equal?/2

{v, _opts} when is_atom(v) ->
fn
{item, _}, {right, _} ->
Common.equal_modules?(item, right)
Common.nodes_equal?(item, right)

item, {right, _} ->
Common.equal_modules?(item, right)
Common.nodes_equal?(item, right)

_, _ ->
false
end
end

Igniter.update_elixir_file(igniter, path, fn zipper ->
with {:ok, zipper} <- Common.move_to_module_using(zipper, Application),
{:ok, zipper} <- Common.move_to_def(zipper, :start, 2) do
with {:ok, zipper} <- Igniter.Code.Module.move_to_module_using(zipper, Application),
{:ok, zipper} <- Igniter.Code.Module.move_to_def(zipper, :start, 2) do
zipper
|> Common.move_to_function_call_in_current_scope(:=, [2], fn call ->
Common.argument_matches_pattern?(call, 0, {:children, _, context} when is_atom(context)) &&
Common.argument_matches_pattern?(call, 1, v when is_list(v))
|> Igniter.Code.Function.move_to_function_call_in_current_scope(:=, [2], fn call ->
Igniter.Code.Function.argument_matches_pattern?(
call,
0,
{:children, _, context} when is_atom(context)
) &&
Igniter.Code.Function.argument_matches_pattern?(call, 1, v when is_list(v))
end)
|> case do
{:ok, zipper} ->
zipper
|> Zipper.down()
|> Zipper.rightmost()
|> Igniter.Common.append_new_to_list(to_supervise, diff_checker)
|> Igniter.Code.List.append_new_to_list(to_supervise, diff_checker)

_ ->
{:error,
Expand All @@ -87,7 +96,7 @@ defmodule Igniter.Application do
end

defp create_application_file(igniter, application) do
path = Igniter.Module.proper_location(application)
path = Igniter.Code.Module.proper_location(application)

contents = """
defmodule #{inspect(application)} do
Expand All @@ -110,14 +119,14 @@ defmodule Igniter.Application do

defp point_to_application_in_mix_exs(igniter, application) do
Igniter.update_elixir_file(igniter, "mix.exs", fn zipper ->
case Common.move_to_module_using(zipper, Mix.Project) do
case Igniter.Code.Module.move_to_module_using(zipper, Mix.Project) do
{:ok, zipper} ->
case Common.move_to_def(zipper, :application, 0) do
case Igniter.Code.Module.move_to_def(zipper, :application, 0) do
{:ok, zipper} ->
zipper
|> Zipper.rightmost()
|> Common.set_keyword_key(:mod, {application, []}, fn z ->
Zipper.replace(z, {application, []})
|> Igniter.Code.Keyword.set_keyword_key(:mod, {application, []}, fn z ->
{:ok, Zipper.replace(z, {application, []})}
end)

_ ->
Expand Down
38 changes: 0 additions & 38 deletions lib/args.ex

This file was deleted.

Loading

0 comments on commit 55d0b06

Please sign in to comment.