update go.mod in temp, if it has relative replace
This commit is contained in:
parent
79310ccb4d
commit
a18bd08ddd
1
go.mod
1
go.mod
@ -11,6 +11,7 @@ require (
|
||||
github.com/tongjingran/copy v1.4.2 // indirect
|
||||
go.uber.org/multierr v1.6.0 // indirect
|
||||
go.uber.org/zap v1.16.0
|
||||
golang.org/x/mod v0.4.2
|
||||
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 // indirect
|
||||
k8s.io/kubectl v0.20.5 // indirect
|
||||
)
|
||||
|
3
go.sum
3
go.sum
@ -446,6 +446,8 @@ golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzB
|
||||
golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
|
||||
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
|
||||
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
@ -581,6 +583,7 @@ golang.org/x/tools v0.0.0-20200616133436-c1934b75d054/go.mod h1:EkVYQZoAsY45+roY
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
|
||||
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=
|
||||
|
@ -3,12 +3,15 @@ package build
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/qiniu/goc/v2/pkg/config"
|
||||
"github.com/qiniu/goc/v2/pkg/log"
|
||||
"github.com/tongjingran/copy"
|
||||
"golang.org/x/mod/modfile"
|
||||
)
|
||||
|
||||
// copyProjectToTmp copies project files to the temporary directory
|
||||
@ -73,3 +76,55 @@ func (b *Build) clean() {
|
||||
log.Debugf("--debug is enabled, keep the temporary project")
|
||||
}
|
||||
}
|
||||
|
||||
// updateGoModFile rewrites the go.mod file in the temporary directory,
|
||||
//
|
||||
// if it has a 'replace' directive, and the directive has a relative local path,
|
||||
// it will be rewritten with a absolute path.
|
||||
//
|
||||
// ex.
|
||||
//
|
||||
// suppose original project is located at /path/to/aa/bb/cc, go.mod contains a directive:
|
||||
// 'replace github.com/qiniu/bar => ../home/foo/bar'
|
||||
//
|
||||
// after the project is copied to temporary directory, it should be rewritten as
|
||||
// 'replace github.com/qiniu/bar => /path/to/aa/bb/home/foo/bar'
|
||||
func (b *Build) updateGoModFile() (updateFlag bool, newModFile []byte) {
|
||||
tempModfile := filepath.Join(config.GocConfig.TmpModProjectDir, "go.mod")
|
||||
buf, err := ioutil.ReadFile(tempModfile)
|
||||
if err != nil {
|
||||
log.Fatalf("cannot find go.mod file in temporary directory: %v", err)
|
||||
}
|
||||
oriGoModFile, err := modfile.Parse(tempModfile, buf, nil)
|
||||
if err != nil {
|
||||
log.Fatalf("cannot parse go.mod: %v", err)
|
||||
}
|
||||
|
||||
updateFlag = false
|
||||
for index := range oriGoModFile.Replace {
|
||||
replace := oriGoModFile.Replace[index]
|
||||
oldPath := replace.Old.Path
|
||||
oldVersion := replace.Old.Version
|
||||
newPath := replace.New.Path
|
||||
newVersion := replace.New.Version
|
||||
// replace to a local filesystem does not have a version
|
||||
// absolute path no need to rewrite
|
||||
if newVersion == "" && !filepath.IsAbs(newPath) {
|
||||
var absPath string
|
||||
fullPath := filepath.Join(config.GocConfig.CurModProjectDir, newPath)
|
||||
absPath, _ = filepath.Abs(fullPath)
|
||||
// DropReplace & AddReplace will not return error
|
||||
// so no need to check the error
|
||||
_ = oriGoModFile.DropReplace(oldPath, oldVersion)
|
||||
_ = oriGoModFile.AddReplace(oldPath, oldVersion, absPath, newVersion)
|
||||
updateFlag = true
|
||||
}
|
||||
}
|
||||
oriGoModFile.Cleanup()
|
||||
// Format will not return error, so ignore the returned error
|
||||
// func (f *File) Format() ([]byte, error) {
|
||||
// return Format(f.Syntax), nil
|
||||
// }
|
||||
newModFile, _ = oriGoModFile.Format()
|
||||
return
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user