/
subrepo.go
54 lines (48 loc) · 1.8 KB
/
subrepo.go
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
package core
import (
"cli"
"fmt"
"path"
"strings"
)
// A Subrepo stores information about a registered subrepository, typically one
// that we have downloaded somehow to bring in third-party deps.
type Subrepo struct {
// The name of the subrepo.
Name string
// The root directory to load it from.
Root string
// If this repo is output by a target, this is the target that creates it. Can be nil.
Target *BuildTarget
// If this repo has a different configuration (e.g. it's for a different architecture), it's stored here
State *BuildState
// True if this subrepo was created for a different architecture
IsCrossCompile bool
}
// SubrepoForArch creates a new subrepo for the given architecture.
func SubrepoForArch(state *BuildState, arch cli.Arch) *Subrepo {
return &Subrepo{
Name: arch.String(),
State: state.ForArch(arch),
IsCrossCompile: true,
}
}
// MakeRelative makes a build label that is within this subrepo relative to it (i.e. strips the leading name part).
// The caller should already know that it is within this repo, otherwise this will panic.
func (s *Subrepo) MakeRelative(label BuildLabel) BuildLabel {
return BuildLabel{s.MakeRelativeName(label.PackageName), label.Name}
}
// MakeRelativeName is as MakeRelative but operates only on the package name.
func (s *Subrepo) MakeRelativeName(name string) string {
// Check for nil, makes it easier to call this without having so many conditionals.
if s == nil {
return name
} else if !strings.HasPrefix(name, s.Name) {
panic(fmt.Errorf("cannot make label %s relative, it is not within this subrepo (%s)", name, s.Name))
}
return strings.TrimPrefix(name[len(s.Name):], "/")
}
// Dir returns the directory for a package of this name.
func (s *Subrepo) Dir(dir string) string {
return path.Join(s.Root, s.MakeRelativeName(dir))
}