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

Modify salad.init task to automatically add TwMerge.Cache #107

Merged
merged 4 commits into from
Dec 9, 2024

Conversation

selenil
Copy link
Contributor

@selenil selenil commented Dec 7, 2024

The salad.init task now also patches the application.ex file to automatically include TwMerge.Cache as part of the setup process.

Summary by Sourcery

Modify the salad.init task to automatically include TwMerge.Cache in the application supervisor setup. Add functionality to patch the Elixir application supervisor with new children, enhancing the initialization process.

New Features:

  • Automatically add TwMerge.Cache to the application supervisor during the salad.init task.

Enhancements:

  • Introduce a new function to patch the Elixir application supervisor with additional children.

Copy link

sourcery-ai bot commented Dec 7, 2024

Reviewer's Guide by Sourcery

This PR enhances the salad.init mix task by adding automatic integration of TwMerge.Cache into the application supervisor. The implementation includes a new ElixirPatcher module that handles the modification of the application.ex file, along with additional functionality to copy Zag-related files.

Sequence diagram for salad.init task modification

sequenceDiagram
    participant User
    participant MixTask as Mix.Tasks.Salad.Init
    participant Patcher as SaladUI.Patcher
    participant ElixirPatcher as SaladUI.Patcher.ElixirPatcher

    User->>MixTask: Run salad.init
    MixTask->>MixTask: Determine environment and paths
    MixTask->>MixTask: Create component path
    MixTask->>MixTask: Write config
    MixTask->>MixTask: Initialize TwMerge.Cache
    MixTask->>ElixirPatcher: Patch application.ex
    ElixirPatcher->>ElixirPatcher: Read application.ex
    ElixirPatcher->>ElixirPatcher: Update content with TwMerge.Cache
    ElixirPatcher->>MixTask: Return success or error
    MixTask->>MixTask: Patch CSS, JS, Tailwind config
    MixTask->>MixTask: Copy Zag files
    MixTask->>MixTask: Install node dependencies
    MixTask->>User: Display completion message
Loading

Class diagram for new ElixirPatcher module

classDiagram
    class SaladUI.Patcher {
        +patch_elixir_application(application_file_path, new_children, description)
    }
    class SaladUI.Patcher.ElixirPatcher {
        +patch_application_supervisor(application_file_path, new_children, description)
        -update_content(content, new_children, description)
        -single_line_list?(list_content)
        -extract_indentation(list_content)
        -build_addition(new_children, description, element_indentation)
    }
    SaladUI.Patcher --> SaladUI.Patcher.ElixirPatcher : uses
Loading

File-Level Changes

Change Details Files
Added new ElixirPatcher module for modifying application supervisor
  • Implemented patch_application_supervisor function to add new children to supervisor
  • Added support for both single-line and multi-line children lists
  • Included description handling for supervisor children
  • Added indentation preservation logic for maintaining code style
lib/salad_ui/patcher/elixir_patcher.ex
Enhanced salad.init task with TwMerge.Cache integration
  • Added init_tw_merge_cache function to patch application.ex
  • Added automatic detection of application.ex file path
  • Integrated TwMerge.Cache initialization into the main task flow
lib/mix/tasks/salad.init.ex
Extended Patcher module with application modification support
  • Added patch_elixir_application function
  • Added documentation for the new patching functionality
lib/salad_ui/patcher.ex
Added Zag files handling functionality
  • Implemented copy_zag_files function to copy Zag-related files
  • Added user notifications about ZagHook installation
lib/mix/tasks/salad.init.ex

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

lib/salad_ui/patcher/elixir_patcher.ex Dismissed Show dismissed Hide dismissed
|> update_content(new_children, description)

case new_content do
{:ok, new_content} -> File.write!(application_file_path, new_content)

Check warning

Code scanning / Sobelow

Directory Traversal in `File` function Warning

Traversal.FileModule: Directory Traversal in File.write!
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @selenil - I've reviewed your changes - here's some feedback:

Overall Comments:

  • The PR includes additional changes (zag files) beyond what's described in the title/description. Please either update the description to reflect all changes or split this into separate PRs for better clarity.
  • The new ElixirPatcher module would benefit from test coverage to ensure reliable supervisor modifications across different application configurations.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@@ -155,6 +175,25 @@ defmodule Mix.Tasks.Salad.Init do
:ok
end

defp copy_zag_files(assets_path) do
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Consider adding explicit error handling for file operations

File operations could fail for various reasons (permissions, disk space). Consider wrapping operations in a with statement to handle errors gracefully

  defp copy_zag_files(assets_path) do
    with {:ok, _} <- ensure_writable(assets_path),
         :ok <- Mix.shell().info("Copying zag files to assets folder") do
      source_path = Path.join(assets_path, "zag")

@selenil selenil force-pushed the feature/patch-application-file branch from f9e113e to 43cfd8a Compare December 7, 2024 21:51
@bluzky
Copy link
Owner

bluzky commented Dec 8, 2024

Thanks @selenil, would you mind adding unit test for new patcher.
BTW, I'm trying to migrate Select component using ZagHook but it's not easy :D

@bluzky bluzky merged commit 2364f9f into bluzky:main Dec 9, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants