Skip to content

Commit 6e9c253

Browse files
authored
feat: add debug flag to vipsgen command (#20)
* readme * readme * funding * chore: CI badge * clean up logs * clean up logs * clean up logs * readme * debug flag * debug flag * go doc
1 parent 106ad1e commit 6e9c253

File tree

10 files changed

+41
-40
lines changed

10 files changed

+41
-40
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github: cshum

.gitignore

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,5 @@ tmp/
99
.env
1010
bin/vipsgen
1111
profile.cov
12-
13-
repomix-output.xml
14-
15-
debug_enums.json
16-
debug_operations.json
17-
debug_image_types.json
12+
*-output.xml
13+
debug_*.json

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# vipsgen
22

3-
[![Go Reference](https://pkg.go.dev/badge/github.com/cshum/vipsgen.svg)](https://pkg.go.dev/github.com/cshum/vipsgen)
4-
![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/cshum/vipsgen)
53
[![CI](https://github.com/cshum/vipsgen/actions/workflows/ci.yml/badge.svg)](https://github.com/cshum/vipsgen/actions/workflows/ci.yml)
4+
[![Go Reference](https://pkg.go.dev/badge/github.com/cshum/vipsgen/vips.svg)](https://pkg.go.dev/github.com/cshum/vipsgen/vips)
5+
![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/cshum/vipsgen)
66

77
vipsgen is a Go binding generator for [libvips](https://github.com/libvips/libvips) - a fast and efficient image processing library.
88

99
Existing Go libvips bindings rely on manually written code that is often incomplete, error-prone, and difficult to maintain as libvips evolves. vipsgen aims to solve this problem by generating type-safe, robust, and fully documented Go bindings using GObject introspection.
1010

1111
vipsgen provides a pre-generated library you can import directly `github.com/cshum/vipsgen/vips`. Also allows code generation via `vipsgen` command that adapts to your specific libvips installation.
1212

13-
- **Coverage**: Comprehensive bindings for over 200 libvips operations
13+
- **Coverage**: Comprehensive bindings for around 300 libvips operations
1414
- **Type-Safe**: Generates proper Go types for libvips enums and structs
1515
- **Idiomatic**: Creates clear Go style code that feels natural to use
1616
- **Streaming**: Includes `VipsSource` bindings with `io.ReadCloser` integration for streaming
@@ -20,7 +20,6 @@ vipsgen provides a pre-generated library you can import directly `github.com/csh
2020
Use homebrew to install vips and pkg-config:
2121
```
2222
brew install vips pkg-config
23-
2423
```
2524

2625
On MacOS, vipsgen may not compile without first setting an environment variable:
@@ -89,7 +88,6 @@ func main() {
8988
// Save the result as WebP file with options
9089
err = image.Webpsave("resized-gopher.webp", &vips.WebpsaveOptions{
9190
Q: 85, // Quality factor (0-100)
92-
Lossless: false, // Use lossy compression
9391
Effort: 4, // Compression effort (0-6)
9492
SmartSubsample: true, // Better chroma subsampling
9593
})
@@ -135,6 +133,7 @@ Options:
135133
-templates string Template directory (uses embedded templates if not specified)
136134
-extract Extract embedded templates and exit
137135
-extract-dir string Directory to extract templates to (default "./templates")
136+
-debug Enable debug json output
138137
```
139138

140139
## Contributing

cmd/vipsgen/main.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,18 @@ package main
22

33
import (
44
"flag"
5-
"fmt"
65
"github.com/cshum/vipsgen/internal/generator"
76
"github.com/cshum/vipsgen/internal/introspection"
87
"github.com/cshum/vipsgen/internal/templates"
98
"log"
109
)
1110

1211
func main() {
13-
// Define flags
1412
extractTemplates := flag.Bool("extract", false, "Extract embedded templates to a directory")
1513
extractDir := flag.String("extract-dir", "./templates", "Directory to extract templates to")
1614
outputDirFlag := flag.String("out", "./vips", "Output directory")
1715
templateDirFlag := flag.String("templates", "", "Template directory (uses embedded templates if not specified)")
16+
isDebug := flag.Bool("debug", false, "Enable debug json output")
1817

1918
flag.Parse()
2019

@@ -24,7 +23,7 @@ func main() {
2423
log.Fatalf("Failed to extract templates: %v", err)
2524
}
2625

27-
fmt.Printf("Templates and static files extracted to: %s\n", *extractDir)
26+
log.Printf("Templates and static files extracted to: %s\n", *extractDir)
2827
return
2928
}
3029

@@ -40,11 +39,11 @@ func main() {
4039
if err != nil {
4140
log.Fatalf("Failed to create template loader: %v", err)
4241
}
43-
fmt.Printf("Using templates from: %s\n", *templateDirFlag)
42+
log.Printf("Using templates from: %s\n", *templateDirFlag)
4443
} else {
4544
// Use embedded templates by default
4645
loader = generator.NewFSTemplateLoader(templates.Templates, funcMap)
47-
fmt.Println("Using embedded templates")
46+
log.Printf("Using embedded templates\n")
4847
}
4948

5049
// Determine output directory
@@ -57,27 +56,27 @@ func main() {
5756
}
5857

5958
// Create operation manager for C-based introspection
60-
vipsIntrospection := introspection.NewIntrospection()
59+
vipsIntrospection := introspection.NewIntrospection(*isDebug)
6160

6261
// Extract image types from operations
6362
imageTypes := vipsIntrospection.DiscoverImageTypes()
6463

6564
// Discover supported savers
6665
supportedSavers := vipsIntrospection.DiscoverSupportedSavers()
67-
fmt.Printf("Discovered supported savers:\n")
66+
log.Printf("Discovered supported savers:\n")
6867
for name, supported := range supportedSavers {
6968
if supported {
70-
fmt.Printf(" - %s: supported\n", name)
69+
log.Printf(" - %s: supported\n", name)
7170
}
7271
}
7372

7473
// Convert GIR data to vipsgen.Operation format
7574
operations := vipsIntrospection.DiscoverOperations()
76-
fmt.Printf("Extracted %d operations from GObject Introspection\n", len(operations))
75+
log.Printf("Extracted %d operations from GObject Introspection\n", len(operations))
7776

7877
// Get enum types
7978
enumTypes := vipsIntrospection.DiscoverEnumTypes()
80-
fmt.Printf("Discovered %d enum types\n", len(enumTypes))
79+
log.Printf("Discovered %d enum types\n", len(enumTypes))
8180

8281
// Create unified template data
8382
templateData := generator.NewTemplateData(operations, enumTypes, imageTypes, supportedSavers)

internal/generator/generate.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package generator
22

33
import (
44
"fmt"
5+
"log"
56
"os"
67
"path/filepath"
78
"strings"
@@ -39,11 +40,9 @@ func Generate(
3940
generatedFiles = append(generatedFiles, outputFile)
4041
}
4142

42-
fmt.Printf("\nSuccessfully generated files from templates: %d\n", len(generatedFiles))
43+
log.Printf("\nSuccessfully generated files from templates: %d\n", len(generatedFiles))
4344
for _, file := range generatedFiles {
44-
fmt.Printf(" - %s\n", file)
45+
log.Printf(" - %s\n", file)
4546
}
46-
fmt.Println("\nAdditional static files were also copied to the output directory.")
47-
4847
return nil
4948
}

internal/generator/templateloader.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package generator
33
import (
44
"fmt"
55
"io/fs"
6+
"log"
67
"os"
78
"path/filepath"
89
"strings"
@@ -166,7 +167,7 @@ func ExtractEmbeddedFS(filesystem fs.FS, destDir string) error {
166167
return fmt.Errorf("failed to write file %s: %v", outPath, err)
167168
}
168169

169-
fmt.Printf(" - Extracted: %s\n", path)
170+
log.Printf(" - Extracted: %s\n", path)
170171
return nil
171172
})
172173

internal/introspection/enum.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package introspection
44
import "C"
55
import (
66
"fmt"
7+
"log"
78
"strings"
89
"unsafe"
910
)
@@ -62,22 +63,24 @@ func (v *Introspection) DiscoverEnumTypes() []EnumTypeInfo {
6263
C.free(unsafe.Pointer(cTypeName))
6364

6465
if exists == 0 {
65-
fmt.Printf("Warning: enum type %s not found in libvips\n", typeName.CName)
66+
log.Printf("Warning: enum type %s not found in libvips\n", typeName.CName)
6667
continue
6768
}
6869

6970
// Try to get the enum values
7071
enumInfo, err := v.getEnumType(typeName.CName, typeName.GoName)
7172
if err != nil {
72-
fmt.Printf("Warning: couldn't process enum type %s: %v\n", typeName.CName, err)
73+
log.Printf("Warning: couldn't process enum type %s: %v\n", typeName.CName, err)
7374
continue
7475
}
7576

7677
// Add successfully processed enum
7778
enumTypes = append(enumTypes, enumInfo)
7879
}
7980

80-
debugJson(enumTypes, "debug_enums.json")
81+
if v.isDebug {
82+
debugJson(enumTypes, "debug_enums.json")
83+
}
8184

8285
return enumTypes
8386
}
@@ -233,7 +236,7 @@ func (v *Introspection) addEnumType(cName, goName string) {
233236
GoName: goName,
234237
})
235238
v.discoveredEnumTypes[cNameLower] = goName
236-
fmt.Printf("Discovered enum type: %s -> %s\n", cName, goName)
239+
log.Printf("Discovered enum type: %s -> %s\n", cName, goName)
237240
}
238241
}
239242

internal/introspection/imagetype.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ func (v *Introspection) DiscoverImageTypes() []ImageTypeInfo {
9898
currentOrder++
9999
}
100100

101-
debugJson(imageTypes, "debug_image_types.json")
101+
if v.isDebug {
102+
debugJson(imageTypes, "debug_image_types.json")
103+
}
102104

103105
return imageTypes
104106
}

internal/introspection/introspection.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ type Introspection struct {
1414
discoveredEnumTypes map[string]string
1515
enumTypeNames []enumTypeName
1616
discoveredImageTypes map[string]ImageTypeInfo
17+
isDebug bool
1718
}
1819

1920
// NewIntrospection creates a new Introspection instance for analyzing libvips
2021
// operations, initializing the libvips library in the process.
21-
func NewIntrospection() *Introspection {
22+
func NewIntrospection(isDebug bool) *Introspection {
2223
// Initialize libvips
2324
if C.vips_init(C.CString("vipsgen")) != 0 {
2425
log.Fatal("Failed to initialize libvips")
@@ -28,5 +29,6 @@ func NewIntrospection() *Introspection {
2829
return &Introspection{
2930
discoveredEnumTypes: make(map[string]string),
3031
discoveredImageTypes: map[string]ImageTypeInfo{},
32+
isDebug: isDebug,
3133
}
3234
}

internal/introspection/operation.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,25 +131,27 @@ func (v *Introspection) DiscoverOperations() []Operation {
131131
if strings.Contains(op.Name, "_target") ||
132132
strings.Contains(op.Name, "_mime") ||
133133
strings.Contains(op.Name, "fitsload_source") {
134-
fmt.Printf("Excluded operation: vips_%s \n", op.Name)
134+
log.Printf("Excluded operation: vips_%s \n", op.Name)
135135
excludedCount++
136136
continue
137137
}
138138
// Check for duplicate Go function names
139139
if seenOperations[op.GoName] {
140-
fmt.Printf("Skipping duplicated operation: vips_%s\n", op.Name)
140+
log.Printf("Skipping duplicated operation: vips_%s\n", op.Name)
141141
duplicateCount++
142142
continue
143143
}
144144
seenOperations[op.GoName] = true
145145

146-
fmt.Printf("Discovered operation: vips_%s \n", op.Name)
146+
log.Printf("Discovered operation: vips_%s \n", op.Name)
147147
operations = append(operations, op)
148148
}
149-
fmt.Printf("Discovered Operations: %d (%d excluded, %d duplicates)\n",
149+
log.Printf("Discovered Operations: %d (%d excluded, %d duplicates)\n",
150150
len(operations), excludedCount, duplicateCount)
151151

152-
debugJson(operations, "debug_operations.json")
152+
if v.isDebug {
153+
debugJson(operations, "debug_operations.json")
154+
}
153155

154156
return operations
155157
}
@@ -534,9 +536,6 @@ func (v *Introspection) mapGTypeToTypes(gtype C.GType, typeName string, isOutput
534536
if cTypeNamePtr != nil {
535537
actualTypeName := C.GoString(cTypeNamePtr)
536538

537-
// Log for debugging
538-
log.Printf("Found object type: %s", actualTypeName)
539-
540539
if isOutput {
541540
return actualTypeName, "*C." + actualTypeName, actualTypeName + "**"
542541
}

0 commit comments

Comments
 (0)