This repository has been archived by the owner on Nov 17, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
gen.go
225 lines (210 loc) · 4.35 KB
/
gen.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"errors"
"fmt"
"go/build"
"os"
"path/filepath"
"sort"
"strings"
)
type importPackages []importPackage
type importPackage struct {
path string
isTestFile bool
}
func (slice importPackages) Len() int {
return len(slice)
}
func (slice importPackages) Less(i, j int) bool {
return slice[i].path < slice[j].path
}
func (slice importPackages) Swap(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
}
const travis_yml = ".travis.yml"
func genTravisYml() error {
_, err := os.Stat(travis_yml)
if err == nil {
return errors.New(".travis.yml already exists")
}
f, err := os.Create(travis_yml)
if err != nil {
return err
}
defer f.Close()
f.WriteString(`language: go
go:
- tip
before_install:
- go get github.com/mattn/gom
script:
- $HOME/gopath/bin/gom install
- $HOME/gopath/bin/gom test
`)
return nil
}
func appendPkg(pkgs []importPackage, pkg string) ([]importPackage, bool) {
for _, ele := range pkgs {
if ele.path == pkg {
return pkgs, false
}
}
return append(pkgs, importPackage{path: pkg}), true
}
func appendPkgs(pkgs, more []importPackage) []importPackage {
for _, pkg := range more {
pkgs, _ = appendPkg(pkgs, pkg.path)
}
return pkgs
}
func scanDirectory(path, srcDir string) (ret []importPackage, err error) {
pkg, err := build.Import(path, srcDir, build.AllowBinary)
if err != nil {
return ret, err
}
for _, imp := range pkg.Imports {
if imp == "C" {
continue
}
switch {
case pkg.Goroot:
// Ignore standard packages
case !build.IsLocalImport(imp):
// Add the external package
ret, _ = appendPkg(ret, imp)
fallthrough
default:
// Does the recursive walk
pkgs, err := scanDirectory(imp, pkg.Dir)
if err != nil {
return ret, err
}
ret = appendPkgs(ret, pkgs)
}
}
retTests := []importPackage{}
isAdd := false
for _, imp := range pkg.TestImports {
switch {
case pkg.Goroot:
// Ignore standard packages
break
case !build.IsLocalImport(imp):
// Add the external package
retTests, isAdd = appendPkg(retTests, imp)
if isAdd {
retTests[len(retTests)-1].isTestFile = true
}
}
}
ret = append(ret, retTests...)
return ret, err
}
func vcsScan(p, target string) (*vcsCmd, string, string) {
name := ""
for _, elem := range strings.Split(target, "/") {
var vcs *vcsCmd
p = filepath.Join(p, elem)
if name == "" {
name = elem
} else {
name += `/` + elem
}
if isDir(filepath.Join(p, ".git")) {
vcs = git
} else if isDir(filepath.Join(p, ".hg")) {
vcs = hg
} else if isDir(filepath.Join(p, ".bzr")) {
vcs = bzr
}
if vcs != nil {
return vcs, name, p
}
}
return nil, "", ""
}
func genGomfile() error {
_, err := os.Stat("Gomfile")
if err == nil {
return errors.New("Gomfile already exists")
}
dir, err := os.Getwd()
if err != nil {
return err
}
all, err := scanDirectory(".", dir)
if err != nil {
return err
}
sort.Sort(importPackages(all))
goms := make([]Gom, 0)
for _, pkg := range all {
for _, p := range filepath.SplitList(os.Getenv("GOPATH")) {
var vcs *vcsCmd
var n string
vcs, n, p = vcsScan(filepath.Join(p, "src"), pkg.path)
if vcs != nil {
found := false
for _, gom := range goms {
if gom.name == n {
found = true
break
}
}
if !found {
gom := Gom{name: n, options: make(map[string]interface{})}
rev, err := vcs.Revision(p)
if err == nil && rev != "" {
gom.options["commit"] = rev
}
if pkg.isTestFile {
gom.options["group"] = "test"
}
goms = append(goms, gom)
}
}
}
}
return writeGomfile("Gomfile", goms)
}
func genGomfileLock() error {
allGoms, err := parseGomfile("Gomfile")
if err != nil {
return err
}
vendor, err := filepath.Abs(vendorFolder)
if err != nil {
return err
}
goms := make([]Gom, 0)
for _, gom := range allGoms {
if group, ok := gom.options["group"]; ok {
if !matchEnv(group) {
continue
}
}
if goos, ok := gom.options["goos"]; ok {
if !matchOS(goos) {
continue
}
}
goms = append(goms, gom)
}
for _, gom := range goms {
var vcs *vcsCmd
var p string
vcs, _, p = vcsScan(vendorSrc(vendor), gom.name)
if vcs != nil {
rev, err := vcs.Revision(p)
if err == nil && rev != "" {
gom.options["commit"] = rev
}
}
}
err = writeGomfile("Gomfile.lock", goms)
if err == nil {
fmt.Println("Gomfile.lock is generated")
}
return err
}