{ return "docs" }
{ return 0 }
{
cfg := struct {
Dir string `json:"dir"`
Dest string `json:"dest"`
}{
Dir: "articles",
Dest: filepath.Join("build", "static"),
}
if len(raw) > 0 {
_ = json.Unmarshal(raw, &cfg)
}
p.src = cfg.Dir
base := filepath.Base(cfg.Dir)
destRoot := filepath.Join(cfg.Dest, base)
return filepath.Walk(cfg.Dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
rel, err := filepath.Rel(cfg.Dir, path)
if err != nil {
return err
}
target := filepath.Join(destRoot, rel)
if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
return err
}
in, err := os.Open(path)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(target)
if err != nil {
return err
}
if _, err := io.Copy(out, in); err != nil {
out.Close()
return err
}
return out.Close()
})
}
{
return strings.HasPrefix(path, p.src)
}
{ plugins.Register(&plugin{}) }
TestBuildAndShouldRebuild ensures the docs plugin copies files from the
source directory to the destination and correctly reports rebuild needs.
{
p := &plugin{}
tmp := t.TempDir()
src := filepath.Join(tmp, "articles")
dest := filepath.Join(tmp, "out")
if err := os.MkdirAll(filepath.Join(src, "a"), 0o755); err != nil {
t.Fatalf("mkdir: %v", err)
}
srcFile := filepath.Join(src, "a", "doc.txt")
if err := os.WriteFile(srcFile, []byte("hello"), 0o644); err != nil {
t.Fatalf("write src: %v", err)
}
cfg := struct {
Dir string `json:"dir"`
Dest string `json:"dest"`
}{Dir: src, Dest: dest}
raw, _ := json.Marshal(cfg)
if err := p.Build(raw); err != nil {
t.Fatalf("Build: %v", err)
}
// File should be copied under dest/<basename>/a/doc.txt
copied := filepath.Join(dest, filepath.Base(src), "a", "doc.txt")
if data, err := os.ReadFile(copied); err != nil || string(data) != "hello" {
t.Fatalf("expected copied file, got %v %q", err, data)
}
if !p.ShouldRebuild(srcFile) {
t.Fatalf("expected ShouldRebuild true for %s", srcFile)
}
if p.ShouldRebuild(filepath.Join(tmp, "other.txt")) {
t.Fatalf("unexpected rebuild for unrelated file")
}
}
import "encoding/json"
import "io"
import "os"
import "path/filepath"
import "strings"
import "github.com/rfwlab/rfw/cmd/rfw/plugins"
import "encoding/json"
import "os"
import "path/filepath"
import "testing"