-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexclusion.go
66 lines (57 loc) · 1.3 KB
/
exclusion.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
package main
import (
"fmt"
"regexp"
"strings"
semver "github.com/openSUSE-zh/node-semver"
)
// Exclusion packages to be excluded. Useful for splitting a big bundle to several small bundles.
type Exclusion map[string]string
// Inspect debug output of Exclusion
func (e Exclusion) Inspect() string {
s := "=== Packages to be excluded ===\n"
s += "|\tPackage | Version |\n"
for k, v := range e {
s += fmt.Sprintf("|\t%s | %s |\n", k, v)
}
s += "=== END ==="
return s
}
// Contains if a package with specified version locates in the Exclusion
func (e Exclusion) Contains(k string, v semver.Semver) bool {
for m, n := range e {
if k != m {
continue
}
c := semver.NewRange(n)
if c.Satisfy(v) {
return true
}
}
return false
}
func parseExcludeString(s string) Exclusion {
e := Exclusion{}
for _, v := range strings.Split(s, ",") {
pkg, ver := parsePackageWithExplicitVersion(v)
if len(ver) == 0 {
e[pkg] = ">= 0.0.0"
} else {
re := regexp.MustCompile(`^\d`)
if re.MatchString(ver) {
e[pkg] = "= " + ver
} else {
// You can pass your own semver constriant
e[pkg] = ver
}
}
}
return e
}
func parsePackageWithExplicitVersion(s string) (string, string) {
a := strings.Split(s, ":")
if len(a) < 2 {
return a[0], ""
}
return a[0], a[1]
}