|
| 1 | +// Package workflow provides a ScriptRegistry for managing JavaScript script bundling. |
| 2 | +// |
| 3 | +// # Script Registry Pattern |
| 4 | +// |
| 5 | +// The ScriptRegistry eliminates the repetitive sync.Once pattern found throughout |
| 6 | +// the codebase for lazy script bundling. Instead of declaring separate variables |
| 7 | +// and getter functions for each script, register scripts once and retrieve them |
| 8 | +// by name. |
| 9 | +// |
| 10 | +// # Before (repetitive pattern): |
| 11 | +// |
| 12 | +// var ( |
| 13 | +// createIssueScript string |
| 14 | +// createIssueScriptOnce sync.Once |
| 15 | +// ) |
| 16 | +// |
| 17 | +// func getCreateIssueScript() string { |
| 18 | +// createIssueScriptOnce.Do(func() { |
| 19 | +// sources := GetJavaScriptSources() |
| 20 | +// bundled, err := BundleJavaScriptFromSources(createIssueScriptSource, sources, "") |
| 21 | +// if err != nil { |
| 22 | +// createIssueScript = createIssueScriptSource |
| 23 | +// } else { |
| 24 | +// createIssueScript = bundled |
| 25 | +// } |
| 26 | +// }) |
| 27 | +// return createIssueScript |
| 28 | +// } |
| 29 | +// |
| 30 | +// # After (using registry): |
| 31 | +// |
| 32 | +// // Registration at package init |
| 33 | +// DefaultScriptRegistry.Register("create_issue", createIssueScriptSource) |
| 34 | +// |
| 35 | +// // Usage anywhere |
| 36 | +// script := DefaultScriptRegistry.Get("create_issue") |
| 37 | +// |
| 38 | +// # Benefits |
| 39 | +// |
| 40 | +// - Eliminates ~15 lines of boilerplate per script (variable pair + getter function) |
| 41 | +// - Centralizes bundling logic |
| 42 | +// - Consistent error handling |
| 43 | +// - Thread-safe lazy initialization |
| 44 | +// - Easy to add new scripts |
| 45 | +package workflow |
| 46 | + |
| 47 | +import ( |
| 48 | + "sync" |
| 49 | + |
| 50 | + "github.com/githubnext/gh-aw/pkg/logger" |
| 51 | +) |
| 52 | + |
| 53 | +var registryLog = logger.New("workflow:script_registry") |
| 54 | + |
| 55 | +// scriptEntry holds the source and bundled versions of a script |
| 56 | +type scriptEntry struct { |
| 57 | + source string |
| 58 | + bundled string |
| 59 | + once sync.Once |
| 60 | +} |
| 61 | + |
| 62 | +// ScriptRegistry manages lazy bundling of JavaScript scripts. |
| 63 | +// It provides a centralized place to register source scripts and retrieve |
| 64 | +// bundled versions on-demand with caching. |
| 65 | +// |
| 66 | +// Thread-safe: All operations use internal synchronization. |
| 67 | +// |
| 68 | +// Usage: |
| 69 | +// |
| 70 | +// registry := NewScriptRegistry() |
| 71 | +// registry.Register("my_script", myScriptSource) |
| 72 | +// bundled := registry.Get("my_script") |
| 73 | +type ScriptRegistry struct { |
| 74 | + mu sync.RWMutex |
| 75 | + scripts map[string]*scriptEntry |
| 76 | +} |
| 77 | + |
| 78 | +// NewScriptRegistry creates a new empty script registry. |
| 79 | +func NewScriptRegistry() *ScriptRegistry { |
| 80 | + return &ScriptRegistry{ |
| 81 | + scripts: make(map[string]*scriptEntry), |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +// Register adds a script source to the registry. |
| 86 | +// The script will be bundled lazily on first access via Get(). |
| 87 | +// |
| 88 | +// Parameters: |
| 89 | +// - name: Unique identifier for the script (e.g., "create_issue", "add_comment") |
| 90 | +// - source: The raw JavaScript source code (typically from go:embed) |
| 91 | +// |
| 92 | +// If a script with the same name already exists, it will be overwritten. |
| 93 | +// This is useful for testing but should be avoided in production. |
| 94 | +func (r *ScriptRegistry) Register(name string, source string) { |
| 95 | + r.mu.Lock() |
| 96 | + defer r.mu.Unlock() |
| 97 | + |
| 98 | + if registryLog.Enabled() { |
| 99 | + registryLog.Printf("Registering script: %s (%d bytes)", name, len(source)) |
| 100 | + } |
| 101 | + |
| 102 | + r.scripts[name] = &scriptEntry{ |
| 103 | + source: source, |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// Get retrieves a bundled script by name. |
| 108 | +// Bundling is performed lazily on first access and cached for subsequent calls. |
| 109 | +// |
| 110 | +// If bundling fails, the original source is returned as a fallback. |
| 111 | +// If the script is not registered, an empty string is returned. |
| 112 | +// |
| 113 | +// Thread-safe: Multiple goroutines can call Get concurrently. |
| 114 | +func (r *ScriptRegistry) Get(name string) string { |
| 115 | + r.mu.RLock() |
| 116 | + entry, exists := r.scripts[name] |
| 117 | + r.mu.RUnlock() |
| 118 | + |
| 119 | + if !exists { |
| 120 | + if registryLog.Enabled() { |
| 121 | + registryLog.Printf("Script not found: %s", name) |
| 122 | + } |
| 123 | + return "" |
| 124 | + } |
| 125 | + |
| 126 | + entry.once.Do(func() { |
| 127 | + if registryLog.Enabled() { |
| 128 | + registryLog.Printf("Bundling script: %s", name) |
| 129 | + } |
| 130 | + |
| 131 | + sources := GetJavaScriptSources() |
| 132 | + bundled, err := BundleJavaScriptFromSources(entry.source, sources, "") |
| 133 | + if err != nil { |
| 134 | + registryLog.Printf("Bundling failed for %s, using source as-is: %v", name, err) |
| 135 | + entry.bundled = entry.source |
| 136 | + } else { |
| 137 | + if registryLog.Enabled() { |
| 138 | + registryLog.Printf("Successfully bundled %s: %d bytes", name, len(bundled)) |
| 139 | + } |
| 140 | + entry.bundled = bundled |
| 141 | + } |
| 142 | + }) |
| 143 | + |
| 144 | + return entry.bundled |
| 145 | +} |
| 146 | + |
| 147 | +// GetSource retrieves the original (unbundled) source for a script. |
| 148 | +// Useful for testing or when bundling is not needed. |
| 149 | +func (r *ScriptRegistry) GetSource(name string) string { |
| 150 | + r.mu.RLock() |
| 151 | + defer r.mu.RUnlock() |
| 152 | + |
| 153 | + entry, exists := r.scripts[name] |
| 154 | + if !exists { |
| 155 | + return "" |
| 156 | + } |
| 157 | + return entry.source |
| 158 | +} |
| 159 | + |
| 160 | +// Has checks if a script is registered in the registry. |
| 161 | +func (r *ScriptRegistry) Has(name string) bool { |
| 162 | + r.mu.RLock() |
| 163 | + defer r.mu.RUnlock() |
| 164 | + |
| 165 | + _, exists := r.scripts[name] |
| 166 | + return exists |
| 167 | +} |
| 168 | + |
| 169 | +// Names returns a list of all registered script names. |
| 170 | +// Useful for debugging and testing. |
| 171 | +func (r *ScriptRegistry) Names() []string { |
| 172 | + r.mu.RLock() |
| 173 | + defer r.mu.RUnlock() |
| 174 | + |
| 175 | + names := make([]string, 0, len(r.scripts)) |
| 176 | + for name := range r.scripts { |
| 177 | + names = append(names, name) |
| 178 | + } |
| 179 | + return names |
| 180 | +} |
| 181 | + |
| 182 | +// DefaultScriptRegistry is the global script registry used by the workflow package. |
| 183 | +// Scripts are registered during package initialization via init() functions. |
| 184 | +var DefaultScriptRegistry = NewScriptRegistry() |
| 185 | + |
| 186 | +// GetScript retrieves a bundled script from the default registry. |
| 187 | +// This is a convenience function equivalent to DefaultScriptRegistry.Get(name). |
| 188 | +func GetScript(name string) string { |
| 189 | + return DefaultScriptRegistry.Get(name) |
| 190 | +} |
0 commit comments