-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
145 lines (125 loc) · 3.14 KB
/
main.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"fmt"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"github.com/soloworks/go-nuget-utils"
"github.com/soloworks/go-nuspec"
)
func main() {
// make new Action
a := newAction()
// Get and verify the Version
ver := a.GetInput("version")
if ver == "commit-tag" {
if strings.HasPrefix(os.Getenv("GITHUB_REF"), "refs/tags/") {
// Extract tag from end of Envar
ver = strings.TrimPrefix(os.Getenv("GITHUB_REF"), "refs/tags/")
} else {
ver = ""
}
}
// If no Version is found, exit with error
if ver == "" {
log.Fatalln("No version supplied or found ($GITHUB_REF="+os.Getenv("GITHUB_REF"), ")")
}
// Check ver against RegEx to be in format x.x.x.x only
if match, _ := regexp.MatchString(`^(([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+))$`, ver); !match {
log.Fatalln("SemVer format incorrect: ", ver)
}
// Get nuspec file - if not arg then first in directory
nsfilename := ""
if a.GetInput("nuspec-file") != "" {
nsfilename = a.GetInput("nuspec-file")
} else {
m, err := filepath.Glob("*.nuspec")
if err != nil {
log.Fatal(err)
}
for _, x := range m {
nsfilename = x
break
}
}
// If no NuSpec is found, exit with error
if nsfilename == "" {
log.Fatalln("No NuSpec supplied or found")
}
// Load the NuSpec file
nsf, err := nuspec.FromFile(nsfilename)
if err != nil {
log.Fatalln(err)
}
// Adjust Version to match provided
nsf.Meta.Version = ver
// Edit all .qplug files to update version from 0.0.0.0-master
m, err := filepath.Glob("content/*.qplug")
if err != nil {
log.Fatalln(err)
}
for _, x := range m {
read, err := ioutil.ReadFile(x)
if err != nil {
log.Fatalln(err)
}
newContents := strings.Replace(string(read), "0.0.0.0-master", ver, -1)
err = ioutil.WriteFile(x, []byte(newContents), 0)
if err != nil {
log.Fatalln(err)
}
}
// Gather Git Release notes
// Convert MarkDown to Description
if x := a.GetInput("md-to-desc"); x != "" {
// Load the file
read, err := ioutil.ReadFile(x)
if err != nil {
log.Fatalln(err)
}
// Do conversion to NuSpec formats - Images
reImg := regexp.MustCompile(`(?:!\[(.*?)\]\((.*?)\))`)
reBraces := regexp.MustCompile(`\((.*)\)`)
nsf.Meta.Description = reImg.ReplaceAllStringFunc(string(read), func(s string) string {
return reBraces.ReplaceAllStringFunc(string(s), func(s string) string {
p := path.Clean(s[1 : len(s)-1])
file, err := os.Open(p)
defer file.Close()
if err != nil {
log.Println(err)
return s
}
image, _, err := image.DecodeConfig(file)
if err == nil {
p = fmt.Sprintf("%s =%dx%d", p, image.Width, image.Height)
}
return "(http://" + p + ")"
})
})
}
//Pack it up
npkg, err := nuget.PackNupkg(nsf, ".", ".")
if err != nil {
log.Fatalln(err)
}
// Push it up
status, _, err := nuget.PushNupkg(npkg, a.GetInput("Api-Key"), a.GetInput("nuget-host"))
if err != nil {
log.Fatalln(err)
}
if status == 201 {
os.Exit(0)
} else {
log.Fatalln("Failed with HTTP Status:", status)
}
// Set Output variables
//println("::set-output name=duration::", strconv.Itoa(dur))
}