Skip to content

feat: [#650] refactor package management to support fluent method#1010

Merged
almas-x merged 6 commits intomasterfrom
almas/#650
Apr 16, 2025
Merged

feat: [#650] refactor package management to support fluent method#1010
almas-x merged 6 commits intomasterfrom
almas/#650

Conversation

@almas-x
Copy link
Contributor

@almas-x almas-x commented Apr 14, 2025

📑 Description

Closes goravel/goravel#650

This PR refactors the package management module to support a more fluent and expressive API, enhancing both readability and developer experience.

🧾 Before

Previously, package installation and uninstallation were handled with verbose struct-based configurations:

// setup.go
package main

import (
	"os"

	contractspackages "github.com/goravel/framework/contracts/packages"
	"github.com/goravel/framework/packages"
	"github.com/goravel/framework/support/path"
)

func main() {
	setup := packages.Setup(os.Args)
	setup.Install(packages.ModifyGoFile{
		File: path.Config("app.go"),
		Modifiers: []contractspackages.GoNodeModifier{
			packages.AddImportSpec(setup.Module),
			packages.AddProviderSpec("&somepkg.ServiceProvider{}"),
		},
	})
	setup.Uninstall(packages.ModifyGoFile{
		File: path.Config("app.go"),
		Modifiers: []contractspackages.GoNodeModifier{
			packages.RemoveImportSpec(setup.Module),
			packages.RemoveProviderSpec("&somepkg.ServiceProvider{}"),
		},
	})

	setup.Execute()
}

✨ After

The new approach introduces fluent method chaining, allowing a cleaner and more declarative setup:

// setup.go
package main

import (
	"os"

	"github.com/goravel/framework/packages"
	"github.com/goravel/framework/packages/match"
	"github.com/goravel/framework/packages/modify"
	"github.com/goravel/framework/support/path"
)

func main() {
	packages.Setup(os.Args).
		Install(
			modify.File(path.Config("app.go")).
				Find(match.Imports()...).Modify(modify.AddImport(packages.GetModulePath())).
				Find(match.Providers()...).Modify(modify.AddProvider("&somepkg.ServiceProvider{}")),
		).
		Uninstall(
			modify.File(path.Config("app.go")).
				Find(match.Imports()...).Modify(modify.RemoveImport(packages.GetModulePath())).
				Find(match.Providers()...).Modify(modify.RemoveProvider("&somepkg.ServiceProvider{}")),
		).
		Execute()
}

💡 Benefits

  • Fluent API: Allows method chaining for clearer, more concise configuration

  • Improved readability: Code is easier to follow and maintain

  • Modular structure: Clear separation of matching and modifying logic via match and modify packages

  • Extensibility: Future enhancements can be integrated more easily thanks to the new design

✅ Checks

  • Added test cases for my code

Copilot AI review requested due to automatic review settings April 14, 2025 08:22
@almas-x almas-x requested a review from a team as a code owner April 14, 2025 08:22
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 26 out of 26 changed files in this pull request and generated no comments.

@almas-x almas-x changed the title feat: [#650]refactor package management to support fluent method chaining feat: [#650]refactor package management to support fluent method Apr 14, 2025
@codecov
Copy link

codecov bot commented Apr 14, 2025

Codecov Report

Attention: Patch coverage is 95.68528% with 17 lines in your changes missing coverage. Please review.

Project coverage is 70.76%. Comparing base (ebdcf85) to head (1bf3400).
Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
packages/modify/actions.go 93.39% 7 Missing ⚠️
packages/modify/modify.go 91.42% 4 Missing and 2 partials ⚠️
packages/setup.go 85.71% 2 Missing and 1 partial ⚠️
packages/modify/utils.go 97.29% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1010      +/-   ##
==========================================
+ Coverage   70.23%   70.76%   +0.53%     
==========================================
  Files         169      170       +1     
  Lines       11456    11575     +119     
==========================================
+ Hits         8046     8191     +145     
+ Misses       3059     3034      -25     
+ Partials      351      350       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@almas-x almas-x changed the title feat: [#650]refactor package management to support fluent method feat: [#650] refactor package management to support fluent method Apr 15, 2025
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 3e75ae1 Previous: 6baccbc Ratio
BenchmarkFile_ReadWrite 315102 ns/op 2072 B/op 28 allocs/op 203967 ns/op 2073 B/op 28 allocs/op 1.54
BenchmarkFile_ReadWrite - ns/op 315102 ns/op 203967 ns/op 1.54

This comment was automatically generated by workflow using github-action-benchmark.

Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

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

Amazing PR 👍 left some nitpicks. Will make a test locally.

Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

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

Amazing 👍

@almas-x almas-x merged commit f5ade0f into master Apr 16, 2025
13 checks passed
@almas-x almas-x deleted the almas/#650 branch April 16, 2025 10:28
almas-x added a commit that referenced this pull request Apr 17, 2025
)

* feat: refactor package management to support fluent method chaining

* chore: optimize code

* feat: add matcher and modifier helpers for commands, migrations, and seeders

* refactor: update code based on review feedback

* chore: rename modify.File to modify.GoFile

* chore: optimize code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generate some fluent methods for package management

3 participants