summaryrefslogtreecommitdiff
path: root/model/source.go
blob: 2844d234a1f967cea68aae212fd6ec7c6d7993ba (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
package model

import (
	"github.com/ewy1/pik/identity"
	"github.com/ewy1/pik/paths"
	"github.com/ewy1/pik/spool"
)

// SourceData is data we want to save from a Source before it has been properly instantiated
type SourceData struct {
	Aliases  []string
	Icon     string
	Path     string
	Wants    []string
	Includes []string
}

// Source is a location containing stuff we can run
// these get created when we find a makefile, .pik folder, etc.
type Source struct {
	identity.Identity
	SourceData
	Tags
	Path       string
	Targets    []Target
	Whitelists map[string][]string
}

// HydratedSource is a Source with additional hydration
// for the menu.
// these do not get created unless we show the menu
type HydratedSource struct {
	*Source
	HydratedTargets []HydratedTarget
	Aliases         []string
	Icon            string
	Git             *GitInfo
}

func (s *Source) Label() string {
	return s.Identity.Full
}

func (s *HydratedSource) Label() string {
	if len(s.Aliases) > 0 {
		return s.Aliases[0]
	}
	return s.Identity.Full
}

func (s *Source) Hydrate(hydrators []Modder) *HydratedSource {
	hs := &HydratedSource{
		Source:          s,
		HydratedTargets: make([]HydratedTarget, 0, len(s.Targets)),
	}
	for _, h := range hydrators {
		err := h.Mod(s, hs)
		if err != nil {
			spool.Warn("%v", err)
		}
	}
	for _, t := range s.Targets {
		if !t.Visible() {
			continue
		}
		ht, err := t.Hydrate(s)
		if err != nil {
			spool.Warn("%v", err)
			continue
		}
		hs.HydratedTargets = append(hs.HydratedTargets, ht)
	}
	return hs
}

func (s *Source) ShortPath() string {
	return paths.ReplaceHome(s.Path)
}