diff --git a/config.go b/config.go index 0fff58e..af6c7e4 100644 --- a/config.go +++ b/config.go @@ -4,13 +4,14 @@ import "time" // Config 配置文件 type Config struct { - Title string `yaml:"title"` // 网站标题 - Seo SEO `yaml:"seo"` // seo相关信息 - Person Person `yaml:"person"` // 博主个人信息 - Theme string `yaml:"theme"` // 主题 - Site Site `yaml:"site"` // 站点配置信息 - Now time.Time // 当前时间 - Deploy Deploy `yaml:"deploy"` + Title string `yaml:"title"` // 网站标题 + SourceVersion string `yaml:"-"` // 资源版本号 + Seo SEO `yaml:"seo"` // seo相关信息 + Person Person `yaml:"person"` // 博主个人信息 + Theme string `yaml:"theme"` // 主题 + Site Site `yaml:"site"` // 站点配置信息 + Now time.Time `yaml:"-"` // 当前时间 + Deploy Deploy `yaml:"deploy"` // 部署配置 } type Site struct { diff --git a/deploy.go b/deploy.go index b977cf6..12e9f49 100644 --- a/deploy.go +++ b/deploy.go @@ -20,6 +20,11 @@ func deployCmd() *cobra.Command { } case GitDeploy: } + + // generate source + if err := generateCmd().Execute(); err != nil { + sfault("generate project failed: %v", err) + } }, Run: func(cmd *cobra.Command, args []string) { config := outter.Config.Deploy @@ -31,6 +36,7 @@ func deployCmd() *cobra.Command { } if err := uploadToUpyun(config.UpyunAuth); err != nil { serr("%v", err) + return } sout("deploy to upyun success") } diff --git a/generate.go b/generate.go index 80a13fa..3a1b06f 100644 --- a/generate.go +++ b/generate.go @@ -18,15 +18,18 @@ import ( func generateCmd() *cobra.Command { var outter Outter - + var startAt time.Time cmd := &cobra.Command{ Use: "generate", Aliases: []string{"g"}, Short: "generate project to dist folder", - Run: func(cmd *cobra.Command, args []string) { - startAt := time.Now() + PreRun: func(cmd *cobra.Command, args []string) { + startAt = time.Now() // 读取配置文件 outter.Config = readConfigFile() + outter.Config.SourceVersion = randString(8) + }, + Run: func(cmd *cobra.Command, args []string) { // 读取资源文件 outter.MetaData = readDataSource() // 读取文章列表 @@ -37,8 +40,11 @@ func generateCmd() *cobra.Command { outter.Theme = readTheme(outter.Config.Theme) // 数据写入模板文件 outter.generate() + + }, + PostRun: func(cmd *cobra.Command, args []string) { endAt := time.Since(startAt) - fmt.Printf("generate used: %s\n", endAt.String()) + sout("generate used: %s", endAt.String()) }, } return cmd diff --git a/output.go b/output.go index d8fabf8..a5b87d7 100644 --- a/output.go +++ b/output.go @@ -81,18 +81,18 @@ func (o *Outter) sourceCopy() { args = []string{"cp", "-r"} } themePath := slash(fmt.Sprintf("./themes/%s/", o.Config.Theme)) - destPath := slash("./dist/") - cmd := exec.Command(args[0], append(args, themePath+"css", slash(destPath+"css/"))...) + destPath := slash("./dist/" + o.Config.SourceVersion + "/") + cmd := exec.Command(args[0], append(args[1:], themePath+"css", slash(destPath+"css/"))...) if err := cmd.Run(); err != nil { sout(cmd.String()) sfault("copy theme css source failed: %v", err) } - cmd = exec.Command(args[0], append(args, themePath+"js", slash(destPath+"js/"))...) + cmd = exec.Command(args[0], append(args[1:], themePath+"js", slash(destPath+"js/"))...) if err := cmd.Run(); err != nil { sout(cmd.String()) sfault("copy theme js source failed: %v", err) } - cmd = exec.Command(args[0], append(args, themePath+"images", slash(destPath+"images/"))...) + cmd = exec.Command(args[0], append(args[1:], themePath+"images", slash(destPath+"images/"))...) if err := cmd.Run(); err != nil { sout(cmd.String()) sfault("copy theme images source failed: %v", err) diff --git a/utils.go b/utils.go index c6f9d9f..0d2b6fb 100644 --- a/utils.go +++ b/utils.go @@ -1,6 +1,8 @@ package main import ( + "crypto/sha256" + "encoding/hex" "fmt" "os" "os/exec" @@ -152,13 +154,20 @@ func goInstall(pkg string) error { } func uploadToUpyun(auth string) error { - _, err := exec.Command("upx", "--auth", auth, "rm", "/").Output() + _, err := exec.Command("upx", "--auth", auth, "rm", "-d", "-a", "/*").Output() if err != nil { return fmt.Errorf("remove old data failed: %v", err) } - _, err = exec.Command("upx", "--auth", auth, "put", "./dist").Output() + _, err = exec.Command("upx", "--auth", auth, "put", "./dist/.", "/").Output() if err != nil { return fmt.Errorf("deploy data failed: %v", err) } return nil } + +func randString(length int) string { + s := sha256.New() + s.Write([]byte(time.Now().String())) + res := s.Sum(nil) + return hex.EncodeToString(res)[:length] +}