Skip to content

Commit

Permalink
improve cli && support automation deploy frontend resource
Browse files Browse the repository at this point in the history
  • Loading branch information
cg33 committed Oct 7, 2019
1 parent 37ac27d commit 43e796f
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 433 deletions.
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ all: run

assets:
find ./ -name ".DS_Store" -depth -exec rm {} \;
make combine
rm -rf dist/*
rm -rf ./template/adminlte/resource/assets/dist
mkdir ./template/adminlte/resource/assets/dist
mkdir ./template/adminlte/resource/assets/dist/js
mkdir ./template/adminlte/resource/assets/dist/css
cp ./template/adminlte/resource/assets/src/js/*.js ./template/adminlte/resource/assets/dist/js/
cp ./template/adminlte/resource/assets/src/css/blue.png ./template/adminlte/resource/assets/dist/css/blue.png
cp -R ./template/adminlte/resource/assets/src/css/fonts ./template/adminlte/resource/assets/dist/css/
cp -R ./template/adminlte/resource/assets/src/img ./template/adminlte/resource/assets/dist/
cp -R ./template/adminlte/resource/assets/src/fonts ./template/adminlte/resource/assets/dist/
make combine
admincli compile asset
make tmpl
make fmt

combine:
find ./ -name ".DS_Store" -depth -exec rm {} \;
Expand Down
70 changes: 64 additions & 6 deletions admincli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,13 @@ func main() {

cmd.Command("asset", "compile asset file for template or component", func(cmd *cli.Cmd) {
var (
rootPath = cmd.StringOpt("path", "./template/adminlte/resource/assets/dist/...", "compile root path")
outputPath = cmd.StringOpt("out", "./template/adminlte/resource/assets.go", "compile output path")
rootPath = cmd.StringOpt("path", "./template/adminlte/resource/assets/dist/", "compile root path")
outputPath = cmd.StringOpt("out", "./template/adminlte/resource/", "compile output path")
packageName = cmd.StringOpt("pa", "resource", "output file package name")
)

cmd.Action = func() {
compileAsset(*rootPath, *outputPath)
compileAsset(*rootPath, *outputPath, *packageName)
}
})
})
Expand Down Expand Up @@ -366,12 +367,69 @@ func checkError(err error) {
}
}

func compileAsset(rootPath, outputPath string) {
func compileAsset(rootPath, outputPath, packageName string) {
cfg := bindata.NewConfig()
cfg.Output = outputPath
cfg.Package = packageName
cfg.Output = outputPath + "assets.go"
cfg.Input = make([]bindata.InputConfig, 0)
cfg.Input = append(cfg.Input, parseInput(rootPath))
cfg.Input = append(cfg.Input, parseInput(rootPath+"..."))
checkError(bindata.Translate(cfg))

rootPathArr := strings.Split(rootPath, "assets")
if len(rootPathArr) > 0 {
listContent := `package ` + packageName + `
var AssetsList = []string{
`
fileNames, err := getAllFiles(rootPath)

if err != nil {
return
}

for _, name := range fileNames {
listContent += ` "` + rootPathArr[1] + strings.Replace(name, rootPath, "", -1)[1:] + `",
`
}

listContent += `
}`

err = ioutil.WriteFile(outputPath+"/assets_list.go", []byte(listContent), 0644)
if err != nil {
return
}
}
}

func getAllFiles(dirPth string) (files []string, err error) {
var dirs []string
dir, err := ioutil.ReadDir(dirPth)
if err != nil {
return nil, err
}

PthSep := string(os.PathSeparator)

for _, fi := range dir {
if fi.IsDir() { // 目录, 递归遍历
dirs = append(dirs, dirPth+PthSep+fi.Name())
_, _ = getAllFiles(dirPth + PthSep + fi.Name())
} else {
// 过滤指定格式
files = append(files, dirPth+PthSep+fi.Name())
}
}

// 读取子目录下文件
for _, table := range dirs {
temp, _ := getAllFiles(table)
for _, temp1 := range temp {
files = append(files, temp1)
}
}

return files, nil
}

func parseInput(path string) bindata.InputConfig {
Expand Down
4 changes: 4 additions & 0 deletions admincli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ func TestGetType(t *testing.T) {
fmt.Println(getType("int(3434)"))
fmt.Println(path.Ext("sdafdfs.css"))
}

func TestGetAllFiles(t *testing.T) {
fmt.Println(getAllFiles("."))
}
2 changes: 1 addition & 1 deletion template/adminlte/adminlte.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,5 @@ func (*Theme) GetAsset(path string) ([]byte, error) {
}

func (*Theme) GetAssetList() []string {
return assets
return resource.AssetsList
}
470 changes: 48 additions & 422 deletions template/adminlte/resource/assets.go

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package adminlte
package resource

var assets = []string{
var AssetsList = []string{
"/dist/css/all.min.css",
"/dist/css/blue.png",
"/dist/css/fonts/6xK3dSBYKcSV-LCoeQqfX1RYOo3qOK7g.ttf",
Expand Down

0 comments on commit 43e796f

Please sign in to comment.