github.com/kotovmak/go-admin@v1.1.1/adm/compile_assets.go (about) 1 package main 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "regexp" 8 "strings" 9 10 "github.com/jteeuwen/go-bindata" 11 ) 12 13 func compileAsset(rootPath, outputPath, packageName string) { 14 cfg := bindata.NewConfig() 15 cfg.Package = packageName 16 cfg.Output = outputPath + "assets.go" 17 cfg.Input = make([]bindata.InputConfig, 0) 18 cfg.Input = append(cfg.Input, parseInput(rootPath+"...")) 19 checkError(bindata.Translate(cfg)) 20 21 rootPathArr := strings.Split(rootPath, "assets") 22 if len(rootPathArr) > 0 { 23 listContent := `package ` + packageName + ` 24 25 var AssetsList = []string{ 26 ` 27 pathsContent := `package ` + packageName + ` 28 29 var AssetPaths = map[string]string{ 30 ` 31 32 fileNames, err := getAllFiles(rootPath) 33 34 if err != nil { 35 return 36 } 37 38 for _, name := range fileNames { 39 listContent += ` "` + rootPathArr[1] + strings.ReplaceAll(name, rootPath, "")[1:] + `", 40 ` 41 ext := filepath.Ext(name) 42 if ext == ".css" || ext == ".js" { 43 fileName := filepath.Base(name) 44 reg, _ := regexp.Compile(".min.(.*?)" + ext) 45 pathsContent += ` "` + reg.ReplaceAllString(fileName, ".min"+ext) + `":"` + 46 rootPathArr[1] + strings.ReplaceAll(name, rootPath, "")[1:] + `", 47 ` 48 } 49 } 50 51 pathsContent += ` 52 }` 53 54 listContent += ` 55 }` 56 57 err = ioutil.WriteFile(outputPath+"/assets_list.go", []byte(listContent), 0644) 58 if err != nil { 59 return 60 } 61 err = ioutil.WriteFile(outputPath+"/assets_path.go", []byte(pathsContent), 0644) 62 if err != nil { 63 return 64 } 65 } 66 } 67 68 func getAllFiles(dirPth string) (files []string, err error) { 69 var dirs []string 70 dir, err := ioutil.ReadDir(dirPth) 71 if err != nil { 72 return nil, err 73 } 74 75 PthSep := string(os.PathSeparator) 76 77 for _, fi := range dir { 78 if fi.IsDir() { 79 dirs = append(dirs, dirPth+PthSep+fi.Name()) 80 _, _ = getAllFiles(dirPth + PthSep + fi.Name()) 81 } else { 82 files = append(files, dirPth+PthSep+fi.Name()) 83 } 84 } 85 86 for _, table := range dirs { 87 temp, _ := getAllFiles(table) 88 files = append(files, temp...) 89 } 90 91 return files, nil 92 } 93 94 func parseInput(path string) bindata.InputConfig { 95 if strings.HasSuffix(path, "/...") { 96 return bindata.InputConfig{ 97 Path: filepath.Clean(path[:len(path)-4]), 98 Recursive: true, 99 } 100 } 101 return bindata.InputConfig{ 102 Path: filepath.Clean(path), 103 Recursive: false, 104 } 105 }