xonecas.io
8356
Wx

www-xonecas-io / files

www-xonecas-io / internal / project / overrides.go

2.8 KB
1package project
2
3import (
4	"bufio"
5	"errors"
6	"fmt"
7	"io/fs"
8	"os"
9	"slices"
10	"strings"
11)
12
13// Overrides fix what the git heuristics get wrong. One rule per line, # starts a comment:
14//
15//	merge <remote URL> <remote URL>   make two remotes one project
16//	rename <name> <new name>          rename a project
17//	hide <name> / show <name>         hide a project, or list a fork or empty one
18//	tag <name> +frontend -backend     add or drop tags or topics; +post-AI also drops pre-AI, and so on
19type Overrides struct {
20	Merge      [][2]string
21	Rename     map[string]string
22	Hide, Show map[string]bool
23	Tag        map[string][]string
24}
25
26// opposite pairs the tags a project has exactly one of.
27var opposite = map[string]string{"pre-AI": "post-AI", "post-AI": "pre-AI", "small": "big", "big": "small"}
28
29var known = map[string]bool{"pre-AI": true, "post-AI": true, "small": true, "big": true, "frontend": true, "backend": true}
30
31// topicNames is the topics' vocabulary, for the tag rules.
32var topicNames = func() map[string]bool {
33	m := map[string]bool{}
34	for _, t := range topics {
35		m[t.name] = true
36	}
37	return m
38}()
39
40// retag applies the tag rules for name to tags, for the tags in vocab.
41func (o Overrides) retag(name string, tags []string, vocab map[string]bool) []string {
42	for _, op := range o.Tag[name] {
43		tag := op[1:]
44		if !vocab[tag] {
45			continue
46		}
47		tags = slices.DeleteFunc(tags, func(t string) bool { return t == tag || t == opposite[tag] })
48		if op[0] == '+' {
49			tags = append(tags, tag)
50		}
51	}
52	return tags
53}
54
55// ReadOverrides parses path. A missing file means no overrides.
56func ReadOverrides(path string) (Overrides, error) {
57	o := Overrides{Rename: map[string]string{}, Hide: map[string]bool{}, Show: map[string]bool{}, Tag: map[string][]string{}}
58	f, err := os.Open(path)
59	if errors.Is(err, fs.ErrNotExist) {
60		return o, nil
61	} else if err != nil {
62		return o, err
63	}
64	defer f.Close()
65	sc := bufio.NewScanner(f)
66	for n := 1; sc.Scan(); n++ {
67		line, _, _ := strings.Cut(sc.Text(), "#")
68		f := strings.Fields(line)
69		switch {
70		case len(f) == 0:
71		case f[0] == "merge" && len(f) == 3:
72			o.Merge = append(o.Merge, [2]string{f[1], f[2]})
73		case f[0] == "rename" && len(f) == 3:
74			o.Rename[f[1]] = f[2]
75		case f[0] == "hide" && len(f) == 2:
76			o.Hide[f[1]] = true
77		case f[0] == "show" && len(f) == 2:
78			o.Show[f[1]] = true
79		case f[0] == "tag" && len(f) >= 3 && validTags(f[2:]):
80			o.Tag[f[1]] = append(o.Tag[f[1]], f[2:]...)
81		default:
82			return o, fmt.Errorf("%s:%d: unknown rule %q", path, n, line)
83		}
84	}
85	return o, sc.Err()
86}
87
88// validTags accepts +tag and -tag for known tags and topics, except dropping a tag that must have an opposite.
89func validTags(ops []string) bool {
90	for _, op := range ops {
91		if len(op) < 2 || !(known[op[1:]] || topicNames[op[1:]]) || (op[0] != '+' && op[0] != '-') || (op[0] == '-' && opposite[op[1:]] != "") {
92			return false
93		}
94	}
95	return true
96}