From 30564d388a8a63c5fe32ba8bab1e5add35a40be9 Mon Sep 17 00:00:00 2001 From: ZhengYang Date: Fri, 1 Aug 2014 18:07:38 +0800 Subject: [PATCH 1/6] adding log information to show prograss --- g_models.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/g_models.go b/g_models.go index a17427f..9607db3 100644 --- a/g_models.go +++ b/g_models.go @@ -202,6 +202,7 @@ func gen(dbms string, connStr string, mode byte, currpath string) { os.Exit(2) } defer db.Close() + ColorLog("[INFO] Analyzing database tables...\n") tableNames := getTableNames(db) tables := getTableObjects(tableNames, db) mvcPath := new(MvcPath) @@ -408,12 +409,15 @@ func deleteAndRecreatePaths(mode byte, paths *MvcPath) { // Newly geneated files will be inside these folders. func writeSourceFiles(tables []*Table, mode byte, paths *MvcPath) { if (O_MODEL & mode) == O_MODEL { + ColorLog("[INFO] Creating model files...\n") writeModelFiles(tables, paths.ModelPath) } if (O_CONTROLLER & mode) == O_CONTROLLER { + ColorLog("[INFO] Creating controller files...\n") writeControllerFiles(tables, paths.ControllerPath) } if (O_ROUTER & mode) == O_ROUTER { + ColorLog("[INFO] Creating router files...\n") writeRouterFile(tables, paths.RouterPath) } } @@ -437,6 +441,7 @@ func writeModelFiles(tables []*Table, mPath string) { ColorLog("[ERRO] Could not write model file to %s\n", fpath) os.Exit(2) } + ColorLog("[INFO] model => %s\n", fpath) formatAndFixImports(fpath) } } @@ -456,6 +461,7 @@ func writeControllerFiles(tables []*Table, cPath string) { ColorLog("[ERRO] Could not write controller file to %s\n", fpath) os.Exit(2) } + ColorLog("[INFO] controller => %s\n", fpath) formatAndFixImports(fpath) } } @@ -481,6 +487,7 @@ func writeRouterFile(tables []*Table, rPath string) { ColorLog("[ERRO] Could not write router file to %s\n", fpath) os.Exit(2) } + ColorLog("[INFO] router => %s\n", fpath) formatAndFixImports(fpath) } From dd8754b848c3928051ee1c9b106e622489dcf9cf Mon Sep 17 00:00:00 2001 From: ZhengYang Date: Fri, 1 Aug 2014 18:29:27 +0800 Subject: [PATCH 2/6] change of file open options:os.O_CREATE|os.O_EXCL|os.O_RDWR --- g_models.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/g_models.go b/g_models.go index 9607db3..a2e8c9c 100644 --- a/g_models.go +++ b/g_models.go @@ -427,7 +427,7 @@ func writeModelFiles(tables []*Table, mPath string) { for _, tb := range tables { filename := getFileName(tb.Name) fpath := path.Join(mPath, filename+".go") - f, _ := os.OpenFile(fpath, os.O_CREATE|os.O_RDWR, 0666) + f, _ := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666) defer f.Close() template := "" if tb.Pk == "" { @@ -454,7 +454,7 @@ func writeControllerFiles(tables []*Table, cPath string) { } filename := getFileName(tb.Name) fpath := path.Join(cPath, filename+".go") - f, _ := os.OpenFile(fpath, os.O_CREATE|os.O_RDWR, 0666) + f, _ := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666) defer f.Close() fileStr := strings.Replace(CTRL_TPL, "{{ctrlName}}", camelCase(tb.Name), -1) if _, err := f.WriteString(fileStr); err != nil { @@ -481,7 +481,7 @@ func writeRouterFile(tables []*Table, rPath string) { // add export controller fpath := path.Join(rPath, "router.go") routerStr := strings.Replace(ROUTER_TPL, "{{nameSpaces}}", strings.Join(nameSpaces, ""), 1) - f, _ := os.OpenFile(fpath, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666) + f, _ := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666) defer f.Close() if _, err := f.WriteString(routerStr); err != nil { ColorLog("[ERRO] Could not write router file to %s\n", fpath) From 4aa8bf8430028122e795cfedaccf1c112ace624b Mon Sep 17 00:00:00 2001 From: ZhengYang Date: Fri, 1 Aug 2014 18:35:28 +0800 Subject: [PATCH 3/6] add header --- g_models.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/g_models.go b/g_models.go index a2e8c9c..13e5a6c 100644 --- a/g_models.go +++ b/g_models.go @@ -1,3 +1,17 @@ +// Copyright 2013 bee authors +// +// Licensed under the Apache License, Version 2.0 (the "License"): you may +// not use this file except in compliance with the License. You may obtain +// a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations +// under the License. + package main import ( From a161ff475559ea564722c45a99989c3ba314dc25 Mon Sep 17 00:00:00 2001 From: ZhengYang Date: Fri, 1 Aug 2014 18:38:32 +0800 Subject: [PATCH 4/6] rename function name for clarity --- g_models.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/g_models.go b/g_models.go index 13e5a6c..0c6db10 100644 --- a/g_models.go +++ b/g_models.go @@ -223,7 +223,7 @@ func gen(dbms string, connStr string, mode byte, currpath string) { mvcPath.ModelPath = path.Join(currpath, "models") mvcPath.ControllerPath = path.Join(currpath, "controllers") mvcPath.RouterPath = path.Join(currpath, "routers") - deleteAndRecreatePaths(mode, mvcPath) + createPaths(mode, mvcPath) writeSourceFiles(tables, mode, mvcPath) } @@ -403,17 +403,14 @@ func getColumns(db *sql.DB, table *Table, blackList map[string]bool) { } // deleteAndRecreatePaths removes several directories completely -func deleteAndRecreatePaths(mode byte, paths *MvcPath) { +func createPaths(mode byte, paths *MvcPath) { if (mode & O_MODEL) == O_MODEL { - //os.RemoveAll(paths.ModelPath) os.Mkdir(paths.ModelPath, 0777) } if (mode & O_CONTROLLER) == O_CONTROLLER { - //os.RemoveAll(paths.ControllerPath) os.Mkdir(paths.ControllerPath, 0777) } if (mode & O_ROUTER) == O_ROUTER { - //os.RemoveAll(paths.RouterPath) os.Mkdir(paths.RouterPath, 0777) } } From 2f6b7fe0a13fc6cafd6f82a198f577c7feb1d3cc Mon Sep 17 00:00:00 2001 From: ZhengYang Date: Fri, 1 Aug 2014 18:48:49 +0800 Subject: [PATCH 5/6] added error message if generated file exists --- g_models.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/g_models.go b/g_models.go index 0c6db10..c1a5d15 100644 --- a/g_models.go +++ b/g_models.go @@ -438,7 +438,11 @@ func writeModelFiles(tables []*Table, mPath string) { for _, tb := range tables { filename := getFileName(tb.Name) fpath := path.Join(mPath, filename+".go") - f, _ := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666) + f, err := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666) + if err != nil { + ColorLog("[ERRO] %v\n", err) + os.Exit(2) + } defer f.Close() template := "" if tb.Pk == "" { @@ -465,7 +469,11 @@ func writeControllerFiles(tables []*Table, cPath string) { } filename := getFileName(tb.Name) fpath := path.Join(cPath, filename+".go") - f, _ := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666) + f, err := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666) + if err != nil { + ColorLog("[ERRO] %v\n", err) + os.Exit(2) + } defer f.Close() fileStr := strings.Replace(CTRL_TPL, "{{ctrlName}}", camelCase(tb.Name), -1) if _, err := f.WriteString(fileStr); err != nil { @@ -492,7 +500,11 @@ func writeRouterFile(tables []*Table, rPath string) { // add export controller fpath := path.Join(rPath, "router.go") routerStr := strings.Replace(ROUTER_TPL, "{{nameSpaces}}", strings.Join(nameSpaces, ""), 1) - f, _ := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666) + f, err := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666) + if err != nil { + ColorLog("[ERRO] %v\n", err) + os.Exit(2) + } defer f.Close() if _, err := f.WriteString(routerStr); err != nil { ColorLog("[ERRO] Could not write router file to %s\n", fpath) From f20b75c370711690702f668a8f448138270a7f06 Mon Sep 17 00:00:00 2001 From: ZhengYang Date: Fri, 1 Aug 2014 18:56:17 +0800 Subject: [PATCH 6/6] uncomment example router namespace: object & user --- g_models.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/g_models.go b/g_models.go index c1a5d15..146bdde 100644 --- a/g_models.go +++ b/g_models.go @@ -749,7 +749,6 @@ import ( func init() { ns := beego.NewNamespace("/v1", - /* beego.NSNamespace("/object", beego.NSInclude( &controllers.ObjectController{}, @@ -760,7 +759,6 @@ func init() { &controllers.UserController{}, ), ), -*/ {{nameSpaces}} ) beego.AddNamespace(ns)