/
base.go
68 lines (55 loc) · 1.39 KB
/
base.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
package expand
import (
"path"
"path/filepath"
"strings"
)
// Base creates a new BaseExpander.
func Base(path string) *BaseExpander {
return &BaseExpander{
base: path,
}
}
const BasePrefix = "//"
// Expand expands paths with leading //-prefix with
// the default project path.
func Expand(base, fp string) string {
if !HasPrefix(fp) {
return fp
}
return path.Join(base, strings.TrimPrefix(
strings.TrimSpace(fp), BasePrefix),
)
}
// ExpandFilepath expands paths with leading //-prefix with
// the default project path.
func ExpandFilepath(base, fp string) string {
if !HasPrefix(fp) {
return fp
}
return filepath.Join(base, strings.TrimPrefix(
strings.TrimSpace(fp), BasePrefix),
)
}
// IsProjectPathPresent says whether the given path is expandable.
func HasPrefix(path string) bool {
return strings.HasPrefix(path, BasePrefix)
}
var _ Expander = Base(".")
// BaseExpander expands based on the given base.
type BaseExpander struct {
base string
}
// Base returns the base path BaseExpander adds.
func (b *BaseExpander) Base() string {
return b.base
}
// Expand replaces BasePrefix at the front with the stored base path.
func (b *BaseExpander) Expand(path string) string {
return Expand(b.base, path)
}
// ExpandFilepath replaces BasePrefix at the front with the
// stored base filepath.
func (b *BaseExpander) ExpandFilepath(path string) string {
return ExpandFilepath(b.base, path)
}