代码拉取完成,页面将自动刷新
同步操作将从 qiqi/orange 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package main
import (
"crypto/md5"
"encoding/hex"
"fmt"
"gitee.com/zhucheer/orange/generator/tpl"
"gitee.com/zhucheer/orange/utils"
"gitee.com/zhucheer/orange/view"
"io/ioutil"
"os"
"strings"
)
func main() {
fmt.Println("run")
argNum := len(os.Args)
if argNum == 2 {
secondArg := os.Args[1]
switch secondArg {
case "build":
buildProject()
}
}
if argNum >= 3 {
secondArg := os.Args[1]
inputArg := os.Args[2]
switch secondArg {
case "create":
createProject(inputArg)
}
}
}
var gitignoreTpl = `*
!.gitignore
`
func createProject(projectName string) {
fmt.Println("create project" + projectName)
projectPath := getProjectPath(projectName)
err := os.Mkdir(projectPath, os.ModePerm)
if err != nil {
fmt.Println(fmt.Sprintf("create project error:%v", err))
return
}
fmt.Println("create directory [" + projectPath + "]")
projectDir := []string{"config", "storage" + utils.DirDot() + "views", "build", "route", "http" + utils.DirDot() + "controller", "http" + utils.DirDot() + "middleware"}
for _, item := range projectDir {
subDir := getProjectPath(projectName, item)
fmt.Println("create directory [" + subDir + "]")
err := os.MkdirAll(subDir, os.ModePerm)
if err != nil {
fmt.Println(fmt.Sprintf("create project dir error:%v", err))
}
}
writeMainFile(projectName)
writeStorage(projectName)
writeBuildDir(projectName)
writeConfigFile(projectName)
writeRouteFile(projectName)
writeIndexFile(projectName)
writeMiddlewareFile(projectName)
fmt.Println("create project success!")
}
func writeBuildDir(projectName string) {
gitignoreTpl = strings.Replace(gitignoreTpl, "\n", "", 1)
path := getProjectPath(projectName, "build", ".gitignore")
err := ioutil.WriteFile(path, []byte(gitignoreTpl), 0644)
if err != nil {
panic(err)
}
fmt.Println("create file [" + path + "]")
}
func writeStorage(projectName string) {
demoTpl := parseTpl("tpldemo", map[string]interface{}{})
path := getProjectPath(projectName, "storage", "views", "demo.tpl")
err := ioutil.WriteFile(path, []byte(demoTpl), 0644)
if err != nil {
panic(err)
}
fmt.Println("create file [" + path + "]")
}
func writeMainFile(projectName string) {
viewData := map[string]interface{}{
"projectName": projectName,
}
maingoTpl := parseTpl("main", viewData)
mainPath := getProjectPath(projectName, "main.go")
err := ioutil.WriteFile(mainPath, []byte(maingoTpl), 0644)
if err != nil {
panic(err)
}
fmt.Println("create file [" + mainPath + "]")
jobgoTpl := parseTpl("job", viewData)
jobPath := getProjectPath(projectName, "job.go")
err = ioutil.WriteFile(jobPath, []byte(jobgoTpl), 0644)
if err != nil {
panic(err)
}
fmt.Println("create file [" + jobPath + "]")
}
func writeConfigFile(projectName string) {
viewData := map[string]interface{}{
"appkey": getRandAppKey(),
}
configTpl := parseTpl("config", viewData)
path := getProjectPath(projectName, "config", "config.toml")
err := ioutil.WriteFile(path, []byte(configTpl), 0644)
if err != nil {
panic(err)
}
fmt.Println("create file [" + path + "]")
}
func writeRouteFile(projectName string) {
viewData := map[string]interface{}{
"projectName": projectName,
}
httpRoutesTpl := parseTpl("route", viewData)
httppath := getProjectPath(projectName, "route", "http.go")
err := ioutil.WriteFile(httppath, []byte(httpRoutesTpl), 0644)
if err != nil {
panic(err)
}
fmt.Println("create file [" + httppath + "]")
jobRoutesTpl := parseTpl("jobRoute", viewData)
jobpath := getProjectPath(projectName, "route", "job.go")
err = ioutil.WriteFile(jobpath, []byte(jobRoutesTpl), 0644)
if err != nil {
panic(err)
}
fmt.Println("create file [" + jobpath + "]")
}
func writeIndexFile(projectName string) {
indexHtml := parseTpl("indexhtml", map[string]interface{}{})
viewData := map[string]interface{}{
"indexHtml": indexHtml,
}
indexgoTpl := parseTpl("indexgo", viewData)
path := getProjectPath(projectName, "http", "controller", "index.go")
err := ioutil.WriteFile(path, []byte(indexgoTpl), 0644)
if err != nil {
panic(err)
}
fmt.Println("create file [" + path + "]")
}
func writeMiddlewareFile(projectName string) {
viewData := map[string]interface{}{}
middlewareTpl := parseTpl("middleware", viewData)
path := getProjectPath(projectName, "http", "middleware", "auth.go")
err := ioutil.WriteFile(path, []byte(middlewareTpl), 0644)
if err != nil {
panic(err)
}
fmt.Println("create file [" + path + "]")
}
func getProjectPath(subdirs ...string) string {
basepath := getGoPath()
for _, item := range subdirs {
basepath = basepath + utils.DirDot() + item
}
return basepath
}
func getGoPath() string {
stdout, _ := utils.ExecShell("go env GOPATH")
stdout = strings.Replace(stdout, "\n", "", -1)
return stdout + utils.DirDot() + "src"
}
func getRandAppKey() string {
randByte := utils.RandomCreateBytes(24)
h := md5.New()
h.Write(randByte)
return hex.EncodeToString(h.Sum(nil))
}
func parseTpl(fileName string, viewData interface{}) string {
tplContent := tpl.GetTmp(fileName)
return view.TextContent(tplContent, viewData)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。