package modfile
import (
)
type WorkFile struct {
Go *Go
Toolchain *Toolchain
Godebug []*Godebug
Use []*Use
Replace []*Replace
Syntax *FileSyntax
}
type Use struct {
Path string
ModulePath string
Syntax *Line
}
func ( string, []byte, VersionFixer) (*WorkFile, error) {
, := parse(, )
if != nil {
return nil,
}
:= &WorkFile{
Syntax: ,
}
var ErrorList
for , := range .Stmt {
switch x := .(type) {
case *Line:
.add(&, , .Token[0], .Token[1:], )
case *LineBlock:
if len(.Token) > 1 {
= append(, Error{
Filename: ,
Pos: .Start,
Err: fmt.Errorf("unknown block type: %s", strings.Join(.Token, " ")),
})
continue
}
switch .Token[0] {
default:
= append(, Error{
Filename: ,
Pos: .Start,
Err: fmt.Errorf("unknown block type: %s", strings.Join(.Token, " ")),
})
continue
case "godebug", "use", "replace":
for , := range .Line {
.add(&, , .Token[0], .Token, )
}
}
}
}
if len() > 0 {
return nil,
}
return , nil
}
func ( *WorkFile) () {
:= 0
for , := range .Use {
if .Path != "" {
.Use[] =
++
}
}
.Use = .Use[:]
= 0
for , := range .Replace {
if .Old.Path != "" {
.Replace[] =
++
}
}
.Replace = .Replace[:]
.Syntax.Cleanup()
}
func ( *WorkFile) ( string) error {
if !GoVersionRE.MatchString() {
return fmt.Errorf("invalid language version %q", )
}
if .Go == nil {
:= &Line{Token: []string{"go", }}
.Go = &Go{
Version: ,
Syntax: ,
}
:= 0
for = 0; < len(.Syntax.Stmt); ++ {
if , := .Syntax.Stmt[].(*CommentBlock); ! {
break
}
}
.Syntax.Stmt = append(append(.Syntax.Stmt[::], ), .Syntax.Stmt[:]...)
} else {
.Go.Version =
.Syntax.updateLine(.Go.Syntax, "go", )
}
return nil
}
func ( *WorkFile) ( string) error {
if !ToolchainRE.MatchString() {
return fmt.Errorf("invalid toolchain name %q", )
}
if .Toolchain == nil {
:= &Line{Token: []string{"toolchain", }}
.Toolchain = &Toolchain{
Name: ,
Syntax: ,
}
:= 0
for = 0; < len(.Syntax.Stmt); ++ {
if , := .Syntax.Stmt[].(*Line); && len(.Token) > 0 && .Token[0] == "go" {
++
goto
}
}
for = 0; < len(.Syntax.Stmt); ++ {
if , := .Syntax.Stmt[].(*CommentBlock); ! {
break
}
}
:
.Syntax.Stmt = append(append(.Syntax.Stmt[::], ), .Syntax.Stmt[:]...)
} else {
.Toolchain.Name =
.Syntax.updateLine(.Toolchain.Syntax, "toolchain", )
}
return nil
}
func ( *WorkFile) () {
if .Go != nil {
.Go.Syntax.markRemoved()
.Go = nil
}
}
func ( *WorkFile) () {
if .Toolchain != nil {
.Toolchain.Syntax.markRemoved()
.Toolchain = nil
}
}
func ( *WorkFile) (, string) error {
:= true
for , := range .Godebug {
if .Key == {
if {
.Value =
.Syntax.updateLine(.Syntax, "godebug", +"="+)
= false
} else {
.Syntax.markRemoved()
* = Godebug{}
}
}
}
if {
.addNewGodebug(, )
}
return nil
}
func ( *WorkFile) (, string) {
:= .Syntax.addLine(nil, "godebug", +"="+)
:= &Godebug{
Key: ,
Value: ,
Syntax: ,
}
.Godebug = append(.Godebug, )
}
func ( *WorkFile) ( string) error {
for , := range .Godebug {
if .Key == {
.Syntax.markRemoved()
* = Godebug{}
}
}
return nil
}
func ( *WorkFile) (, string) error {
:= true
for , := range .Use {
if .Path == {
if {
.ModulePath =
.Syntax.updateLine(.Syntax, "use", AutoQuote())
= false
} else {
.Syntax.markRemoved()
* = Use{}
}
}
}
if {
.AddNewUse(, )
}
return nil
}
func ( *WorkFile) (, string) {
:= .Syntax.addLine(nil, "use", AutoQuote())
.Use = append(.Use, &Use{Path: , ModulePath: , Syntax: })
}
func ( *WorkFile) ( []*Use) {
:= make(map[string]string)
for , := range {
[.Path] = .ModulePath
}
for , := range .Use {
if , := [.Path]; {
.ModulePath =
} else {
.Syntax.markRemoved()
* = Use{}
}
}
for , := range {
.AddNewUse(, )
}
.SortBlocks()
}
func ( *WorkFile) ( string) error {
for , := range .Use {
if .Path == {
.Syntax.markRemoved()
* = Use{}
}
}
return nil
}
func ( *WorkFile) (, , , string) error {
return addReplace(.Syntax, &.Replace, , , , )
}
func ( *WorkFile) (, string) error {
for , := range .Replace {
if .Old.Path == && .Old.Version == {
.Syntax.markRemoved()
* = Replace{}
}
}
return nil
}
func ( *WorkFile) () {
.removeDups()
for , := range .Syntax.Stmt {
, := .(*LineBlock)
if ! {
continue
}
slices.SortStableFunc(.Line, compareLine)
}
}
func ( *WorkFile) () {
removeDups(.Syntax, nil, &.Replace, nil, nil)
}