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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
2020-12-14 12:16:01 +00:00
|
|
|
"strings"
|
2020-05-21 06:30:41 +00:00
|
|
|
|
2020-12-21 05:18:49 +00:00
|
|
|
cc "github.com/otiai10/copy" // conflit with builtin copy
|
2020-05-21 06:30:41 +00:00
|
|
|
"github.com/qiniu/goc/pkg/cover"
|
2020-12-21 05:18:49 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-05-21 06:30:41 +00:00
|
|
|
)
|
|
|
|
|
2020-12-18 11:54:46 +00:00
|
|
|
func (b *Build) cpProject() {
|
2020-05-21 06:30:41 +00:00
|
|
|
visited := make(map[string]bool)
|
2020-06-12 09:51:10 +00:00
|
|
|
for k, v := range b.Pkgs {
|
|
|
|
dst := filepath.Join(b.TmpDir, "src", k)
|
2020-05-21 06:30:41 +00:00
|
|
|
src := v.Dir
|
|
|
|
|
|
|
|
if _, ok := visited[src]; ok {
|
|
|
|
// Skip if already copied
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:16:01 +00:00
|
|
|
if err := b.copyDir(v); err != nil {
|
2020-06-16 05:21:28 +00:00
|
|
|
log.Errorf("Failed to Copy the folder from %v to %v, the error is: %v ", src, dst, err)
|
2020-05-21 06:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
visited[src] = true
|
2020-12-14 02:05:59 +00:00
|
|
|
}
|
|
|
|
if b.IsMod {
|
|
|
|
for _, v := range b.Pkgs {
|
|
|
|
if v.Name == "main" {
|
|
|
|
dst := filepath.Join(b.TmpDir, "go.mod")
|
|
|
|
src := filepath.Join(v.Module.Dir, "go.mod")
|
2020-12-21 05:18:49 +00:00
|
|
|
if err := cc.Copy(src, dst); err != nil {
|
2020-12-14 02:05:59 +00:00
|
|
|
log.Errorf("Failed to Copy the go mod file from %v to %v, the error is: %v ", src, dst, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dst = filepath.Join(b.TmpDir, "go.sum")
|
|
|
|
src = filepath.Join(v.Module.Dir, "go.sum")
|
2020-12-21 05:18:49 +00:00
|
|
|
if err := cc.Copy(src, dst); err != nil && !strings.Contains(err.Error(), "no such file or directory") {
|
2020-12-14 02:05:59 +00:00
|
|
|
log.Errorf("Failed to Copy the go mod file from %v to %v, the error is: %v ", src, dst, err)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2020-05-21 06:30:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 12:16:01 +00:00
|
|
|
func (b *Build) copyDir(pkg *cover.Package) error {
|
2020-12-14 02:05:59 +00:00
|
|
|
fileList := []string{}
|
|
|
|
dir := pkg.Dir
|
2020-12-21 05:18:49 +00:00
|
|
|
fileList = getFileListNeedsCopy(pkg)
|
2020-12-14 02:05:59 +00:00
|
|
|
for _, file := range fileList {
|
|
|
|
p := filepath.Join(dir, file)
|
|
|
|
var src, root string
|
|
|
|
if pkg.Root == "" {
|
|
|
|
root = b.WorkingDir // use workingDir instead when the root is empty.
|
2020-06-16 05:21:28 +00:00
|
|
|
} else {
|
2020-12-14 02:05:59 +00:00
|
|
|
root = pkg.Root
|
|
|
|
}
|
2020-12-14 12:16:01 +00:00
|
|
|
src = strings.TrimPrefix(pkg.Dir, root) // get the relative path of the files
|
|
|
|
dst := filepath.Join(b.TmpDir, src, file) // it will adapt the case where src is ""
|
2020-12-21 05:18:49 +00:00
|
|
|
if err := cc.Copy(p, dst); err != nil {
|
2020-12-14 02:05:59 +00:00
|
|
|
log.Errorf("Failed to Copy the folder from %v to %v, the error is: %v ", src, dst, err)
|
|
|
|
return err
|
2020-06-16 05:21:28 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-14 02:05:59 +00:00
|
|
|
return nil
|
2020-06-16 05:21:28 +00:00
|
|
|
}
|
2020-12-21 05:18:49 +00:00
|
|
|
|
|
|
|
// original implementation
|
|
|
|
// goos: darwin
|
|
|
|
// goarch: amd64
|
|
|
|
// pkg: github.com/qiniu/goc/pkg/build
|
|
|
|
// BenchmarkGetFileList-4 1535029 778 ns/op 1488 B/op 5 allocs/op
|
|
|
|
// func getFileListNeedsCopy(pkg *cover.Package) []string {
|
|
|
|
// fileList := []string{}
|
|
|
|
|
|
|
|
// fileList = append(fileList, pkg.GoFiles...)
|
|
|
|
// fileList = append(fileList, pkg.CompiledGoFiles...)
|
|
|
|
// fileList = append(fileList, pkg.IgnoredGoFiles...)
|
|
|
|
// fileList = append(fileList, pkg.CFiles...)
|
|
|
|
// fileList = append(fileList, pkg.CXXFiles...)
|
|
|
|
// fileList = append(fileList, pkg.MFiles...)
|
|
|
|
// fileList = append(fileList, pkg.HFiles...)
|
|
|
|
// fileList = append(fileList, pkg.FFiles...)
|
|
|
|
// fileList = append(fileList, pkg.SFiles...)
|
|
|
|
// fileList = append(fileList, pkg.SwigCXXFiles...)
|
|
|
|
// fileList = append(fileList, pkg.SwigFiles...)
|
|
|
|
// fileList = append(fileList, pkg.SysoFiles...)
|
|
|
|
|
|
|
|
// return fileList
|
|
|
|
// }
|
|
|
|
|
|
|
|
// new implementation
|
|
|
|
// goos: darwin
|
|
|
|
// goarch: amd64
|
|
|
|
// pkg: github.com/qiniu/goc/pkg/build
|
|
|
|
// BenchmarkGetFileList-4 3884557 298 ns/op 576 B/op 1 allocs/op
|
|
|
|
func getFileListNeedsCopy(pkg *cover.Package) []string {
|
|
|
|
fileSlices := [][]string{
|
|
|
|
pkg.GoFiles,
|
|
|
|
pkg.CompiledGoFiles,
|
|
|
|
pkg.IgnoredGoFiles,
|
|
|
|
pkg.CFiles,
|
|
|
|
pkg.CXXFiles,
|
|
|
|
pkg.MFiles,
|
|
|
|
pkg.HFiles,
|
|
|
|
pkg.FFiles,
|
|
|
|
pkg.SFiles,
|
|
|
|
pkg.SwigCXXFiles,
|
|
|
|
pkg.SwigFiles,
|
|
|
|
pkg.SysoFiles,
|
|
|
|
}
|
|
|
|
|
|
|
|
var totalLen int
|
|
|
|
for _, s := range fileSlices {
|
|
|
|
totalLen += len(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp := make([]string, totalLen)
|
|
|
|
var i int
|
|
|
|
for _, s := range fileSlices {
|
|
|
|
i += copy(tmp[i:], s)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tmp
|
|
|
|
}
|