Skip to content

Commit 2a0acd8

Browse files
committed
reintroduce module manager, again!
1 parent b732b1c commit 2a0acd8

File tree

5 files changed

+125
-11
lines changed

5 files changed

+125
-11
lines changed

internal/memex/commands.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/systemshift/memex/internal/memex/core"
1212
"github.com/systemshift/memex/internal/memex/migration"
13+
"github.com/systemshift/memex/internal/memex/modules"
1314
"github.com/systemshift/memex/internal/memex/repository"
1415
)
1516

@@ -34,7 +35,7 @@ func ModuleCommand(args ...string) error {
3435
switch cmd {
3536
case "list":
3637
// List installed modules
37-
modules := ListModules()
38+
modules := modules.ListModules()
3839
if len(modules) == 0 {
3940
fmt.Println("No modules installed")
4041
return nil
@@ -85,17 +86,9 @@ func ModuleCommand(args ...string) error {
8586
return fmt.Errorf("install-dev requires module path")
8687
}
8788
modulePath := args[1]
88-
fullPath := fmt.Sprintf("github.com/systemshift/%s", modulePath)
89-
90-
// Add require directive first
91-
cmd := exec.Command("go", "mod", "edit", "-require", fmt.Sprintf("%[email protected]", fullPath))
92-
cmd.Dir = "."
93-
if err := cmd.Run(); err != nil {
94-
return fmt.Errorf("adding require directive: %w", err)
95-
}
9689

97-
// Add replace directive
98-
cmd = exec.Command("go", "mod", "edit", "-replace", fmt.Sprintf("%s=./%s", fullPath, modulePath))
90+
// Add replace directive to use local module
91+
cmd := exec.Command("go", "mod", "edit", "-replace", fmt.Sprintf("github.com/systemshift/%s=./%s", modulePath, modulePath))
9992
cmd.Dir = "."
10093
if err := cmd.Run(); err != nil {
10194
return fmt.Errorf("adding replace directive: %w", err)

internal/memex/module/loader.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package module
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
)
7+
8+
// LoadModule loads a module from a path
9+
func LoadModule(modulePath string) error {
10+
// Add replace directive to use local module
11+
cmd := exec.Command("go", "mod", "edit", "-replace", fmt.Sprintf("github.com/systemshift/%s=./%s", modulePath, modulePath))
12+
cmd.Dir = "."
13+
if err := cmd.Run(); err != nil {
14+
return fmt.Errorf("adding replace directive: %w", err)
15+
}
16+
17+
// Run go mod tidy to download dependencies
18+
cmd = exec.Command("go", "mod", "tidy")
19+
cmd.Dir = "."
20+
if err := cmd.Run(); err != nil {
21+
return fmt.Errorf("tidying modules: %w", err)
22+
}
23+
24+
// The module's init() function will register it when imported
25+
return nil
26+
}

internal/memex/modules/registry.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package modules
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/systemshift/memex/internal/memex/core"
7+
)
8+
9+
// Registry manages registered modules
10+
type Registry struct {
11+
modules map[string]core.Module
12+
}
13+
14+
// NewRegistry creates a new registry
15+
func NewRegistry() *Registry {
16+
return &Registry{
17+
modules: make(map[string]core.Module),
18+
}
19+
}
20+
21+
// RegisterModule registers a module
22+
func (r *Registry) RegisterModule(m core.Module) error {
23+
if _, exists := r.modules[m.ID()]; exists {
24+
return fmt.Errorf("module already registered: %s", m.ID())
25+
}
26+
r.modules[m.ID()] = m
27+
return nil
28+
}
29+
30+
// GetModule returns a module by ID
31+
func (r *Registry) GetModule(id string) (core.Module, bool) {
32+
m, exists := r.modules[id]
33+
return m, exists
34+
}
35+
36+
// ListModules returns all registered modules
37+
func (r *Registry) ListModules() []core.Module {
38+
modules := make([]core.Module, 0, len(r.modules))
39+
for _, m := range r.modules {
40+
modules = append(modules, m)
41+
}
42+
return modules
43+
}
44+
45+
// Global registry instance
46+
var DefaultRegistry = NewRegistry()
47+
48+
// Helper functions that use the default registry
49+
50+
func RegisterModule(m core.Module) error {
51+
return DefaultRegistry.RegisterModule(m)
52+
}
53+
54+
func GetModule(id string) (core.Module, bool) {
55+
return DefaultRegistry.GetModule(id)
56+
}
57+
58+
func ListModules() []core.Module {
59+
return DefaultRegistry.ListModules()
60+
}

pkg/memex/registry.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Package memex provides the public API for memex
2+
package memex
3+
4+
import (
5+
"github.com/systemshift/memex/internal/memex/core"
6+
"github.com/systemshift/memex/internal/memex/modules"
7+
)
8+
9+
// Re-export core types
10+
type (
11+
Command = core.Command
12+
Module = core.Module
13+
Repository = core.Repository
14+
)
15+
16+
// RegisterModule registers a module with memex
17+
func RegisterModule(m Module) error {
18+
return modules.RegisterModule(m)
19+
}
20+
21+
// GetModule returns a module by ID
22+
func GetModule(id string) (Module, bool) {
23+
return modules.GetModule(id)
24+
}
25+
26+
// ListModules returns all registered modules
27+
func ListModules() []Module {
28+
return modules.ListModules()
29+
}

pkg/module/module.go

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

77
"github.com/systemshift/memex/internal/memex/core"
8+
"github.com/systemshift/memex/internal/memex/modules"
89
)
910

1011
// Re-export core types for module authors
@@ -16,6 +17,11 @@ type (
1617
Repository = core.Repository
1718
)
1819

20+
// Register registers a module with memex
21+
func Register(m Module) error {
22+
return modules.RegisterModule(m)
23+
}
24+
1925
// Base provides a base implementation of Module interface
2026
type Base struct {
2127
id string

0 commit comments

Comments
 (0)