plugin struct

Fields:

  • src (string)
  • dst (string)

Methods:

Name


Returns:
  • string

Show/Hide Method Body
{ return "assets" }

Priority


Returns:
  • int

Show/Hide Method Body
{ return 0 }

Build


Parameters:
  • raw json.RawMessage

Returns:
  • error

Show/Hide Method Body
{
	cfg := struct {
		Dir  string `json:"dir"`
		Dest string `json:"dest"`
	}{
		Dir:  "assets",
		Dest: "dist",
	}
	if len(raw) > 0 {
		_ = json.Unmarshal(raw, &cfg)
	}
	p.src = cfg.Dir
	p.dst = cfg.Dest
	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(cfg.Dest, 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
		}
		if err := out.Close(); err != nil {
			return err
		}
		log.Printf("assets: copied %s", target)
		return nil
	})
}

ShouldRebuild


Parameters:
  • path string

Returns:
  • bool

Show/Hide Method Body
{
	return strings.HasPrefix(path, p.src)
}

init function

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

TestBuildAndShouldRebuild function

TestBuildAndShouldRebuild verifies that the assets plugin copies files and

reports rebuild requirements for files within the source directory.

Parameters:

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

	tmp := t.TempDir()
	src := filepath.Join(tmp, "assets")
	dest := filepath.Join(tmp, "dist")
	if err := os.MkdirAll(filepath.Join(src, "img"), 0o755); err != nil {
		t.Fatalf("mkdir: %v", err)
	}
	srcFile := filepath.Join(src, "img", "logo.png")
	if err := os.WriteFile(srcFile, []byte("data"), 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)
	}

	copied := filepath.Join(dest, "img", "logo.png")
	if data, err := os.ReadFile(copied); err != nil || string(data) != "data" {
		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")) {
		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"

strings import

Import example:

import "strings"

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"