Skip to content

Commit

Permalink
Merge pull request #193 from liveview-native/bc-codegen-improvements
Browse files Browse the repository at this point in the history
Codegen improvements
  • Loading branch information
bcardarella authored Aug 7, 2024
2 parents fb296c7 + 4ea1beb commit 4d5aedf
Show file tree
Hide file tree
Showing 4 changed files with 246 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 4d5aedf

Please sign in to comment.