-
Notifications
You must be signed in to change notification settings - Fork 1
/
tmp_proj.go
132 lines (121 loc) · 3.32 KB
/
tmp_proj.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package stroo
import (
"errors"
"fmt"
"golang.org/x/tools/go/packages"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"regexp"
"strings"
)
type TemporaryProject struct {
Config *packages.Config
Modules []TemporaryPackage
TempDirName string
packsWritten map[string]map[string]string
}
func (e *TemporaryProject) Filename(tmpPackName, tmpPackFileName string) string {
return filepath.Join(e.goPathDir(tmpPackName), "src", tmpPackName, tmpPackFileName)
}
func (e *TemporaryProject) Finalize() error {
e.Config.Env = append(e.Config.Env, "GO111MODULE=off")
goPath := ""
for tempPack := range e.packsWritten {
if goPath != "" {
goPath += string(filepath.ListSeparator)
}
dir := e.goPathDir(tempPack)
goPath += dir
}
e.Config.Env = append(e.Config.Env, "GOPATH="+goPath)
return nil
}
// must be called by the caller, as in `defer tmpProj.Cleanup()`
func (e *TemporaryProject) Cleanup() {
if e.TempDirName == "" {
return
}
if err := filepath.Walk(e.TempDirName, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info.IsDir() {
_ = os.Chmod(path, 0777)
}
return nil
}); err != nil {
log.Printf("error walking : %v\n", err)
}
if err := os.RemoveAll(e.TempDirName); err != nil {
log.Printf("error removing : %v\n", err)
}
e.TempDirName = ""
}
func (e *TemporaryProject) File(tmpPackName, tmpPackFileName string) string {
if m := e.packsWritten[tmpPackName]; m != nil {
return m[tmpPackFileName]
}
return ""
}
var versionSuffixRE = regexp.MustCompile(`v\d+`)
func (e *TemporaryProject) goPathDir(tmpPackName string) string {
dir := path.Base(tmpPackName)
if versionSuffixRE.MatchString(dir) {
dir = path.Base(path.Dir(tmpPackName)) + "_" + dir
}
return filepath.Join(e.TempDirName, dir)
}
type TemporaryPackage struct {
Name string
Files map[string]interface{}
}
func CreateTempProj(tmpPacks []TemporaryPackage) (*TemporaryProject, error) {
if len(tmpPacks) == 0 {
return nil, errors.New("at least one temporary package is required")
}
dirName := strings.Replace(tmpPacks[0].Name, "/", "_", -1)
tempFolder, err := ioutil.TempDir("", dirName)
if err != nil {
return nil, err
}
result := &TemporaryProject{
Config: &packages.Config{
Dir: tempFolder,
Env: append(os.Environ(), "GOPACKAGESDRIVER=off", "GOROOT="),
Tests: true,
Mode: packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedImports,
},
Modules: tmpPacks,
TempDirName: tempFolder,
packsWritten: map[string]map[string]string{},
}
for _, tPack := range tmpPacks {
for filePiece, value := range tPack.Files {
fullPath := result.Filename(tPack.Name, filepath.FromSlash(filePiece))
existingFilePiece, has := result.packsWritten[tPack.Name]
if !has {
existingFilePiece = map[string]string{}
result.packsWritten[tPack.Name] = existingFilePiece
}
existingFilePiece[filePiece] = fullPath
if err := os.MkdirAll(filepath.Dir(fullPath), 0755); err != nil {
return nil, err
}
switch value := value.(type) {
case string:
if err := ioutil.WriteFile(fullPath, []byte(value), 0644); err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("invalid type %T in files, must be string", value)
}
}
}
if err := result.Finalize(); err != nil {
return nil, err
}
return result, nil
}