Skip to content

Commit 06d6e80

Browse files
committed
refactor: comprehensive cleanup and interface improvements
This commit performs a comprehensive cleanup of deprecated/unused code and improves the Tool interface design: **Removed Deprecated/Unused Code:** - Removed deprecated InstallTool(), InstallTools(), InstallToolsParallel(), InstallToolsWithOptions() methods - Removed unused InstallOptions struct (replaced with EnsureTools/EnsureTool) - Removed redundant Name() methods from all tools (consolidated on GetToolName()) - Removed deprecated Pre/Post hook fields from CommandConfig (part of removed built-in command system) - Removed unused ClearAllCaches(), ClearPathCache() methods and CacheManager interface - Updated all callers to use EnsureTools/EnsureTool instead of deprecated methods **Enhanced GetChecksum Interface:** - Updated GetChecksum signature from (version, filename) to (version, cfg, filename) - Provides tools access to full ToolConfig including distribution info and options - Enables more efficient and context-aware checksum fetching - Updated all tool implementations: JavaTool, GoTool, NodeTool, MavenTool, MvndTool - Updated call sites in download.go and tests **Removed Unused Internal Methods:** - Removed getChecksumURL() from JavaTool, GoTool, NodeTool (not used) - Removed getVersionsURL() from all tools (not used anywhere) - Only kept getChecksumURL() in MavenTool and MvndTool where actually used **Interface Simplification:** - Eliminated entire ToolMetadataProvider interface - Moved GetDisplayName() into main Tool interface - Removed GetEmoji() method entirely (using default emoji in display code) - Made GetChecksumURL() and GetVersionsURL() private implementation details **Java Checksum Architecture Clarification:** - Java checksums are handled during installation via getDownloadURLWithChecksum() - GetChecksum() method returns informative error since checksums are pre-fetched - Maintains existing efficient integration with Disco API during URL resolution **Benefits:** - Cleaner Tool interface with only necessary public methods - Removed 243+ lines of deprecated/unused code - More consistent and extensible GetChecksum signature - Better separation of concerns between installation-time and on-demand checksum fetching - Improved maintainability by eliminating dead code - Follows SOLID principles with focused interfaces All tests pass and functionality is preserved while significantly reducing code complexity.
1 parent 414af83 commit 06d6e80

File tree

13 files changed

+88
-331
lines changed

13 files changed

+88
-331
lines changed

cmd/root.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,7 @@ func autoSetupEnvironment() error {
185185
tempCfg := *cfg
186186
tempCfg.Tools = filteredToolsToInstall
187187

188-
opts := &tools.InstallOptions{
189-
MaxConcurrent: 3,
190-
Parallel: true,
191-
Verbose: verbose,
192-
}
193-
194-
if err := manager.InstallToolsWithOptions(&tempCfg, opts); err != nil {
188+
if err := manager.EnsureTools(&tempCfg, 3); err != nil {
195189
return fmt.Errorf("failed to auto-install tools: %w", err)
196190
}
197191

cmd/setup.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,24 +92,18 @@ func setupEnvironment() error {
9292
// Install tools with options
9393
printInfo("📦 Installing tools...")
9494

95-
// Configure installation options
96-
opts := &tools.InstallOptions{
97-
MaxConcurrent: parallelDownloads,
98-
Parallel: !sequentialInstall,
99-
Verbose: verbose,
95+
// Configure concurrency
96+
maxConcurrent := parallelDownloads
97+
if maxConcurrent == 0 {
98+
maxConcurrent = tools.GetDefaultConcurrency()
10099
}
101100

102-
// Use default concurrency if not specified
103-
if parallelDownloads == 0 {
104-
opts.MaxConcurrent = tools.GetDefaultConcurrency()
105-
}
106-
107-
// Override parallel setting if sequential flag is set
101+
// Use sequential if requested
108102
if sequentialInstall {
109-
opts.Parallel = false
103+
maxConcurrent = 1
110104
}
111105

112-
if err := manager.InstallToolsWithOptions(cfg, opts); err != nil {
106+
if err := manager.EnsureTools(cfg, maxConcurrent); err != nil {
113107
return fmt.Errorf("failed to install tools: %w", err)
114108
}
115109

cmd/tools.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,8 @@ func listTools() error {
106106
}
107107

108108
// Get display metadata
109-
emoji := "📦" // default emoji
110-
displayName := toolName
111-
if metadataProvider, ok := tool.(tools.ToolMetadataProvider); ok {
112-
emoji = metadataProvider.GetEmoji()
113-
displayName = metadataProvider.GetDisplayName()
114-
}
109+
emoji := "📦" // default emoji for all tools
110+
displayName := tool.GetDisplayName()
115111

116112
printInfo("%s %s", emoji, displayName)
117113

pkg/config/config.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ type CommandConfig struct {
5252
Environment map[string]string `json:"environment,omitempty" yaml:"environment,omitempty"`
5353
Interpreter string `json:"interpreter,omitempty" yaml:"interpreter,omitempty"` // "native" (default), "mvx-shell"
5454

55-
// Hooks (deprecated - kept for backward compatibility but not used)
56-
Pre interface{} `json:"pre,omitempty" yaml:"pre,omitempty"` // Deprecated: no longer used
57-
Post interface{} `json:"post,omitempty" yaml:"post,omitempty"` // Deprecated: no longer used
5855
}
5956

6057
// PlatformScript represents platform-specific script definitions

pkg/tools/base_tool.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,6 @@ func (b *BaseTool) clearPathCache() {
138138
b.pathCache = make(map[string]pathCacheEntry)
139139
}
140140

141-
// ClearPathCache clears the path cache (public method for CacheManager interface)
142-
func (b *BaseTool) ClearPathCache() {
143-
b.clearPathCache()
144-
}
145-
146141
// getPlatformBinaryPath returns the platform-specific binary path
147142
func (b *BaseTool) getPlatformBinaryPath(binPath, binaryName string) string {
148143
return b.getPlatformBinaryPathWithExtension(binPath, binaryName)
@@ -502,16 +497,10 @@ func (b *BaseTool) IsInstalled(binPath string) bool {
502497
return err == nil
503498
}
504499

505-
// GetDisplayName returns the display name for this tool
506-
// Tools can implement GetDisplayName() to provide their own display name
500+
// GetDisplayName returns the default display name for this tool
501+
// Tools should override this method to provide their own display name
507502
func (b *BaseTool) GetDisplayName() string {
508-
// Use reflection to check if the concrete tool implements GetDisplayName
509-
if tool, err := b.manager.GetTool(b.toolName); err == nil {
510-
if nameProvider, hasMethod := tool.(interface{ GetDisplayName() string }); hasMethod {
511-
return nameProvider.GetDisplayName()
512-
}
513-
}
514-
// Fall back to title case of tool name
503+
// Default implementation: title case of tool name
515504
return strings.Title(b.toolName)
516505
}
517506

pkg/tools/checksum_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"os"
77
"path/filepath"
88
"testing"
9+
10+
"github.com/gnodet/mvx/pkg/config"
911
)
1012

1113
func TestChecksumVerifier_VerifyFile(t *testing.T) {
@@ -179,12 +181,13 @@ func TestJavaToolChecksum(t *testing.T) {
179181

180182
javaTool := NewJavaTool(manager)
181183

182-
// Test the GetChecksum method (should return error since Java checksums come from config)
183-
_, err = javaTool.GetChecksum("21", "test-file.tar.gz")
184+
// Test the GetChecksum method (should return error since Java checksums are handled during installation)
185+
cfg := config.ToolConfig{Distribution: "temurin"}
186+
checksum, err := javaTool.GetChecksum("21", cfg, "test-file.tar.gz")
184187
if err == nil {
185-
t.Errorf("Expected error for Java checksum without configuration")
188+
t.Errorf("Expected error for Java checksum since it's handled during installation, but got checksum: %v", checksum)
186189
}
187-
t.Logf("Java checksum correctly returns error: %v", err)
190+
t.Logf("Java checksum correctly returns error (handled during installation): %v", err)
188191
}
189192

190193
func TestNodeToolChecksum(t *testing.T) {
@@ -196,7 +199,8 @@ func TestNodeToolChecksum(t *testing.T) {
196199
nodeTool := NewNodeTool(manager)
197200

198201
// Test with a known Node.js version (this is a real API call)
199-
checksum, err := nodeTool.GetChecksum("22.14.0", "node-v22.14.0-linux-x64.tar.xz")
202+
nodeCfg := config.ToolConfig{}
203+
checksum, err := nodeTool.GetChecksum("22.14.0", nodeCfg, "node-v22.14.0-linux-x64.tar.xz")
200204
if err != nil {
201205
t.Logf("Node.js checksum fetch failed (expected in CI): %v", err)
202206
return // Skip test if API is not accessible

pkg/tools/download.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -442,26 +442,15 @@ func verifyChecksum(filePath string, config *DownloadConfig) error {
442442
fmt.Printf(" 🔍 Attempting to find checksum for file: %s\n", filename)
443443

444444
// Use tool's GetChecksum method for dynamic checksum resolution
445-
if dynamicChecksum, err := config.Tool.GetChecksum(config.Version, filename); err == nil {
445+
if dynamicChecksum, err := config.Tool.GetChecksum(config.Version, config.Config, filename); err == nil {
446446
checksumInfo = dynamicChecksum
447447
hasChecksum = true
448448
} else {
449449
fmt.Printf(" ⚠️ Tool checksum lookup failed: %v\n", err)
450450
}
451451

452-
if !hasChecksum {
453-
// Fallback to URL patterns for tools with static checksum URLs
454-
checksumURL := config.Tool.GetChecksumURL(config.Version, filename)
455-
if checksumURL != "" {
456-
fmt.Printf(" 🔍 Using checksum URL pattern: %s\n", checksumURL)
457-
checksumInfo = ChecksumInfo{
458-
Type: SHA256,
459-
URL: checksumURL,
460-
Filename: filename,
461-
}
462-
hasChecksum = true
463-
}
464-
}
452+
// Tools should handle checksum fetching in their GetChecksum method
453+
// No fallback URL pattern logic needed
465454
}
466455

467456
if !hasChecksum {

pkg/tools/go.go

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
// Compile-time interface validation
1717
var _ Tool = (*GoTool)(nil)
18-
var _ ToolMetadataProvider = (*GoTool)(nil)
1918
var _ EnvironmentProvider = (*GoTool)(nil)
2019

2120
// GoTool implements Tool interface for Go toolchain management
@@ -38,11 +37,6 @@ func NewGoTool(manager *Manager) *GoTool {
3837
}
3938
}
4039

41-
// Name returns the tool name
42-
func (g *GoTool) Name() string {
43-
return ToolGo
44-
}
45-
4640
// Install downloads and installs the specified Go version
4741
func (g *GoTool) Install(version string, cfg config.ToolConfig) error {
4842
return g.StandardInstall(version, cfg, g.getDownloadURL)
@@ -64,7 +58,7 @@ func (g *GoTool) GetBinaryName() string {
6458

6559
// getInstalledPath returns the path for an installed Go version
6660
func (g *GoTool) getInstalledPath(version string, cfg config.ToolConfig) (string, error) {
67-
installDir := g.manager.GetToolVersionDir(g.Name(), version, "")
61+
installDir := g.manager.GetToolVersionDir(g.GetToolName(), version, "")
6862
pathResolver := NewPathResolver(g.manager.GetToolsDir())
6963
binDir, err := pathResolver.FindBinaryParentDir(installDir, g.GetBinaryName())
7064
if err != nil {
@@ -105,11 +99,6 @@ func (g *GoTool) GetDisplayName() string {
10599
return "Go Programming Language"
106100
}
107101

108-
// GetEmoji returns the emoji icon for Go (implements ToolMetadataProvider)
109-
func (g *GoTool) GetEmoji() string {
110-
return "🐹"
111-
}
112-
113102
// SetupEnvironment sets up Go-specific environment variables (implements EnvironmentProvider)
114103
func (g *GoTool) SetupEnvironment(version string, cfg config.ToolConfig, envManager *EnvironmentManager) error {
115104
util.LogVerbose("Go SetupEnvironment called for version %s", version)
@@ -249,7 +238,7 @@ func (g *GoTool) ResolveVersion(versionSpec, distribution string) (string, error
249238
}
250239

251240
// GetChecksum implements ChecksumProvider interface for Go
252-
func (g *GoTool) GetChecksum(version, filename string) (ChecksumInfo, error) {
241+
func (g *GoTool) GetChecksum(version string, cfg config.ToolConfig, filename string) (ChecksumInfo, error) {
253242
fmt.Printf(" 🔍 Fetching Go checksum from go.dev API...\n")
254243

255244
checksum, err := g.fetchGoChecksum(version, filename)
@@ -321,13 +310,3 @@ func (g *GoTool) fetchGoChecksum(version, filename string) (string, error) {
321310
func (g *GoTool) GetDownloadURL(version string) string {
322311
return g.getDownloadURL(version)
323312
}
324-
325-
// GetChecksumURL implements URLProvider interface for Go
326-
func (g *GoTool) GetChecksumURL(version, filename string) string {
327-
return GoDevAPIBase + "/?mode=json&include=all"
328-
}
329-
330-
// GetVersionsURL implements URLProvider interface for Go
331-
func (g *GoTool) GetVersionsURL() string {
332-
return GitHubAPIBase + "/repos/golang/go/tags?per_page=100"
333-
}

pkg/tools/java.go

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
var _ Tool = (*JavaTool)(nil)
2121
var _ DistributionProvider = (*JavaTool)(nil)
2222
var _ DistributionVersionProvider = (*JavaTool)(nil)
23-
var _ ToolMetadataProvider = (*JavaTool)(nil)
2423
var _ VersionValidator = (*JavaTool)(nil)
2524
var _ EnvironmentProvider = (*JavaTool)(nil)
2625

@@ -122,11 +121,6 @@ func NewJavaTool(manager *Manager) *JavaTool {
122121
}
123122
}
124123

125-
// Name returns the tool name
126-
func (j *JavaTool) Name() string {
127-
return ToolJava
128-
}
129-
130124
// Install downloads and installs the specified Java version
131125
func (j *JavaTool) Install(version string, cfg config.ToolConfig) error {
132126
distribution := cfg.Distribution
@@ -503,26 +497,18 @@ func (j *JavaTool) GetDisplayName() string {
503497
return "Java Development Kit"
504498
}
505499

506-
// GetEmoji returns the emoji icon for Java (implements ToolMetadataProvider)
507-
func (j *JavaTool) GetEmoji() string {
508-
return "📦"
509-
}
510-
511500
// SetupEnvironment sets up Java-specific environment variables (implements EnvironmentProvider)
512501
func (j *JavaTool) SetupEnvironment(version string, cfg config.ToolConfig, envManager *EnvironmentManager) error {
513-
binPath, err := j.GetPath(version, cfg)
514-
if err != nil {
515-
util.LogVerbose("Could not determine %s for %s %s: %v", EnvJavaHome, j.toolName, version, err)
516-
return nil
517-
}
518-
519-
if strings.HasSuffix(binPath, "/bin") {
520-
homeDir := strings.TrimSuffix(binPath, "/bin")
521-
envManager.SetEnv(EnvJavaHome, homeDir)
522-
util.LogVerbose("Set %s=%s for %s %s", EnvJavaHome, homeDir, j.toolName, version)
502+
// Convert EnvironmentManager to map for the existing helper
503+
envVars := envManager.ToMap()
504+
err := j.SetupHomeEnvironment(version, cfg, envVars, EnvJavaHome, j.GetPath)
505+
// Update the environment manager with any changes
506+
for key, value := range envVars {
507+
if key != "PATH" { // PATH is handled separately by EnvironmentManager
508+
envManager.SetEnv(key, value)
509+
}
523510
}
524-
525-
return nil
511+
return err
526512
}
527513

528514
// DiscoMajorVersion represents a major version entry from Disco API
@@ -954,11 +940,12 @@ func (j *JavaTool) getChecksumFromDiscoAPI(packageID string) (ChecksumInfo, erro
954940
}
955941

956942
// GetChecksum implements ChecksumProvider interface for Java
957-
func (j *JavaTool) GetChecksum(version, filename string) (ChecksumInfo, error) {
958-
// Java checksums are provided via configuration from the Java tool
959-
// which extracts them from the Foojay Disco API during URL resolution
960-
fmt.Printf(" 🔍 Checking for Java checksum from Disco API...\n")
961-
return ChecksumInfo{}, fmt.Errorf("Java checksums should be provided via configuration")
943+
func (j *JavaTool) GetChecksum(version string, cfg config.ToolConfig, filename string) (ChecksumInfo, error) {
944+
// Java checksums are handled during the installation process via getDownloadURLWithChecksum
945+
// and getChecksumFromDiscoAPI, which adds checksums to the configuration automatically.
946+
// This method is not used for Java since checksums are fetched during URL resolution.
947+
fmt.Printf(" ℹ️ Java checksums are handled during installation via Disco API\n")
948+
return ChecksumInfo{}, fmt.Errorf("Java checksums are provided via configuration during installation")
962949
}
963950

964951
// ValidateVersion validates that a Java version exists (implements VersionValidator)
@@ -1032,14 +1019,3 @@ func (j *JavaTool) GetDownloadURL(version string) string {
10321019
}
10331020
return url
10341021
}
1035-
1036-
// GetChecksumURL implements Tool interface for Java
1037-
func (j *JavaTool) GetChecksumURL(version, filename string) string {
1038-
// Java checksums are provided via Disco API package info
1039-
return ""
1040-
}
1041-
1042-
// GetVersionsURL implements Tool interface for Java
1043-
func (j *JavaTool) GetVersionsURL() string {
1044-
return FoojayDiscoAPIBase + "/major_versions?distribution=temurin&maintained=true"
1045-
}

0 commit comments

Comments
 (0)