pkg

package
v0.0.0-...-5993f49 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2020 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package pkg provides code for loading templates, checking and generating source code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Package

type Package struct {
	*loader.Config

	// Dir is the directory where jig will look for the package files.
	Dir string

	// Name is the package name found in the first source file that is scanned from the package dir.
	Name string
	// contains filtered or unexported fields
}

Package manages the package being checked for errors and where source code will be generated when errors indicate missing types etc.

func NewPackage

func NewPackage(dir string) *Package

NewPackage creates a package given a single directory where the source of the package lives.

func (*Package) AddFile

func (p *Package) AddFile(file *ast.File)

AddFile is used by ParseDir() and GenerateSource(). It replaces the file stored by path in the fileset. The call is idempotent.

func (*Package) Check

func (p *Package) Check() ([]error, error)

Check will typecheck the currently parsed package source and return all errors that were found. This will also import and parse all dependencies. After Check() has finished the package contains the contents of all imported packages and therefore LoadTemplates() may be caled to locate and load templates from those imported packages.

func (*Package) Filepath

func (p *Package) Filepath(file *ast.File) string

Filepath will return the filepath for a given *ast.File param.

func (*Package) GenerateSource

func (p *Package) GenerateSource(packageName, name, source string) error

GenerateSource will take the passed name and source and add it to the package. You want multiple generated fragments to share a physical file on disk.

func (*Package) GenerateSourceAppendFile

func (p *Package) GenerateSourceAppendFile(filename *template.Template, packageName, name, source string) error

GenerateSourceAppendFile will generate the source and append it to a shared source file. Duh!

func (*Package) GeneratedFileset

func (p *Package) GeneratedFileset() map[*ast.File]struct{}

GeneratedFileset returns a map containing the set of files that have generated fragments in them.

func (*Package) HasGeneratedSource

func (p *Package) HasGeneratedSource(name string) bool

HasGeneratedSource is used in the templ.PackageWriter interface to prevent fragments from being generated multiple times.

func (*Package) LoadGeneratePragmas

func (p *Package) LoadGeneratePragmas() (messages []string)

func (*Package) LoadTemplates

func (p *Package) LoadTemplates(tplr templ.Templater) (messages []string, err error)

LoadTemplates is used to go through all the ast.File(s) in all the packages and then turn all jigs that are found in those files into templates.

func (*Package) LoadTemplatesFromFile

func (p *Package) LoadTemplatesFromFile(file *ast.File, ignoreSupportTemplates bool) []*jig

LoadTemplatesFromFile will parse comments in a file to find //jig:template entries that declare templates and use that to determine source range of the associated template definition. Then walk the file ast and extract source in the ranges determined before and add it to the correct jig.

func (*Package) ParseDir

func (p *Package) ParseDir() error

ParseDir will add .go files found in the package directory to the internal list of files. This will also detect if a file contains previously generated source

func (*Package) PkgSpec

func (p *Package) PkgSpec() []loader.PkgSpec

PkgSpec returns PkgSpec instances with Path and Files correctly initialized.

func (*Package) RemoveFileset

func (p *Package) RemoveFileset(fileset map[*ast.File]struct{}) (messages []string, err error)

RemoveFileset will remove the passed set of files from the PkgSpec files slice.

func (*Package) RemoveGeneratedSources

func (p *Package) RemoveGeneratedSources() ([]string, error)

RemoveGeneratedSources will remove all files that contain generated source from the package dir.

func (*Package) ScanForGeneratedSources

func (p *Package) ScanForGeneratedSources(file *ast.File)

ScanForGeneratedSources looks in the comments of the file for comment pragma //jig:name and adds the name to the list of generated source fragments. This allows multiple source fragments to be stored in a single file.

func (Package) SuggestTypesToGenerate

func (Package) SuggestTypesToGenerate(errs []error) []string

SuggestTypesToGenerate will suggest specialization signatures for types that are missing based on the error detected. e.g. if the error mentions and ObservableInt that is missing a field or method MapFloat32 then the specialization signature will become "ObservableInt MapFloat32" this signature can then be used by specialization code to generate the required type, field or method definition.

func (*Package) Typemap

func (p *Package) Typemap() map[string]string

func (*Package) WriteFile

func (p *Package) WriteFile(output io.Writer, file *ast.File) error

WriteFile will write the given file to the given output.

func (*Package) WriteFileset

func (p *Package) WriteFileset(fileset map[*ast.File]struct{}) (messages []string, err error)

WriteFileset writes the set of files that contains generated source to disk.

func (*Package) WriteGeneratedSources

func (p *Package) WriteGeneratedSources() ([]string, error)

WriteGeneratedSources is used to write the generated sources to file(s).

Jump to

Keyboard shortcuts

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