mirror of
				https://github.com/beego/bee.git
				synced 2025-10-31 12:33:21 +00:00 
			
		
		
		
	支持go mod
This commit is contained in:
		| @@ -16,26 +16,31 @@ package new | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"github.com/beego/bee/cmd/commands/version" | ||||||
| 	"os" | 	"os" | ||||||
| 	path "path/filepath" | 	path "path/filepath" | ||||||
|  | 	"runtime" | ||||||
| 	"strings" | 	"strings" | ||||||
|  |  | ||||||
| 	"github.com/beego/bee/cmd/commands" | 	"github.com/beego/bee/cmd/commands" | ||||||
| 	"github.com/beego/bee/cmd/commands/version" | 	"github.com/beego/bee/logger" | ||||||
| 	beeLogger "github.com/beego/bee/logger" |  | ||||||
| 	"github.com/beego/bee/logger/colors" | 	"github.com/beego/bee/logger/colors" | ||||||
| 	"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,13 @@ func init() { | |||||||
|     beego.Router("/", &controllers.MainController{}) |     beego.Router("/", &controllers.MainController{}) | ||||||
| } | } | ||||||
| ` | ` | ||||||
|  | var goMod = ` | ||||||
|  | module %s | ||||||
|  |  | ||||||
|  | go %s | ||||||
|  |  | ||||||
|  | require github.com/astaxie/beego %s | ||||||
|  | ` | ||||||
| var test = `package test | var test = `package test | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
| @@ -245,18 +256,39 @@ 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 { | ||||||
| 	if err != nil { | 		cmd.Flag.Parse(args[1:]) | ||||||
| 		beeLogger.Log.Fatalf("%s", err) | 	} else { | ||||||
|  | 		module = "false" | ||||||
|  | 	} | ||||||
|  | 	appPath := `` | ||||||
|  | 	packPath := `` | ||||||
|  | 	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 { | ||||||
|  | 			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) { | ||||||
| @@ -310,6 +342,10 @@ func CreateApp(cmd *commands.Command, args []string) int { | |||||||
| 	fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "main.go"), "\x1b[0m") | 	fmt.Fprintf(output, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", path.Join(appPath, "main.go"), "\x1b[0m") | ||||||
| 	utils.WriteToFile(path.Join(appPath, "main.go"), strings.Replace(maingo, "{{.Appname}}", packPath, -1)) | 	utils.WriteToFile(path.Join(appPath, "main.go"), strings.Replace(maingo, "{{.Appname}}", packPath, -1)) | ||||||
|  |  | ||||||
|  | 	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())) | ||||||
|  | 	} | ||||||
| 	beeLogger.Log.Success("New application successfully created!") | 	beeLogger.Log.Success("New application successfully created!") | ||||||
| 	return 0 | 	return 0 | ||||||
| } | } | ||||||
|   | |||||||
| @@ -29,10 +29,18 @@ import ( | |||||||
| 	"time" | 	"time" | ||||||
| 	"unicode" | 	"unicode" | ||||||
|  |  | ||||||
| 	beeLogger "github.com/beego/bee/logger" | 	"github.com/beego/bee/logger" | ||||||
| 	"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 { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 qiantao
					qiantao