/
create.go
90 lines (71 loc) · 2.09 KB
/
create.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
package create
import (
"io/ioutil"
"os"
"strings"
"github.com/henrylee2cn/goutil"
tp "github.com/henrylee2cn/teleport"
"github.com/xiaoenai/tp-micro/micro/create/tpl"
"github.com/xiaoenai/tp-micro/micro/info"
)
// MicroTpl template file name
const MicroTpl = "__tp-micro__tpl__.go"
// MicroGenLock the file is used to markup generated project
const MicroGenLock = "__tp-micro__gen__.lock"
// CreateProject creates a project.
func CreateProject(force, newdoc bool) {
tp.Infof("Generating project: %s", info.ProjPath())
os.MkdirAll(info.AbsPath(), os.FileMode(0755))
err := os.Chdir(info.AbsPath())
if err != nil {
tp.Fatalf("[micro] Jump working directory failed: %v", err)
}
force = force || !goutil.FileExists(MicroGenLock)
// creates base files
if force {
tpl.Create()
}
// read temptale file
b, err := ioutil.ReadFile(MicroTpl)
if err != nil {
b = []byte(strings.Replace(__tpl__, "__PROJ_NAME__", info.ProjName(), -1))
}
// new project code
proj := NewProject(b)
proj.Generator(force, force || newdoc)
// write template file
f, err := os.OpenFile(MicroTpl, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm)
if err != nil {
tp.Fatalf("[micro] Create files error: %v", err)
}
defer f.Close()
f.Write(formatSource(b))
tpl.RestoreAsset("./", MicroGenLock)
tp.Infof("Completed code generation!")
}
// CreateDoc creates a project doc.
func CreateDoc() {
tp.Infof("Generating README.md: %s", info.ProjPath())
os.MkdirAll(info.AbsPath(), os.FileMode(0755))
err := os.Chdir(info.AbsPath())
if err != nil {
tp.Fatalf("[micro] Jump working directory failed: %v", err)
}
// read temptale file
b, err := ioutil.ReadFile(MicroTpl)
if err != nil {
b = []byte(strings.Replace(__tpl__, "__PROJ_NAME__", info.ProjName(), -1))
}
// new project code
proj := NewProject(b)
proj.gen()
proj.genAndWriteReadmeFile()
// write template file
f, err := os.OpenFile(MicroTpl, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm)
if err != nil {
tp.Fatalf("[micro] Create files error: %v", err)
}
defer f.Close()
f.Write(formatSource(b))
tp.Infof("Completed README.md generation!")
}