github.com/goreleaser/goreleaser@v1.25.1/internal/pipe/chocolatey/nuspec.go (about)

     1  package chocolatey
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/xml"
     6  	"strings"
     7  )
     8  
     9  const schema = "http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd"
    10  
    11  // Nuspec represents a Nuget/Chocolatey Nuspec.
    12  // More info: https://learn.microsoft.com/en-us/nuget/reference/nuspec
    13  // https://docs.chocolatey.org/en-us/create/create-packages
    14  type Nuspec struct {
    15  	XMLName  xml.Name `xml:"package"`
    16  	Xmlns    string   `xml:"xmlns,attr,omitempty"`
    17  	Metadata Metadata `xml:"metadata"`
    18  	Files    Files    `xml:"files,omitempty"`
    19  }
    20  
    21  // Metadata contains information about a single package.
    22  type Metadata struct {
    23  	ID                       string        `xml:"id"`
    24  	Version                  string        `xml:"version"`
    25  	PackageSourceURL         string        `xml:"packageSourceUrl,omitempty"`
    26  	Owners                   string        `xml:"owners,omitempty"`
    27  	Title                    string        `xml:"title,omitempty"`
    28  	Authors                  string        `xml:"authors"`
    29  	ProjectURL               string        `xml:"projectUrl,omitempty"`
    30  	IconURL                  string        `xml:"iconUrl,omitempty"`
    31  	Copyright                string        `xml:"copyright,omitempty"`
    32  	LicenseURL               string        `xml:"licenseUrl,omitempty"`
    33  	RequireLicenseAcceptance bool          `xml:"requireLicenseAcceptance"`
    34  	ProjectSourceURL         string        `xml:"projectSourceUrl,omitempty"`
    35  	DocsURL                  string        `xml:"docsUrl,omitempty"`
    36  	BugTrackerURL            string        `xml:"bugTrackerUrl,omitempty"`
    37  	Tags                     string        `xml:"tags,omitempty"`
    38  	Summary                  string        `xml:"summary,omitempty"`
    39  	Description              string        `xml:"description"`
    40  	ReleaseNotes             string        `xml:"releaseNotes,omitempty"`
    41  	Dependencies             *Dependencies `xml:"dependencies,omitempty"`
    42  }
    43  
    44  // Dependency represents a dependency element.
    45  type Dependency struct {
    46  	ID      string `xml:"id,attr"`
    47  	Version string `xml:"version,attr,omitempty"`
    48  }
    49  
    50  // Dependencies represents a collection zero or more dependency elements.
    51  type Dependencies struct {
    52  	Dependency []Dependency `xml:"dependency"`
    53  }
    54  
    55  // File represents a file to be copied.
    56  type File struct {
    57  	Source string `xml:"src,attr"`
    58  	Target string `xml:"target,attr,omitempty"`
    59  }
    60  
    61  // Files represents files that will be copied during packaging.
    62  type Files struct {
    63  	File []File `xml:"file"`
    64  }
    65  
    66  // Bytes marshals the Nuspec into XML format and return as []byte.
    67  func (m *Nuspec) Bytes() ([]byte, error) {
    68  	b := &bytes.Buffer{}
    69  	b.WriteString(strings.ToLower(xml.Header))
    70  
    71  	enc := xml.NewEncoder(b)
    72  	enc.Indent("", "  ")
    73  
    74  	if err := enc.Encode(m); err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	out := b.Bytes()
    79  
    80  	// Follows the nuget specification of self-closing xml tags.
    81  	tags := []string{"dependency", "file"}
    82  	for _, tag := range tags {
    83  		out = bytes.ReplaceAll(out, []byte("></"+tag+">"), []byte(" />"))
    84  	}
    85  
    86  	return out, nil
    87  }