func TestCustomParse(t *testing.T) { c := new(Custom) c.Files = []string{ "../_example/simple/public/", } c.Base = "../_example/simple/" c.Prefix = "prefix_test/" c.Exclude = []string{ "public/assets/data/exclude_me.txt", } c.Replace = []Replacer{ { File: "public/assets/data/test*.json", Replace: map[string]string{ "{world}": "earth", "{EMAIL}": "*****@*****.**", }, }, } files := make(map[string]*file.File) dirs := new(dir.Dir) oldFiles := c.Files c.Files = []string{"../sa8vuj948127498/*"} defaultCompression := compression.NewGzip() sharedConfig := new(SharedConfig) sharedConfig.Output = "124g98j13409b1341" sharedConfig.Compression = defaultCompression err := c.Parse(&files, &dirs, sharedConfig) assert.Error(t, err) c.Files = oldFiles err = c.Parse(&files, &dirs, sharedConfig) assert.NoError(t, err) assert.NotNil(t, files) assert.NotNil(t, dirs) // insert \r on windows var isWindows string if runtime.GOOS == "windows" { isWindows = "\r" } for _, f := range files { assert.True(t, strings.HasPrefix(f.Path, c.Prefix)) assert.NotEqual(t, "exclude_me.txt", f.Name) if f.Name == "test1.json" { e := "{" + isWindows + "\n \"he\": \"llo\"," + isWindows + "\n \"replace_test\": \"earth\"" + isWindows + "\n}" assert.Equal(t, e, data2str(f.Data)) } else if f.Name == "test2.json" { e := "{" + isWindows + "\n \"email\": \"[email protected]\"" + isWindows + "\n}" assert.Equal(t, e, data2str(f.Data)) } } ds := dirs.Clean() var blacklist []string for _, d := range ds { assert.True(t, strings.HasPrefix(d, c.Prefix)) assert.NotContains(t, blacklist, d) blacklist = append(blacklist, d) } }
func TestTemplate(t *testing.T) { var err error files := make(map[string]*file.File) files["test_file.txt"] = &file.File{ Name: "test_file.txt", Path: "static/test_file.txt", Data: `[]byte("\x12\x34\x56\x78\x10")`, } dirs := new(dir.Dir) dirs.Insert("static/") tp := new(Template) err = tp.Set("ayy lmao") assert.Error(t, err) assert.Equal(t, `Error: Template must be "files" or "file"`, err.Error()) err = tp.Set("files") assert.NoError(t, err) assert.Equal(t, "files", tp.name) defaultCompression := compression.NewGzip() tp.Variables = struct { Pkg string Files map[string]*file.File Spread bool DirList []string Compression *compression.Options Debug bool }{ Pkg: "main", Files: files, Spread: false, DirList: dirs.Clean(), Compression: defaultCompression.Options, } tp.template = "wrong {{.Err pudding" tmpl, err := tp.Exec() assert.Error(t, err) assert.Empty(t, tmpl) tp.template = "wrong{{if .Error}} pudding {{end}}" tmpl, err = tp.Exec() assert.Error(t, err) assert.Empty(t, tmpl) err = tp.Set("files") tmpl, err = tp.Exec() assert.NoError(t, err) assert.NotEmpty(t, tmpl) s := string(tmpl) assert.True(t, strings.Contains(s, `var FileStaticTestFileTxt = []byte("\x12\x34\x56\x78\x10")`)) assert.True(t, strings.Contains(s, `err = FS.Mkdir("static/", 0777)`)) assert.True(t, strings.Contains(s, `f, err = FS.OpenFile("static/test_file.txt", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)`)) // now with spread err = tp.Set("file") assert.NoError(t, err) assert.Equal(t, "file", tp.name) defaultCompression = compression.NewGzip() tp.Variables = struct { Pkg string Path string Name string Dir [][]string Data string Compression *compression.Options }{ Pkg: "main", Path: files["test_file.txt"].Path, Name: files["test_file.txt"].Name, Dir: dirs.List, Data: files["test_file.txt"].Data, Compression: defaultCompression.Options, } tmpl, err = tp.Exec() assert.NoError(t, err) assert.NotEmpty(t, tmpl) s = string(tmpl) assert.True(t, strings.Contains(s, `var FileStaticTestFileTxt = []byte("\x12\x34\x56\x78\x10")`)) assert.True(t, strings.Contains(s, `f, err := FS.OpenFile("static/test_file.txt", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)`)) }
func main() { runtime.GOMAXPROCS(runtime.NumCPU()) // create config and try to get b0x file from args f := new(config.File) err = f.FromArg(true) if err != nil { log.Fatal(err) } // load b0x file's config cfg, err = f.Load() if err != nil { log.Fatal(err) } err = cfg.Defaults() if err != nil { log.Fatal(err) } // creates a config that can be inserTed into custom // without causing a import cycle sharedConfig := new(custom.SharedConfig) sharedConfig.Output = cfg.Output sharedConfig.Compression = compression.NewGzip() sharedConfig.Compression.Options = cfg.Compression // loop through b0x's [custom] objects for _, c := range cfg.Custom { err = c.Parse(&files, &dirs, sharedConfig) if err != nil { log.Fatal(err) } } // create files template and exec it t := new(template.Template) t.Set("files") t.Variables = struct { Pkg string Files map[string]*file.File Spread bool DirList []string Compression *compression.Options Debug bool }{ Pkg: cfg.Pkg, Files: files, Spread: cfg.Spread, DirList: dirs.Clean(), Compression: cfg.Compression, Debug: cfg.Debug, } tmpl, err := t.Exec() if err != nil { log.Fatal(err) } // create dest folder when it doesn't exists if !utils.Exists(cfg.Dest) { err = os.MkdirAll(cfg.Dest, 0777) if err != nil { log.Fatal(err) } } // gofmt if cfg.Fmt { tmpl, err = format.Source(tmpl) if err != nil { log.Fatal(err) } } // write final execuTed template into the destination file err = ioutil.WriteFile(cfg.Dest+cfg.Output, tmpl, 0777) if err != nil { log.Fatal(err) } // write spread files if cfg.Spread { a := strings.Split(path.Dir(cfg.Dest), "/") dirName := a[len(a)-1:][0] for _, f := range files { a := strings.Split(path.Dir(f.Path), "/") fileDirName := a[len(a)-1:][0] if dirName == fileDirName { continue } // transform / to _ and some other chars... customName := "b0xfile_" + utils.FixName(f.Path) + ".go" // creates file template and exec it t := new(template.Template) t.Set("file") t.Variables = struct { Pkg string Path string Name string Dir [][]string Data string Compression *compression.Options }{ Pkg: cfg.Pkg, Path: f.Path, Name: f.Name, Dir: dirs.List, Data: f.Data, Compression: cfg.Compression, } tmpl, err := t.Exec() if err != nil { log.Fatal(err) } // gofmt if cfg.Fmt { tmpl, err = format.Source(tmpl) if err != nil { log.Fatal(err) } } // write final execuTed template into the destination file err = ioutil.WriteFile(cfg.Dest+customName, tmpl, 0777) if err != nil { log.Fatal(err) } } } // success log.Println("fileb0x:", cfg.Dest+cfg.Output, "written!") }