mghash

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 5, 2022 License: MIT Imports: 16 Imported by: 0

README

Mghash

This is mghash, an extension to Mage for computing hash-based dependencies.

Mage already includes functions for skipping recompilation of targets whose file modtimes are newer than those of their sources. This library does the same thing, but based on content hashes, not file modtimes.

Example

This Magefile snippet defines a rule called Generate for generating foo.pb.go from foo.proto. Before running protoc, the contents of foo.proto and foo.pb.go are hashed together with the text of the protoc command. If the resulting hash is present in the hashes.sqlite databaase then foo.pb.go is considered to be up to date and is not rebuilt. Otherwise the protoc command runs, after which the hash is recomputed and added to the database.

import (
  "github.com/bobg/mghash/sqlite"
)

func Generate() error {
  db, err := sqlite.Open("hashes.sqlite")
  if err != nil { ... }
  defer db.Close()
  
  mg.Deps(&mghash.Fn{DB: db, Rule: mghash.JRule{
    Sources: []string{"foo.proto"},
    Targets: []string{"foo.pb.go"},
    Command: []string{"protoc", "-I.", "--go_out=.", "foo.proto"},
  }})
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB interface {
	// Has tells whether the database contains the given entry.
	Has(context.Context, []byte) (bool, error)

	// Add adds an entry to the database.
	Add(context.Context, []byte) error
}

DB is a database for storing hashes. It must permit concurrent operations safely. It may expire entries to save space.

type Fn

type Fn struct {
	DB   DB
	Rule Rule
}

Fn is an mg.Fn (see https://pkg.go.dev/github.com/magefile/mage/mg#Fn) that knows how to skip rebuilding a target that is up-to-date with respect to its sources. "Up-to-date" here does not refer to file modtimes, but rather to content hashes.

func (*Fn) ID

func (f *Fn) ID() string

func (*Fn) Name

func (f *Fn) Name() string

func (*Fn) Run

func (f *Fn) Run(ctx context.Context) error

type JRule

type JRule struct {
	Sources []string `json:"sources"`
	Targets []string `json:"targets"`
	Command []string `json:"command"`
}

JRule is a Rule that lists a set of source files and a set of target files, and includes a command for producing targets from sources.

func (JRule) ContentHash

func (jr JRule) ContentHash(_ context.Context) ([]byte, error)

func (JRule) RuleHash

func (jr JRule) RuleHash() []byte

func (JRule) Run

func (jr JRule) Run(ctx context.Context) error

func (JRule) String

func (jr JRule) String() string

type Rule

type Rule interface {
	fmt.Stringer

	// RuleHash produces the hash of this rule.
	// This should be a strong, collision-resistant value
	// that is sensitive to changes in the rule itself
	// but not in any of its sources or targets.
	RuleHash() []byte

	// ContentHash produces a hash that incorporates information about the rule
	// combined with the state of all sources and targets.
	// This should be a strong, collision-resistant value.
	ContentHash(context.Context) ([]byte, error)

	// Run is a function that can generate this rule's targets.
	Run(context.Context) error
}

Rule knows how to report a hash representing itself, and another hash representing itself plus the state of all sources and targets; and how to produce its targets from its sources.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL