-
Notifications
You must be signed in to change notification settings - Fork 2
/
composer.go
96 lines (71 loc) · 1.93 KB
/
composer.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
package composer
import (
"encoding/json"
"io/ioutil"
"os"
"regexp"
"strings"
)
//noinspection GoNameStartsWithPackageName
type ComposerFile map[string]interface{}
type Source struct {
Type string `json:"type"`
Url string `json:"url"`
Reference string `json:"reference"`
}
func DeriveVersion(tagOrBranchName string, isBranch bool) (version string, normalizedVersion string, error error) {
version = tagOrBranchName
if isBranch == false {
// strip the release- prefix from tags if present
version = strings.Replace(version, "release-", "", -1)
} else {
rawBranch := strings.Replace(version, "origin/", "", 1)
parsedBranch, err := NormalizeBranch(rawBranch)
if err != nil || "9999999-dev" == parsedBranch {
version = "dev-" + rawBranch
} else {
prefix := ""
if rawBranch[0:1] == "v" {
prefix = "v"
}
exp := regexp.MustCompile(`(\.9{7})+`)
version = prefix + exp.ReplaceAllString(parsedBranch, ".x")
}
}
parsedVersion, err := NormaliseVersion(version, "")
if err != nil {
return "", "", err
}
normalizedVersion = parsedVersion
return
}
func LoadFile(path string) (file ComposerFile, error error) {
rawComposerFile, err := ioutil.ReadFile(path + "/composer.json")
if err != nil {
return nil, err
}
error = json.Unmarshal(rawComposerFile, &file)
return
}
func MutateComposerFile(path, version, normalizedVersion string, source *Source) error {
data, err := LoadFile(path)
if err != nil {
return err
}
data["version"] = version
data["version_normalized"] = normalizedVersion
if source != nil {
data["source"] = source
}
// Truncate on open, and in write mode only
file, err := os.OpenFile(path+"/composer.json", os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer file.Close()
// Required to prevent goland from escaping "<", ">", and "&".
enc := json.NewEncoder(file)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
return enc.Encode(&data)
}