Compare commits

..

3 Commits

Author SHA1 Message Date
fancl 75ca055772 生成器去除默认的生成的包名称 2024-12-13 16:31:32 +08:00
fancl 7d1ecab9fb 修改生成器的默认版本 2024-12-13 16:28:59 +08:00
fancl e7fc3614a8 优化生成工具逻辑 2024-12-13 16:24:43 +08:00
1 changed files with 21 additions and 19 deletions

View File

@ -7,6 +7,7 @@ import (
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"git.nobla.cn/golang/kos/util/env"
"io" "io"
"net/http" "net/http"
"os" "os"
@ -18,8 +19,8 @@ import (
) )
var ( var (
packageNameFlag = flag.String("package-name", "github.com/golang/example", "Application package name") packageNameFlag = flag.String("package-name", "", "应用程序包名称")
versionFlag = flag.String("version", "v0.0.1", "Application package version") versionFlag = flag.String("version", env.Get("MOTO_VERSION", "v0.0.3"), "模板版本号,可以使用环境变量 MOTO_VERSION")
) )
var ( var (
@ -70,29 +71,25 @@ func extractFile(file string, dirname string) (err error) {
if header.Typeflag == tar.TypeXGlobalHeader { if header.Typeflag == tar.TypeXGlobalHeader {
continue continue
} }
fileInfo := header.FileInfo()
if prefix == "" && header.Typeflag == tar.TypeDir { if prefix == "" && header.Typeflag == tar.TypeDir {
prefix = header.Name prefix = header.Name
continue continue
} }
filename := path.Join(dirname, strings.TrimPrefix(header.Name, prefix)) realname := strings.TrimPrefix(header.Name, prefix)
if strings.HasPrefix(realname, "generator") {
continue
}
filename := path.Join(dirname, realname)
if header.Typeflag == tar.TypeDir { if header.Typeflag == tar.TypeDir {
if err = os.MkdirAll(filename, fileInfo.Mode().Perm()); err != nil { if err = os.MkdirAll(filename, 0755); err != nil {
return return
} }
if runtime.GOOS != "windows" {
if err = os.Chmod(filename, fileInfo.Mode().Perm()); err != nil {
return
}
}
} else { } else {
if err = writeFile(filename, tarReader); err != nil { if err = writeFile(filename, tarReader); err != nil {
return err return err
} }
if runtime.GOOS != "windows" { if runtime.GOOS != "windows" {
if err = os.Chmod(filename, fileInfo.Mode().Perm()); err != nil { os.Chmod(filename, 0644)
return
}
} }
} }
} }
@ -163,28 +160,33 @@ func main() {
) )
flag.Parse() flag.Parse()
packageName = *packageNameFlag packageName = *packageNameFlag
if packageName == "" {
if len(os.Args) > 1 {
packageName = os.Args[1]
}
}
pos = strings.LastIndexByte(packageName, '/') pos = strings.LastIndexByte(packageName, '/')
if pos == -1 { if pos == -1 {
fmt.Println("package name invalid") fmt.Println("应用包名称是无效的")
os.Exit(1) os.Exit(1)
} }
appname = packageName[pos:] appname = packageName[pos:]
if dirname, err = os.Getwd(); err != nil { if dirname, err = os.Getwd(); err != nil {
fmt.Println(err.Error()) fmt.Println("获取应用目录失败:" + err.Error())
os.Exit(1) os.Exit(1)
} }
dirname = path.Join(dirname, appname) dirname = path.Join(dirname, appname)
if err = os.MkdirAll(dirname, 0755); err != nil { if err = os.MkdirAll(dirname, 0755); err != nil {
fmt.Println(err.Error()) fmt.Println("创建应用目录失败:" + err.Error())
os.Exit(1) os.Exit(1)
} }
if err = downloadPackage(*versionFlag, dirname); err != nil { if err = downloadPackage(*versionFlag, dirname); err != nil {
fmt.Println("download file error:" + err.Error()) fmt.Println("下载模板文件失败:" + err.Error())
os.Exit(1) os.Exit(1)
} }
if err = replaceFiles(dirname, oldPackageName, packageName); err != nil { if err = replaceFiles(dirname, oldPackageName, packageName); err != nil {
fmt.Println(err.Error()) fmt.Println("生产应用文件失败:" + err.Error())
os.Exit(1) os.Exit(1)
} }
fmt.Println("Create project successful !!!") fmt.Println("创建项目成功")
} }