2020-05-21 06:30:41 +00:00
|
|
|
/*
|
2020-05-25 16:19:20 +00:00
|
|
|
Copyright 2020 Qiniu Cloud (qiniu.com)
|
2020-05-21 06:30:41 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-05-21 06:58:40 +00:00
|
|
|
package cmd
|
2020-05-21 06:30:41 +00:00
|
|
|
|
|
|
|
import (
|
2020-06-18 09:03:14 +00:00
|
|
|
"os"
|
|
|
|
|
2020-06-18 08:20:54 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-06-16 09:00:38 +00:00
|
|
|
|
2020-06-13 10:15:51 +00:00
|
|
|
"github.com/qiniu/goc/pkg/build"
|
2020-06-14 11:37:27 +00:00
|
|
|
"github.com/qiniu/goc/pkg/cover"
|
2020-05-21 06:30:41 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var buildCmd = &cobra.Command{
|
|
|
|
Use: "build",
|
|
|
|
Short: "Do cover for all go files and execute go build command",
|
2020-06-03 09:12:47 +00:00
|
|
|
Long: `
|
2020-06-18 08:20:54 +00:00
|
|
|
Build command will copy the project code and its necessary dependencies to a temporary directory, then do cover for the target, binaries will be generated to their original place.
|
2020-06-16 05:21:28 +00:00
|
|
|
`,
|
2020-06-03 09:12:47 +00:00
|
|
|
Example: `
|
|
|
|
# Build the current binary with cover variables injected. The binary will be generated in the current folder.
|
2020-06-27 05:24:57 +00:00
|
|
|
goc build .
|
2020-06-03 09:12:47 +00:00
|
|
|
|
2020-06-05 02:17:51 +00:00
|
|
|
# Build the current binary with cover variables injected, and set the registry center to http://127.0.0.1:7777.
|
2020-06-03 09:12:47 +00:00
|
|
|
goc build --center=http://127.0.0.1:7777
|
|
|
|
|
2020-06-05 02:17:51 +00:00
|
|
|
# Build the current binary with cover variables injected, and redirect output to /to/this/path.
|
2020-06-16 05:21:28 +00:00
|
|
|
goc build --output /to/this/path
|
2020-06-03 09:12:47 +00:00
|
|
|
|
2020-06-05 02:17:51 +00:00
|
|
|
# Build the current binary with cover variables injected, and set necessary build flags: -ldflags "-extldflags -static" -tags="embed kodo".
|
2020-06-16 05:21:28 +00:00
|
|
|
goc build --buildflags="-ldflags '-extldflags -static' -tags='embed kodo'"
|
2020-06-03 09:12:47 +00:00
|
|
|
`,
|
2020-05-21 06:30:41 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2020-06-18 09:03:14 +00:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Fail to build: %v", err)
|
|
|
|
}
|
|
|
|
runBuild(args, wd)
|
2020-05-21 06:30:41 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-06-13 10:15:51 +00:00
|
|
|
var buildOutput string
|
|
|
|
|
2020-05-21 06:30:41 +00:00
|
|
|
func init() {
|
2020-06-12 09:51:10 +00:00
|
|
|
addBuildFlags(buildCmd.Flags())
|
2020-06-29 11:23:38 +00:00
|
|
|
buildCmd.Flags().StringVarP(&buildOutput, "output", "o", "", "it forces build to write the resulting executable to the named output file")
|
2020-05-21 06:30:41 +00:00
|
|
|
rootCmd.AddCommand(buildCmd)
|
|
|
|
}
|
2020-06-16 09:00:38 +00:00
|
|
|
|
2020-06-18 09:03:14 +00:00
|
|
|
func runBuild(args []string, wd string) {
|
|
|
|
gocBuild, err := build.NewBuild(buildFlags, args, wd, buildOutput)
|
2020-06-16 09:00:38 +00:00
|
|
|
if err != nil {
|
2020-06-18 08:20:54 +00:00
|
|
|
log.Fatalf("Fail to build: %v", err)
|
2020-06-16 09:00:38 +00:00
|
|
|
}
|
|
|
|
// remove temporary directory if needed
|
|
|
|
defer gocBuild.Clean()
|
|
|
|
// doCover with original buildFlags, with new GOPATH( tmp:original )
|
|
|
|
// in the tmp directory
|
2020-07-06 13:20:39 +00:00
|
|
|
cover.Execute(buildFlags, gocBuild.NewGOPATH, gocBuild.TmpDir, coverMode.String(), agentPort.String(), center)
|
2020-06-16 09:00:38 +00:00
|
|
|
// do install in the temporary directory
|
|
|
|
err = gocBuild.Build()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Fail to build: %v", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|