This repository has been archived by the owner on Feb 24, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 579
/
Copy pathapp.go
154 lines (139 loc) · 3.66 KB
/
app.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
package meta
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"github.com/gobuffalo/envy"
"github.com/markbates/inflect"
)
var modsOn = (strings.TrimSpace(envy.Get("GO111MODULE", "off")) == "on")
func init() {
if modsOn {
fmt.Println("experimental go modules support has been enabled [GO111MODULE=on]")
}
}
// App represents meta data for a Buffalo application on disk
type App struct {
Pwd string `json:"pwd"`
Root string `json:"root"`
GoPath string `json:"go_path"`
Name inflect.Name `json:"name"`
Bin string `json:"bin"`
PackagePkg string `json:"package_path"`
ActionsPkg string `json:"actions_path"`
ModelsPkg string `json:"models_path"`
GriftsPkg string `json:"grifts_path"`
VCS string `json:"vcs"`
WithPop bool `json:"with_pop"`
WithSQLite bool `json:"with_sqlite"`
WithDep bool `json:"with_dep"`
WithWebpack bool `json:"with_webpack"`
WithYarn bool `json:"with_yarn"`
WithDocker bool `json:"with_docker"`
WithGrifts bool `json:"with_grifts"`
WithModules bool `json:"with_modules"`
}
// New App based on the details found at the provided root path
func New(root string) App {
pwd, _ := os.Getwd()
if root == "." {
root = pwd
}
// Handle symlinks
var oldPwd = pwd
pwd = ResolveSymlinks(pwd)
os.Chdir(pwd)
if runtime.GOOS != "windows" {
// On Non-Windows OS, os.Getwd() uses PWD env var as a preferred
// way to get the working dir.
os.Setenv("PWD", pwd)
}
defer func() {
// Restore PWD
os.Chdir(oldPwd)
if runtime.GOOS != "windows" {
os.Setenv("PWD", oldPwd)
}
}()
// Gather meta data
name := inflect.Name(filepath.Base(root))
pp := envy.CurrentPackage()
if filepath.Base(pp) != string(name) {
pp = path.Join(pp, string(name))
}
if modsOn {
if !strings.HasPrefix(pwd, filepath.Join(envy.GoPath(), "src")) {
pp = name.String()
}
}
app := App{
Pwd: pwd,
Root: root,
GoPath: envy.GoPath(),
Name: name,
PackagePkg: pp,
ActionsPkg: pp + "/actions",
ModelsPkg: pp + "/models",
GriftsPkg: pp + "/grifts",
WithModules: modsOn,
}
app.Bin = filepath.Join("bin", filepath.Base(root))
if runtime.GOOS == "windows" {
app.Bin += ".exe"
}
db := filepath.Join(root, "database.yml")
if _, err := os.Stat(db); err == nil {
app.WithPop = true
if b, err := ioutil.ReadFile(db); err == nil {
app.WithSQLite = bytes.Contains(bytes.ToLower(b), []byte("sqlite"))
}
}
if _, err := os.Stat(filepath.Join(root, "Gopkg.toml")); err == nil {
app.WithDep = true
}
if _, err := os.Stat(filepath.Join(root, "webpack.config.js")); err == nil {
app.WithWebpack = true
}
if _, err := os.Stat(filepath.Join(root, "yarn.lock")); err == nil {
app.WithYarn = true
}
if _, err := os.Stat(filepath.Join(root, "Dockerfile")); err == nil {
app.WithDocker = true
}
if _, err := os.Stat(filepath.Join(root, "grifts")); err == nil {
app.WithGrifts = true
}
if _, err := os.Stat(filepath.Join(root, ".git")); err == nil {
app.VCS = "git"
} else if _, err := os.Stat(filepath.Join(root, ".bzr")); err == nil {
app.VCS = "bzr"
}
return app
}
// ResolveSymlinks takes a path and gets the pointed path
// if the original one is a symlink.
func ResolveSymlinks(p string) string {
cd, err := os.Lstat(p)
if err != nil {
return p
}
if cd.Mode()&os.ModeSymlink != 0 {
// This is a symlink
r, err := filepath.EvalSymlinks(p)
if err != nil {
return p
}
return r
}
return p
}
func (a App) String() string {
b, _ := json.Marshal(a)
return string(b)
}