-
Notifications
You must be signed in to change notification settings - Fork 7
/
infoplist.go
103 lines (81 loc) · 2.11 KB
/
infoplist.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
package main
import (
"github.com/blang/semver"
"howett.net/plist"
"strconv"
)
const (
versionKey = "CFBundleShortVersionString"
buildNumberKey = "CFBundleVersion"
)
type InfoPlist struct {
Path string
object map[string]interface{}
raw []byte
}
func NewInfoPlist(bytes []byte) (*InfoPlist, error) {
var object map[string]interface{}
if _, err := plist.Unmarshal(bytes, &object); err != nil {
return nil, err
}
infoPlist := InfoPlist{
object: object,
raw: bytes,
}
return &infoPlist, nil
}
func (infoPlist *InfoPlist) VersionString() string {
return infoPlist.object[versionKey].(string)
}
func (infoPlist *InfoPlist) BuildNumberString() string {
return infoPlist.object[buildNumberKey].(string)
}
func (infoPlist *InfoPlist) NextMajor() (string, error) {
versionString := infoPlist.VersionString()
version, err := semver.Make(versionString)
if err != nil {
return versionString, err
}
version.Major += 1
version.Minor = 0
version.Patch = 0
return version.String(), nil
}
func (infoPlist *InfoPlist) NextMinor() (string, error) {
versionString := infoPlist.VersionString()
version, err := semver.Make(versionString)
if err != nil {
return versionString, err
}
version.Minor += 1
version.Patch = 0
return version.String(), nil
}
func (infoPlist *InfoPlist) NextPatch() (string, error) {
versionString := infoPlist.VersionString()
version, err := semver.Make(versionString)
if err != nil {
return versionString, err
}
version.Patch += 1
return version.String(), nil
}
func (infoPlist *InfoPlist) NextBuildNumber() (string, error) {
buildNumberString := infoPlist.BuildNumberString()
buildNumber, err := strconv.Atoi(buildNumberString)
if err != nil {
return buildNumberString, err
}
return strconv.Itoa(buildNumber + 1), nil
}
func (infoPlist *InfoPlist) SetVersion(version string, build string) {
infoPlist.object[versionKey] = version
infoPlist.object[buildNumberKey] = build
}
func (infoPlist *InfoPlist) serialized() ([]byte, error) {
bytes, err := plist.MarshalIndent(&infoPlist.object, plist.XMLFormat, "\t")
if err != nil {
return nil, err
}
return bytes, nil
}