{ return "copy" }
{ return 0 }
{
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
}
{
for _, r := range p.rules {
if ok, _ := doublestar.PathMatch(r.From, path); ok {
return true
}
}
return false
}
{ plugins.Register(&plugin{}) }
{
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 ensures files matching patterns are copied
and rebuilds are triggered for matched paths.
{
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")
}
}
import "encoding/json"
import "io"
import "log"
import "os"
import "path/filepath"
import "github.com/bmatcuk/doublestar/v4"
import "github.com/rfwlab/rfw/cmd/rfw/plugins"
import "encoding/json"
import "os"
import "path/filepath"
import "testing"