forked from ponzu-cms/ponzu
/
options.go
184 lines (153 loc) · 3.55 KB
/
options.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
package main
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
var ponzuRepo = []string{"github.com", "ponzu-cms", "ponzu"}
func getAnswer() (string, error) {
var answer string
_, err := fmt.Scanf("%s\n", &answer)
if err != nil {
if err.Error() == "unexpected newline" {
answer = ""
} else {
return "", err
}
}
answer = strings.ToLower(answer)
return answer, nil
}
func vendorCorePackages(path string) error {
vendorPath := filepath.Join(path, "cmd", "ponzu", "vendor", "github.com", "ponzu-cms", "ponzu")
err := os.MkdirAll(vendorPath, os.ModeDir|os.ModePerm)
if err != nil {
return err
}
dirs := []string{"content", "management", "system"}
for _, dir := range dirs {
err = os.Rename(filepath.Join(path, dir), filepath.Join(vendorPath, dir))
if err != nil {
return err
}
}
// create a user content directory at project root
contentPath := filepath.Join(path, "content")
err = os.Mkdir(contentPath, os.ModeDir|os.ModePerm)
if err != nil {
return err
}
return nil
}
func copyFileNoRoot(src, dst string) error {
noRoot := strings.Split(src, string(filepath.Separator))[1:]
path := filepath.Join(noRoot...)
dstFile, err := os.Create(filepath.Join(dst, path))
defer dstFile.Close()
if err != nil {
return err
}
srcFile, err := os.Open(src)
defer srcFile.Close()
if err != nil {
return err
}
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return err
}
return nil
}
func copyFilesWarnConflicts(srcDir, dstDir string, conflicts []string) error {
err := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
for _, conflict := range conflicts {
if info.Name() == conflict {
fmt.Println("Ponzu couldn't fully build your project:")
fmt.Println("You must rename the following file, as it conflicts with Ponzu core:")
fmt.Println(path)
fmt.Println("")
fmt.Println("Once the files above have been renamed, run '$ ponzu build' to retry.")
return errors.New("Ponzu has very few internal conflicts, sorry for the inconvenience.")
}
}
if info.IsDir() {
// don't copy root directory
if path == srcDir {
return nil
}
if len(path) > len(srcDir) {
path = path[len(srcDir)+1:]
}
dir := filepath.Join(dstDir, path)
err := os.MkdirAll(dir, os.ModeDir|os.ModePerm)
if err != nil {
return err
}
return nil
}
err = copyFileNoRoot(path, dstDir)
if err != nil {
return err
}
return nil
})
if err != nil {
return err
}
return nil
}
func emptyDir(path string) error {
d, err := os.Open(path)
if err != nil {
return err
}
defer d.Close()
names, err := d.Readdirnames(-1)
if err != nil {
return err
}
for _, name := range names {
err = os.RemoveAll(filepath.Join(path, name))
if err != nil {
return err
}
}
return nil
}
func copyAll(src, dst string) error {
err := filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
sep := string(filepath.Separator)
// base == the ponzu project dir + string(filepath.Separator)
parts := strings.Split(src, sep)
base := strings.Join(parts[:len(parts)-1], sep)
base += sep
target := filepath.Join(dst, path[len(base):])
// if its a directory, make dir in dst
if info.IsDir() {
err := os.MkdirAll(target, os.ModeDir|os.ModePerm)
if err != nil {
return err
}
} else {
// if its a file, move file to dir of dst
err = os.Rename(path, target)
if err != nil {
return err
}
}
return nil
})
if err != nil {
return err
}
return nil
}