-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpack.go
197 lines (176 loc) · 4.62 KB
/
pack.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package nuget
import (
"archive/zip"
"bytes"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"strings"
nuspec "github.com/soloworks/go-nuspec"
)
// PackNupkg produces a .nupkg file in byte format
func PackNupkg(ns *nuspec.NuSpec, basePath string, outputPath string) ([]byte, error) {
// Assume filename from ID
nsfilename := ns.Meta.ID + ".nuspec"
// Fix file references to match current underlying os
for x, f := range ns.Files.File {
ns.Files.File[x].Source = strings.ReplaceAll(f.Source, `\`, string(os.PathSeparator))
ns.Files.File[x].Target = strings.ReplaceAll(f.Target, `\`, string(os.PathSeparator))
}
// Create a buffer to write our archive to.
buf := new(bytes.Buffer)
// Create a new zip archive
w := zip.NewWriter(buf)
defer w.Close()
// Create a new Contenttypes Structure
ct := NewContentTypes()
// Add .nuspec to Archive
b, err := ns.ToBytes()
if err != nil {
return nil, err
}
if err := archiveFile(filepath.Base(nsfilename), w, b); err != nil {
return nil, err
}
ct.Add(filepath.Ext(nsfilename))
// Process files
// If there are no file globs specified then
if len(ns.Files.File) == 0 {
// walk the basePath and zip up all found files. Everything.]
err = filepath.Walk(basePath, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() && filepath.Base(path) != filepath.Base(nsfilename) {
// Open the file
x, err := os.Open(path)
if err != nil {
return err
}
// Gather all contents
y, err := ioutil.ReadAll(x)
if err != nil {
return err
}
// Set relative path for file in archive
p, err := filepath.Rel(basePath, path)
if err != nil {
return err
}
// Store the file
if err := archiveFile(p, w, y); err != nil {
return err
}
// Add extension to the Rels file
ct.Add(filepath.Ext(p))
}
return nil
})
if err != nil {
return nil, err
}
} else {
// For each of the specified globs, get files an put in target
for _, f := range ns.Files.File {
// Apply glob
// ToDo: Fix Source from Windows to Current via os.seperator and strings replace
matches, err := filepath.Glob(filepath.Join(basePath, f.Source))
if err != nil {
return nil, err
}
for _, m := range matches {
info, err := os.Stat(m)
if !info.IsDir() && filepath.Base(m) != filepath.Base(nsfilename) {
// Open the file
x, err := os.Open(m)
if err != nil {
return nil, err
}
// Gather all contents
y, err := ioutil.ReadAll(x)
if err != nil {
return nil, err
}
// Set relative path for file in archive
p, err := filepath.Rel(basePath, m)
if err != nil {
return nil, err
}
// Overide path if Target is set
if f.Target != "" {
p = filepath.Join(f.Target, filepath.Base(m))
}
// Store the file
if err := archiveFile(p, w, y); err != nil {
return nil, err
}
// Add extension to the Rels file
ct.Add(filepath.Ext(p))
}
if err != nil {
return nil, err
}
}
}
}
// Create and add .psmdcp file to Archive
pf := NewPsmdcpFile()
pf.Creator = ns.Meta.Authors
pf.Description = ns.Meta.Description
pf.Identifier = ns.Meta.ID
pf.Version = ns.Meta.Version
pf.Keywords = ns.Meta.Tags
pf.LastModifiedBy = "go-nuget"
b, err = pf.ToBytes()
if err != nil {
return nil, err
}
pfn := "package/services/metadata/core-properties/" + randomString(32) + ".psmdcp"
if err := archiveFile(pfn, w, b); err != nil {
return nil, err
}
ct.Add(filepath.Ext(pfn))
// Create and add .rels to Archive
rf := NewRelFile()
rf.Add("http://schemas.microsoft.com/packaging/2010/07/manifest", "/"+filepath.Base(nsfilename))
rf.Add("http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties", pfn)
b, err = rf.ToBytes()
if err != nil {
return nil, err
}
if err := archiveFile(filepath.Join("_rels", ".rels"), w, b); err != nil {
return nil, err
}
ct.Add(filepath.Ext(".rels"))
// Add [Content_Types].xml to Archive
b, err = ct.ToBytes()
if err != nil {
return nil, err
}
if err := archiveFile(`[Content_Types].xml`, w, b); err != nil {
return nil, err
}
// Close the zipwriter
w.Close()
// Return
return buf.Bytes(), nil
}
func archiveFile(fn string, w *zip.Writer, b []byte) error {
// Create the file in the zip
f, err := w.Create(fn)
if err != nil {
return err
}
// Write .nuspec bytes to file
_, err = f.Write([]byte(b))
if err != nil {
return err
}
return nil
}
const letterBytes = "abcdef0123456789"
func randomString(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}