gogenerate

package
v0.0.0-...-5fef39c Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2024 License: MIT, MIT Imports: 14 Imported by: 32

README

gogenerate

Package gogenerate exposes some of the unexported internals of the go generate command as a convenience for the authors of go generate generators.

go get -u myitcv.io/gogenerate

Documentation

Overview

Package gogenerate exposes some of the unexported internals of the go generate command as a convenience for the authors of go generate generators. See https://github.com/myitcv/gogenerate/wiki/Go-Generate-Notes for further notes on such generators. It also exposes some convenience functions that might be useful to authors of generators

Index

Constants

View Source
const (
	GOARCH    = "GOARCH"
	GOFILE    = "GOFILE"
	GOLINE    = "GOLINE"
	GOOS      = "GOOS"
	GOPACKAGE = "GOPACKAGE"
	GOPATH    = "GOPATH"

	GoGeneratePrefix = "//go:generate"
)

These constants correspond in name and value to the details given in go generate --help

View Source
const (
	// FlagLog is the name of the common flag shared between go generate generators
	// to control logging verbosity.
	FlagLog = "gglog"

	// FlagOutDirPrefix is the prefix used for flags that indicate an output
	// directory for a generator
	FlagOutDirPrefix = "outdir:"

	// FlagInFilesPrefix is the prefix used for flags that indicate a glob
	// pattern of input files for a generator
	FlagInFilesPrefix = "infiles:"

	// FlagLicenseFile is the name of the common flag shared between go generate generators
	// to provide a license header file.
	FlagLicenseFile = "licenseFile"
)

Variables

This section is empty.

Functions

func AnyFileGeneratedBy

func AnyFileGeneratedBy(path string, cmd string) bool

AnyFileGeneratedBy returns true if the base name of the supplied path is a file that would have been generated by the supplied cmd. Unlike FileGeneratedBy this is not limited to .go files. The extension of the file is also returned

func AnyFileIsGenerated

func AnyFileIsGenerated(path string) (string, string, bool)

AnyFileIsGenerated determines wheter the file located at path is generated or not and if it is generated returns the base name of the generator that generated it and the file extension

func CommentLicenseHeader

func CommentLicenseHeader(file *string) (string, error)

CommentLicenseHeader is a convenience function to be used in conjunction with LicenseFileFlag; if a filename is provided it reads the contents of the file and returns a line-commented transformation of the contents with a final blank newline

func DefaultLogLevel

func DefaultLogLevel(f *string, ll LogLevel)

DefaultLogLevel is provided simply as a convenience along with LogFlag to ensure a default LogLevel in a flag variable. This is necessary because of the interplay between go generate argument parsing and the advice given for log levels via gogenerate.

func DirFunc

func DirFunc(pkg string, dir, file string, f func(line int, dirArgs []string) error) error

DirFunc runs f(cmds) on each go generate directive (as defined by go generate -help) found in the absolute-named file that is part of package pkg

func DirFuncFunc

func DirFuncFunc(pkg string, dir, file string, hasDirPrefix func([]byte) (string, bool), f func(prefix string, line int, dirArgs []string) error) error

DirFuncFunc runs f(cmds) on each go generate directive identified by hasDirPrefix found in the absolute-named file that is part of package pkg. Note, you will likely want hasDirPrefix to distinguish between potentially ambiguous directives that share a common prefix. This is typically done by ensuring that the prefix is followed by some whitespace. The prefix returned in the callback to f will be the result of strings.TrimSpace.

func FileGeneratedBy

func FileGeneratedBy(path string, cmd string) bool

FileGeneratedBy returns true if the base name of the supplied path is a Go file that would have been generated by the supplied cmd

func FileIsGenerated

func FileIsGenerated(path string) (string, bool)

FileIsGenerated determines wheter the Go file located at path is generated or not and if it is generated returns the base name of the generator that generated it

func FilesContainingCmd

func FilesContainingCmd(dir string, command string, tags map[string]bool) (map[string]int, error)

FilesContainingCmd returns a map of Go file name (defined by go list as GoFiles + CgoFiles + TestGoFiles + XTestGoFiles) in the directory dir, to a count of the number of times directive command appears in that file (after quote and variable expansion as described by go generate -help). Commands can be gobin commands or plain PATH-based command calls. When comparing PATH-based commands, the filepath.Base of each is compared. The file names will, by definition, be relative to dir

func LicenseFileFlag

func LicenseFileFlag(fs *flag.FlagSet) *string

LicenseFileFlag defines a command line string flag named according to the constant FlagLicenseFile and returns a pointer ot the string that flag set

func LogFlag

func LogFlag(fs *flag.FlagSet) *string

LogFlag defines a command line string flag named according to the constant FlagLog and returns a pointer to the string the flag sets

func NameFile

func NameFile(name string, cmd string) string

NameFile returns a file name that conforms with the pattern associated with files generated by the provided command

func NameFileFromFile

func NameFileFromFile(name string, cmd string) (string, bool)

NameFileFromFile uses the provided filename as a template and returns a generated filename consistent with the provided command

func NameTestFile

func NameTestFile(name string, cmd string) string

NameTestFile returns a file name that conforms with the pattern associated with files generated by the provided command

func OutPkgFlag

func OutPkgFlag(key string, fs *flag.FlagSet) *outputs

OutPkgFlag defines a new flag "outpkg:"+key that can accept a list of package specifications that represent output targets above and beyond the default of self.

Types

type LogLevel

type LogLevel string
const (
	LogInfo    LogLevel = "info"
	LogWarning LogLevel = "warning"
	LogError   LogLevel = "error"
	LogFatal   LogLevel = "fatal"
)

The various log levels supported by the flag returned by LogFlag()

Directories

Path Synopsis
_testFiles

Jump to

Keyboard shortcuts

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