astutil

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2025 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ReplacementModeInsert = "insert"
	ReplacementModeDelete = "delete"
)
View Source
const OutrigImportPath = "github.com/outrigdev/outrig"
View Source
const ScopeGo = "go"

Variables

This section is empty.

Functions

func AddOutrigImport

func AddOutrigImport(fset *token.FileSet, node *ast.File) bool

AddOutrigImport checks if the outrig import exists in the AST node and adds it if not present. Returns true if the import was added, false if it already existed.

func AddOutrigImportReplacement

func AddOutrigImportReplacement(state *TransformState, file *ModifiedFile) error

func AddOutrigSDKDependency

func AddOutrigSDKDependency(tempGoModPath string, verbose bool, cfg config.Config) error

AddOutrigSDKDependency adds the version locked Outrig SDK to the temp go.mod file

func ApplyReplacements

func ApplyReplacements(fileBytes []byte, rs []Replacement) []byte

func DetectToolchainVersion

func DetectToolchainVersion(pkgDir string) (string, error)

DetectToolchainVersion runs "go env GOVERSION" to get the Go toolchain version

func DetermineMainDirAndPatterns added in v0.9.1

func DetermineMainDirAndPatterns(workingDir string, goFiles []string) (string, []string, error)

DetermineMainDirAndPatterns determines the main directory and file patterns from the provided Go files

func FindMainFileAST

func FindMainFileAST(transformState *TransformState) (*ast.File, error)

FindMainFileAST finds the main file AST from the parsed packages

func FindMainFunction

func FindMainFunction(node *ast.File) *ast.FuncDecl

FindMainFunction returns the main function declaration if it exists with proper signature in package main, nil otherwise

func GenerateTempFileName

func GenerateTempFileName(originalPath string) string

GenerateTempFileName creates a unique filename for the temp directory by hashing the original path

func GetModuleName

func GetModuleName(goModPath string) (string, error)

GetModuleName reads a go.mod file and returns the module name

func HasImport

func HasImport(node *ast.File, importPath string) bool

HasImport checks if the given import path exists in the AST node

func IsModuleInWorkspace

func IsModuleInWorkspace(transformState *TransformState) (bool, error)

IsModuleInWorkspace checks if the current module is listed in the go.work file

func MakeLineDirective

func MakeLineDirective(filePath string, lineNum int) string

MakeLineDirective creates a //line directive string for the given file path and line number

func ParseGoWorkFile

func ParseGoWorkFile(goWorkPath string) ([]string, error)

ParseGoWorkFile parses a go.work file and returns the absolute paths of modules listed in the use directive

func WriteASTToFile

func WriteASTToFile(fset *token.FileSet, node *ast.File, fileName string) error

WriteASTToFile writes an AST node to a file using the provided file set

func WriteModifiedFile

func WriteModifiedFile(state *TransformState, modifiedFile *ModifiedFile) (string, error)

WriteModifiedFile applies the replacements to the original file content, generates a temporary filename, and writes the modified content to the temp file. Returns the path to the written temporary file.

Types

type AstFileWrap

type AstFileWrap struct {
	OriginalPath string
	ModifiedAST  *ast.File
	FileSet      *token.FileSet
	WasModified  bool
}

AstFileWrap represents a Go file that has been processed with AST transformations

func (*AstFileWrap) WriteToTempFile

func (a *AstFileWrap) WriteToTempFile(tempDir string) (string, error)

WriteToTempFile writes the AST to a temporary file in the specified directory

type BuildArgs

type BuildArgs struct {
	GoFiles      []string
	BuildFlags   []string
	ProgramArgs  []string
	WorkingDir   string        // will always be set (will not be empty)
	MainDir      string        // absolute path to main directory
	FilePatterns []string      // file patterns for packages.Load
	Config       config.Config // loaded configuration (must be set)
	Verbose      bool
	ConfigFile   string
}

BuildArgs contains the build configuration for loading Go files

type ModifiedFile

type ModifiedFile struct {
	FileAST           *ast.File
	Replacements      []Replacement
	RawBytes          []byte
	Modified          bool
	OutrigImportAdded bool
}

func MakeModifiedFile

func MakeModifiedFile(state *TransformState, fileAST *ast.File) (*ModifiedFile, error)

func (*ModifiedFile) AddInsert

func (mf *ModifiedFile) AddInsert(pos int64, text string)

AddInsert adds an insert replacement at the specified position

func (*ModifiedFile) AddInsertStmt

func (mf *ModifiedFile) AddInsertStmt(pos token.Position, text string)

AddInsertStmt adds an insert replacement at the specified position with automatic line directive handling. It checks the raw bytes to see if there's a newline after the position and adjusts accordingly. The token.Position provides enough information to add the "//line" directive and handle line numbering.

func (*ModifiedFile) AddLineDirective

func (mf *ModifiedFile) AddLineDirective(pos int64, fileName string, lineNum int)

AddLineDirective adds a line directive replacement at the specified position

func (*ModifiedFile) BackupToLineStart

func (mf *ModifiedFile) BackupToLineStart(pos int64) int64

BackupToLineStart backs up from the given position to find the start of the line. Returns either position 0 (start of file) or the position right after a newline character.

type OutrigDirective

type OutrigDirective struct {
	Go OutrigGoDirective
}

OutrigDirective represents a parsed //outrig comment directive

func ParseOutrigDirective

func ParseOutrigDirective(comments []*ast.CommentGroup, scope string) OutrigDirective

ParseOutrigDirective looks for //outrig comments in the comment group and returns the combined directive

func ParseOutrigDirectiveForStmt

func ParseOutrigDirectiveForStmt(fset *token.FileSet, file *ast.File, stmt ast.Stmt, scope string) OutrigDirective

ParseOutrigDirectiveForStmt looks for //outrig comments immediately before the given statement

type OutrigGoDirective

type OutrigGoDirective struct {
	Name string
	Tags string
}

type Replacement

type Replacement struct {
	Mode     string
	StartPos int64
	EndPos   int64
	NewText  []byte
}

type TransformState

type TransformState struct {
	FileSet          *token.FileSet
	PackageMap       map[string]*packages.Package
	Packages         []*packages.Package
	MainPkg          *packages.Package // never nil - always contains the main package
	OverlayMap       map[string]string
	ModifiedFiles    map[string]*ModifiedFile
	GoModPath        string // absolute path to go.mod file
	GoWorkPath       string // absolute path to go.work file (empty if not found)
	ToolchainVersion string // Go toolchain version from "go env GOVERSION"
	MainDir          string // absolute path to main directory
	TempDir          string
	Verbose          bool
	Config           config.Config
}

TransformState contains the state for AST transformations including FileSet and packages

func LoadGoFiles

func LoadGoFiles(buildArgs BuildArgs) (*TransformState, error)

LoadGoFiles loads the specified Go files using packages.Load with "file=" prefix and returns a TransformState containing the FileSet and package information

func (*TransformState) GetFilePath

func (ts *TransformState) GetFilePath(astFile *ast.File) string

GetFilePath returns the file path for the given AST file using the FileSet

Jump to

Keyboard shortcuts

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