rule struct

Fields:

  • From (string) - json:"from"
  • To (string) - json:"to"

plugin struct

Fields:

  • rules ([]rule)

Methods:

Name


Returns:
  • string

Show/Hide Method Body
{ return "copy" }

Priority


Returns:
  • int

Show/Hide Method Body
{ return 0 }

Build


Parameters:
  • raw json.RawMessage

Returns:
  • error

Show/Hide Method Body
{
	cfg := struct {
		Files []rule `json:"files"`
	}{}
	if len(raw) > 0 {
		if err := json.Unmarshal(raw, &cfg); err != nil {
			return err
		}
	}
	p.rules = cfg.Files
	for _, r := range p.rules {
		matches, err := doublestar.Glob(os.DirFS("."), r.From)
		if err != nil {
			return err
		}
		base, _ := doublestar.SplitPattern(r.From)
		base = filepath.FromSlash(base)
		for _, m := range matches {
			path := filepath.FromSlash(m)
			info, err := os.Stat(path)
			if err != nil {
				return err
			}
			if info.IsDir() {
				continue
			}
			rel, err := filepath.Rel(base, path)
			if err != nil {
				return err
			}
			dst := filepath.Join(r.To, rel)
			if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil {
				return err
			}
			if err := copyFile(path, dst); err != nil {
				return err
			}
			log.Printf("copy: copied %s", dst)
		}
	}
	return nil
}

ShouldRebuild


Parameters:
  • path string

Returns:
  • bool

Show/Hide Method Body
{
	for _, r := range p.rules {
		if ok, _ := doublestar.PathMatch(r.From, path); ok {
			return true
		}
	}
	return false
}

init function

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

copyFile function

Parameters:

  • src string
  • dst string

Returns:

  • error
Show/Hide Function Body
{
	in, err := os.Open(src)
	if err != nil {
		return err
	}
	defer in.Close()

	out, err := os.Create(dst)
	if err != nil {
		return err
	}
	if _, err := io.Copy(out, in); err != nil {
		out.Close()
		return err
	}
	return out.Close()
}

TestBuildAndShouldRebuild function

TestBuildAndShouldRebuild ensures files matching patterns are copied

and rebuilds are triggered for matched paths.

Parameters:

  • t *testing.T
Show/Hide Function Body
{
	p := &plugin{}
	tmp := t.TempDir()

	cwd, _ := os.Getwd()
	defer os.Chdir(cwd)
	if err := os.Chdir(tmp); err != nil {
		t.Fatalf("chdir: %v", err)
	}

	srcRoot := filepath.Join("examples", "components")
	if err := os.MkdirAll(filepath.Join(srcRoot, "templates"), 0o755); err != nil {
		t.Fatalf("mkdir: %v", err)
	}
	fileA := filepath.Join(srcRoot, "comp.txt")
	if err := os.WriteFile(fileA, []byte("a"), 0o644); err != nil {
		t.Fatalf("write: %v", err)
	}
	fileB := filepath.Join(srcRoot, "templates", "tpl.txt")
	if err := os.WriteFile(fileB, []byte("b"), 0o644); err != nil {
		t.Fatalf("write: %v", err)
	}

	destRoot := filepath.Join("build", "static", "examples", "components")
	cfg := struct {
		Files []struct {
			From string `json:"from"`
			To   string `json:"to"`
		} `json:"files"`
	}{
		Files: []struct {
			From string `json:"from"`
			To   string `json:"to"`
		}{{
			From: filepath.Join(srcRoot, "**", "*"),
			To:   destRoot,
		}},
	}
	raw, _ := json.Marshal(cfg)
	if err := p.Build(raw); err != nil {
		t.Fatalf("Build: %v", err)
	}

	absDest := filepath.Join(tmp, destRoot)
	if data, err := os.ReadFile(filepath.Join(absDest, "comp.txt")); err != nil || string(data) != "a" {
		t.Fatalf("comp.txt not copied: %v %s", err, data)
	}
	if data, err := os.ReadFile(filepath.Join(absDest, "templates", "tpl.txt")); err != nil || string(data) != "b" {
		t.Fatalf("tpl.txt not copied: %v %s", err, data)
	}

	if !p.ShouldRebuild(fileA) {
		t.Fatalf("expected ShouldRebuild true for %s", fileA)
	}
	if p.ShouldRebuild(filepath.Join("other.txt")) {
		t.Fatalf("unexpected rebuild for unrelated file")
	}
}

encoding/json import

Import example:

import "encoding/json"

io import

Import example:

import "io"

log import

Import example:

import "log"

os import

Import example:

import "os"

path/filepath import

Import example:

import "path/filepath"

github.com/bmatcuk/doublestar/v4 import

Import example:

import "github.com/bmatcuk/doublestar/v4"

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

Import example:

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

encoding/json import

Import example:

import "encoding/json"

os import

Import example:

import "os"

path/filepath import

Import example:

import "path/filepath"

testing import

Import example:

import "testing"