Skip to content

Commit

Permalink
Fix generators when component/system contains "end" (#75)
Browse files Browse the repository at this point in the history
* Fix generator for components with end in the name

* Same fix but for systems

* Simplify regex

* Cover possible edge case for components
  • Loading branch information
APB9785 authored Jan 24, 2024
1 parent d227472 commit 51336b2
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/mix/tasks/ecsx.gen.system.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule Mix.Tasks.Ecsx.Gen.System do
|> ensure_list_format()

new_contents =
[before_systems, "def systems do\n ", new_list, "\n end", after_systems]
[before_systems, "def systems do\n ", new_list, "\n end\n", after_systems]
|> IO.iodata_to_binary()
|> Code.format_string!()

Expand All @@ -62,7 +62,7 @@ defmodule Mix.Tasks.Ecsx.Gen.System do
defp parse_manager(path) do
file = Helpers.read_manager_file!(path)
[top, rest] = String.split(file, "def systems do", parts: 2)
[list, bottom] = String.split(rest, "end", parts: 2)
[list, bottom] = String.split(rest, ~r"\send\n", parts: 2)

{top, bottom, list}
end
Expand Down
4 changes: 2 additions & 2 deletions lib/mix/tasks/ecsx/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ defmodule Mix.Tasks.ECSx.Helpers do
|> ensure_list_format()

new_contents =
[before_components, "def components do\n ", new_list, "\n end", after_components]
[before_components, "def components do\n ", new_list, "\n end\n", after_components]
|> IO.iodata_to_binary()
|> Code.format_string!()

Expand All @@ -47,7 +47,7 @@ defmodule Mix.Tasks.ECSx.Helpers do
defp parse_manager_components(path) do
file = read_manager_file!(path)
[top, rest] = String.split(file, "def components do", parts: 2)
[list, bottom] = String.split(rest, "end", parts: 2)
[list, bottom] = String.split(rest, ~r"\send\n", parts: 2)

{top, bottom, list}
end
Expand Down
46 changes: 46 additions & 0 deletions test/mix/tasks/ecsx.gen.component_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,50 @@ defmodule Mix.Tasks.Ecsx.Gen.ComponentTest do
end)
end)
end

test "handles component types with 'end' in the name" do
Mix.Project.in_project(:my_app, ".", fn _module ->
Mix.Tasks.Ecsx.Gen.Component.run(["LegendaryComponent", "binary"])
Mix.Tasks.Ecsx.Gen.Component.run(["AnotherComponent", "integer"])

manager_file = File.read!("lib/my_app/manager.ex")

assert manager_file ==
"""
defmodule MyApp.Manager do
@moduledoc \"\"\"
ECSx manager.
\"\"\"
use ECSx.Manager
def setup do
# Seed persistent components only for the first server start
# (This will not be run on subsequent app restarts)
:ok
end
def startup do
# Load ephemeral components during first server start and again
# on every subsequent app restart
:ok
end
# Declare all valid Component types
def components do
[
MyApp.Components.AnotherComponent,
MyApp.Components.LegendaryComponent
]
end
# Declare all Systems to run
def systems do
[
# MyApp.Systems.SampleSystem
]
end
end
"""
end)
end
end
46 changes: 46 additions & 0 deletions test/mix/tasks/ecsx.gen.system_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,50 @@ defmodule Mix.Tasks.Ecsx.Gen.SystemTest do
end)
end)
end

test "handles systems with 'end' in the name" do
Mix.Project.in_project(:my_app, ".", fn _module ->
Mix.Tasks.Ecsx.Gen.System.run(["LegendSystem"])
Mix.Tasks.Ecsx.Gen.System.run(["BarSystem"])

manager_file = File.read!("lib/my_app/manager.ex")

assert manager_file ==
"""
defmodule MyApp.Manager do
@moduledoc \"\"\"
ECSx manager.
\"\"\"
use ECSx.Manager
def setup do
# Seed persistent components only for the first server start
# (This will not be run on subsequent app restarts)
:ok
end
def startup do
# Load ephemeral components during first server start and again
# on every subsequent app restart
:ok
end
# Declare all valid Component types
def components do
[
# MyApp.Components.SampleComponent
]
end
# Declare all Systems to run
def systems do
[
MyApp.Systems.BarSystem,
MyApp.Systems.LegendSystem
]
end
end
"""
end)
end
end

0 comments on commit 51336b2

Please sign in to comment.