Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,50 @@ import (
"strings"

"github.com/rose-pine/rose-pine-bloom/color"
"github.com/rose-pine/rose-pine-bloom/config"
)

// 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 *config.BuildTemplateConfig) error {
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 *config.BuildConfig) error {
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 *config.BuildConfig) error {
func generateThemes(cfg *Options) error {
templates, err := getTemplateFiles(cfg.Template)
if err != nil {
return err
Expand Down Expand Up @@ -64,7 +86,7 @@ func generateThemes(cfg *config.BuildConfig) error {
return nil
}

func generateThemeFile(cfg *config.BuildConfig, templatePath string, content []byte, variant color.VariantMeta, accent string) error {
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" {
Expand All @@ -79,7 +101,7 @@ func generateThemeFile(cfg *config.BuildConfig, templatePath string, content []b
return writeFile(outputPath, []byte(result))
}

func createTemplates(cfg *config.BuildTemplateConfig) error {
func createTemplates(cfg *TemplateOptions) error {
files, err := getTemplateFiles(cfg.Input)
if err != nil {
return err
Expand Down Expand Up @@ -134,7 +156,7 @@ func createTemplates(cfg *config.BuildTemplateConfig) error {
return nil
}

func processTemplate(content string, cfg *config.BuildConfig, variant color.VariantMeta, accent string) string {
func processTemplate(content string, cfg *Options, variant color.VariantMeta, accent string) string {
result := content

// Replace metadata
Expand Down Expand Up @@ -242,7 +264,7 @@ func writeFile(outputPath string, content []byte) error {
return os.WriteFile(outputPath, content, 0644)
}

func buildOutputPath(cfg *config.BuildConfig, templatePath string, variant color.VariantMeta, accent string) string {
func buildOutputPath(cfg *Options, templatePath string, variant color.VariantMeta, accent string) string {
ext := filepath.Ext(templatePath)
var outputFile, outputDir string

Expand Down
7 changes: 3 additions & 4 deletions builder/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"testing"

"github.com/rose-pine/rose-pine-bloom/color"
"github.com/rose-pine/rose-pine-bloom/config"
)

func setupTest(t *testing.T) string {
Expand All @@ -24,7 +23,7 @@ func setupTest(t *testing.T) string {
return tmpDir
}

func buildFromTemplate(t *testing.T, template string, cfg *config.BuildConfig) {
func buildFromTemplate(t *testing.T, template string, cfg *Options) {
templatePath := filepath.Join(cfg.Output, "template.json")
if err := os.WriteFile(templatePath, []byte(template), 0644); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -58,7 +57,7 @@ func assertJSONField(t *testing.T, result map[string]any, field, want string) {
}

// testConfig provides standard config
var testConfig = config.BuildConfig{
var testConfig = Options{
Template: "",
Output: "",
Prefix: "$",
Expand All @@ -68,7 +67,7 @@ var testConfig = config.BuildConfig{
Spaces: true,
}

var testBuildTemplateConfig = config.BuildTemplateConfig{
var testBuildTemplateConfig = TemplateOptions{
Input: "",
Output: "",
Variant: "moon",
Expand Down
3 changes: 1 addition & 2 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/rose-pine/rose-pine-bloom/builder"
"github.com/rose-pine/rose-pine-bloom/color"
"github.com/rose-pine/rose-pine-bloom/config"
"github.com/spf13/cobra"
)

Expand All @@ -33,7 +32,7 @@ var buildCmd = &cobra.Command{

fmt.Printf("Building themes from %s...\n", template)

err := builder.Build(&config.BuildConfig{
err := builder.Build(&builder.Options{
Template: template,
Output: outputDir,
Prefix: prefix,
Expand Down
3 changes: 1 addition & 2 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"time"

"github.com/rose-pine/rose-pine-bloom/builder"
"github.com/rose-pine/rose-pine-bloom/config"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -48,7 +47,7 @@ var initCmd = &cobra.Command{
themeFile := args[0]
fmt.Printf("Creating template from %s...\n", themeFile)

err := builder.BuildTemplate(&config.BuildTemplateConfig{
err := builder.BuildTemplate(&builder.TemplateOptions{
Input: themeFile,
Output: output,
Variant: variant,
Expand Down
22 changes: 0 additions & 22 deletions config/config.go

This file was deleted.