forked from gobuffalo/buffalo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.go
74 lines (64 loc) · 1.55 KB
/
files.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
package generators
import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/gobuffalo/envy"
)
// File represents the file to be templated
type File struct {
ReadPath string
WritePath string
Body string
}
// Files is a slice of File
type Files []File
// Find all the .tmpl files inside the buffalo GOPATH
func Find(path string) (Files, error) {
gp, err := goPath()
if err != nil {
return nil, err
}
root := filepath.Join(gp, "src", "github.com", "gobuffalo", "buffalo", "generators", path, "templates")
files := Files{}
err = filepath.Walk(root, func(p string, info os.FileInfo, err error) error {
if info != nil && !info.IsDir() {
if filepath.Ext(p) == ".tmpl" {
f := File{ReadPath: p}
rel := strings.TrimPrefix(p, root)
paths := strings.Split(rel, string(os.PathSeparator))
li := len(paths) - 1
base := paths[li]
base = strings.TrimSuffix(base, ".tmpl")
if strings.HasPrefix(base, "dot-") {
base = "." + strings.TrimPrefix(base, "dot-")
}
paths[li] = base
f.WritePath = filepath.Join(paths...)
b, err := ioutil.ReadFile(p)
if err != nil {
return err
}
f.Body = string(b)
files = append(files, f)
}
}
return nil
})
return files, err
}
func goPath() (string, error) {
for _, path := range envy.GoPaths() {
pp := filepath.Join(path, "src", "github.com", "gobuffalo", "buffalo")
if exists(pp) {
return path, nil
}
}
return "", errors.New("buffalo was not found")
}
func exists(path string) bool {
_, err := os.Stat(path)
return err == nil
}