Exemple #1
0
func TestTemplate(t *testing.T) {
	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)
	tp.Set("files")
	tp.Variables = struct {
		Pkg     string
		Files   map[string]*file.File
		Spread  bool
		DirList []string
	}{
		Pkg:     "main",
		Files:   files,
		Spread:  false,
		DirList: dirs.Clean(),
	}

	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
	tp.Set("file")
	tp.Variables = struct {
		Pkg  string
		Path string
		Name string
		Dir  [][]string
		Data string
	}{
		Pkg:  "main",
		Path: files["test_file.txt"].Path,
		Name: files["test_file.txt"].Name,
		Dir:  dirs.List,
		Data: files["test_file.txt"].Data,
	}

	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)`))
}
Exemple #2
0
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)

	err := c.Parse(&files, &dirs)
	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)
	}
}
Exemple #3
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	var err error
	var cfg *config.Config

	// create config and try to get b0x file from args
	j := new(config.JSON)
	err = j.FromArg()

	// required info
	if err != nil {
		log.Fatal(err)
	}

	// load b0x file
	cfg, err = j.Load()
	cfg.Defaults()

	files := make(map[string]*file.File)
	dirs := new(dir.Dir)

	// loop through b0x's [custom] objects
	for _, c := range cfg.Custom {
		err = c.Parse(&files, &dirs)
		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
	}{
		Pkg:     cfg.Pkg,
		Files:   files,
		Spread:  cfg.Spread,
		DirList: dirs.Clean(),
	}
	tmpl, err := t.Exec()
	if err != nil {
		log.Fatal(err)
	}

	// create dest folder when it doesn't exists
	if _, err := os.Stat(cfg.Dest); os.IsNotExist(err) {
		err = os.MkdirAll(cfg.Dest, 0777)
		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 spreaded 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
			}{
				Pkg:  cfg.Pkg,
				Path: f.Path,
				Name: f.Name,
				Dir:  dirs.List,
				Data: f.Data,
			}
			tmpl, err := t.Exec()
			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, "writen!")
}
Exemple #4
0
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)`))
}