Skip to content

Commit b521fbc

Browse files
authored
refactor(depsync): add centralized dependency sync and integrate it into bump commands (#188)
1 parent c2c75ce commit b521fbc

5 files changed

Lines changed: 81 additions & 66 deletions

File tree

internal/commands/bump/bump_plugins_test.go

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

11+
"github.com/indaco/sley/internal/commands/depsync"
1112
"github.com/indaco/sley/internal/config"
1213
"github.com/indaco/sley/internal/plugins"
1314
"github.com/indaco/sley/internal/plugins/auditlog"
@@ -214,13 +215,13 @@ func TestSyncDependencies(t *testing.T) {
214215
t.Run("nil checker returns nil", func(t *testing.T) {
215216
registry := plugins.NewPluginRegistry()
216217
dependencycheck.GetDependencyCheckerFn = func() dependencycheck.DependencyChecker { return nil }
217-
err := syncDependencies(registry, version)
218+
err := depsync.SyncDependencies(registry, version)
218219
if err != nil {
219220
t.Errorf("expected nil error, got %v", err)
220221
}
221222
})
222223

223-
// Note: syncDependencies uses type assertion to *DependencyCheckerPlugin
224+
// Note: shared.SyncDependencies uses type assertion to *DependencyCheckerPlugin
224225
// so mock implementations will be treated as disabled and return nil
225226
}
226227

internal/commands/bump/common.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55

66
"github.com/indaco/sley/internal/clix"
7+
"github.com/indaco/sley/internal/commands/depsync"
78
"github.com/indaco/sley/internal/config"
89
"github.com/indaco/sley/internal/operations"
910
"github.com/indaco/sley/internal/plugins"
@@ -121,7 +122,7 @@ func executePreBumpValidations(registry *plugins.PluginRegistry, newVersion, pre
121122
// used to avoid showing it twice in the dependency sync output.
122123
func executePostBumpActions(registry *plugins.PluginRegistry, newVersion, previousVersion semver.SemVersion, bumpType, bumpedPath string) error {
123124
// Sync dependency files after updating .version
124-
if err := syncDependencies(registry, newVersion, bumpedPath); err != nil {
125+
if err := depsync.SyncDependencies(registry, newVersion, bumpedPath); err != nil {
125126
return err
126127
}
127128

internal/commands/bump/helpers.go

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -200,68 +200,6 @@ func validateDependencyConsistency(registry *plugins.PluginRegistry, version sem
200200
return nil
201201
}
202202

203-
// syncDependencies updates all configured dependency files to match the new version.
204-
// Returns nil if dependency checker is not enabled or auto-sync is disabled.
205-
// The bumpedPaths parameter contains paths that were already bumped as modules
206-
// and should be excluded from the output (they're still synced but not displayed twice).
207-
func syncDependencies(registry *plugins.PluginRegistry, version semver.SemVersion, bumpedPaths ...string) error {
208-
dc := registry.GetDependencyChecker()
209-
if dc == nil {
210-
return nil
211-
}
212-
213-
plugin, ok := dc.(*dependencycheck.DependencyCheckerPlugin)
214-
if !ok || !plugin.IsEnabled() || !plugin.GetConfig().AutoSync {
215-
return nil
216-
}
217-
218-
files := plugin.GetConfig().Files
219-
if len(files) == 0 {
220-
return nil
221-
}
222-
223-
if err := dc.SyncVersions(version.String()); err != nil {
224-
return fmt.Errorf("failed to sync dependency versions: %w", err)
225-
}
226-
227-
// Build set of bumped paths for quick lookup
228-
bumpedSet := make(map[string]bool, len(bumpedPaths))
229-
for _, p := range bumpedPaths {
230-
bumpedSet[p] = true
231-
}
232-
233-
// Filter files to only show ones not already bumped as modules
234-
var additionalFiles []dependencycheck.FileConfig
235-
for _, file := range files {
236-
if !bumpedSet[file.Path] {
237-
additionalFiles = append(additionalFiles, file)
238-
}
239-
}
240-
241-
// Only print section if there are additional files to show
242-
if len(additionalFiles) > 0 {
243-
fmt.Println("Sync dependencies")
244-
for _, file := range additionalFiles {
245-
name := deriveDependencyName(file.Path)
246-
fmt.Printf(" %s %s %s%s\n", printer.SuccessBadge("✓"), name, printer.Faint("("+file.Path+")"), printer.Faint(": "+version.String()))
247-
}
248-
}
249-
250-
return nil
251-
}
252-
253-
// deriveDependencyName extracts a display name from a file path.
254-
// For .version files, uses the parent directory name.
255-
// For other files (package.json, etc.), uses the filename.
256-
func deriveDependencyName(path string) string {
257-
base := filepath.Base(path)
258-
if base == ".version" {
259-
dir := filepath.Dir(path)
260-
return filepath.Base(dir)
261-
}
262-
return base
263-
}
264-
265203
// generateChangelogAfterBump generates changelog entries if changelog generator is enabled.
266204
// Returns nil if changelog generator is not enabled.
267205
func generateChangelogAfterBump(registry *plugins.PluginRegistry, version, _ semver.SemVersion, bumpType string) error {

internal/commands/bump/multimodule.go

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

77
"github.com/indaco/sley/internal/clix"
8+
"github.com/indaco/sley/internal/commands/depsync"
89
"github.com/indaco/sley/internal/core"
910
"github.com/indaco/sley/internal/operations"
1011
"github.com/indaco/sley/internal/plugins"
@@ -70,7 +71,7 @@ func runMultiModuleBump(
7071
parsedVersion, err := semver.ParseVersion(newVersion)
7172
if err == nil {
7273
bumpedPaths := getBumpedModulePaths(results)
73-
if err := syncDependencies(registry, parsedVersion, bumpedPaths...); err != nil {
74+
if err := depsync.SyncDependencies(registry, parsedVersion, bumpedPaths...); err != nil {
7475
return err
7576
}
7677
}

internal/commands/depsync/sync.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Package depsync provides dependency synchronization for CLI commands.
2+
package depsync
3+
4+
import (
5+
"fmt"
6+
"path/filepath"
7+
8+
"github.com/indaco/sley/internal/plugins"
9+
"github.com/indaco/sley/internal/plugins/dependencycheck"
10+
"github.com/indaco/sley/internal/printer"
11+
"github.com/indaco/sley/internal/semver"
12+
)
13+
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).
18+
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+
plugin, ok := dc.(*dependencycheck.DependencyCheckerPlugin)
25+
if !ok || !plugin.IsEnabled() || !plugin.GetConfig().AutoSync {
26+
return nil
27+
}
28+
29+
files := plugin.GetConfig().Files
30+
if len(files) == 0 {
31+
return nil
32+
}
33+
34+
if err := dc.SyncVersions(version.String()); err != nil {
35+
return fmt.Errorf("failed to sync dependency versions: %w", err)
36+
}
37+
38+
// Build set of bumped paths for quick lookup
39+
bumpedSet := make(map[string]bool, len(bumpedPaths))
40+
for _, p := range bumpedPaths {
41+
bumpedSet[p] = true
42+
}
43+
44+
// Filter files to only show ones not already bumped as modules
45+
var additionalFiles []dependencycheck.FileConfig
46+
for _, file := range files {
47+
if !bumpedSet[file.Path] {
48+
additionalFiles = append(additionalFiles, file)
49+
}
50+
}
51+
52+
// Only print section if there are additional files to show
53+
if len(additionalFiles) > 0 {
54+
fmt.Println("Sync dependencies")
55+
for _, file := range additionalFiles {
56+
name := deriveDependencyName(file.Path)
57+
fmt.Printf(" %s %s %s%s\n", printer.SuccessBadge("✓"), name, printer.Faint("("+file.Path+")"), printer.Faint(": "+version.String()))
58+
}
59+
}
60+
61+
return nil
62+
}
63+
64+
// deriveDependencyName extracts a display name from a file path.
65+
// For .version files, uses the parent directory name.
66+
// For other files (package.json, etc.), uses the filename.
67+
func deriveDependencyName(path string) string {
68+
base := filepath.Base(path)
69+
if base == ".version" {
70+
dir := filepath.Dir(path)
71+
return filepath.Base(dir)
72+
}
73+
return base
74+
}

0 commit comments

Comments
 (0)