aboutsummaryrefslogtreecommitdiff
path: root/internal/engine/plan.go
blob: b136bdfcddcccbb474b79e05adc24e1ca24fbf16 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// SPDX-License-Identifier: GPL-3.0-or-later

package engine

import (
	"context"
	"time"

	"git.labunix.xyz/krino/internal/plan"
)

// DirPlan is one directory's plan: the match Result it was built from (for
// the counts and warnings a front end renders), the chains plan.Build
// produced from its matched files, and Elapsed (D12), which - unlike
// Result.Elapsed, stopped inside Match before plan.Build ever runs - spans
// both Match and Build, so a front end reporting how long planning took is
// not undercounting it. It lives here, not in internal/plan, because
// internal/plan must not import internal/engine (engine already imports
// plan).
type DirPlan struct {
	Dir     *Dir
	Chains  []plan.Chain
	Result  *Result
	Elapsed time.Duration
}

// Plan matches d, then turns every matched file into a chain per spec
// §7.1. Unmatched files never reach plan.Build: they have no rules, hence
// no steps, and Result.Unmatched already says they were not matched.
// Chains keep Match's Rel order, since Matched is already sorted that way
// and Build's output lines up with its input position for position. claims
// is the run-scoped claim set (A3): the caller creates one *plan.Claims per
// run and passes the same instance to every directory's Plan call, so two
// directories claiming one destination resolve the collision instead of
// both silently landing on it.
func (e *Engine) Plan(ctx context.Context, d *Dir, claims *plan.Claims) (*DirPlan, error) {
	return e.PlanAt(ctx, d, claims, e.Now())
}

// PlanAt is Plan with the run's own clock: {now:FMT} is "the start of the
// run" (spec §7.3), so every directory of one run stamps the same moment.
// Reading the clock per directory let a review pause split one run's output
// across two folders, and across midnight across two dates.
func (e *Engine) PlanAt(ctx context.Context, d *Dir, claims *plan.Claims, now time.Time) (*DirPlan, error) {
	started := time.Now()
	r, err := e.Match(ctx, d)
	if err != nil {
		return nil, err
	}

	inputs := make([]plan.Input, len(r.Matched))
	for i, fm := range r.Matched {
		inputs[i] = plan.Input{File: fm.File, Rules: planRules(fm.Rules), NoDelete: fm.NoDelete}
	}

	chains := plan.Build(d.Root, inputs, now, plan.OS{}, claims)
	return &DirPlan{Dir: d, Chains: chains, Result: r, Elapsed: time.Since(started)}, nil
}

// planRules converts a file's matching rules into what plan.Build takes.
// Explain builds the chain of one file the same way (GUI design §1.3), so
// both go through this.
func planRules(matched []RuleMatch) []plan.RuleMatch {
	rules := make([]plan.RuleMatch, len(matched))
	for i, rm := range matched {
		rules[i] = plan.RuleMatch{
			Name:     rm.Rule.Name,
			Actions:  rm.Rule.Conf.Actions,
			Settings: rm.Rule.Settings,
			Captures: rm.Captures,
			Reasons:  rm.Reasons,
		}
	}
	return rules
}