-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuilder.go
More file actions
291 lines (244 loc) · 7.46 KB
/
builder.go
File metadata and controls
291 lines (244 loc) · 7.46 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package builder
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/rose-pine/rose-pine-bloom/color"
)
// Options contains configuration for building themes from templates.
type Options struct {
Template string
Output string
Prefix string
Format string
Plain bool
Commas bool
Spaces bool
}
// TemplateOptions contains configuration for creating templates from existing theme files.
type TemplateOptions struct {
Input string
Output string
Variant string
Prefix string
Format string
Plain bool
Commas bool
Spaces bool
}
var variantValueRegex = regexp.MustCompile(`\$\((.*?)\|(.*?)\|(.*?)\)`)
func BuildTemplate(cfg *TemplateOptions) error {
if err := os.MkdirAll(cfg.Output, 0755); err != nil {
return fmt.Errorf("failed to create output directory: %w", err)
}
return createTemplates(cfg)
}
func Build(cfg *Options) error {
if err := os.MkdirAll(cfg.Output, 0755); err != nil {
return fmt.Errorf("failed to create output directory: %w", err)
}
return generateThemes(cfg)
}
func generateThemes(cfg *Options) error {
templates, err := getTemplateFiles(cfg.Template)
if err != nil {
return err
}
for _, templatePath := range templates {
content, err := os.ReadFile(templatePath)
if err != nil {
return err
}
hasAccent := strings.Contains(string(content), cfg.Prefix+"accent")
for _, variant := range color.Variants {
if hasAccent {
for _, accent := range color.Accents {
if err := generateThemeFile(cfg, templatePath, content, variant, accent); err != nil {
return err
}
}
} else {
if err := generateThemeFile(cfg, templatePath, content, variant, ""); err != nil {
return err
}
}
}
}
return nil
}
func generateThemeFile(cfg *Options, templatePath string, content []byte, variant color.VariantMeta, accent string) error {
result := processTemplate(string(content), cfg, variant, accent)
if filepath.Ext(templatePath) == ".json" {
var prettyJSON bytes.Buffer
if err := json.Indent(&prettyJSON, []byte(result), "", " "); err != nil {
return err
}
result = prettyJSON.String()
}
outputPath := buildOutputPath(cfg, templatePath, variant, accent)
return writeFile(outputPath, []byte(result))
}
func createTemplates(cfg *TemplateOptions) error {
files, err := getTemplateFiles(cfg.Input)
if err != nil {
return err
}
variant := getVariant(cfg.Variant)
for _, file := range files {
content, err := os.ReadFile(file)
if err != nil {
return err
}
result := string(content)
matchFound := false
// Replace colors with variables
for colorName, c := range variant.Colors.Iter() {
colorValue := color.FormatColor(c, color.ColorFormat(cfg.Format), cfg.Plain, cfg.Commas, cfg.Spaces)
before := result
result = strings.ReplaceAll(result, colorValue, cfg.Prefix+colorName)
if before != result {
matchFound = true
}
}
// Replace metadata
result = strings.ReplaceAll(result, variant.Id, cfg.Prefix+"id")
result = strings.ReplaceAll(result, variant.Name, cfg.Prefix+"name")
result = strings.ReplaceAll(result, variant.Description, cfg.Prefix+"description")
if !matchFound {
fmt.Printf("\033[33mNo matches for specified format (%s). Available formats:\n\033[0m", cfg.Format)
table, err := color.FormatsTable()
if err != nil {
return fmt.Errorf("failed to get formats table: %w", err)
}
fmt.Println(table)
}
outputFile := "template" + filepath.Ext(file)
if ext := filepath.Ext(file); ext != "" {
outputFile = "template" + ext
}
outputPath := filepath.Join(cfg.Output, outputFile)
if err := writeFile(outputPath, []byte(result)); err != nil {
return err
}
}
return nil
}
func processTemplate(content string, cfg *Options, variant color.VariantMeta, accent string) string {
result := content
// Replace metadata
result = strings.ReplaceAll(result, cfg.Prefix+"id", variant.Id)
result = strings.ReplaceAll(result, cfg.Prefix+"name", variant.Name)
result = strings.ReplaceAll(result, cfg.Prefix+"type", variant.Appearance)
result = strings.ReplaceAll(result, cfg.Prefix+"appearance", variant.Appearance)
result = strings.ReplaceAll(result, cfg.Prefix+"description", variant.Description)
// Replace accent variables
if accent != "" {
result = strings.ReplaceAll(result, cfg.Prefix+"accentname", accent)
result = strings.ReplaceAll(result, cfg.Prefix+"accent", cfg.Prefix+accent)
if color, ok := variant.Colors.Get(accent); ok && color.On != "" {
result = strings.ReplaceAll(result, cfg.Prefix+"onaccent", cfg.Prefix+color.On)
}
}
// Replace colors and handle alpha variants
for colorName, c := range variant.Colors.Iter() {
varName := cfg.Prefix + colorName
// Handle alpha variants (e.g. $base/50)
alphaRegex := regexp.MustCompile(regexp.QuoteMeta(varName) + `/(\d+)`)
result = alphaRegex.ReplaceAllStringFunc(result, func(match string) string {
alpha, _ := strconv.ParseFloat(alphaRegex.FindStringSubmatch(match)[1], 64)
colorCopy := *c
normalizedAlpha := alpha / 100
colorCopy.Alpha = &normalizedAlpha
return color.FormatColor(&colorCopy, color.ColorFormat(cfg.Format), cfg.Plain, cfg.Commas, cfg.Spaces)
})
// Replace regular color variable
result = strings.ReplaceAll(result, varName, color.FormatColor(c, color.ColorFormat(cfg.Format), cfg.Plain, cfg.Commas, cfg.Spaces))
}
// Process variant-specific values $(main|moon|dawn)
result = variantValueRegex.ReplaceAllStringFunc(result, func(match string) string {
parts := variantValueRegex.FindStringSubmatch(match)
if len(parts) != 4 {
return match
}
switch variant.Id {
case "rose-pine":
return parts[1]
case "rose-pine-moon":
return parts[2]
case "rose-pine-dawn":
return parts[3]
default:
return match
}
})
return result
}
func getVariant(create string) color.VariantMeta {
switch create {
case "main":
return color.MainVariantMeta
case "moon":
return color.MoonVariantMeta
case "dawn":
return color.DawnVariantMeta
default:
return color.MainVariantMeta
}
}
func getTemplateFiles(templatePath string) ([]string, error) {
var files []string
info, err := os.Stat(templatePath)
if err != nil {
return nil, err
}
if info.IsDir() {
err := filepath.Walk(templatePath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
files = append(files, path)
}
return nil
})
if err != nil {
return nil, err
}
} else {
files = append(files, templatePath)
}
return files, nil
}
func writeFile(outputPath string, content []byte) error {
if err := os.MkdirAll(filepath.Dir(outputPath), 0755); err != nil {
return err
}
return os.WriteFile(outputPath, content, 0644)
}
func buildOutputPath(cfg *Options, templatePath string, variant color.VariantMeta, accent string) string {
ext := filepath.Ext(templatePath)
var outputFile, outputDir string
if accent != "" {
outputDir = filepath.Join(cfg.Output, variant.Id)
outputFile = variant.Id + "-" + accent + ext
} else {
outputDir = cfg.Output
outputFile = variant.Id + ext
}
// Handle directory templates
if templateInfo, err := os.Stat(cfg.Template); err == nil && templateInfo.IsDir() {
dir, _ := strings.CutPrefix(filepath.Dir(templatePath), filepath.Clean(cfg.Template))
if accent != "" {
outputDir = filepath.Join(cfg.Output, accent, variant.Id) + dir
} else {
outputDir = filepath.Join(cfg.Output, variant.Id) + dir
}
outputFile = filepath.Base(templatePath)
}
return filepath.Join(outputDir, outputFile)
}