Skip to content

Commit 78497b3

Browse files
authored
feat: consolidate build command with logging (#77)
1 parent 7aab83e commit 78497b3

File tree

6 files changed

+89
-133
lines changed

6 files changed

+89
-133
lines changed

builder/builder_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ var testConfig = config.BuildConfig{
7171
var testBuildTemplateConfig = config.BuildTemplateConfig{
7272
Input: "",
7373
Output: "",
74+
Variant: "moon",
7475
Prefix: "$",
7576
Format: "hex",
76-
Variant: "moon",
7777
Plain: false,
7878
Commas: true,
7979
Spaces: true,
@@ -135,7 +135,6 @@ var testVariants = []struct {
135135
}
136136

137137
func TestColorFormatting(t *testing.T) {
138-
139138
tests := []struct {
140139
name string
141140
format color.ColorFormat

cmd/build.go

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,36 @@ package cmd
33
import (
44
"fmt"
55
"os"
6+
"slices"
7+
"strings"
68

79
"github.com/rose-pine/rose-pine-bloom/builder"
10+
"github.com/rose-pine/rose-pine-bloom/color"
811
"github.com/rose-pine/rose-pine-bloom/config"
912
"github.com/spf13/cobra"
1013
)
1114

15+
var (
16+
outputDir string
17+
prefix string
18+
format string
19+
plain bool
20+
noCommas bool
21+
noSpaces bool
22+
)
23+
1224
var buildCmd = &cobra.Command{
13-
Use: "build [flags] <template>",
14-
Short: "Builds the theme files from templates",
15-
Long: `Builds the theme files from templates.
16-
17-
This command processes the theme templates and generates the final theme files that can be used in applications.
18-
The template argument specifies the template file or directory to process.
19-
If the template argument is a directory, it will process all templates in that directory.`,
20-
Args: cobra.ExactArgs(1),
25+
Use: "build <template>",
26+
Short: "Generate theme files from template",
27+
Args: cobra.ExactArgs(1),
2128
PreRunE: func(cmd *cobra.Command, args []string) error {
2229
return validateFormat(format)
2330
},
2431
Run: func(cmd *cobra.Command, args []string) {
2532
template := args[0]
2633

34+
fmt.Printf("Building themes from %s...\n", template)
35+
2736
err := builder.Build(&config.BuildConfig{
2837
Template: template,
2938
Output: outputDir,
@@ -34,21 +43,37 @@ var buildCmd = &cobra.Command{
3443
Spaces: !noSpaces,
3544
})
3645
if err != nil {
37-
fmt.Fprintf(os.Stderr, "Error building theme: %v\n", err)
46+
fmt.Fprintf(os.Stderr, "Error building themes: %v\n", err)
3847
os.Exit(1)
3948
}
49+
50+
fmt.Printf("Themes generated in %s\n", outputDir)
4051
},
4152
}
4253

4354
func init() {
44-
45-
buildCmd.Flags().StringVarP(&outputDir, "output", "o", "dist", "Directory for generated files")
46-
buildCmd.Flags().StringVarP(&prefix, "prefix", "p", "$", "Color variable prefix")
55+
buildCmd.Flags().StringVarP(&outputDir, "output", "o", "dist", "Output directory")
56+
buildCmd.Flags().StringVarP(&prefix, "prefix", "p", "$", "Variable prefix")
4757
buildCmd.Flags().StringVarP(&format, "format", "f", "hex", formatFlagUsage())
48-
buildCmd.Flags().BoolVar(&plain, "plain", false, "Remove decorators from color values")
49-
buildCmd.Flags().BoolVar(&noCommas, "no-commas", false, "Remove commas from color values")
50-
buildCmd.Flags().BoolVar(&noSpaces, "no-spaces", false, "Remove spaces from color values")
58+
buildCmd.Flags().BoolVar(&plain, "plain", false, "Remove decorators")
59+
buildCmd.Flags().BoolVar(&noCommas, "no-commas", false, "Remove commas")
60+
buildCmd.Flags().BoolVar(&noSpaces, "no-spaces", false, "Remove spaces")
5161

5262
rootCmd.AddCommand(buildCmd)
63+
}
64+
65+
func formatFlagUsage() string {
66+
table, err := color.FormatsTable()
67+
if err != nil {
68+
fmt.Printf("Error generating format table: %v", err)
69+
os.Exit(1)
70+
}
71+
return fmt.Sprintf("Color format:\n%s", table)
72+
}
5373

74+
func validateFormat(format string) error {
75+
if slices.Contains(color.AllFormats, format) {
76+
return nil
77+
}
78+
return fmt.Errorf("invalid format '%s'. Valid formats: %s", format, strings.Join(color.AllFormats, ", "))
5479
}

cmd/buildFlags.go

Lines changed: 0 additions & 35 deletions
This file was deleted.

cmd/buildTemplate.go

Lines changed: 0 additions & 47 deletions
This file was deleted.

cmd/init.go

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,66 @@ package cmd
22

33
import (
44
"fmt"
5+
"os"
56

7+
"github.com/rose-pine/rose-pine-bloom/builder"
8+
"github.com/rose-pine/rose-pine-bloom/config"
69
"github.com/rose-pine/rose-pine-bloom/docs"
710
"github.com/spf13/cobra"
811
)
912

13+
var (
14+
variant string
15+
output string
16+
)
17+
1018
var initCmd = &cobra.Command{
11-
Use: "init",
12-
Short: "Project initialization",
13-
Long: `Creates files to start a new Rosé Pine theme project.`,
19+
Use: "init [theme-file]",
20+
Short: "Initialise new theme",
21+
Args: cobra.MaximumNArgs(1),
1422
Run: func(cmd *cobra.Command, args []string) {
23+
fmt.Println("Creating theme files...")
24+
1525
if err := docs.EnsureReadme(); err != nil {
16-
fmt.Println("unable to update README:", err)
26+
fmt.Fprintf(os.Stderr, "Error creating README: %v\n", err)
27+
} else {
28+
fmt.Println("Created README.md")
1729
}
30+
1831
if err := docs.EnsureLicense(); err != nil {
19-
fmt.Println("unable to update LICENSE:", err)
32+
fmt.Fprintf(os.Stderr, "Error creating LICENSE: %v\n", err)
33+
} else {
34+
fmt.Println("Created LICENSE")
35+
}
36+
37+
if len(args) > 0 {
38+
themeFile := args[0]
39+
fmt.Printf("Creating template from %s...\n", themeFile)
40+
41+
err := builder.BuildTemplate(&config.BuildTemplateConfig{
42+
Input: themeFile,
43+
Output: output,
44+
Variant: variant,
45+
Prefix: "$",
46+
Format: "hex",
47+
Plain: false,
48+
Commas: true,
49+
Spaces: true,
50+
})
51+
if err != nil {
52+
fmt.Fprintf(os.Stderr, "Error creating template: %v\n", err)
53+
os.Exit(1)
54+
}
55+
56+
fmt.Printf("Template created in %s\n", output)
2057
}
58+
59+
fmt.Println("Theme initialised")
2160
},
2261
}
2362

2463
func init() {
64+
initCmd.Flags().StringVarP(&variant, "variant", "v", "main", "Theme variant (main, moon, dawn)")
65+
initCmd.Flags().StringVarP(&output, "output", "o", ".", "Template output directory")
2566
rootCmd.AddCommand(initCmd)
26-
27-
// Here you will define your flags and configuration settings.
28-
29-
// Cobra supports Persistent Flags which will work for this command
30-
// and all subcommands, e.g.:
31-
// initCmd.PersistentFlags().String("foo", "", "A help for foo")
32-
33-
// Cobra supports local flags which will only run when this command
34-
// is called directly, e.g.:
35-
// initCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
3667
}

cmd/root.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,13 @@ import (
66
"github.com/spf13/cobra"
77
)
88

9-
var (
10-
// version will be set at build time using -ldflags
11-
version = "dev"
12-
)
9+
var version = "dev"
1310

1411
var rootCmd = &cobra.Command{
1512
Use: "bloom",
16-
Short: "The Rosé Pine theme generator",
17-
Long: `The Rosé Pine theme generator.`,
18-
// Uncomment the following line if your bare application
19-
// has an action associated with it:
20-
// Run: func(cmd *cobra.Command, args []string) { },
13+
Short: "Generate Rosé Pine theme files from templates",
2114
}
2215

23-
// Execute adds all child commands to the root command and sets flags appropriately.
24-
// This is called by main.main(). It only needs to happen once to the rootCmd.
2516
func Execute() {
2617
rootCmd.Version = version
2718

@@ -32,12 +23,4 @@ func Execute() {
3223
}
3324

3425
func init() {
35-
// Here you will define your flags and configuration settings.
36-
// Cobra supports persistent flags, which, if defined here,
37-
// will be global for your application.
38-
39-
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.rose-pine-bloom.yaml)")
40-
41-
// Cobra also supports local flags, which will only run
42-
// when this action is called directly.
4326
}

0 commit comments

Comments
 (0)