Skip to content

Commit 742e26e

Browse files
authored
refactor: move SyncDependencies to operations, fix hooks and code quality (#232)
* refactor(depsync): move SyncDependencies to operations package * refactor(hooks): create fresh runner per call instead of package-level default * refactor: use typed errors * chore: run go fmt
1 parent fbed672 commit 742e26e

File tree

9 files changed

+88
-76
lines changed

9 files changed

+88
-76
lines changed

internal/commands/bump/bump_plugins_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"strings"
99
"testing"
1010

11-
"github.com/indaco/sley/internal/commands/depsync"
1211
"github.com/indaco/sley/internal/config"
12+
"github.com/indaco/sley/internal/operations"
1313
"github.com/indaco/sley/internal/plugins"
1414
"github.com/indaco/sley/internal/plugins/dependencycheck"
1515
"github.com/indaco/sley/internal/plugins/tagmanager"
@@ -151,7 +151,7 @@ func TestSyncDependencies(t *testing.T) {
151151

152152
t.Run("nil checker returns nil", func(t *testing.T) {
153153
registry := plugins.NewPluginRegistry()
154-
err := depsync.SyncDependencies(registry, version)
154+
err := operations.SyncDependencies(registry, version)
155155
if err != nil {
156156
t.Errorf("expected nil error, got %v", err)
157157
}

internal/commands/bump/common.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66

77
"github.com/indaco/sley/internal/clix"
8-
"github.com/indaco/sley/internal/commands/depsync"
98
"github.com/indaco/sley/internal/config"
109
"github.com/indaco/sley/internal/core"
1110
"github.com/indaco/sley/internal/operations"
@@ -119,7 +118,7 @@ func executePreBumpValidations(registry *plugins.PluginRegistry, newVersion, pre
119118
// used to avoid showing it twice in the dependency sync output.
120119
func executePostBumpActions(registry *plugins.PluginRegistry, newVersion, previousVersion semver.SemVersion, bumpType, bumpedPath string) error {
121120
// Sync dependency files after updating .version
122-
if err := depsync.SyncDependencies(registry, newVersion, bumpedPath); err != nil {
121+
if err := operations.SyncDependencies(registry, newVersion, bumpedPath); err != nil {
123122
return err
124123
}
125124

internal/commands/bump/multimodule.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66

77
"github.com/indaco/sley/internal/clix"
8-
"github.com/indaco/sley/internal/commands/depsync"
98
"github.com/indaco/sley/internal/core"
109
"github.com/indaco/sley/internal/operations"
1110
"github.com/indaco/sley/internal/plugins"
@@ -72,7 +71,7 @@ func runMultiModuleBump(
7271
parsedVersion, err := semver.ParseVersion(newVersion)
7372
if err == nil {
7473
bumpedPaths := getBumpedModulePaths(results)
75-
if err := depsync.SyncDependencies(registry, parsedVersion, bumpedPaths...); err != nil {
74+
if err := operations.SyncDependencies(registry, parsedVersion, bumpedPaths...); err != nil {
7675
return err
7776
}
7877
}

internal/commands/depsync/sync.go

Lines changed: 4 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,13 @@
22
package depsync
33

44
import (
5-
"fmt"
6-
"path/filepath"
7-
5+
"github.com/indaco/sley/internal/operations"
86
"github.com/indaco/sley/internal/plugins"
9-
"github.com/indaco/sley/internal/plugins/dependencycheck"
10-
"github.com/indaco/sley/internal/printer"
117
"github.com/indaco/sley/internal/semver"
128
)
139

14-
// SyncDependencies updates all configured dependency files to match the new version.
15-
// Returns nil if dependency checker is not enabled or auto-sync is disabled.
16-
// The bumpedPaths parameter contains paths that were already bumped as modules
17-
// and should be excluded from the output (they're still synced but not displayed twice).
10+
// SyncDependencies delegates to operations.SyncDependencies.
11+
// Kept for backward compatibility with existing callers outside the bump command.
1812
func SyncDependencies(registry *plugins.PluginRegistry, version semver.SemVersion, bumpedPaths ...string) error {
19-
dc := registry.GetDependencyChecker()
20-
if dc == nil {
21-
return nil
22-
}
23-
24-
if !dc.IsEnabled() || !dc.GetConfig().AutoSync {
25-
return nil
26-
}
27-
28-
files := dc.GetConfig().Files
29-
if len(files) == 0 {
30-
return nil
31-
}
32-
33-
if err := dc.SyncVersions(version.String()); err != nil {
34-
return fmt.Errorf("failed to sync dependency versions: %w", err)
35-
}
36-
37-
// Build set of bumped paths for quick lookup
38-
bumpedSet := make(map[string]bool, len(bumpedPaths))
39-
for _, p := range bumpedPaths {
40-
bumpedSet[p] = true
41-
}
42-
43-
// Filter files to only show ones not already bumped as modules
44-
var additionalFiles []dependencycheck.FileConfig
45-
for _, file := range files {
46-
if !bumpedSet[file.Path] {
47-
additionalFiles = append(additionalFiles, file)
48-
}
49-
}
50-
51-
// Only print section if there are additional files to show
52-
if len(additionalFiles) > 0 {
53-
fmt.Println("Sync dependencies")
54-
for _, file := range additionalFiles {
55-
name := deriveDependencyName(file.Path)
56-
fmt.Printf(" %s %s %s%s\n", printer.SuccessBadge("✓"), name, printer.Faint("("+file.Path+")"), printer.Faint(": "+version.String()))
57-
}
58-
}
59-
60-
return nil
61-
}
62-
63-
// deriveDependencyName extracts a display name from a file path.
64-
// For .version files, uses the parent directory name.
65-
// For other files (package.json, etc.), uses the filename.
66-
func deriveDependencyName(path string) string {
67-
base := filepath.Base(path)
68-
if base == ".version" {
69-
dir := filepath.Dir(path)
70-
return filepath.Base(dir)
71-
}
72-
return base
13+
return operations.SyncDependencies(registry, version, bumpedPaths...)
7314
}

internal/commands/depsync/sync_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package depsync
33
import (
44
"testing"
55

6+
"github.com/indaco/sley/internal/operations"
67
"github.com/indaco/sley/internal/plugins"
78
"github.com/indaco/sley/internal/semver"
89
)
@@ -37,7 +38,7 @@ func TestDeriveDependencyName(t *testing.T) {
3738

3839
for _, tt := range tests {
3940
t.Run(tt.name, func(t *testing.T) {
40-
got := deriveDependencyName(tt.path)
41+
got := operations.DeriveDependencyName(tt.path)
4142
if got != tt.expected {
4243
t.Errorf("deriveDependencyName(%q) = %q, want %q", tt.path, got, tt.expected)
4344
}

internal/hooks/hooks.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,11 @@ var (
9797
preReleaseHooks []PreReleaseHook
9898
)
9999

100-
// defaultRunner is the default PreReleaseHookRunner for backward compatibility.
101-
var defaultRunner = NewPreReleaseHookRunner(nil, nil)
102-
103100
// RunPreReleaseHooks runs all registered pre-release hooks.
101+
// A fresh runner is created each call so it always sees the current hook list.
104102
func RunPreReleaseHooks(ctx context.Context, skip bool) error {
105-
return defaultRunner.Run(ctx, skip)
103+
runner := NewPreReleaseHookRunner(nil, nil)
104+
return runner.Run(ctx, skip)
106105
}
107106

108107
func RegisterPreReleaseHook(h PreReleaseHook) {

internal/operations/depsync.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package operations
2+
3+
import (
4+
"fmt"
5+
"path/filepath"
6+
7+
"github.com/indaco/sley/internal/plugins"
8+
"github.com/indaco/sley/internal/plugins/dependencycheck"
9+
"github.com/indaco/sley/internal/printer"
10+
"github.com/indaco/sley/internal/semver"
11+
)
12+
13+
// SyncDependencies updates all configured dependency files to match the new version.
14+
// Returns nil if dependency checker is not enabled or auto-sync is disabled.
15+
// The bumpedPaths parameter contains paths that were already bumped as modules
16+
// and should be excluded from the output (they're still synced but not displayed twice).
17+
func SyncDependencies(registry *plugins.PluginRegistry, version semver.SemVersion, bumpedPaths ...string) error {
18+
dc := registry.GetDependencyChecker()
19+
if dc == nil {
20+
return nil
21+
}
22+
23+
if !dc.IsEnabled() || !dc.GetConfig().AutoSync {
24+
return nil
25+
}
26+
27+
files := dc.GetConfig().Files
28+
if len(files) == 0 {
29+
return nil
30+
}
31+
32+
if err := dc.SyncVersions(version.String()); err != nil {
33+
return fmt.Errorf("failed to sync dependency versions: %w", err)
34+
}
35+
36+
// Build set of bumped paths for quick lookup
37+
bumpedSet := make(map[string]bool, len(bumpedPaths))
38+
for _, p := range bumpedPaths {
39+
bumpedSet[p] = true
40+
}
41+
42+
// Filter files to only show ones not already bumped as modules
43+
var additionalFiles []dependencycheck.FileConfig
44+
for _, file := range files {
45+
if !bumpedSet[file.Path] {
46+
additionalFiles = append(additionalFiles, file)
47+
}
48+
}
49+
50+
// Only print section if there are additional files to show
51+
if len(additionalFiles) > 0 {
52+
fmt.Println("Sync dependencies")
53+
for _, file := range additionalFiles {
54+
name := DeriveDependencyName(file.Path)
55+
fmt.Printf(" %s %s %s%s\n", printer.SuccessBadge("✓"), name, printer.Faint("("+file.Path+")"), printer.Faint(": "+version.String()))
56+
}
57+
}
58+
59+
return nil
60+
}
61+
62+
// DeriveDependencyName extracts a display name from a file path.
63+
// For .version files, uses the parent directory name.
64+
// For other files (package.json, etc.), uses the filename.
65+
func DeriveDependencyName(path string) string {
66+
base := filepath.Base(path)
67+
if base == ".version" {
68+
dir := filepath.Dir(path)
69+
return filepath.Base(dir)
70+
}
71+
return base
72+
}

internal/plugins/commitparser/plugin.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ var (
2424

2525
// CommitParser defines the interface for parsing a list of commit messages
2626
// and determining the corresponding semver bump type.
27-
2827
type CommitParser interface {
2928
Name() string
3029
Description() string

internal/semver/version.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"regexp"
77
"strconv"
88
"strings"
9+
10+
"github.com/indaco/sley/internal/apperrors"
911
)
1012

1113
// SemVersion represents a semantic version (major.minor.patch-preRelease+build).
@@ -170,7 +172,7 @@ func BumpByLabel(v SemVersion, label string) (SemVersion, error) {
170172
case "major":
171173
return SemVersion{Major: v.Major + 1, Minor: 0, Patch: 0}, nil
172174
default:
173-
return SemVersion{}, fmt.Errorf("invalid bump label: %s", label)
175+
return SemVersion{}, &apperrors.InvalidBumpTypeError{BumpType: label}
174176
}
175177
}
176178

0 commit comments

Comments
 (0)