-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathios_handler.go
100 lines (79 loc) · 2.56 KB
/
ios_handler.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
package main
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"strings"
"github.com/DHowett/go-plist"
multierror "github.com/hashicorp/go-multierror"
)
type iOSHandler struct {
}
func (h iOSHandler) isPackage(filename string) bool {
return strings.ToLower(getFilename(filename)) == "info.plist"
}
func (h iOSHandler) getPackageInfo(filePath string) (packageInfo, error) {
byteValue := readFile(filePath)
data, err := h.read(byteValue)
if err != nil {
return packageInfo{Path: filePath, HasError: true}, err
}
return packageInfo{
Name: data["CFBundleDisplayName"].(string),
Version: data["CFBundleShortVersionString"].(string),
InternalVersion: data["CFBundleVersion"].(string),
Path: filePath,
}, nil
}
func (h iOSHandler) read(data []byte) (map[string]interface{}, error) {
var result error
buffer := bytes.NewReader(data)
decoder := plist.NewDecoder(buffer)
var decodeInterface = map[string]interface{}{}
err := decoder.Decode(&decodeInterface)
if err != nil {
return decodeInterface, err
}
if _, exists := decodeInterface["CFBundleDisplayName"]; !exists {
result = multierror.Append(result, fmt.Errorf("Missing property %v", "CFBundleDisplayName"))
}
if _, exists := decodeInterface["CFBundleVersion"]; !exists {
result = multierror.Append(result, fmt.Errorf("Missing property %v", "CFBundleVersion"))
}
if _, exists := decodeInterface["CFBundleShortVersionString"]; !exists {
result = multierror.Append(result, fmt.Errorf("Missing property %v", "CFBundleShortVersionString"))
}
return decodeInterface, result
}
func (h iOSHandler) changePackageVersion(file packageInfo, newVersion string) error {
// open file with all data
byteValue := readFile(file.Path)
processedBytes, err := h.applyVersion(byteValue, newVersion)
if err != nil {
return fmt.Errorf("Invalid plist file: %v", file.Path)
}
saveFile(file.Path, processedBytes)
return nil
}
func (h iOSHandler) applyVersion(byteValue []byte, newVersion string) ([]byte, error) {
buffer := bytes.NewReader(byteValue)
decoder := plist.NewDecoder(buffer)
var data = map[string]interface{}{}
err := decoder.Decode(&data)
if err != nil {
return nil, errors.New("Invalid plist")
}
// increment version
data["CFBundleVersion"] = newVersion
data["CFBundleShortVersionString"] = newVersion
// write data inside buffer
var bufferedData bytes.Buffer
binary.Write(&bufferedData, binary.BigEndian, data)
// encode data
encoder := plist.NewEncoder(&bufferedData)
encoder.Indent("\t")
err = encoder.Encode(data)
check(err)
return bufferedData.Bytes(), nil
}