genutils

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

README

genutils

Utilities for writing Go code generators.

Getting Started

The genutils command line tool is used to . Run the following command to create a generator cmd, that can be used to generate code using 2 markers.

go run github.com/alexandremahdhaoui/genutils/cmd/genutils@latest --cmd gencmd --generators="yourgen:./pkg/gen,anothergen:./pkg/gen"
Cmd
// cmd/yourgen/main.go
package main

import (
	genutils "github.com/alexandremahdhaoui/genutils"
	gen "github.com/alexandremahdhaoui/genutils/pkg/gen"
)

const (
	name                    = "gencmd"
	description             = "TODO: Please write a description here."
	helper                  = "TODO: Please write an example here."
	yourgenGeneratorName    = "yourgen"
	anothergenGeneratorName = "anothergen"
)

func main() {
	genutils.
        New(name).
        WithDescription(description).
        WithHelper(helper).
        WithGenerator(yourgenGeneratorName, gen.YourgenGenerator{}).
        WithGenerator(anothergenGeneratorName, gen.AnothergenGenerator{}).
        Apply().
        Run()
}
Generator
// pkg/yourgen/yourgen.go
package gen

import (
	genall "sigs.k8s.io/controller-tools/pkg/genall"
	markers "sigs.k8s.io/controller-tools/pkg/markers"
)

var yourgenMarkerDefinition = markers.Must(markers.MakeDefinition("gencmd:yourgen", markers.DescribesType, YourgenGenerator{}))

type YourgenGenerator struct {
	HeaderFile string `marker:",omitempty"`
	Year       string `marker:",omitempty"`
}

func (YourgenGenerator) RegisterMarkers(into *markers.Registry) error {
	if err := markers.RegisterAll(into, yourgenMarkerDefinition); err != nil {
		return err
	}
	into.AddHelp(yourgenMarkerDefinition, markers.SimpleHelp("object", ""))
	return nil
}

func (g YourgenGenerator) Generate(ctx *genall.GenerationContext) error {
	// TODO: ADD YOUR CODE HERE
	for _, root := range ctx.Roots {
		root.NeedTypesInfo()
		markerSet, err := markers.PackageMarkers(ctx.Collector, root)
		if err != nil {
			return err
		}
		markerValues := markerSet[yourgenMarkerDefinition.Name]
		if len(markerValues) == 0 {
			continue
		}
		// TODO: YOU CAN ALSO ADD YOUR CODE HERE
	}
	// TODO: OR HERE
	return nil
}

Use your new generator

//+gencmd:yourgen

type YourStruct struct{}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GeneratedFilename

func GeneratedFilename(prefix, name string) string

func Title

func Title(s string) string

func WriteFile

func WriteFile(o WriteFileOption) error

Types

type Builder

type Builder func() Cmd

func New

func New(name string) Builder

func (Builder) Apply

func (b Builder) Apply() Cmd

func (Builder) WithDescription

func (b Builder) WithDescription(description string) Builder

func (Builder) WithGenerator

func (b Builder) WithGenerator(key string, generator genall.Generator) Builder

func (Builder) WithGenerators

func (b Builder) WithGenerators(generators map[string]genall.Generator) Builder

func (Builder) WithHelper

func (b Builder) WithHelper(helper string) Builder

func (Builder) WithOutputRule

func (b Builder) WithOutputRule(key string, outputRule genall.OutputRule) Builder

func (Builder) WithOutputRules

func (b Builder) WithOutputRules(outputRules map[string]genall.OutputRule) Builder

type Cmd

type Cmd struct {
	// contains filtered or unexported fields
}

func (Cmd) Run

func (c Cmd) Run()

type WriteFileOption

type WriteFileOption struct {
	// CmdName is the name of the command line used to
	CmdName    string
	Filename   string
	HeaderFile string

	Buffer *bytes.Buffer
	Ctx    *genall.GenerationContext
	Root   *loader.Package
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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