-
Notifications
You must be signed in to change notification settings - Fork 4
/
path.go
150 lines (122 loc) · 3.71 KB
/
path.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package core
import (
"fmt"
"path"
"reflect"
"strings"
)
// Path represents an on-disk path that is either an input to or an output from a BuildStep (or both).
type Path interface {
Absolute() string
Relative() string
String() string
WithExt(ext string) OutPath
WithPrefix(prefix string) OutPath
WithSuffix(suffix string) OutPath
}
type PkgPath interface {
SrcDir() string
}
// inPath is a path relative to the workspace source directory.
type inPath struct {
rel string
abs string
}
// Absolute returns the absolute path.
func (p inPath) Absolute() string {
if p.abs != "" {
return p.abs
}
return path.Join(input.SourceDir, p.rel)
}
// Relative returns the path relative to the workspace source directory.
func (p inPath) Relative() string {
return p.rel
}
// WithExt creates an OutPath with the same relative path and the given extension.
func (p inPath) WithExt(ext string) OutPath {
return outPath{p.rel}.WithExt(ext)
}
// WithPrefix creates an OutPath with the same relative path and the given prefix.
func (p inPath) WithPrefix(prefix string) OutPath {
return outPath{p.rel}.WithPrefix(prefix)
}
// WithSuffix creates an OutPath with the same relative path and the given suffix.
func (p inPath) WithSuffix(suffix string) OutPath {
return outPath{p.rel}.WithSuffix(suffix)
}
// String representation of an inPath is its quoted absolute path.
func (p inPath) String() string {
return p.Absolute()
}
// OutPath is a path relative to the workspace build directory.
type OutPath interface {
Path
forceOutPath()
}
type outPath struct {
rel string
}
// Absolute returns the absolute path.
func (p outPath) Absolute() string {
return path.Join(input.OutputDir, p.rel)
}
// Relative returns the path relative to the workspace build directory.
func (p outPath) Relative() string {
return p.rel
}
// WithExt creates an OutPath with the same relative path and the given extension.
func (p outPath) WithExt(ext string) OutPath {
oldExt := path.Ext(p.rel)
newRel := fmt.Sprintf("%s.%s", strings.TrimSuffix(p.rel, oldExt), ext)
return outPath{newRel}
}
// WithPrefix creates an OutPath with the same relative path and the given prefix.
func (p outPath) WithPrefix(prefix string) OutPath {
return outPath{path.Join(path.Dir(p.rel), prefix+path.Base(p.rel))}
}
// WithSuffix creates an OutPath with the same relative path and the given suffix.
func (p outPath) WithSuffix(suffix string) OutPath {
return outPath{p.rel + suffix}
}
// String representation of an OutPath is its quoted absolute path.
func (p outPath) String() string {
return p.Absolute()
}
// forceOutPath makes sure that inPath or Path cannot be used as OutPath.
func (p outPath) forceOutPath() {}
// GlobalPath is a global path.
type GlobalPath interface {
Absolute() string
}
type globalPath struct {
abs string
}
// Absolute returns absolute path.
func (p globalPath) Absolute() string {
return p.abs
}
// String representation of a globalPath is its quoted absolute path.
func (p globalPath) String() string {
return p.Absolute()
}
// NewInPath creates an inPath for a path relativ to the source directory.
func NewInPath(pkg PkgPath, p string) Path {
return inPath{path.Join(reflect.TypeOf(pkg).PkgPath(), p), path.Join(pkg.SrcDir(), p)}
}
// NewOutPath creates an OutPath for a path relativ to the build directory.
func NewOutPath(pkg interface{}, p string) OutPath {
return outPath{path.Join(reflect.TypeOf(pkg).PkgPath(), p)}
}
// NewGlobalPath creates a globalPath.
func NewGlobalPath(p string) GlobalPath {
return globalPath{p}
}
// BuildPath returns a path relative to the build directory.
func BuildPath(p string) OutPath {
return outPath{p}
}
// SourcePath returns a path relative to the source directory.
func SourcePath(p string) Path {
return inPath{p, ""}
}