Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codegen improvements #193

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading