Skip to content

Commit 6c8ee88

Browse files
authored
refactor: move everything to packages (#71)
1 parent 1746719 commit 6c8ee88

File tree

9 files changed

+251
-242
lines changed

9 files changed

+251
-242
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ docs:
1313
test:
1414
go test ./...
1515

16+
.PHONY: fmt
17+
fmt:
18+
go fmt ./...
19+

builder.go renamed to builder/builder.go

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package builder
22

33
import (
44
"bytes"
@@ -9,11 +9,14 @@ import (
99
"regexp"
1010
"strconv"
1111
"strings"
12+
13+
"github.com/rose-pine/rose-pine-bloom/color"
14+
"github.com/rose-pine/rose-pine-bloom/config"
1215
)
1316

1417
var variantValueRegex = regexp.MustCompile(`\$\((.*?)\|(.*?)\|(.*?)\)`)
1518

16-
func Build(cfg *Config) error {
19+
func Build(cfg *config.Config) error {
1720
if err := os.MkdirAll(cfg.Output, 0755); err != nil {
1821
return fmt.Errorf("failed to create output directory: %w", err)
1922
}
@@ -25,7 +28,7 @@ func Build(cfg *Config) error {
2528
return generateThemes(cfg)
2629
}
2730

28-
func generateThemes(cfg *Config) error {
31+
func generateThemes(cfg *config.Config) error {
2932
templates, err := getTemplateFiles(cfg.Template)
3033
if err != nil {
3134
return err
@@ -39,9 +42,9 @@ func generateThemes(cfg *Config) error {
3942

4043
hasAccent := strings.Contains(string(content), cfg.Prefix+"accent")
4144

42-
for _, variant := range variants {
45+
for _, variant := range color.Variants {
4346
if hasAccent {
44-
for _, accent := range accents {
47+
for _, accent := range color.Accents {
4548
if err := generateThemeFile(cfg, templatePath, content, variant, accent); err != nil {
4649
return err
4750
}
@@ -57,7 +60,7 @@ func generateThemes(cfg *Config) error {
5760
return nil
5861
}
5962

60-
func generateThemeFile(cfg *Config, templatePath string, content []byte, variant VariantMeta, accent string) error {
63+
func generateThemeFile(cfg *config.Config, templatePath string, content []byte, variant color.VariantMeta, accent string) error {
6164
result := processTemplate(string(content), cfg, variant, accent)
6265

6366
if filepath.Ext(templatePath) == ".json" {
@@ -72,7 +75,7 @@ func generateThemeFile(cfg *Config, templatePath string, content []byte, variant
7275
return writeFile(outputPath, []byte(result))
7376
}
7477

75-
func createTemplates(cfg *Config) error {
78+
func createTemplates(cfg *config.Config) error {
7679
files, err := getTemplateFiles(cfg.Template)
7780
if err != nil {
7881
return err
@@ -90,8 +93,8 @@ func createTemplates(cfg *Config) error {
9093
matchFound := false
9194

9295
// Replace colors with variables
93-
for colorName, color := range variant.Colors.Iter() {
94-
colorValue := formatColor(color, ColorFormat(cfg.Format), cfg.Plain, cfg.Commas, cfg.Spaces)
96+
for colorName, c := range variant.Colors.Iter() {
97+
colorValue := color.FormatColor(c, color.ColorFormat(cfg.Format), cfg.Plain, cfg.Commas, cfg.Spaces)
9598
before := result
9699
result = strings.ReplaceAll(result, colorValue, cfg.Prefix+colorName)
97100
if before != result {
@@ -106,7 +109,7 @@ func createTemplates(cfg *Config) error {
106109

107110
if !matchFound {
108111
fmt.Printf("\033[33mNo matches for specified format (%s). Available formats:\n\033[0m", cfg.Format)
109-
printFormatsTable()
112+
color.PrintFormatsTable()
110113
}
111114

112115
outputFile := "template" + filepath.Ext(file)
@@ -123,7 +126,7 @@ func createTemplates(cfg *Config) error {
123126
return nil
124127
}
125128

126-
func processTemplate(content string, cfg *Config, variant VariantMeta, accent string) string {
129+
func processTemplate(content string, cfg *config.Config, variant color.VariantMeta, accent string) string {
127130
result := content
128131

129132
// Replace metadata
@@ -144,21 +147,21 @@ func processTemplate(content string, cfg *Config, variant VariantMeta, accent st
144147
}
145148

146149
// Replace colors and handle alpha variants
147-
for colorName, color := range variant.Colors.Iter() {
150+
for colorName, c := range variant.Colors.Iter() {
148151
varName := cfg.Prefix + colorName
149152

150153
// Handle alpha variants (e.g. $base/50)
151154
alphaRegex := regexp.MustCompile(regexp.QuoteMeta(varName) + `/(\d+)`)
152155
result = alphaRegex.ReplaceAllStringFunc(result, func(match string) string {
153156
alpha, _ := strconv.ParseFloat(alphaRegex.FindStringSubmatch(match)[1], 64)
154-
colorCopy := *color
157+
colorCopy := *c
155158
normalizedAlpha := alpha / 100
156159
colorCopy.Alpha = &normalizedAlpha
157-
return formatColor(&colorCopy, ColorFormat(cfg.Format), cfg.Plain, cfg.Commas, cfg.Spaces)
160+
return color.FormatColor(&colorCopy, color.ColorFormat(cfg.Format), cfg.Plain, cfg.Commas, cfg.Spaces)
158161
})
159162

160163
// Replace regular color variable
161-
result = strings.ReplaceAll(result, varName, formatColor(color, ColorFormat(cfg.Format), cfg.Plain, cfg.Commas, cfg.Spaces))
164+
result = strings.ReplaceAll(result, varName, color.FormatColor(c, color.ColorFormat(cfg.Format), cfg.Plain, cfg.Commas, cfg.Spaces))
162165
}
163166

164167
// Process variant-specific values $(main|moon|dawn)
@@ -183,16 +186,16 @@ func processTemplate(content string, cfg *Config, variant VariantMeta, accent st
183186
return result
184187
}
185188

186-
func getVariant(create string) VariantMeta {
189+
func getVariant(create string) color.VariantMeta {
187190
switch create {
188191
case "main":
189-
return MainVariantMeta
192+
return color.MainVariantMeta
190193
case "moon":
191-
return MoonVariantMeta
194+
return color.MoonVariantMeta
192195
case "dawn":
193-
return DawnVariantMeta
196+
return color.DawnVariantMeta
194197
default:
195-
return MainVariantMeta
198+
return color.MainVariantMeta
196199
}
197200
}
198201

@@ -231,7 +234,7 @@ func writeFile(outputPath string, content []byte) error {
231234
return os.WriteFile(outputPath, content, 0644)
232235
}
233236

234-
func buildOutputPath(cfg *Config, templatePath string, variant VariantMeta, accent string) string {
237+
func buildOutputPath(cfg *config.Config, templatePath string, variant color.VariantMeta, accent string) string {
235238
ext := filepath.Ext(templatePath)
236239
var outputFile, outputDir string
237240

builder_test.go renamed to builder/builder_test.go

Lines changed: 63 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
package main
1+
package builder
22

33
import (
44
"encoding/json"
55
"os"
66
"path/filepath"
77
"testing"
8+
9+
"github.com/rose-pine/rose-pine-bloom/color"
10+
"github.com/rose-pine/rose-pine-bloom/config"
811
)
912

1013
func setupTest(t *testing.T) (string, func()) {
@@ -15,7 +18,7 @@ func setupTest(t *testing.T) (string, func()) {
1518
return tmpDir, func() { os.RemoveAll(tmpDir) }
1619
}
1720

18-
func buildFromTemplate(t *testing.T, template string, cfg *Config) {
21+
func buildFromTemplate(t *testing.T, template string, cfg *config.Config) {
1922
templatePath := filepath.Join(cfg.Output, "template.json")
2023
if err := os.WriteFile(templatePath, []byte(template), 0644); err != nil {
2124
t.Fatal(err)
@@ -49,7 +52,7 @@ func assertJSONField(t *testing.T, result map[string]any, field, want string) {
4952
}
5053

5154
// testConfig provides standard config
52-
var testConfig = Config{
55+
var testConfig = config.Config{
5356
Template: "",
5457
Output: "",
5558
Prefix: "$",
@@ -60,9 +63,9 @@ var testConfig = Config{
6063
}
6164

6265
// testColor provides a standard color
63-
var testColor = &Color{
64-
HSL: hsl(2, 55, 83),
65-
RGB: rgb(235, 188, 186),
66+
var testColor = &color.Color{
67+
HSL: color.HSL{2, 55, 83},
68+
RGB: color.RGB{235, 188, 186},
6669
}
6770

6871
// testTemplate provides a standard template
@@ -118,51 +121,51 @@ func TestColorFormatting(t *testing.T) {
118121

119122
tests := []struct {
120123
name string
121-
format ColorFormat
124+
format color.ColorFormat
122125
plain bool
123126
commas bool
124127
spaces bool
125128
want string
126129
}{
127-
{"hex", FormatHex, false, true, true, "#ebbcba"},
128-
{"hex plain", FormatHex, true, true, true, "ebbcba"},
129-
130-
{"hsl", FormatHSL, false, true, true, "hsl(2, 55%, 83%)"},
131-
{"hsl no-commas", FormatHSL, false, false, true, "hsl(2 55% 83%)"},
132-
{"hsl no-spaces", FormatHSL, false, true, false, "hsl(2,55%,83%)"},
133-
{"hsl plain", FormatHSL, true, true, true, "2, 55%, 83%"},
134-
{"hsl plain no-commas", FormatHSL, true, false, true, "2 55% 83%"},
135-
{"hsl plain no-spaces", FormatHSL, true, true, false, "2,55%,83%"},
136-
137-
{"hsl-array", FormatHSLArray, false, true, true, "[2, 0.55, 0.83]"},
138-
{"hsl-array plain", FormatHSLArray, true, true, true, "2, 0.55, 0.83"},
139-
{"hsl-array no-commas", FormatHSLArray, false, false, true, "[2 0.55 0.83]"},
140-
{"hsl-array no-spaces", FormatHSLArray, false, true, false, "[2,0.55,0.83]"},
141-
142-
{"hsl-css", FormatHSLCSS, false, true, true, "hsl(2deg 55% 83%)"},
143-
{"hsl-css plain", FormatHSLCSS, true, true, true, "2deg 55% 83%"},
144-
145-
{"rgb", FormatRGB, false, true, true, "rgb(235, 188, 186)"},
146-
{"rgb no-commas", FormatRGB, false, false, true, "rgb(235 188 186)"},
147-
{"rgb no-spaces", FormatRGB, false, true, false, "rgb(235,188,186)"},
148-
{"rgb plain", FormatRGB, true, true, true, "235, 188, 186"},
149-
{"rgb plain no-commas", FormatRGB, true, false, true, "235 188 186"},
150-
{"rgb plain no-spaces", FormatRGB, true, true, false, "235,188,186"},
151-
152-
{"rgb-array", FormatRGBArray, false, true, true, "[235, 188, 186]"},
153-
{"rgb-array plain", FormatRGBArray, true, true, true, "235, 188, 186"},
154-
{"rgb-array no-commas", FormatRGBArray, false, false, true, "[235 188 186]"},
155-
{"rgb-array no-spaces", FormatRGBArray, false, true, false, "[235,188,186]"},
156-
157-
{"rgb-css", FormatRGBCSS, false, true, true, "rgb(235 188 186)"},
158-
{"rgb-css plain", FormatRGBCSS, true, true, true, "235 188 186"},
159-
160-
{"ansi", FormatAnsi, false, true, true, "235;188;186"},
130+
{"hex", color.FormatHex, false, true, true, "#ebbcba"},
131+
{"hex plain", color.FormatHex, true, true, true, "ebbcba"},
132+
133+
{"hsl", color.FormatHSL, false, true, true, "hsl(2, 55%, 83%)"},
134+
{"hsl no-commas", color.FormatHSL, false, false, true, "hsl(2 55% 83%)"},
135+
{"hsl no-spaces", color.FormatHSL, false, true, false, "hsl(2,55%,83%)"},
136+
{"hsl plain", color.FormatHSL, true, true, true, "2, 55%, 83%"},
137+
{"hsl plain no-commas", color.FormatHSL, true, false, true, "2 55% 83%"},
138+
{"hsl plain no-spaces", color.FormatHSL, true, true, false, "2,55%,83%"},
139+
140+
{"hsl-array", color.FormatHSLArray, false, true, true, "[2, 0.55, 0.83]"},
141+
{"hsl-array plain", color.FormatHSLArray, true, true, true, "2, 0.55, 0.83"},
142+
{"hsl-array no-commas", color.FormatHSLArray, false, false, true, "[2 0.55 0.83]"},
143+
{"hsl-array no-spaces", color.FormatHSLArray, false, true, false, "[2,0.55,0.83]"},
144+
145+
{"hsl-css", color.FormatHSLCSS, false, true, true, "hsl(2deg 55% 83%)"},
146+
{"hsl-css plain", color.FormatHSLCSS, true, true, true, "2deg 55% 83%"},
147+
148+
{"rgb", color.FormatRGB, false, true, true, "rgb(235, 188, 186)"},
149+
{"rgb no-commas", color.FormatRGB, false, false, true, "rgb(235 188 186)"},
150+
{"rgb no-spaces", color.FormatRGB, false, true, false, "rgb(235,188,186)"},
151+
{"rgb plain", color.FormatRGB, true, true, true, "235, 188, 186"},
152+
{"rgb plain no-commas", color.FormatRGB, true, false, true, "235 188 186"},
153+
{"rgb plain no-spaces", color.FormatRGB, true, true, false, "235,188,186"},
154+
155+
{"rgb-array", color.FormatRGBArray, false, true, true, "[235, 188, 186]"},
156+
{"rgb-array plain", color.FormatRGBArray, true, true, true, "235, 188, 186"},
157+
{"rgb-array no-commas", color.FormatRGBArray, false, false, true, "[235 188 186]"},
158+
{"rgb-array no-spaces", color.FormatRGBArray, false, true, false, "[235,188,186]"},
159+
160+
{"rgb-css", color.FormatRGBCSS, false, true, true, "rgb(235 188 186)"},
161+
{"rgb-css plain", color.FormatRGBCSS, true, true, true, "235 188 186"},
162+
163+
{"ansi", color.FormatAnsi, false, true, true, "235;188;186"},
161164
}
162165

163166
for _, tt := range tests {
164167
t.Run(tt.name, func(t *testing.T) {
165-
got := formatColor(testColor, tt.format, tt.plain, tt.commas, tt.spaces)
168+
got := color.FormatColor(testColor, tt.format, tt.plain, tt.commas, tt.spaces)
166169
if got != tt.want {
167170
t.Errorf("formatColor() = %v, want %v", got, tt.want)
168171
}
@@ -172,45 +175,45 @@ func TestColorFormatting(t *testing.T) {
172175

173176
func TestAlphaFormatting(t *testing.T) {
174177
alpha := 0.5
175-
color := &Color{
178+
c := &color.Color{
176179
HSL: testColor.HSL,
177180
RGB: testColor.RGB,
178181
Alpha: &alpha,
179182
}
180183

181184
tests := []struct {
182185
name string
183-
format ColorFormat
186+
format color.ColorFormat
184187
plain bool
185188
want string
186189
}{
187-
{"hex", FormatHex, false, "#ebbcba80"},
188-
{"hex plain", FormatHex, true, "ebbcba80"},
190+
{"hex", color.FormatHex, false, "#ebbcba80"},
191+
{"hex plain", color.FormatHex, true, "ebbcba80"},
189192

190-
{"hsl", FormatHSL, false, "hsla(2, 55%, 83%, 0.5)"},
191-
{"hsl plain", FormatHSL, true, "2, 55%, 83%, 0.5"},
193+
{"hsl", color.FormatHSL, false, "hsla(2, 55%, 83%, 0.5)"},
194+
{"hsl plain", color.FormatHSL, true, "2, 55%, 83%, 0.5"},
192195

193-
{"hsl-css", FormatHSLCSS, false, "hsl(2deg 55% 83% / 0.5)"},
194-
{"hsl-css plain", FormatHSLCSS, true, "2deg 55% 83% / 0.5"},
196+
{"hsl-css", color.FormatHSLCSS, false, "hsl(2deg 55% 83% / 0.5)"},
197+
{"hsl-css plain", color.FormatHSLCSS, true, "2deg 55% 83% / 0.5"},
195198

196-
{"hsl-array", FormatHSLArray, false, "[2, 0.55, 0.83, 0.5]"},
197-
{"hsl-array plain", FormatHSLArray, true, "2, 0.55, 0.83, 0.5"},
199+
{"hsl-array", color.FormatHSLArray, false, "[2, 0.55, 0.83, 0.5]"},
200+
{"hsl-array plain", color.FormatHSLArray, true, "2, 0.55, 0.83, 0.5"},
198201

199-
{"rgb", FormatRGB, false, "rgba(235, 188, 186, 0.5)"},
200-
{"rgb plain", FormatRGB, true, "235, 188, 186, 0.5"},
202+
{"rgb", color.FormatRGB, false, "rgba(235, 188, 186, 0.5)"},
203+
{"rgb plain", color.FormatRGB, true, "235, 188, 186, 0.5"},
201204

202-
{"rgb-css", FormatRGBCSS, false, "rgb(235 188 186 / 0.5)"},
203-
{"rgb-css plain", FormatRGBCSS, true, "235 188 186 / 0.5"},
205+
{"rgb-css", color.FormatRGBCSS, false, "rgb(235 188 186 / 0.5)"},
206+
{"rgb-css plain", color.FormatRGBCSS, true, "235 188 186 / 0.5"},
204207

205-
{"rgb-array", FormatRGBArray, false, "[235, 188, 186, 0.5]"},
206-
{"rgb-array plain", FormatRGBArray, true, "235, 188, 186, 0.5"},
208+
{"rgb-array", color.FormatRGBArray, false, "[235, 188, 186, 0.5]"},
209+
{"rgb-array plain", color.FormatRGBArray, true, "235, 188, 186, 0.5"},
207210

208-
{"ansi", FormatAnsi, false, "235;188;186;0.5"},
211+
{"ansi", color.FormatAnsi, false, "235;188;186;0.5"},
209212
}
210213

211214
for _, tt := range tests {
212215
t.Run(tt.name, func(t *testing.T) {
213-
got := formatColor(color, tt.format, tt.plain, true, true)
216+
got := color.FormatColor(c, tt.format, tt.plain, true, true)
214217
if got != tt.want {
215218
t.Errorf("formatColor() = %v, want %v", got, tt.want)
216219
}

config.go renamed to color/color.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
1-
package main
2-
3-
type Config struct {
4-
Template string
5-
Output string
6-
Prefix string
7-
Format string
8-
Create string
9-
Plain bool
10-
Commas bool
11-
Spaces bool
12-
}
1+
package color
132

143
type ColorFormat string
154

0 commit comments

Comments
 (0)