-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen.go
167 lines (153 loc) · 3.07 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
package main
import (
"errors"
"fmt"
"go/build"
"os"
"path/filepath"
"sort"
"strings"
)
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
}
// http://code.google.com/p/go/source/browse/src/cmd/go/pkg.go?name=go1.1.2#96
func isStandardImport(path string) bool {
return !strings.Contains(path, ".")
}
func appendPkg(pkgs []string, pkg string) []string {
for _, ele := range pkgs {
if ele == pkg {
return pkgs
}
}
return append(pkgs, pkg)
}
func appendPkgs(pkgs, more []string) []string {
for _, pkg := range more {
pkgs = appendPkg(pkgs, pkg)
}
return pkgs
}
func scanDirectory(path, srcDir string) (ret []string, err error) {
pkg, err := build.Import(path, srcDir, build.AllowBinary)
if err != nil {
return ret, err
}
for _, imp := range pkg.Imports {
switch {
case isStandardImport(imp):
// 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)
}
}
return ret, err
}
func genGomfile() error {
_, err := os.Stat("Gomfile")
if err == nil {
return errors.New("Gomfile already exists")
}
f, err := os.Create("Gomfile")
if err != nil {
return err
}
defer f.Close()
dir, err := os.Getwd()
if err != nil {
return err
}
all, err := scanDirectory(".", dir)
if err != nil {
return err
}
sort.Strings(all)
for _, pkg := range all {
fmt.Fprintf(f, "gom '%s'\n", pkg)
}
return nil
}
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
p := filepath.Join(vendorSrc(vendor), gom.name)
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 {
rev, err := vcs.Revision(p)
if err == nil && rev != "" {
gom.options["commit"] = rev
}
}
}
f, err := os.Create("Gomfile.lock")
if err != nil {
return err
}
defer f.Close()
for _, gom := range goms {
if rev, ok := gom.options["commit"]; ok {
fmt.Fprintf(f, "gom '%s', :commit => '%s'\n", gom.name, rev.(string))
} else {
fmt.Fprintf(f, "gom '%s'\n", gom.name)
}
}
fmt.Println("Gomfile.lock is generated")
return nil
}