Documentation
¶
Index ¶
- Constants
- func AddOutrigImport(fset *token.FileSet, node *ast.File) bool
- func AddOutrigImportReplacement(state *TransformState, file *ModifiedFile) error
- func AddOutrigSDKDependency(tempGoModPath string, verbose bool, cfg config.Config) error
- func ApplyReplacements(fileBytes []byte, rs []Replacement) []byte
- func DetectToolchainVersion(pkgDir string) (string, error)
- func DetermineMainDirAndPatterns(workingDir string, goFiles []string) (string, []string, error)
- func FindMainFileAST(transformState *TransformState) (*ast.File, error)
- func FindMainFunction(node *ast.File) *ast.FuncDecl
- func GenerateTempFileName(originalPath string) string
- func GetModuleName(goModPath string) (string, error)
- func HasImport(node *ast.File, importPath string) bool
- func IsModuleInWorkspace(transformState *TransformState) (bool, error)
- func MakeLineDirective(filePath string, lineNum int) string
- func ParseGoWorkFile(goWorkPath string) ([]string, error)
- func WriteASTToFile(fset *token.FileSet, node *ast.File, fileName string) error
- func WriteModifiedFile(state *TransformState, modifiedFile *ModifiedFile) (string, error)
- type AstFileWrap
- type BuildArgs
- type ModifiedFile
- type OutrigDirective
- type OutrigGoDirective
- type Replacement
- type TransformState
Constants ¶
const ( ReplacementModeInsert = "insert" ReplacementModeDelete = "delete" )
const OutrigImportPath = "github.com/outrigdev/outrig"
const ScopeGo = "go"
Variables ¶
This section is empty.
Functions ¶
func AddOutrigImport ¶
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 ¶
AddOutrigSDKDependency adds the version locked Outrig SDK to the temp go.mod file
func ApplyReplacements ¶
func ApplyReplacements(fileBytes []byte, rs []Replacement) []byte
func DetectToolchainVersion ¶
DetectToolchainVersion runs "go env GOVERSION" to get the Go toolchain version
func DetermineMainDirAndPatterns ¶ added in v0.9.1
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 ¶
FindMainFunction returns the main function declaration if it exists with proper signature in package main, nil otherwise
func GenerateTempFileName ¶
GenerateTempFileName creates a unique filename for the temp directory by hashing the original path
func GetModuleName ¶
GetModuleName reads a go.mod file and returns the module name
func IsModuleInWorkspace ¶
func IsModuleInWorkspace(transformState *TransformState) (bool, error)
IsModuleInWorkspace checks if the current module is listed in the go.work file
func MakeLineDirective ¶
MakeLineDirective creates a //line directive string for the given file path and line number
func ParseGoWorkFile ¶
ParseGoWorkFile parses a go.work file and returns the absolute paths of modules listed in the use directive
func WriteASTToFile ¶
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 Replacement ¶
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