github.com/oNaiPs/go-generate-fast@v0.3.0/src/plugins/esc/esc.go (about) 1 package plugin_esc 2 3 import ( 4 "os" 5 "path/filepath" 6 "regexp" 7 8 "github.com/oNaiPs/go-generate-fast/src/plugins" 9 "go.uber.org/zap" 10 ) 11 12 type EscPlugin struct { 13 plugins.Plugin 14 } 15 16 func (p *EscPlugin) Name() string { 17 return "esc" 18 } 19 20 func (p *EscPlugin) Matches(opts plugins.GenerateOpts) bool { 21 return opts.ExecutableName == "esc" || 22 opts.GoPackage == "github.com/mjibson/esc" 23 } 24 25 func (p *EscPlugin) ComputeInputOutputFiles(opts plugins.GenerateOpts) *plugins.InputOutputFiles { 26 27 conf := parseArgs(opts.SanitizedArgs) 28 if conf == nil { 29 return nil 30 } 31 32 var err error 33 34 ioFiles := plugins.InputOutputFiles{} 35 36 escFiles := []string{} 37 var ignoreRegexp *regexp.Regexp 38 if conf.Ignore != "" { 39 ignoreRegexp, err = regexp.Compile(conf.Ignore) 40 if err != nil { 41 zap.S().Warn("esc parsing error: %s", err) 42 return nil 43 } 44 } 45 var includeRegexp *regexp.Regexp 46 if conf.Include != "" { 47 includeRegexp, err = regexp.Compile(conf.Include) 48 if err != nil { 49 zap.S().Warn("esc parsing error: %s", err) 50 return nil 51 } 52 } 53 54 directories := []string{} 55 for _, base := range conf.Files { 56 57 files := []string{base} 58 for len(files) > 0 { 59 fname := files[0] 60 files = files[1:] 61 if ignoreRegexp != nil && ignoreRegexp.MatchString(fname) { 62 continue 63 } 64 f, err := os.Open(fname) 65 if err != nil { 66 zap.S().Warn("esc parsing error: %s", err) 67 return nil 68 } 69 fi, err := f.Stat() 70 if err != nil { 71 zap.S().Warn("esc parsing error: %s", err) 72 return nil 73 } 74 fpath := filepath.ToSlash(fname) 75 if fi.IsDir() { 76 fis, err := f.Readdir(0) 77 if err != nil { 78 zap.S().Warn("esc parsing error: %s", err) 79 return nil 80 } 81 for _, fi := range fis { 82 childFName := filepath.Join(fname, fi.Name()) 83 files = append(files, childFName) 84 if ignoreRegexp != nil && ignoreRegexp.MatchString(childFName) { 85 continue 86 } 87 } 88 directories = append(directories, fpath) 89 } else if includeRegexp == nil || includeRegexp.MatchString(fname) { 90 escFiles = append(escFiles, fpath) 91 } 92 f.Close() 93 } 94 } 95 96 ioFiles.InputFiles = append(ioFiles.InputFiles, escFiles...) 97 98 fakeOutFileName := "static.go" 99 if conf.OutputFile != "" { 100 fakeOutFileName = conf.OutputFile 101 } 102 ioFiles.OutputFiles = append(ioFiles.OutputFiles, fakeOutFileName) 103 ioFiles.Extra = directories 104 105 return &ioFiles 106 } 107 108 func init() { 109 plugins.RegisterPlugin(&EscPlugin{}) 110 } 111 112 func New() { 113 plugins.RegisterPlugin(&EscPlugin{}) 114 }