-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.go
108 lines (90 loc) · 2.88 KB
/
builder.go
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)
func Build(cfg *Config) error {
if err := os.MkdirAll(cfg.Output, 0755); err != nil {
return fmt.Errorf("failed to create output directory: %w", err)
}
if err := generateVariants(cfg); err != nil {
return fmt.Errorf("failed to generate variants: %w", err)
}
return nil
}
func generateVariants(cfg *Config) error {
templateContent, err := os.ReadFile(cfg.Template)
if err != nil {
return fmt.Errorf("failed to read template: %w", err)
}
variants := []struct {
id, name, variantType string
colors Variant
}{
{"rose-pine", "Rosé Pine", "dark", MainVariant},
{"rose-pine-moon", "Rosé Pine Moon", "dark", MoonVariant},
{"rose-pine-dawn", "Rosé Pine Dawn", "light", DawnVariant},
}
for _, v := range variants {
if err := processVariant(cfg, templateContent, v); err != nil {
return fmt.Errorf("failed to process %s: %w", v.id, err)
}
}
return nil
}
var variantValueRegex = regexp.MustCompile(`\$\((.*?)\|(.*?)\|(.*?)\)`)
func processVariant(cfg *Config, templateContent []byte, v struct {
id, name, variantType string
colors Variant
}) error {
result := string(templateContent)
result = strings.ReplaceAll(result, cfg.Prefix+"id", v.id)
result = strings.ReplaceAll(result, cfg.Prefix+"name", v.name)
result = strings.ReplaceAll(result, cfg.Prefix+"type", v.variantType)
result = strings.ReplaceAll(result, cfg.Prefix+"description",
"All natural pine, faux fur and a bit of soho vibes for the classy minimalist")
for colorName, color := range v.colors.Colors {
varName := cfg.Prefix + colorName
alphaRegex := regexp.MustCompile(regexp.QuoteMeta(varName) + `/(\d+)`)
result = alphaRegex.ReplaceAllStringFunc(result, func(match string) string {
alpha, _ := strconv.ParseFloat(alphaRegex.FindStringSubmatch(match)[1], 64)
colorCopy := *color
normalizedAlpha := alpha / 100
colorCopy.Alpha = &normalizedAlpha
return formatColor(&colorCopy, ColorFormat(cfg.Format), cfg.StripSpaces)
})
result = strings.ReplaceAll(result, varName, formatColor(color, ColorFormat(cfg.Format), cfg.StripSpaces))
}
result = variantValueRegex.ReplaceAllStringFunc(result, func(match string) string {
parts := variantValueRegex.FindStringSubmatch(match)
if len(parts) != 4 {
return match
}
switch v.id {
case "rose-pine":
return parts[1]
case "rose-pine-moon":
return parts[2]
case "rose-pine-dawn":
return parts[3]
default:
return match
}
})
ext := filepath.Ext(cfg.Template)
if ext == ".json" {
var prettyJSON bytes.Buffer
if err := json.Indent(&prettyJSON, []byte(result), "", " "); err != nil {
return fmt.Errorf("failed to format JSON: %w", err)
}
result = prettyJSON.String()
}
outputPath := filepath.Join(cfg.Output, v.id+ext)
return os.WriteFile(outputPath, []byte(result), 0644)
}