Skip to content

Commit

Permalink
feat: add cache and clean features (#337)
Browse files Browse the repository at this point in the history
* feat#336 add cache and clean features

* compatible with windows and Unix

* perfect the content

* add one todo
  • Loading branch information
ywanbing committed May 19, 2022
1 parent 1549b9d commit e297ae4
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 36 deletions.
9 changes: 5 additions & 4 deletions cmd/jupiter/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ USAGE:
jupiter [global options] command [command options] [arguments...]
COMMANDS:
new, n generate code framework
run, r auto restart program when files changed
update, upgrade Upgrade to the latest version
help, h Shows a list of commands or help for one command
clean clear all cached
new, n generate code framework
run, r auto restart program when files changed
update, upgrade Upgrade to the latest version
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--help, -h show help
Expand Down
11 changes: 11 additions & 0 deletions cmd/jupiter/internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ var Commands = []cli.Command{
Usage: "choose template",
Value: "jupiter-layout",
},
cli.BoolFlag{
Name: "clean",
Usage: "clear cached and re pull templates",
},
},
Action: func(c *cli.Context) error {
return New(c)
Expand Down Expand Up @@ -59,4 +63,11 @@ var Commands = []cli.Command{
return Update(c)
},
},
{
Name: "clean",
Usage: "clear all cached",
Action: func(c *cli.Context) error {
return Clean(c)
},
},
}
50 changes: 50 additions & 0 deletions cmd/jupiter/internal/cmd/cmd_clean.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2022 Douyu
//
// 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 cmd

import (
"fmt"
"os"
"path/filepath"

"github.com/fatih/color"
"github.com/urfave/cli"
)

// Clean 清除所有的缓存
func Clean(c *cli.Context) error {
// 1. 清除已经存在的临时模板文件
if err := cleanTempLayout(); err != nil {
return err
}

// 2. clean other ...

color.Green("clear complete ...")
return nil
}

// 清除已经存在的临时模板文件
func cleanTempLayout() error {
fmt.Println("clear temp project layout ...")
// 查看临时文件之中是否已经存在该文件夹
tempPath := filepath.Join(os.TempDir(), "local_temp_jupiter_layout")
// 需要刷新,提前清理缓存的文件
if err := os.RemoveAll(tempPath); err != nil {
return err
}

return nil
}
69 changes: 37 additions & 32 deletions cmd/jupiter/internal/cmd/cmd_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ const (

// New 生成项目
func New(c *cli.Context) error {
return generate(c, c.String("type"))
return generate(c, c.String("type"), c.Bool("clean"))
}

// generate 生成项目
func generate(c *cli.Context, cmd string) error {
func generate(c *cli.Context, cmd string, clean bool) error {
if len(c.Args().First()) == 0 {
return errors.New("no project name like test-go found")
}
Expand All @@ -53,7 +53,7 @@ func generate(c *cli.Context, cmd string) error {

files := make([]file, 0)

gitFileInfos := getFileInfosByGit(cmd)
gitFileInfos := getFileInfosByGit(cmd, clean)
for _, f := range gitFileInfos {
files = append(files, *f)
}
Expand Down Expand Up @@ -165,7 +165,7 @@ func create(c config) error {
// 替换为真实的c.Dir
tpl := strings.ReplaceAll(
string(file.Tmpl),
gitOriginModulePath, //+"/"+c.Type,
gitOriginModulePath, // +"/"+c.Type,
c.Dir)

if err := write(c, f, tpl); err != nil {
Expand Down Expand Up @@ -197,39 +197,50 @@ func write(c config, file, tmpl string) error {

// getFileInfosByGit 从git拉取最新的模板代码 并抽象成map[相对路径]文件流
// name: 生成的项目类型
func getFileInfosByGit(name string) (
fileInfos map[string]*file) {
fileInfos = make(map[string]*file)
// 存放于git的模板地址
gitPath := "https://" + gitOriginModulePath + ".git"

// clone最新仓库的master分支
func getFileInfosByGit(name string, clean bool) (fileInfos map[string]*file) {
if clean {
if err := cleanTempLayout(); err != nil {
panic(err)
}
}
// 设置clone别名 避免冲突
finalTemplateDir := "local_temp_jupiter_layout"
fmt.Println("git", "clone", gitPath)

cmd := exec.Command("git", "clone", gitPath, finalTemplateDir, "-b", "main", "--depth=1")
if err := cmd.Run(); err != nil {
// 查看临时文件之中是否已经存在该文件夹
tempPath := filepath.Join(os.TempDir(), "local_temp_jupiter_layout")
// os.Stat 获取文件信息
_, err := os.Stat(tempPath)
if os.IsNotExist(err) {
// 存放于git的模板地址
gitPath := "https://" + gitOriginModulePath + ".git"

fmt.Println("git", "clone", gitPath)

// clone最新仓库的master分支
// 不存在则拉取模板
cmd := exec.Command("git", "clone", gitPath, tempPath, "-b", "main", "--depth=1")
if err := cmd.Run(); err != nil {
panic(err)
}
} else if os.IsExist(err) || err == nil {
// 判断是否需要刷新模板信息
// todo ... 后面有时间再加上
} else {
// 这里的错误,是说明出现了未知的错误,应该抛出
panic(err)
}

fileInfos = make(map[string]*file)
// 获取模板的文件流
// io/fs为1.16新增标准库 低版本不支持
// os.FileInfo实现了和io/fs.FileInfo相同的接口 确保go低版本可以成功编译通过
err := filepath.Walk("./"+finalTemplateDir, func(path string, info os.FileInfo, err error) error {

err = filepath.Walk(tempPath, func(path string, info os.FileInfo, err error) error {
// 过滤git目录中文件
if !info.IsDir() && !strings.Contains(path, ".git/") {

fullPath := strings.ReplaceAll(path, "\\", "/")
bs, err := ioutil.ReadFile(fullPath)
if !info.IsDir() && !strings.Contains(strings.ReplaceAll(path, "\\", "/"), ".git/") {
bs, err := ioutil.ReadFile(path)
if err != nil {
fmt.Printf("[jupiter] Read file failed: fullPath=[%v] err=[%v]",
fullPath, err)

fmt.Printf("[jupiter] Read file failed: fullPath=[%v] err=[%v]", path, err)
}

fullPath = strings.ReplaceAll(fullPath, finalTemplateDir, "")
fullPath := strings.ReplaceAll(path, tempPath, "")
fileInfos[fullPath] = &file{fullPath, bs}
}
return nil
Expand All @@ -238,11 +249,5 @@ func getFileInfosByGit(name string) (
panic(err)
}

// 删除clone的仓库
err = os.RemoveAll("./" + finalTemplateDir)
if err != nil {
panic(err)
}

return fileInfos
}

0 comments on commit e297ae4

Please sign in to comment.