-
Notifications
You must be signed in to change notification settings - Fork 0
/
nuspec.go
127 lines (115 loc) · 3.25 KB
/
nuspec.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
package nuspec
import (
"bytes"
"encoding/xml"
"io"
"io/ioutil"
"os"
)
// File is used in the NuSpec struct
type File struct {
Source string `xml:"src,attr"`
Target string `xml:"target,attr"`
}
// Dependency is used in the File struct
type Dependency struct {
ID string `xml:"id,attr"`
Version string `xml:"version,attr"`
}
// NuSpec Represents a .nuspec XML file found in the root of the .nupck files
type NuSpec struct {
XMLName xml.Name `xml:"package"`
Xmlns string `xml:"xmlns,attr,omitempty"`
Meta struct { // MetaData
ID string `xml:"id"`
Version string `xml:"version"`
Title string `xml:"title,omitempty"`
Authors string `xml:"authors"`
Owners string `xml:"owners,omitempty"`
LicenseURL string `xml:"licenseUrl,omitempty"`
License struct {
Text string `xml:",chardata"`
Type string `xml:"type,attr"`
} `xml:"license,omitempty"`
ProjectURL string `xml:"projectUrl,omitempty"`
IconURL string `xml:"iconUrl,omitempty"`
ReqLicenseAccept bool `xml:"requireLicenseAcceptance"`
Description string `xml:"description"`
ReleaseNotes string `xml:"releaseNotes,omitempty"`
Copyright string `xml:"copyright,omitempty"`
Summary string `xml:"summary,omitempty"`
Language string `xml:"language,omitempty"`
Tags string `xml:"tags,omitempty"`
Dependencies struct {
Dependency []Dependency `xml:"dependency"`
} `xml:"dependencies,omitempty"`
} `xml:"metadata"`
Files struct {
File []File `xml:"file"`
} `xml:"files,omitempty"`
}
// New returns a populated skeleton for a Nuget Packages request (/Packages)
func New() *NuSpec {
nsf := NuSpec{}
nsf.Xmlns = `http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd`
return &nsf
}
// FromFile reads a nuspec file from the file system
func FromFile(fn string) (*NuSpec, error) {
// Open File
xmlFile, err := os.Open(fn)
if err != nil {
return nil, err
}
// Read all file
b, err := ioutil.ReadAll(xmlFile)
if err != nil {
return nil, err
}
// Unmarshal into struct
// Create empty struct
var nsf NuSpec
err = xml.Unmarshal(b, &nsf)
if err != nil {
return nil, err
}
return &nsf, nil
}
// FromBytes reads a nuspec file from a byte array
func FromBytes(b []byte) (*NuSpec, error) {
nsf := NuSpec{}
err := xml.Unmarshal(b, &nsf)
if err != nil {
return nil, err
}
return &nsf, nil
}
// FromReader reads a nuspec file from a byte array
func FromReader(r io.ReadCloser) (*NuSpec, error) {
// Read contents of reader
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
return FromBytes(b)
}
// ToBytes exports the nuspec to bytes in XML format
func (nsf *NuSpec) ToBytes() ([]byte, error) {
var b bytes.Buffer
// Unmarshal into XML
output, err := xml.MarshalIndent(nsf, "", " ")
if err != nil {
return nil, err
}
// Self-Close any empty XML elements (to match original Nuget output)
// This assumes Indented Marshalling above, non Indented will break XML
for bytes.Contains(output, []byte(`></`)) {
i := bytes.Index(output, []byte(`></`))
j := bytes.Index(output[i+1:], []byte(`>`))
output = append(output[:i], append([]byte(` /`), output[i+j+1:]...)...)
}
// Write the XML Header
b.WriteString(xml.Header)
b.Write(output)
return b.Bytes(), nil
}