mghash

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2022 License: MIT Imports: 18 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. (See github.com/magefile/mage/target.) 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"
  "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

func Proto added in v0.2.0

func Proto(sources, targets []string, options ...ProtoOpt) mghash.Rule

Proto produces a Rule for compiling protocol buffers to Go.

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: the existing target was computed from sources that are byte-for-byte the same now as they were when the target was built.

func (*Fn) ID

func (f *Fn) ID() string

ID implements mg.Fn.

func (*Fn) Name

func (f *Fn) Name() string

Name implements mg.Fn.

func (*Fn) Run

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

Run implements mg.Fn.

type JRule

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

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 JDir added in v0.2.0

func JDir(dir string) ([]JRule, error)

JDir parses a file named .mghash.json in the given directory, if there is one, returning the JRules it contains. The default directory for any JRules not specifying one is dir.

func JTree added in v0.2.0

func JTree(dir string) ([]JRule, error)

JTree walks the tree rooted at dir, looking for .mghash.json files and parsing the JRules out of them using JDir.

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 ProtoOpt added in v0.2.0

type ProtoOpt func(*protoCmd)

func ProtoDirs added in v0.2.0

func ProtoDirs(dirs ...string) ProtoOpt

func Protoc added in v0.2.0

func Protoc(name string) ProtoOpt

func ProtocArgs added in v0.2.0

func ProtocArgs(args ...string) ProtoOpt

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