protogen

package
v0.26.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 31, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const CacheVersion = 1

CacheVersion is the current cache format version.

View Source
const (
	// CppBuildTag is the build tag prepended to C++ files.
	CppBuildTag = "//go:build deps_only && cgo"
)
View Source
const DefaultCacheFile = ".protoc-manifest.json"

DefaultCacheFile is the default cache file name.

View Source
const DefaultGoLiteFeatures = "marshal+unmarshal+size+equal+json+clone+text"

DefaultGoLiteFeatures is the default set of go-lite features to enable.

Variables

This section is empty.

Functions

func DiscoverProtoFiles

func DiscoverProtoFiles(projectDir string, patterns []string) ([]string, error)

DiscoverProtoFiles finds proto files matching the given patterns. Uses git ls-files to find tracked proto files.

func FindGeneratedFilesForProto

func FindGeneratedFilesForProto(protoFile, projectDir, modulePath string) ([]string, error)

FindGeneratedFilesForProto finds actual generated files for a proto file using glob.

func GetGeneratedFiles

func GetGeneratedFiles(protoFile, projectDir, modulePath string, hasGo, hasTS bool) []string

GetGeneratedFiles returns the expected generated file paths for a proto file.

func GetGoModule

func GetGoModule(projectDir string) (string, error)

GetGoModule reads the module path from go.mod in the given directory.

func GetPackageKey

func GetPackageKey(modulePath, protoFile string) string

GetPackageKey generates a cache key for a proto file. Uses the format: "module/path/to/dir;package_name"

Types

type Cache

type Cache struct {
	// Version is the cache format version.
	Version int `json:"version"`
	// ProtocFlagsHash is the hash of the protoc flags.
	ProtocFlagsHash string `json:"protocFlagsHash"`
	// ToolVersions stores tool version strings for cache invalidation.
	ToolVersions string `json:"toolVersions,omitempty"`
	// Packages maps package identifiers to package info.
	Packages map[string]*PackageInfo `json:"packages"`
}

Cache represents the protoc manifest cache.

func LoadCache

func LoadCache(path string) (*Cache, error)

LoadCache loads the cache from a file. Returns an empty cache if the file doesn't exist.

func NewCache

func NewCache() *Cache

NewCache creates a new empty cache.

func (*Cache) CleanOrphanedPackages

func (c *Cache) CleanOrphanedPackages(currentPackages map[string]struct{})

CleanOrphanedPackages removes packages from the cache that no longer have proto files.

func (*Cache) NeedsRegeneration

func (c *Cache) NeedsRegeneration(packageKey string, protoFiles []string, projectDir string, flagsHash string, force bool) (bool, error)

NeedsRegeneration checks if a proto file needs regeneration. Returns true if: - The file is not in the cache - The file content hash has changed - The protoc flags have changed - Force is true

func (*Cache) Save

func (c *Cache) Save(path string) error

Save writes the cache to a file.

func (*Cache) SetProtocFlags

func (c *Cache) SetProtocFlags(flags []string)

SetProtocFlags sets the protoc flags hash.

func (*Cache) SetToolVersions

func (c *Cache) SetToolVersions(versions string)

SetToolVersions sets the tool versions string.

func (*Cache) UpdatePackage

func (c *Cache) UpdatePackage(packageKey string, protoFiles []string, generatedFiles []string, projectDir string) error

UpdatePackage updates the cache for a package after generation.

type Config

type Config struct {
	// ProjectDir is the project directory.
	// If empty, uses the current working directory.
	ProjectDir string
	// Targets is the list of proto file glob patterns to process.
	// Default: ["./*.proto"]
	Targets []string
	// Force regenerates all files regardless of cache.
	Force bool
	// CacheFile is the path to the cache file.
	// Default: ".protoc-manifest.json"
	CacheFile string
	// Verbose enables verbose output.
	Verbose bool
	// GoLiteFeatures is the go-lite features to enable.
	// Default: "marshal+unmarshal+size+equal+json+clone+text"
	GoLiteFeatures string
	// ToolsDir is the tools directory containing plugin binaries.
	// Default: ".tools"
	ToolsDir string
	// ExtraArgs contains any additional protoc arguments.
	ExtraArgs []string
}

Config contains the configuration for proto generation.

func NewConfig

func NewConfig() *Config

NewConfig returns a new Config with default values.

func (*Config) GetCacheFilePath

func (c *Config) GetCacheFilePath() (string, error)

GetCacheFilePath returns the absolute path to the cache file.

func (*Config) GetGoModule

func (c *Config) GetGoModule() (string, error)

GetGoModule returns the Go module path from go.mod.

func (*Config) GetProjectDir

func (c *Config) GetProjectDir() (string, error)

GetProjectDir returns the project directory, defaulting to cwd.

func (*Config) GetToolsDir

func (c *Config) GetToolsDir() (string, error)

GetToolsDir returns the absolute path to the tools directory.

func (*Config) HasGoMod

func (c *Config) HasGoMod() (bool, error)

HasGoMod checks if go.mod exists in the project directory.

func (*Config) HasPackageJSON

func (c *Config) HasPackageJSON() (bool, error)

HasPackageJSON checks if package.json exists in the project directory.

type Generator

type Generator struct {
	// Config is the generator configuration.
	Config *Config
	// Plugins contains the discovered plugins.
	Plugins *Plugins
	// Cache is the manifest cache.
	Cache *Cache
	// ProjectDir is the resolved project directory.
	ProjectDir string
	// ModulePath is the Go module path.
	ModulePath string
	// VendorDir is the vendor directory.
	VendorDir string
	// OutDir is the output directory (same as VendorDir).
	OutDir string
	// Verbose enables verbose output.
	Verbose bool
	// Stdout is where to write standard output.
	Stdout io.Writer
	// Stderr is where to write error output.
	Stderr io.Writer
}

Generator handles protobuf code generation.

func NewGenerator

func NewGenerator(cfg *Config) (*Generator, error)

NewGenerator creates a new Generator.

func (*Generator) Clean

func (g *Generator) Clean() error

Clean removes all generated files and the cache.

func (*Generator) Generate

func (g *Generator) Generate(ctx context.Context) error

Generate runs the proto generation.

type NativePluginHandler

type NativePluginHandler struct {
	// Plugins is the configured plugins.
	Plugins *Plugins
	// Verbose enables verbose output.
	Verbose bool
}

NativePluginHandler implements go-protoc-wasi's PluginHandler interface. It spawns native plugin processes and handles IPC.

func NewNativePluginHandler

func NewNativePluginHandler(plugins *Plugins, verbose bool) *NativePluginHandler

NewNativePluginHandler creates a new NativePluginHandler.

func (*NativePluginHandler) Communicate

func (h *NativePluginHandler) Communicate(ctx context.Context, program string, searchPath bool, input []byte) ([]byte, error)

Communicate implements the PluginHandler interface. It spawns a plugin process, sends the CodeGeneratorRequest via stdin, and returns the CodeGeneratorResponse from stdout.

type PackageInfo

type PackageInfo struct {
	// Hash is the content hash of all proto files in this package.
	Hash string `json:"hash"`
	// GeneratedFiles is the list of generated output files.
	GeneratedFiles []string `json:"generatedFiles"`
	// ProtoFiles is the list of source proto file paths.
	ProtoFiles []string `json:"protoFiles"`
	// LastGenerated is the timestamp of the last generation.
	LastGenerated time.Time `json:"lastGenerated"`
}

PackageInfo contains cached information about a proto package.

type Plugin

type Plugin struct {
	// Name is the plugin name (e.g., "go-lite", "es-lite").
	Name string
	// BinaryName is the executable name (e.g., "protoc-gen-go-lite").
	BinaryName string
	// Path is the full path to the plugin binary.
	Path string
	// Type is the plugin type.
	Type PluginType
	// OutFlag is the output flag name (e.g., "go-lite_out").
	OutFlag string
	// Options are the plugin options.
	Options map[string]string
}

Plugin represents a protoc plugin configuration.

type PluginType

type PluginType int

PluginType represents the type of protoc plugin.

const (
	PluginTypeGo PluginType = iota
	PluginTypeTypeScript
	PluginTypeCpp
)

type Plugins

type Plugins struct {
	// GoLite is the protoc-gen-go-lite plugin.
	GoLite *Plugin
	// GoStarpc is the protoc-gen-go-starpc plugin.
	GoStarpc *Plugin
	// ESLite is the protoc-gen-es-lite plugin.
	ESLite *Plugin
	// ESStarpc is the protoc-gen-es-starpc plugin.
	ESStarpc *Plugin
}

Plugins holds the configured plugins for a project.

func DiscoverPlugins

func DiscoverPlugins(cfg *Config) (*Plugins, error)

DiscoverPlugins finds and configures available plugins.

func (*Plugins) GetProtocArgs

func (p *Plugins) GetProtocArgs(outDir string) []string

GetProtocArgs returns the protoc arguments for all configured plugins. Note: We don't pass --plugin=<path> because the WASI protoc can't access host binaries. Instead, the PluginHandler intercepts plugin calls by name.

func (*Plugins) HasGoPlugins

func (p *Plugins) HasGoPlugins() bool

HasGoPlugins returns true if Go plugins are configured.

func (*Plugins) HasTSPlugins

func (p *Plugins) HasTSPlugins() bool

HasTSPlugins returns true if TypeScript plugins are configured.

type PostProcessor

type PostProcessor struct {
	// ProjectDir is the project directory.
	ProjectDir string
	// ModulePath is the Go module path.
	ModulePath string
	// VendorDir is the vendor directory path.
	VendorDir string
	// Verbose enables verbose output.
	Verbose bool
}

PostProcessor handles post-processing of generated files.

func NewPostProcessor

func NewPostProcessor(projectDir, modulePath string, verbose bool) *PostProcessor

NewPostProcessor creates a new PostProcessor.

func (*PostProcessor) ProcessAllCppFiles

func (p *PostProcessor) ProcessAllCppFiles(dir string) error

ProcessAllCppFiles finds and processes all C++ files in a directory.

func (*PostProcessor) ProcessAllTsFiles

func (p *PostProcessor) ProcessAllTsFiles(dir string) error

ProcessAllTsFiles finds and processes all TypeScript files in a directory.

func (*PostProcessor) ProcessCppFile

func (p *PostProcessor) ProcessCppFile(filePath string) error

ProcessCppFile processes a C++ file. - Prepends the Go build tag if not present. - Rewrites include paths from absolute to relative.

func (*PostProcessor) ProcessGeneratedFiles

func (p *PostProcessor) ProcessGeneratedFiles(protoFile string) error

ProcessGeneratedFiles processes all generated files for a proto file.

func (*PostProcessor) ProcessTsFile

func (p *PostProcessor) ProcessTsFile(filePath string) error

ProcessTsFile processes a TypeScript file. Rewrites relative import paths to @go/ format.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL