Skip to content

Commit

Permalink
Codegen improvements
Browse files Browse the repository at this point in the history
* Codegen.patch/3 now returns result tuple for better error handling
* Codegens are grouped by file and type to merge all codegens for a single pass on each
* Don't error on missing files
* Don't error on unhandled codegen scenarios
  • Loading branch information
bcardarella committed Aug 7, 2024
1 parent fb296c7 commit 9d81149
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 170 deletions.
39 changes: 18 additions & 21 deletions lib/mix/live_view_native/codegen.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ defmodule Mix.LiveViewNative.CodeGen do
alias Sourceror.Zipper

def patch(source, change, opts \\ []) do
patches = build_patches(source, change, opts)

Sourceror.patch_string(source, patches)
case build_patches(source, change, opts) do
{:error, msg} -> {:error, msg}
patches when is_list(patches) ->
{:ok, Sourceror.patch_string(source, patches)}
end
end

defp build_patches(source, change, opts) do
Expand All @@ -21,11 +23,7 @@ defmodule Mix.LiveViewNative.CodeGen do
{_, :head} -> inject_head(source, change)
{_, :eof} -> inject_eof(source, change)
_ ->
if fail_msg = Keyword.get(opts, :fail_msg) do
Mix.shell.info(fail_msg)
end

[]
{:error, Keyword.get(opts, :fail_msg, "")}
end
end

Expand All @@ -40,14 +38,13 @@ defmodule Mix.LiveViewNative.CodeGen do

[build_patch(range, change)]
:error ->
"""
The following change failed to be applied to #{path}
#{change}
"""
|> Mix.shell.info()
msg =
"""
The following change failed to be applied to #{path}
[]
#{change}
"""
{:error, msg}
end
end

Expand All @@ -62,14 +59,14 @@ defmodule Mix.LiveViewNative.CodeGen do

[build_patch(range, change)]
:error ->
"""
The following change failed to be applied to #{path}
msg =
"""
The following change failed to be applied to #{path}
#{change}
"""
|> Mix.shell.info()
#{change}
"""

[]
{:error, msg}
end
end

Expand Down
Loading

0 comments on commit 9d81149

Please sign in to comment.