plugin struct

Fields:

  • dir (string)
  • file (string)

Methods:

Name


Returns:
  • string

Show/Hide Method Body
{ return "pages" }

Priority


Returns:
  • int

Show/Hide Method Body
{ return 0 }

PreBuild


Parameters:
  • raw json.RawMessage

Returns:
  • error

Show/Hide Method Body
{
	cfg := struct {
		Dir string `json:"dir"`
	}{Dir: "pages"}
	if len(raw) > 0 {
		_ = json.Unmarshal(raw, &cfg)
	}
	p.dir = cfg.Dir
	routes := []struct{ Path, Comp string }{}
	err := filepath.WalkDir(p.dir, func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}
		if d.IsDir() {
			return nil
		}
		if filepath.Ext(path) != ".go" || strings.HasSuffix(d.Name(), "_test.go") || strings.HasSuffix(d.Name(), "_gen.go") {
			return nil
		}
		rel, err := filepath.Rel(p.dir, path)
		if err != nil {
			return err
		}
		r, c := deriveRoute(rel)
		routes = append(routes, struct{ Path, Comp string }{r, c})
		return nil
	})
	if err != nil {
		return err
	}
	if len(routes) == 0 {
		return nil
	}
	var b strings.Builder
	b.WriteString("// Code generated by pages plugin. DO NOT EDIT.\n")
	b.WriteString("package pages\n\n")
	b.WriteString("import \"github.com/rfwlab/rfw/v1/router\"\n\n")
	b.WriteString("func init() {\n")
	for _, r := range routes {
		b.WriteString("\trouter.RegisterRoute(router.Route{Path: \"" + r.Path + "\", Component: " + r.Comp + "})\n")
	}
	b.WriteString("}\n")
	p.file = filepath.Join(p.dir, "routes_gen.go")
	return os.WriteFile(p.file, []byte(b.String()), 0o644)
}

Build


Parameters:
  • raw json.RawMessage

Returns:
  • error

Show/Hide Method Body
{ return nil }

PostBuild


Parameters:
  • raw json.RawMessage

Returns:
  • error

Show/Hide Method Body
{
	if p.file != "" {
		_ = os.Remove(p.file)
	}
	return nil
}

ShouldRebuild


Parameters:
  • path string

Returns:
  • bool

Show/Hide Method Body
{
	if !strings.HasPrefix(path, p.dir) {
		return false
	}
	return !strings.HasSuffix(path, "_gen.go")
}

init function

Show/Hide Function Body
{ plugins.Register(&plugin{}) }

deriveRoute function

Parameters:

  • rel string

Returns:

  • string
  • string
Show/Hide Function Body
{
	rel = strings.TrimSuffix(rel, ".go")
	segments := strings.Split(rel, string(filepath.Separator))
	pathSegs := []string{}
	nameSegs := []string{}
	for i, s := range segments {
		isLast := i == len(segments)-1
		if isLast && s == "index" {
			nameSegs = append(nameSegs, "Index")
			continue
		}
		if strings.HasPrefix(s, "[") && strings.HasSuffix(s, "]") {
			param := s[1 : len(s)-1]
			pathSegs = append(pathSegs, ":"+param)
			nameSegs = append(nameSegs, toPascal(param))
		} else {
			pathSegs = append(pathSegs, s)
			nameSegs = append(nameSegs, toPascal(s))
		}
	}
	path := "/" + strings.Join(pathSegs, "/")
	if len(pathSegs) == 0 {
		path = "/"
	}
	comp := strings.Join(nameSegs, "")
	return path, comp
}

toPascal function

Parameters:

  • s string

Returns:

  • string
Show/Hide Function Body
{
	s = strings.ReplaceAll(s, "-", " ")
	s = strings.ReplaceAll(s, "_", " ")
	parts := strings.Fields(s)
	for i, p := range parts {
		parts[i] = strings.ToUpper(p[:1]) + strings.ToLower(p[1:])
	}
	return strings.Join(parts, "")
}

TestDeriveRoute function

Parameters:

  • t *testing.T
Show/Hide Function Body
{
	tests := map[string]struct{ path, comp string }{
		"index.go":           {"/", "Index"},
		"about.go":           {"/about", "About"},
		"blog/index.go":      {"/blog", "BlogIndex"},
		"posts/[id].go":      {"/posts/:id", "PostsId"},
		"[user]/settings.go": {"/:user/settings", "UserSettings"},
	}
	for in, exp := range tests {
		p, c := deriveRoute(in)
		if p != exp.path || c != exp.comp {
			t.Errorf("%s => (%s,%s), want (%s,%s)", in, p, c, exp.path, exp.comp)
		}
	}
}

encoding/json import

Import example:

import "encoding/json"

io/fs import

Import example:

import "io/fs"

os import

Import example:

import "os"

path/filepath import

Import example:

import "path/filepath"

strings import

Import example:

import "strings"

github.com/rfwlab/rfw/cmd/rfw/plugins import

Import example:

import "github.com/rfwlab/rfw/cmd/rfw/plugins"

testing import

Import example:

import "testing"