-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.go
More file actions
159 lines (143 loc) · 4.88 KB
/
generator.go
File metadata and controls
159 lines (143 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package sdk
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/GoCodeAlone/workflow/dynamic"
"github.com/GoCodeAlone/workflow/plugin"
)
// TemplateGenerator scaffolds new plugin projects with a manifest and component skeleton.
type TemplateGenerator struct{}
// NewTemplateGenerator creates a new TemplateGenerator.
func NewTemplateGenerator() *TemplateGenerator {
return &TemplateGenerator{}
}
// GenerateOptions configures what gets generated.
type GenerateOptions struct {
Name string
Version string
Author string
Description string
License string
OutputDir string
WithContract bool
}
// Generate creates a new plugin directory with manifest and component skeleton.
func (g *TemplateGenerator) Generate(opts GenerateOptions) error {
if opts.Name == "" {
return fmt.Errorf("plugin name is required")
}
if opts.Version == "" {
opts.Version = "0.1.0"
}
if opts.Author == "" {
return fmt.Errorf("author is required")
}
if opts.Description == "" {
opts.Description = "A workflow plugin"
}
if opts.OutputDir == "" {
opts.OutputDir = opts.Name
}
// Validate the name
manifest := &plugin.PluginManifest{
Name: opts.Name,
Version: opts.Version,
Author: opts.Author,
Description: opts.Description,
License: opts.License,
}
if opts.WithContract {
manifest.Contract = dynamic.NewFieldContract()
manifest.Contract.RequiredInputs["input"] = dynamic.FieldSpec{
Type: dynamic.FieldTypeString,
Description: "Example input field",
}
manifest.Contract.Outputs["output"] = dynamic.FieldSpec{
Type: dynamic.FieldTypeString,
Description: "Example output field",
}
}
if err := manifest.Validate(); err != nil {
return fmt.Errorf("generated manifest is invalid: %w", err)
}
// Create output directory
if err := os.MkdirAll(opts.OutputDir, 0750); err != nil {
return fmt.Errorf("create output directory: %w", err)
}
// Write manifest
manifestPath := filepath.Join(opts.OutputDir, "plugin.json")
if err := plugin.SaveManifest(manifestPath, manifest); err != nil {
return fmt.Errorf("write manifest: %w", err)
}
// Write component skeleton
componentPath := filepath.Join(opts.OutputDir, opts.Name+".go")
source := generateComponentSource(opts)
if err := os.WriteFile(componentPath, []byte(source), 0600); err != nil {
return fmt.Errorf("write component: %w", err)
}
return nil
}
func generateComponentSource(opts GenerateOptions) string {
funcName := toCamelCase(opts.Name)
var b strings.Builder
b.WriteString("package component\n\n")
b.WriteString("import (\n")
b.WriteString("\t\"context\"\n")
b.WriteString(")\n\n")
fmt.Fprintf(&b, "// Name returns the name of the %s plugin.\n", opts.Name)
fmt.Fprintf(&b, "func Name() string { return %q }\n\n", opts.Name)
fmt.Fprintf(&b, "// Init initializes the %s plugin.\n", funcName)
b.WriteString("func Init(services map[string]interface{}) error {\n")
b.WriteString("\treturn nil\n")
b.WriteString("}\n\n")
fmt.Fprintf(&b, "// Start starts the %s plugin.\n", funcName)
b.WriteString("func Start(ctx context.Context) error {\n")
b.WriteString("\treturn nil\n")
b.WriteString("}\n\n")
fmt.Fprintf(&b, "// Stop stops the %s plugin.\n", funcName)
b.WriteString("func Stop(ctx context.Context) error {\n")
b.WriteString("\treturn nil\n")
b.WriteString("}\n\n")
fmt.Fprintf(&b, "// Execute runs the %s plugin logic.\n", funcName)
b.WriteString("func Execute(ctx context.Context, params map[string]interface{}) (map[string]interface{}, error) {\n")
b.WriteString("\tresult := map[string]interface{}{\n")
b.WriteString("\t\t\"status\": \"ok\",\n")
b.WriteString("\t}\n")
b.WriteString("\treturn result, nil\n")
b.WriteString("}\n")
if opts.WithContract {
b.WriteString("\n// Contract declares the input/output contract for this plugin.\n")
b.WriteString("func Contract() map[string]interface{} {\n")
b.WriteString("\treturn map[string]interface{}{\n")
b.WriteString("\t\t\"required_inputs\": map[string]interface{}{\n")
b.WriteString("\t\t\t\"input\": map[string]interface{}{\n")
b.WriteString("\t\t\t\t\"type\": \"string\",\n")
b.WriteString("\t\t\t\t\"description\": \"Example input field\",\n")
b.WriteString("\t\t\t},\n")
b.WriteString("\t\t},\n")
b.WriteString("\t\t\"outputs\": map[string]interface{}{\n")
b.WriteString("\t\t\t\"output\": map[string]interface{}{\n")
b.WriteString("\t\t\t\t\"type\": \"string\",\n")
b.WriteString("\t\t\t\t\"description\": \"Example output field\",\n")
b.WriteString("\t\t\t},\n")
b.WriteString("\t\t},\n")
b.WriteString("\t}\n")
b.WriteString("}\n")
}
return b.String()
}
// toCamelCase converts a hyphenated name like "my-plugin" to "MyPlugin".
func toCamelCase(s string) string {
parts := strings.Split(s, "-")
var b strings.Builder
for _, p := range parts {
if p == "" {
continue
}
b.WriteString(strings.ToUpper(p[:1]))
b.WriteString(p[1:])
}
return b.String()
}