summaryrefslogtreecommitdiff
path: root/model/tags.go
blob: 87820092df248cd8c8299435cf50824b75475408 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package model

import (
	"slices"
	"strings"
)

// Tag is some text which is contained in a filename which triggers pik functionality
type Tag struct {
	Name        string
	Description string
}
type TagAction func(src *Source)

// New creates a new tag and registers it in the subsystems
func New(input string, description string) *Tag {
	t := &Tag{
		Name:        input,
		Description: description,
	}
	TagMap[input] = t
	TagList = append(TagList, t)
	return t
}

var (
	// Here will force the target to run in the current directory instead of the source directory
	Here = New("here", "run target in the currrent working directory instead of the source root. Same as invoking with `pik --here`.")
	// Pre turns the target into a trigger, causing it to be triggered before another target gets ran
	Pre = New("pre", "in non-single mode, run this target before the actually invoked target (this one must succeed)")
	// Post turns the target into a trigger, causing it to be triggered after another target gets ran and exits succesfully
	Post = New("post", "in non-single mode, run this target after the actually invoked target (if it succeeded)")
	// Final turns the target into a trigger, causing it to be triggered after another target gets ran
	Final = New("final", "in non-single mode, run this target after the actually invoked target regardless of failure")
	// Hidden means the target is not visible in the menu
	Hidden = New("hidden", "do not show this target in the TUI. Still invokable through the command line.")
	// Single means this target will not use any triggers
	Single = New("single", "do not run any triggers (like pre, post, and final). Same as invoking with `pik --single`.")
	// Override means this should be selected instead of a non-override target, if possible
	Override = New("override", "the .override tag will make this target get selected over other targets with the same name. Use this when your machine needs a custom workflow. It is discouraged to check these into version control, as everyone will receive your override.")
)

func (t *Tag) String() string {
	return t.Name
}

var TagList []*Tag

var TagMap = map[string]*Tag{}

type Tags []*Tag

func (t Tags) AnyOf(expected ...*Tag) bool {
	if len(expected) > 1 && len(t) == 0 {
		return false
	}
	if len(expected) == 0 {
		return true
	}
	for _, e := range expected {
		if slices.Contains(t, e) {
			return true
		}
	}
	return false
}

func (t Tags) Has(expected *Tag) bool {
	return slices.Contains(t, expected)
}

func TagsFromFilename(filename string) Tags {
	var tags Tags
	// if hidden
	if strings.HasPrefix(filename, ".") {
		filename = strings.TrimPrefix(filename, ".")
		tags = append(tags, Hidden)
	}

	parts := strings.Split(filename, ".")
	if len(parts) == 1 {
		return nil
	}

	for _, p := range parts {
		p = strings.ToLower(p)
		if TagMap[p] != nil {
			tags = append(tags, TagMap[p])
		}
	}

	return tags
}

func (t Tags) Visible() bool {
	return !t.AnyOf(Hidden, Pre, Post, Final)
}