goc/pkg/build/tmpfolder.go

160 lines
3.8 KiB
Go
Raw Normal View History

/*
2020-05-25 16:19:20 +00:00
Copyright 2020 Qiniu Cloud (qiniu.com)
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 (
"crypto/sha256"
"fmt"
2020-06-13 10:15:51 +00:00
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"os"
"path/filepath"
"strings"
"github.com/qiniu/goc/pkg/cover"
)
2020-06-13 10:15:51 +00:00
func (b *Build) MvProjectsToTmp() {
2020-06-12 09:51:10 +00:00
listArgs := []string{"-json"}
if len(b.BuildFlags) != 0 {
listArgs = append(listArgs, b.BuildFlags)
}
listArgs = append(listArgs, "./...")
2020-06-12 09:51:10 +00:00
b.Pkgs = cover.ListPackages(".", strings.Join(listArgs, " "), "")
2020-06-12 09:51:10 +00:00
b.mvProjectsToTmp()
2020-06-13 10:15:51 +00:00
b.OriGOPATH = os.Getenv("GOPATH")
2020-06-12 09:51:10 +00:00
if b.IsMod == true {
b.NewGOPATH = ""
2020-06-13 10:15:51 +00:00
} else if b.OriGOPATH == "" {
2020-06-12 09:51:10 +00:00
b.NewGOPATH = b.TmpDir
} else {
2020-06-13 10:15:51 +00:00
b.NewGOPATH = fmt.Sprintf("%v:%v", b.TmpDir, b.OriGOPATH)
}
2020-06-12 09:51:10 +00:00
log.Printf("New GOPATH: %v", b.NewGOPATH)
return
}
2020-06-12 09:51:10 +00:00
func (b *Build) mvProjectsToTmp() {
path, err := os.Getwd()
if err != nil {
log.Fatalf("Cannot get current working directoy, the error is: %v", err)
}
2020-06-12 09:51:10 +00:00
b.TmpDir = filepath.Join(os.TempDir(), TmpFolderName(path))
// Delete previous tmp folder and its content
2020-06-12 09:51:10 +00:00
os.RemoveAll(b.TmpDir)
// Create a new tmp folder
2020-06-12 09:51:10 +00:00
err = os.MkdirAll(filepath.Join(b.TmpDir, "src"), os.ModePerm)
if err != nil {
log.Fatalf("Fail to create the temporary build directory. The err is: %v", err)
}
2020-06-12 09:51:10 +00:00
log.Printf("Tmp project generated in: %v", b.TmpDir)
2020-06-12 09:51:10 +00:00
// set Build.IsMod flag, so we dont have to call checkIfLegacyProject another time
if b.checkIfLegacyProject() {
b.cpLegacyProject()
} else {
2020-06-12 09:51:10 +00:00
b.IsMod = true
b.cpGoModulesProject()
}
2020-06-12 09:51:10 +00:00
b.getTmpwd()
2020-06-12 09:51:10 +00:00
log.Printf("New workingdir in tmp directory in: %v", b.TmpWorkingDir)
}
func TmpFolderName(path string) string {
sum := sha256.Sum256([]byte(path))
h := fmt.Sprintf("%x", sum[:6])
return "goc-" + h
}
2020-06-12 09:51:10 +00:00
// checkIfLegacyProject Check if it is go module project
// true legacy
2020-06-07 06:03:47 +00:00
// false go mod
2020-06-12 09:51:10 +00:00
func (b *Build) checkIfLegacyProject() bool {
for _, v := range b.Pkgs {
2020-06-13 10:15:51 +00:00
// get root
b.Root = v.Root
if v.Module == nil {
return true
}
return false
}
log.Fatalln("Should never be reached....")
return false
}
2020-06-12 09:51:10 +00:00
// getTmpwd get the corresponding working directory in the temporary working directory
// and store it in the Build.tmpWorkdingDir
func (b *Build) getTmpwd() {
for _, pkg := range b.Pkgs {
path, err := os.Getwd()
if err != nil {
2020-06-07 06:03:47 +00:00
log.Fatalf("Cannot get current working directory, the error is: %v", err)
}
index := -1
var parentPath string
2020-06-12 09:51:10 +00:00
if b.IsMod == false {
index = strings.Index(path, pkg.Root)
parentPath = pkg.Root
} else {
index = strings.Index(path, pkg.Module.Dir)
parentPath = pkg.Module.Dir
}
if index == -1 {
log.Fatalf("goc install not executed in project directory.")
}
2020-06-12 09:51:10 +00:00
b.TmpWorkingDir = filepath.Join(b.TmpDir, path[len(parentPath):])
// log.Printf("New building directory in: %v", tmpwd)
2020-06-12 09:51:10 +00:00
return
}
log.Fatalln("Should never be reached....")
2020-06-12 09:51:10 +00:00
return
}
2020-06-12 09:51:10 +00:00
func (b *Build) findWhereToInstall() string {
if GOBIN := os.Getenv("GOBIN"); GOBIN != "" {
return GOBIN
}
// old GOPATH dir
GOPATH := os.Getenv("GOPATH")
2020-06-12 09:51:10 +00:00
if false == b.IsMod {
for _, v := range b.Pkgs {
return filepath.Join(v.Root, "bin")
}
}
if GOPATH != "" {
return filepath.Join(strings.Split(GOPATH, ":")[0], "bin")
}
return filepath.Join(os.Getenv("HOME"), "go", "bin")
}
2020-06-13 10:15:51 +00:00
func (b *Build) RemoveTmpDir() {
debuggoc := viper.GetBool("debuggoc")
if debuggoc == false {
if b.TmpDir != "" {
os.RemoveAll(b.TmpDir)
}
}
}