2021-05-23 14:23:35 +00:00
|
|
|
|
package build
|
|
|
|
|
|
|
|
|
|
import (
|
2021-06-20 13:14:21 +00:00
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
|
|
|
|
|
|
|
|
|
"github.com/qiniu/goc/v2/pkg/config"
|
2021-06-17 11:53:00 +00:00
|
|
|
|
"github.com/qiniu/goc/v2/pkg/cover"
|
2021-05-23 14:23:35 +00:00
|
|
|
|
"github.com/qiniu/goc/v2/pkg/flag"
|
|
|
|
|
"github.com/qiniu/goc/v2/pkg/log"
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Build struct a build
|
|
|
|
|
// most configurations are stored in global variables: config.GocConfig & config.GoConfig
|
|
|
|
|
type Build struct {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewBuild creates a Build struct
|
|
|
|
|
//
|
|
|
|
|
// consumes args, get package dirs, read project meta info.
|
|
|
|
|
func NewBuild(cmd *cobra.Command, args []string) *Build {
|
|
|
|
|
b := &Build{}
|
2021-06-20 13:14:21 +00:00
|
|
|
|
// 1. 解析 goc 命令行和 go 命令行
|
2021-05-23 14:23:35 +00:00
|
|
|
|
remainedArgs := flag.BuildCmdArgsParse(cmd, args)
|
2021-06-20 13:14:21 +00:00
|
|
|
|
// 2. 解析 go 包位置
|
2021-05-23 14:23:35 +00:00
|
|
|
|
flag.GetPackagesDir(remainedArgs)
|
2021-06-20 13:14:21 +00:00
|
|
|
|
// 3. 读取工程元信息:go.mod, pkgs list ...
|
2021-05-23 14:23:35 +00:00
|
|
|
|
b.readProjectMetaInfo()
|
2021-06-20 13:14:21 +00:00
|
|
|
|
// 4. 展示元信息
|
2021-05-23 14:23:35 +00:00
|
|
|
|
b.displayProjectMetaInfo()
|
|
|
|
|
|
|
|
|
|
return b
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build starts go build
|
|
|
|
|
//
|
|
|
|
|
// 1. copy project to temp,
|
|
|
|
|
// 2. inject cover variables and functions into the project,
|
|
|
|
|
// 3. build the project in temp.
|
|
|
|
|
func (b *Build) Build() {
|
2021-06-20 13:14:21 +00:00
|
|
|
|
// 1. 拷贝至临时目录
|
2021-05-23 14:23:35 +00:00
|
|
|
|
b.copyProjectToTmp()
|
2021-06-17 11:53:00 +00:00
|
|
|
|
// defer b.clean()
|
2021-05-23 14:23:35 +00:00
|
|
|
|
log.Donef("project copied to temporary directory")
|
2021-06-20 13:14:21 +00:00
|
|
|
|
// 2. inject cover vars
|
2021-06-17 11:53:00 +00:00
|
|
|
|
cover.Inject()
|
2021-06-20 13:14:21 +00:00
|
|
|
|
// 3. build in the temp project
|
|
|
|
|
b.doBuildInTemp()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (b *Build) doBuildInTemp() {
|
|
|
|
|
goflags := config.GocConfig.Goflags
|
|
|
|
|
// 检查用户是否自定义了 -o
|
|
|
|
|
oSet := false
|
|
|
|
|
for _, flag := range goflags {
|
|
|
|
|
if flag == "-o" {
|
|
|
|
|
oSet = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果没被设置就加一个至原命令执行的目录
|
|
|
|
|
if !oSet {
|
|
|
|
|
goflags = append(goflags, "-o", config.GocConfig.CurWd)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pacakges := config.GocConfig.TmpPkgDir
|
|
|
|
|
if config.GocConfig.ContainSpecialPattern {
|
|
|
|
|
pacakges = pacakges + "/..."
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
goflags = append(goflags, pacakges)
|
|
|
|
|
|
|
|
|
|
args := []string{"build"}
|
|
|
|
|
args = append(args, goflags...)
|
|
|
|
|
// go 命令行由 go build [-o output] [build flags] [packages] 组成
|
|
|
|
|
cmd := exec.Command("go", args...)
|
|
|
|
|
cmd.Dir = config.GocConfig.TmpModProjectDir
|
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
|
|
log.Infof("go build cmd is: %v", cmd.Args)
|
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
|
log.Fatalf("fail to execute go build: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if err := cmd.Wait(); err != nil {
|
|
|
|
|
log.Fatalf("fail to execute go build: %v", err)
|
|
|
|
|
}
|
2021-06-20 08:25:30 +00:00
|
|
|
|
|
2021-06-20 13:14:21 +00:00
|
|
|
|
// done
|
|
|
|
|
log.Donef("go build done")
|
2021-05-23 14:23:35 +00:00
|
|
|
|
}
|