11package main
22
33import (
4- "archive/zip"
5- "bytes"
64 "errors"
75 "fmt"
86 "io/ioutil"
97 "log"
108 "os"
119 "path/filepath"
1210
11+ nuget "github.com/soloworks/go-nuget"
1312 nuspec "github.com/soloworks/go-nuspec"
1413 "github.com/urfave/cli"
1514)
1615
17- func archiveFile (fn string , w * zip.Writer , b []byte ) {
18-
19- // Check and convert filepath to `/` if required
20- fn = filepath .ToSlash (fn )
21-
22- // Create the file in the zip
23- f , err := w .Create (fn )
24- checkError (err )
25-
26- // Write .nuspec bytes to file
27- _ , err = f .Write ([]byte (b ))
28- checkError (err )
29- }
30-
3116func cliPackNupkg (c * cli.Context ) error {
3217
3318 // Get filename from command line
@@ -61,7 +46,7 @@ func cliPackNupkg(c *cli.Context) error {
6146 outputPath = op
6247 }
6348
64- b , err := PackNupkg (ns , basePath , outputPath )
49+ b , err := nuget . PackNupkg (ns , basePath , outputPath )
6550 checkError (err )
6651
6752 // Override Version if option is set
@@ -87,116 +72,3 @@ func cliPackNupkg(c *cli.Context) error {
8772
8873 return nil
8974}
90-
91- // PackNupkg produces a .nupkg file in byte format
92- func PackNupkg (ns * nuspec.NuSpec , basePath string , outputPath string ) ([]byte , error ) {
93-
94- // Assume filename from ID
95- nsfilename := ns .Meta .ID + ".nuspec"
96-
97- // Create a buffer to write our archive to.
98- buf := new (bytes.Buffer )
99-
100- // Create a new zip archive
101- w := zip .NewWriter (buf )
102- defer w .Close ()
103-
104- // Create a new Contenttypes Structure
105- ct := NewContentTypes ()
106-
107- // Add .nuspec to Archive
108- b , err := ns .ToBytes ()
109- checkError (err )
110- archiveFile (filepath .Base (nsfilename ), w , b )
111- ct .Add (filepath .Ext (nsfilename ))
112-
113- // Process files
114- // If there are no file globs specified then
115- if len (ns .Files .File ) == 0 {
116- // walk the basePath and zip up all found files. Everything.]
117- err = filepath .Walk (basePath , func (path string , info os.FileInfo , err error ) error {
118- if ! info .IsDir () && filepath .Base (path ) != filepath .Base (nsfilename ) {
119- // Open the file
120- x , err := os .Open (path )
121- checkError (err )
122- // Gather all contents
123- y , err := ioutil .ReadAll (x )
124- checkError (err )
125- // Set relative path for file in archive
126- p , err := filepath .Rel (basePath , path )
127- checkError (err )
128- // Store the file
129- archiveFile (p , w , y )
130- // Add extension to the Rels file
131- ct .Add (filepath .Ext (p ))
132- }
133- return nil
134- })
135- checkError (err )
136- } else {
137- // For each of the specified globs, get files an put in target
138- for _ , f := range ns .Files .File {
139- // Apply glob, cater for
140- matches , err := filepath .Glob (filepath .ToSlash (filepath .Join (basePath , f .Source )))
141- checkError (err )
142- for _ , m := range matches {
143- info , err := os .Stat (m )
144- if ! info .IsDir () && filepath .Base (m ) != filepath .Base (nsfilename ) {
145- // Open the file
146- x , err := os .Open (m )
147- checkError (err )
148- // Gather all contents
149- y , err := ioutil .ReadAll (x )
150- checkError (err )
151- // Set relative path for file in archive
152- p , err := filepath .Rel (basePath , m )
153- checkError (err )
154- // Overide path if Target is set
155- if f .Target != "" {
156- p = filepath .Join (f .Target , filepath .Base (m ))
157- }
158- // Store the file
159- archiveFile (p , w , y )
160- // Add extension to the Rels file
161- ct .Add (filepath .Ext (p ))
162- }
163- checkError (err )
164- }
165- }
166- }
167-
168- // Create and add .psmdcp file to Archive
169- pf := NewPsmdcpFile ()
170- pf .Creator = ns .Meta .Authors
171- pf .Description = ns .Meta .Description
172- pf .Identifier = ns .Meta .ID
173- pf .Version = ns .Meta .Version
174- pf .Keywords = ns .Meta .Tags
175- pf .LastModifiedBy = "go-nuget"
176- b , err = pf .ToBytes ()
177- checkError (err )
178- pfn := "package/services/metadata/core-properties/" + randomString (32 ) + ".psmdcp"
179- archiveFile (pfn , w , b )
180- ct .Add (filepath .Ext (pfn ))
181-
182- // Create and add .rels to Archive
183- rf := NewRelFile ()
184- rf .Add ("http://schemas.microsoft.com/packaging/2010/07/manifest" , "/" + filepath .Base (nsfilename ))
185- rf .Add ("http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" , pfn )
186-
187- b , err = rf .ToBytes ()
188- checkError (err )
189- archiveFile (filepath .Join ("_rels" , ".rels" ), w , b )
190- ct .Add (filepath .Ext (".rels" ))
191-
192- // Add [Content_Types].xml to Archive
193- b , err = ct .ToBytes ()
194- checkError (err )
195- archiveFile (`[Content_Types].xml` , w , b )
196-
197- // Close the zipwriter
198- w .Close ()
199-
200- // Return
201- return buf .Bytes (), nil
202- }
0 commit comments