mirror of
https://github.com/beego/bee.git
synced 2024-11-24 03:20:55 +00:00
Merge pull request #674 from guhan121/develop
generate go module project
This commit is contained in:
commit
bb5e0435c9
19
.travis.yml
19
.travis.yml
@ -1,20 +1,21 @@
|
|||||||
language: go
|
language: go
|
||||||
go:
|
go:
|
||||||
- 1.10.3
|
- 1.12.17
|
||||||
install:
|
install:
|
||||||
- export PATH=$PATH:$HOME/gopath/bin
|
- export PATH=$PATH:$HOME/gopath/bin
|
||||||
- go get -u github.com/opennota/check/cmd/structcheck
|
- go get -u github.com/opennota/check/cmd/structcheck
|
||||||
- go get -u honnef.co/go/tools/cmd/gosimple
|
|
||||||
- go get -u honnef.co/go/tools/cmd/staticcheck
|
- go get -u honnef.co/go/tools/cmd/staticcheck
|
||||||
- go get -u honnef.co/go/tools/cmd/unused
|
|
||||||
- go get -u github.com/mdempsky/unconvert
|
- go get -u github.com/mdempsky/unconvert
|
||||||
- go get -u github.com/gordonklaus/ineffassign
|
- go get -u github.com/gordonklaus/ineffassign
|
||||||
script:
|
script:
|
||||||
|
- pwd
|
||||||
|
- cd $(dirname `dirname $(pwd)`)/beego/bee
|
||||||
|
- export GO111MODULE="on"
|
||||||
|
- go mod download
|
||||||
- find . ! \( -path './vendor' -prune \) -type f -name '*.go' -print0 | xargs -0 gofmt -l -s
|
- find . ! \( -path './vendor' -prune \) -type f -name '*.go' -print0 | xargs -0 gofmt -l -s
|
||||||
- go vet $(go list ./... | grep -v /vendor/)
|
- go list ./... | grep -v /vendor/ | grep -v /pkg/mod/
|
||||||
- structcheck $(go list ./... | grep -v /vendor/)
|
- go vet $(go list ./... | grep -v /vendor/ | grep -v /pkg/mod/ )
|
||||||
- gosimple -ignore "$(cat gosimple.ignore)" $(go list ./... | grep -v /vendor/)
|
- structcheck $(go list ./... | grep -v /vendor/ | grep -v /pkg/mod/ )
|
||||||
- staticcheck -ignore "$(cat staticcheck.ignore)" $(go list ./... | grep -v /vendor/)
|
- staticcheck $(go list ./... | grep -v /vendor/ | grep -v /pkg/mod/ )
|
||||||
- unused $(go list ./... | grep -v /vendor/)
|
- unconvert $(go list ./... | grep -v /vendor/ | grep -v /pkg/mod/ )
|
||||||
- unconvert $(go list ./... | grep -v /vendor/)
|
|
||||||
- ineffassign .
|
- ineffassign .
|
||||||
|
@ -16,8 +16,10 @@ package apiapp
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/beego/bee/logger/colors"
|
||||||
"os"
|
"os"
|
||||||
path "path/filepath"
|
path "path/filepath"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/beego/bee/cmd/commands"
|
"github.com/beego/bee/cmd/commands"
|
||||||
@ -35,7 +37,7 @@ var CmdApiapp = &commands.Command{
|
|||||||
The command 'api' creates a Beego API application.
|
The command 'api' creates a Beego API application.
|
||||||
|
|
||||||
{{"Example:"|bold}}
|
{{"Example:"|bold}}
|
||||||
$ bee api [appname] [-tables=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
|
$ bee api [appname] [-tables=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-module=true] [-beego=v1.12.1]
|
||||||
|
|
||||||
If 'conn' argument is empty, the command will generate an example API application. Otherwise the command
|
If 'conn' argument is empty, the command will generate an example API application. Otherwise the command
|
||||||
will connect to your database and generate models based on the existing tables.
|
will connect to your database and generate models based on the existing tables.
|
||||||
@ -43,6 +45,7 @@ var CmdApiapp = &commands.Command{
|
|||||||
The command 'api' creates a folder named [appname] with the following structure:
|
The command 'api' creates a folder named [appname] with the following structure:
|
||||||
|
|
||||||
├── main.go
|
├── main.go
|
||||||
|
├── go.mod
|
||||||
├── {{"conf"|foldername}}
|
├── {{"conf"|foldername}}
|
||||||
│ └── app.conf
|
│ └── app.conf
|
||||||
├── {{"controllers"|foldername}}
|
├── {{"controllers"|foldername}}
|
||||||
@ -103,6 +106,14 @@ func main() {
|
|||||||
beego.Run()
|
beego.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
`
|
||||||
|
var goMod = `
|
||||||
|
module %s
|
||||||
|
|
||||||
|
go %s
|
||||||
|
|
||||||
|
require github.com/astaxie/beego %s
|
||||||
|
require github.com/smartystreets/goconvey v1.6.4
|
||||||
`
|
`
|
||||||
|
|
||||||
var apirouter = `// @APIVersion 1.0.0
|
var apirouter = `// @APIVersion 1.0.0
|
||||||
@ -533,11 +544,15 @@ func TestGet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
`
|
`
|
||||||
|
var module utils.DocValue
|
||||||
|
var beegoVersion utils.DocValue
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
CmdApiapp.Flag.Var(&generate.Tables, "tables", "List of table names separated by a comma.")
|
CmdApiapp.Flag.Var(&generate.Tables, "tables", "List of table names separated by a comma.")
|
||||||
CmdApiapp.Flag.Var(&generate.SQLDriver, "driver", "Database driver. Either mysql, postgres or sqlite.")
|
CmdApiapp.Flag.Var(&generate.SQLDriver, "driver", "Database driver. Either mysql, postgres or sqlite.")
|
||||||
CmdApiapp.Flag.Var(&generate.SQLConn, "conn", "Connection string used by the driver to connect to a database instance.")
|
CmdApiapp.Flag.Var(&generate.SQLConn, "conn", "Connection string used by the driver to connect to a database instance.")
|
||||||
|
CmdApiapp.Flag.Var(&module, "module", "Support go modules")
|
||||||
|
CmdApiapp.Flag.Var(&beegoVersion, "beego", "set beego version,only take effect by -module=true")
|
||||||
commands.AvailableCommands = append(commands.AvailableCommands, CmdApiapp)
|
commands.AvailableCommands = append(commands.AvailableCommands, CmdApiapp)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -548,14 +563,38 @@ func createAPI(cmd *commands.Command, args []string) int {
|
|||||||
beeLogger.Log.Fatal("Argument [appname] is missing")
|
beeLogger.Log.Fatal("Argument [appname] is missing")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(args) > 1 {
|
if len(args) >= 2 {
|
||||||
err := cmd.Flag.Parse(args[1:])
|
cmd.Flag.Parse(args[1:])
|
||||||
|
} else {
|
||||||
|
module = "false"
|
||||||
|
}
|
||||||
|
var appPath string
|
||||||
|
var packPath string
|
||||||
|
var err error
|
||||||
|
if module != `true` {
|
||||||
|
beeLogger.Log.Info("generate api project support GOPATH")
|
||||||
|
version.ShowShortVersionBanner()
|
||||||
|
appPath, packPath, err = utils.CheckEnv(args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
beeLogger.Log.Error(err.Error())
|
beeLogger.Log.Fatalf("%s", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
beeLogger.Log.Info("generate api project support go modules.")
|
||||||
|
appPath = path.Join(utils.GetBeeWorkPath(), args[0])
|
||||||
|
packPath = args[0]
|
||||||
|
if beegoVersion.String() == `` {
|
||||||
|
beegoVersion.Set(`v1.12.1`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if utils.IsExist(appPath) {
|
||||||
|
beeLogger.Log.Errorf(colors.Bold("Application '%s' already exists"), appPath)
|
||||||
|
beeLogger.Log.Warn(colors.Bold("Do you want to overwrite it? [Yes|No] "))
|
||||||
|
if !utils.AskForConfirmation() {
|
||||||
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
appPath, packPath, err := utils.CheckEnv(args[0])
|
|
||||||
appName := path.Base(args[0])
|
appName := path.Base(args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
beeLogger.Log.Fatalf("%s", err)
|
beeLogger.Log.Fatalf("%s", err)
|
||||||
@ -567,6 +606,10 @@ func createAPI(cmd *commands.Command, args []string) int {
|
|||||||
beeLogger.Log.Info("Creating API...")
|
beeLogger.Log.Info("Creating API...")
|
||||||
|
|
||||||
os.MkdirAll(appPath, 0755)
|
os.MkdirAll(appPath, 0755)
|
||||||
|
if module == `true` { //generate first for calc model name
|
||||||
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "go.mod"), "\x1b[0m")
|
||||||
|
utils.WriteToFile(path.Join(appPath, "go.mod"), fmt.Sprintf(goMod, packPath, runtime.Version()[2:], beegoVersion.String()))
|
||||||
|
}
|
||||||
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", appPath, "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", appPath, "\x1b[0m")
|
||||||
os.Mkdir(path.Join(appPath, "conf"), 0755)
|
os.Mkdir(path.Join(appPath, "conf"), 0755)
|
||||||
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "conf"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "conf"), "\x1b[0m")
|
||||||
|
@ -81,15 +81,6 @@ func GenerateCode(cmd *commands.Command, args []string) int {
|
|||||||
beeLogger.Log.Fatal("Command is missing")
|
beeLogger.Log.Fatal("Command is missing")
|
||||||
}
|
}
|
||||||
|
|
||||||
gps := utils.GetGOPATHs()
|
|
||||||
if len(gps) == 0 {
|
|
||||||
beeLogger.Log.Fatal("GOPATH environment variable is not set or empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
gopath := gps[0]
|
|
||||||
|
|
||||||
beeLogger.Log.Debugf("GOPATH: %s", utils.FILE(), utils.LINE(), gopath)
|
|
||||||
|
|
||||||
gcmd := args[0]
|
gcmd := args[0]
|
||||||
switch gcmd {
|
switch gcmd {
|
||||||
case "scaffold":
|
case "scaffold":
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package hprose
|
package hprose
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/beego/bee/logger/colors"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"path"
|
"path"
|
||||||
@ -24,7 +26,7 @@ var CmdHproseapp = &commands.Command{
|
|||||||
|
|
||||||
{{"To scaffold out your application, use:"|bold}}
|
{{"To scaffold out your application, use:"|bold}}
|
||||||
|
|
||||||
$ bee hprose [appname] [-tables=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"]
|
$ bee hprose [appname] [-tables=""] [-driver=mysql] [-conn="root:@tcp(127.0.0.1:3306)/test"] [-module=true] [-beego=v1.12.1]
|
||||||
|
|
||||||
If 'conn' is empty, the command will generate a sample application. Otherwise the command
|
If 'conn' is empty, the command will generate a sample application. Otherwise the command
|
||||||
will connect to your database and generate models based on the existing tables.
|
will connect to your database and generate models based on the existing tables.
|
||||||
@ -32,6 +34,7 @@ var CmdHproseapp = &commands.Command{
|
|||||||
The command 'hprose' creates a folder named [appname] with the following structure:
|
The command 'hprose' creates a folder named [appname] with the following structure:
|
||||||
|
|
||||||
├── main.go
|
├── main.go
|
||||||
|
├── go.mod
|
||||||
├── {{"conf"|foldername}}
|
├── {{"conf"|foldername}}
|
||||||
│ └── app.conf
|
│ └── app.conf
|
||||||
└── {{"models"|foldername}}
|
└── {{"models"|foldername}}
|
||||||
@ -42,34 +45,76 @@ var CmdHproseapp = &commands.Command{
|
|||||||
Run: createhprose,
|
Run: createhprose,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var goMod = `
|
||||||
|
module %s
|
||||||
|
|
||||||
|
go %s
|
||||||
|
|
||||||
|
require github.com/astaxie/beego %s
|
||||||
|
require github.com/smartystreets/goconvey v1.6.4
|
||||||
|
`
|
||||||
|
|
||||||
|
var module utils.DocValue
|
||||||
|
var beegoVersion utils.DocValue
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
CmdHproseapp.Flag.Var(&generate.Tables, "tables", "List of table names separated by a comma.")
|
CmdHproseapp.Flag.Var(&generate.Tables, "tables", "List of table names separated by a comma.")
|
||||||
CmdHproseapp.Flag.Var(&generate.SQLDriver, "driver", "Database driver. Either mysql, postgres or sqlite.")
|
CmdHproseapp.Flag.Var(&generate.SQLDriver, "driver", "Database driver. Either mysql, postgres or sqlite.")
|
||||||
CmdHproseapp.Flag.Var(&generate.SQLConn, "conn", "Connection string used by the driver to connect to a database instance.")
|
CmdHproseapp.Flag.Var(&generate.SQLConn, "conn", "Connection string used by the driver to connect to a database instance.")
|
||||||
|
CmdHproseapp.Flag.Var(&module, "module", "Support go modules")
|
||||||
|
CmdHproseapp.Flag.Var(&beegoVersion, "beego", "set beego version,only take effect by -module=true")
|
||||||
commands.AvailableCommands = append(commands.AvailableCommands, CmdHproseapp)
|
commands.AvailableCommands = append(commands.AvailableCommands, CmdHproseapp)
|
||||||
}
|
}
|
||||||
|
|
||||||
func createhprose(cmd *commands.Command, args []string) int {
|
func createhprose(cmd *commands.Command, args []string) int {
|
||||||
output := cmd.Out()
|
output := cmd.Out()
|
||||||
|
if len(args) == 0 {
|
||||||
if len(args) != 1 {
|
|
||||||
beeLogger.Log.Fatal("Argument [appname] is missing")
|
beeLogger.Log.Fatal("Argument [appname] is missing")
|
||||||
}
|
}
|
||||||
|
|
||||||
curpath, _ := os.Getwd()
|
curpath, _ := os.Getwd()
|
||||||
if len(args) > 1 {
|
if len(args) > 1 {
|
||||||
cmd.Flag.Parse(args[1:])
|
cmd.Flag.Parse(args[1:])
|
||||||
|
} else {
|
||||||
|
module = "false"
|
||||||
}
|
}
|
||||||
apppath, packpath, err := utils.CheckEnv(args[0])
|
var apppath string
|
||||||
|
var packpath string
|
||||||
|
var err error
|
||||||
|
if module != `true` {
|
||||||
|
beeLogger.Log.Info("generate api project support GOPATH")
|
||||||
|
version.ShowShortVersionBanner()
|
||||||
|
apppath, packpath, err = utils.CheckEnv(args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
beeLogger.Log.Fatalf("%s", err)
|
beeLogger.Log.Fatalf("%s", err)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
beeLogger.Log.Info("generate api project support go modules.")
|
||||||
|
apppath = path.Join(utils.GetBeeWorkPath(), args[0])
|
||||||
|
packpath = args[0]
|
||||||
|
if beegoVersion.String() == `` {
|
||||||
|
beegoVersion.Set(`v1.12.1`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if utils.IsExist(apppath) {
|
||||||
|
beeLogger.Log.Errorf(colors.Bold("Application '%s' already exists"), apppath)
|
||||||
|
beeLogger.Log.Warn(colors.Bold("Do you want to overwrite it? [Yes|No] "))
|
||||||
|
if !utils.AskForConfirmation() {
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if generate.SQLDriver == "" {
|
if generate.SQLDriver == "" {
|
||||||
generate.SQLDriver = "mysql"
|
generate.SQLDriver = "mysql"
|
||||||
}
|
}
|
||||||
beeLogger.Log.Info("Creating Hprose application...")
|
beeLogger.Log.Info("Creating Hprose application...")
|
||||||
|
|
||||||
os.MkdirAll(apppath, 0755)
|
os.MkdirAll(apppath, 0755)
|
||||||
|
if module == `true` { //generate first for calc model name
|
||||||
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "go.mod"), "\x1b[0m")
|
||||||
|
utils.WriteToFile(path.Join(apppath, "go.mod"), fmt.Sprintf(goMod, packpath, runtime.Version()[2:], beegoVersion.String()))
|
||||||
|
}
|
||||||
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", apppath, "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", apppath, "\x1b[0m")
|
||||||
os.Mkdir(path.Join(apppath, "conf"), 0755)
|
os.Mkdir(path.Join(apppath, "conf"), 0755)
|
||||||
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "conf"), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(apppath, "conf"), "\x1b[0m")
|
||||||
|
@ -71,15 +71,6 @@ func init() {
|
|||||||
func RunMigration(cmd *commands.Command, args []string) int {
|
func RunMigration(cmd *commands.Command, args []string) int {
|
||||||
currpath, _ := os.Getwd()
|
currpath, _ := os.Getwd()
|
||||||
|
|
||||||
gps := utils.GetGOPATHs()
|
|
||||||
if len(gps) == 0 {
|
|
||||||
beeLogger.Log.Fatal("GOPATH environment variable is not set or empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
gopath := gps[0]
|
|
||||||
|
|
||||||
beeLogger.Log.Debugf("GOPATH: %s", utils.FILE(), utils.LINE(), gopath)
|
|
||||||
|
|
||||||
// Getting command line arguments
|
// Getting command line arguments
|
||||||
if len(args) != 0 {
|
if len(args) != 0 {
|
||||||
cmd.Flag.Parse(args[1:])
|
cmd.Flag.Parse(args[1:])
|
||||||
|
@ -18,6 +18,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
path "path/filepath"
|
path "path/filepath"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/beego/bee/cmd/commands"
|
"github.com/beego/bee/cmd/commands"
|
||||||
@ -27,15 +28,19 @@ import (
|
|||||||
"github.com/beego/bee/utils"
|
"github.com/beego/bee/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var module utils.DocValue
|
||||||
|
var beegoVersion utils.DocValue
|
||||||
|
|
||||||
var CmdNew = &commands.Command{
|
var CmdNew = &commands.Command{
|
||||||
UsageLine: "new [appname]",
|
UsageLine: "new [appname] [-module=true] [-beego=v1.12.1]",
|
||||||
Short: "Creates a Beego application",
|
Short: "Creates a Beego application",
|
||||||
Long: `
|
Long: `
|
||||||
Creates a Beego application for the given app name in the current directory.
|
Creates a Beego application for the given app name in the current directory.
|
||||||
|
now supoort generate a go modules project
|
||||||
The command 'new' creates a folder named [appname] and generates the following structure:
|
The command 'new' creates a folder named [appname] [-module=true] [-beego=v1.12.1] and generates the following structure:
|
||||||
|
|
||||||
├── main.go
|
├── main.go
|
||||||
|
├── go.mod
|
||||||
├── {{"conf"|foldername}}
|
├── {{"conf"|foldername}}
|
||||||
│ └── app.conf
|
│ └── app.conf
|
||||||
├── {{"controllers"|foldername}}
|
├── {{"controllers"|foldername}}
|
||||||
@ -53,7 +58,7 @@ Creates a Beego application for the given app name in the current directory.
|
|||||||
└── index.tpl
|
└── index.tpl
|
||||||
|
|
||||||
`,
|
`,
|
||||||
PreRun: func(cmd *commands.Command, args []string) { version.ShowShortVersionBanner() },
|
PreRun: nil,
|
||||||
Run: CreateApp,
|
Run: CreateApp,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,7 +90,14 @@ func init() {
|
|||||||
beego.Router("/", &controllers.MainController{})
|
beego.Router("/", &controllers.MainController{})
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
var goMod = `
|
||||||
|
module %s
|
||||||
|
|
||||||
|
go %s
|
||||||
|
|
||||||
|
require github.com/astaxie/beego %s
|
||||||
|
require github.com/smartystreets/goconvey v1.6.4
|
||||||
|
`
|
||||||
var test = `package test
|
var test = `package test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -245,19 +257,40 @@ var reloadJsClient = `function b(a){var c=new WebSocket(a);c.onclose=function(){
|
|||||||
`
|
`
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
CmdNew.Flag.Var(&module, "module", "Support go modules")
|
||||||
|
CmdNew.Flag.Var(&beegoVersion, "beego", "set beego version,only take effect by -module=true")
|
||||||
commands.AvailableCommands = append(commands.AvailableCommands, CmdNew)
|
commands.AvailableCommands = append(commands.AvailableCommands, CmdNew)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateApp(cmd *commands.Command, args []string) int {
|
func CreateApp(cmd *commands.Command, args []string) int {
|
||||||
output := cmd.Out()
|
output := cmd.Out()
|
||||||
if len(args) != 1 {
|
if len(args) == 0 {
|
||||||
beeLogger.Log.Fatal("Argument [appname] is missing")
|
beeLogger.Log.Fatal("Argument [appname] is missing")
|
||||||
}
|
}
|
||||||
|
|
||||||
appPath, packPath, err := utils.CheckEnv(args[0])
|
if len(args) >= 2 {
|
||||||
|
cmd.Flag.Parse(args[1:])
|
||||||
|
} else {
|
||||||
|
module = "false"
|
||||||
|
}
|
||||||
|
var appPath string
|
||||||
|
var packPath string
|
||||||
|
var err error
|
||||||
|
if module != `true` {
|
||||||
|
beeLogger.Log.Info("generate new project support GOPATH")
|
||||||
|
version.ShowShortVersionBanner()
|
||||||
|
appPath, packPath, err = utils.CheckEnv(args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
beeLogger.Log.Fatalf("%s", err)
|
beeLogger.Log.Fatalf("%s", err)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
beeLogger.Log.Info("generate new project support go modules.")
|
||||||
|
appPath = path.Join(utils.GetBeeWorkPath(), args[0])
|
||||||
|
packPath = args[0]
|
||||||
|
if beegoVersion.String() == `` {
|
||||||
|
beegoVersion.Set(`v1.12.1`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if utils.IsExist(appPath) {
|
if utils.IsExist(appPath) {
|
||||||
beeLogger.Log.Errorf(colors.Bold("Application '%s' already exists"), appPath)
|
beeLogger.Log.Errorf(colors.Bold("Application '%s' already exists"), appPath)
|
||||||
@ -270,6 +303,10 @@ func CreateApp(cmd *commands.Command, args []string) int {
|
|||||||
beeLogger.Log.Info("Creating application...")
|
beeLogger.Log.Info("Creating application...")
|
||||||
|
|
||||||
os.MkdirAll(appPath, 0755)
|
os.MkdirAll(appPath, 0755)
|
||||||
|
if module == `true` {
|
||||||
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "go.mod"), "\x1b[0m")
|
||||||
|
utils.WriteToFile(path.Join(appPath, "go.mod"), fmt.Sprintf(goMod, packPath, runtime.Version()[2:], beegoVersion.String()))
|
||||||
|
}
|
||||||
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", appPath+string(path.Separator), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", appPath+string(path.Separator), "\x1b[0m")
|
||||||
os.Mkdir(path.Join(appPath, "conf"), 0755)
|
os.Mkdir(path.Join(appPath, "conf"), 0755)
|
||||||
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "conf")+string(path.Separator), "\x1b[0m")
|
fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "conf")+string(path.Separator), "\x1b[0m")
|
||||||
|
@ -40,6 +40,7 @@ var CmdPack = &commands.Command{
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
appPath string
|
appPath string
|
||||||
|
appName string
|
||||||
excludeP string
|
excludeP string
|
||||||
excludeS string
|
excludeS string
|
||||||
outputP string
|
outputP string
|
||||||
@ -57,6 +58,7 @@ func init() {
|
|||||||
fs := flag.NewFlagSet("pack", flag.ContinueOnError)
|
fs := flag.NewFlagSet("pack", flag.ContinueOnError)
|
||||||
fs.StringVar(&appPath, "p", "", "Set the application path. Defaults to the current path.")
|
fs.StringVar(&appPath, "p", "", "Set the application path. Defaults to the current path.")
|
||||||
fs.BoolVar(&build, "b", true, "Tell the command to do a build for the current platform. Defaults to true.")
|
fs.BoolVar(&build, "b", true, "Tell the command to do a build for the current platform. Defaults to true.")
|
||||||
|
fs.StringVar(&appName, "a", "", "Set the application name. Defaults to the dir name.")
|
||||||
fs.StringVar(&buildArgs, "ba", "", "Specify additional args for Go build.")
|
fs.StringVar(&buildArgs, "ba", "", "Specify additional args for Go build.")
|
||||||
fs.Var(&buildEnvs, "be", "Specify additional env variables for Go build. e.g. GOARCH=arm.")
|
fs.Var(&buildEnvs, "be", "Specify additional env variables for Go build. e.g. GOARCH=arm.")
|
||||||
fs.StringVar(&outputP, "o", "", "Set the compressed file output path. Defaults to the current path.")
|
fs.StringVar(&outputP, "o", "", "Set the compressed file output path. Defaults to the current path.")
|
||||||
@ -445,7 +447,9 @@ func packApp(cmd *commands.Command, args []string) int {
|
|||||||
|
|
||||||
beeLogger.Log.Infof("Packaging application on '%s'...", thePath)
|
beeLogger.Log.Infof("Packaging application on '%s'...", thePath)
|
||||||
|
|
||||||
appName := path.Base(thePath)
|
if len(appName) == 0 {
|
||||||
|
appName = path.Base(thePath)
|
||||||
|
}
|
||||||
|
|
||||||
goos := runtime.GOOS
|
goos := runtime.GOOS
|
||||||
if v, found := syscall.Getenv("GOOS"); found {
|
if v, found := syscall.Getenv("GOOS"); found {
|
||||||
@ -470,7 +474,7 @@ func packApp(cmd *commands.Command, args []string) int {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
if build {
|
if build {
|
||||||
beeLogger.Log.Info("Building application...")
|
beeLogger.Log.Infof("Building application (%v)...", appName)
|
||||||
var envs []string
|
var envs []string
|
||||||
for _, env := range buildEnvs {
|
for _, env := range buildEnvs {
|
||||||
parts := strings.SplitN(env, "=", 2)
|
parts := strings.SplitN(env, "=", 2)
|
||||||
@ -553,7 +557,8 @@ func packApp(cmd *commands.Command, args []string) int {
|
|||||||
exs = append(exs, p)
|
exs = append(exs, p)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
exs = append(exs, `go.mod`)
|
||||||
|
exs = append(exs, `go.sum`)
|
||||||
var exr []*regexp.Regexp
|
var exr []*regexp.Regexp
|
||||||
for _, r := range excludeR {
|
for _, r := range excludeR {
|
||||||
if len(r) > 0 {
|
if len(r) > 0 {
|
||||||
|
@ -46,6 +46,8 @@ var (
|
|||||||
excludedPaths utils.StrFlags
|
excludedPaths utils.StrFlags
|
||||||
// Pass through to -tags arg of "go build"
|
// Pass through to -tags arg of "go build"
|
||||||
buildTags string
|
buildTags string
|
||||||
|
// Pass through to -ldflags arg of "go build"
|
||||||
|
buildLDFlags string
|
||||||
// Application path
|
// Application path
|
||||||
currpath string
|
currpath string
|
||||||
// Application name
|
// Application name
|
||||||
@ -72,6 +74,7 @@ func init() {
|
|||||||
CmdRun.Flag.Var(&excludedPaths, "e", "List of paths to exclude.")
|
CmdRun.Flag.Var(&excludedPaths, "e", "List of paths to exclude.")
|
||||||
CmdRun.Flag.BoolVar(&vendorWatch, "vendor", false, "Enable watch vendor folder.")
|
CmdRun.Flag.BoolVar(&vendorWatch, "vendor", false, "Enable watch vendor folder.")
|
||||||
CmdRun.Flag.StringVar(&buildTags, "tags", "", "Set the build tags. See: https://golang.org/pkg/go/build/")
|
CmdRun.Flag.StringVar(&buildTags, "tags", "", "Set the build tags. See: https://golang.org/pkg/go/build/")
|
||||||
|
CmdRun.Flag.StringVar(&buildLDFlags, "ldflags", "", "Set the build ldflags. See: https://golang.org/pkg/go/build/")
|
||||||
CmdRun.Flag.StringVar(&runmode, "runmode", "", "Set the Beego run mode.")
|
CmdRun.Flag.StringVar(&runmode, "runmode", "", "Set the Beego run mode.")
|
||||||
CmdRun.Flag.StringVar(&runargs, "runargs", "", "Extra args to run application")
|
CmdRun.Flag.StringVar(&runargs, "runargs", "", "Extra args to run application")
|
||||||
CmdRun.Flag.Var(&extraPackages, "ex", "List of extra package to watch.")
|
CmdRun.Flag.Var(&extraPackages, "ex", "List of extra package to watch.")
|
||||||
|
@ -39,11 +39,11 @@ var (
|
|||||||
watchExts = config.Conf.WatchExts
|
watchExts = config.Conf.WatchExts
|
||||||
watchExtsStatic = config.Conf.WatchExtsStatic
|
watchExtsStatic = config.Conf.WatchExtsStatic
|
||||||
ignoredFilesRegExps = []string{
|
ignoredFilesRegExps = []string{
|
||||||
`.#(\w+).go`,
|
`.#(\w+).go$`,
|
||||||
`.(\w+).go.swp`,
|
`.(\w+).go.swp$`,
|
||||||
`(\w+).go~`,
|
`(\w+).go~$`,
|
||||||
`(\w+).tmp`,
|
`(\w+).tmp$`,
|
||||||
`commentsRouter_controllers.go`,
|
`commentsRouter_controllers.go$`,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -158,6 +158,9 @@ func AutoBuild(files []string, isgenerate bool) {
|
|||||||
if buildTags != "" {
|
if buildTags != "" {
|
||||||
args = append(args, "-tags", buildTags)
|
args = append(args, "-tags", buildTags)
|
||||||
}
|
}
|
||||||
|
if buildLDFlags != "" {
|
||||||
|
args = append(args, "-ldflags", buildLDFlags)
|
||||||
|
}
|
||||||
args = append(args, files...)
|
args = append(args, files...)
|
||||||
|
|
||||||
bcmd := exec.Command(cmdName, args...)
|
bcmd := exec.Command(cmdName, args...)
|
||||||
|
@ -124,7 +124,7 @@ func GetBeegoVersion() string {
|
|||||||
}
|
}
|
||||||
wgopath := utils.GetGOPATHs()
|
wgopath := utils.GetGOPATHs()
|
||||||
if len(wgopath) == 0 {
|
if len(wgopath) == 0 {
|
||||||
beeLogger.Log.Error("You need to set GOPATH environment variable")
|
beeLogger.Log.Error("GOPATH environment is empty,may be you use `go module`")
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
for _, wg := range wgopath {
|
for _, wg := range wgopath {
|
||||||
|
@ -15,8 +15,10 @@
|
|||||||
package generate
|
package generate
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -948,10 +950,39 @@ func getFileName(tbName string) (filename string) {
|
|||||||
func getPackagePath(curpath string) (packpath string) {
|
func getPackagePath(curpath string) (packpath string) {
|
||||||
gopath := os.Getenv("GOPATH")
|
gopath := os.Getenv("GOPATH")
|
||||||
if gopath == "" {
|
if gopath == "" {
|
||||||
beeLogger.Log.Fatal("GOPATH environment variable is not set or empty")
|
info := "GOPATH environment variable is not set or empty"
|
||||||
|
gomodpath := filepath.Join(curpath, `go.mod`)
|
||||||
|
re, err := regexp.Compile(`^module\s+(.+)$`)
|
||||||
|
if err != nil {
|
||||||
|
beeLogger.Log.Error(info)
|
||||||
|
beeLogger.Log.Fatalf("try `go.mod` generate regexp error:%s", err)
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
fd, err := os.Open(gomodpath)
|
||||||
|
if err != nil {
|
||||||
|
beeLogger.Log.Error(info)
|
||||||
|
beeLogger.Log.Fatalf("try `go.mod` Error while reading 'go.mod',%s", gomodpath)
|
||||||
|
}
|
||||||
|
reader := bufio.NewReader(fd)
|
||||||
|
for {
|
||||||
|
byteLine, _, er := reader.ReadLine()
|
||||||
|
if er != nil && er != io.EOF {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if er == io.EOF {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
line := string(byteLine)
|
||||||
|
s := re.FindStringSubmatch(line)
|
||||||
|
if len(s) >= 2 {
|
||||||
|
return s[1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
beeLogger.Log.Error(info)
|
||||||
|
beeLogger.Log.Fatalf("try `go.mod` Error while parse 'go.mod',%s", gomodpath)
|
||||||
|
} else {
|
||||||
beeLogger.Log.Debugf("GOPATH: %s", utils.FILE(), utils.LINE(), gopath)
|
beeLogger.Log.Debugf("GOPATH: %s", utils.FILE(), utils.LINE(), gopath)
|
||||||
|
}
|
||||||
|
|
||||||
appsrcpath := ""
|
appsrcpath := ""
|
||||||
haspath := false
|
haspath := false
|
||||||
|
@ -432,12 +432,16 @@ func analyseControllerPkg(vendorPath, localName, pkgpath string) {
|
|||||||
pps := strings.Split(pkgpath, "/")
|
pps := strings.Split(pkgpath, "/")
|
||||||
importlist[pps[len(pps)-1]] = pkgpath
|
importlist[pps[len(pps)-1]] = pkgpath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pkgRealpath := ""
|
||||||
|
|
||||||
|
if os.Getenv(`GO111MODULE`) == `on` {
|
||||||
|
pkgRealpath = filepath.Join(bu.GetBeeWorkPath(), "..", pkgpath)
|
||||||
|
} else {
|
||||||
gopaths := bu.GetGOPATHs()
|
gopaths := bu.GetGOPATHs()
|
||||||
if len(gopaths) == 0 {
|
if len(gopaths) == 0 {
|
||||||
beeLogger.Log.Fatal("GOPATH environment variable is not set or empty")
|
beeLogger.Log.Fatal("GOPATH environment variable is not set or empty")
|
||||||
}
|
}
|
||||||
pkgRealpath := ""
|
|
||||||
|
|
||||||
wg, _ := filepath.EvalSymlinks(filepath.Join(vendorPath, pkgpath))
|
wg, _ := filepath.EvalSymlinks(filepath.Join(vendorPath, pkgpath))
|
||||||
if utils.FileExists(wg) {
|
if utils.FileExists(wg) {
|
||||||
pkgRealpath = wg
|
pkgRealpath = wg
|
||||||
@ -451,6 +455,7 @@ func analyseControllerPkg(vendorPath, localName, pkgpath string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if pkgRealpath != "" {
|
if pkgRealpath != "" {
|
||||||
if _, ok := pkgCache[pkgpath]; ok {
|
if _, ok := pkgCache[pkgpath]; ok {
|
||||||
return
|
return
|
||||||
@ -468,6 +473,7 @@ func analyseControllerPkg(vendorPath, localName, pkgpath string) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
beeLogger.Log.Fatalf("Error while parsing dir at '%s': %s", pkgpath, err)
|
beeLogger.Log.Fatalf("Error while parsing dir at '%s': %s", pkgpath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, pkg := range astPkgs {
|
for _, pkg := range astPkgs {
|
||||||
for _, fl := range pkg.Files {
|
for _, fl := range pkg.Files {
|
||||||
for _, d := range fl.Decls {
|
for _, d := range fl.Decls {
|
||||||
|
@ -33,6 +33,14 @@ import (
|
|||||||
"github.com/beego/bee/logger/colors"
|
"github.com/beego/bee/logger/colors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func GetBeeWorkPath() string {
|
||||||
|
beePath, err := filepath.Abs(filepath.Dir(os.Args[0]))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return beePath
|
||||||
|
}
|
||||||
|
|
||||||
// Go is a basic promise implementation: it wraps calls a function in a goroutine
|
// Go is a basic promise implementation: it wraps calls a function in a goroutine
|
||||||
// and returns a channel which will later return the function's return value.
|
// and returns a channel which will later return the function's return value.
|
||||||
func Go(f func() error) chan error {
|
func Go(f func() error) chan error {
|
||||||
@ -305,6 +313,7 @@ func Tmpl(text string, data interface{}) {
|
|||||||
func CheckEnv(appname string) (apppath, packpath string, err error) {
|
func CheckEnv(appname string) (apppath, packpath string, err error) {
|
||||||
gps := GetGOPATHs()
|
gps := GetGOPATHs()
|
||||||
if len(gps) == 0 {
|
if len(gps) == 0 {
|
||||||
|
beeLogger.Log.Error("if you want new a go module project,please add param `-module=true` and set env `G111MODULE=on`")
|
||||||
beeLogger.Log.Fatal("GOPATH environment variable is not set or empty")
|
beeLogger.Log.Fatal("GOPATH environment variable is not set or empty")
|
||||||
}
|
}
|
||||||
currpath, _ := os.Getwd()
|
currpath, _ := os.Getwd()
|
||||||
|
Loading…
Reference in New Issue
Block a user