fix: optional elements and xml schema

This commit is contained in:
Michael Kriese 2024-01-24 00:13:22 +01:00
parent a715984a42
commit 6ea6895a36
No known key found for this signature in database
GPG key ID: B83F553A0724D44E

View file

@ -80,12 +80,13 @@ type nuspecPackageTypes struct {
} }
type nuspecRepository struct { type nuspecRepository struct {
URL string `xml:"url,attr"` URL string `xml:"url,attr,omitempty"`
Type string `xml:"type,attr,omitempty"`
} }
type nuspecDependency struct { type nuspecDependency struct {
ID string `xml:"id,attr"` ID string `xml:"id,attr"`
Version string `xml:"version,attr"` Version string `xml:"version,attr"`
Exclude string `xml:"exclude,attr"` Exclude string `xml:"exclude,attr,omitempty"`
} }
type nuspecGroup struct { type nuspecGroup struct {
@ -101,16 +102,18 @@ type nuspeceMetadata struct {
ID string `xml:"id"` ID string `xml:"id"`
Version string `xml:"version"` Version string `xml:"version"`
Authors string `xml:"authors"` Authors string `xml:"authors"`
RequireLicenseAcceptance bool `xml:"requireLicenseAcceptance"` RequireLicenseAcceptance bool `xml:"requireLicenseAcceptance,omitempty"`
ProjectURL string `xml:"projectUrl"` ProjectURL string `xml:"projectUrl,omitempty"`
Description string `xml:"description"` Description string `xml:"description"`
ReleaseNotes string `xml:"releaseNotes"` ReleaseNotes string `xml:"releaseNotes,omitempty"`
PackageTypes nuspecPackageTypes `xml:"packageTypes"` PackageTypes *nuspecPackageTypes `xml:"packageTypes,omitempty"`
Repository nuspecRepository `xml:"repository"` Repository *nuspecRepository `xml:"repository,omitempty"`
Dependencies nuspecDependencies `xml:"dependencies"` Dependencies *nuspecDependencies `xml:"dependencies,omitempty"`
} }
type nuspecPackage struct { type nuspecPackage struct {
XMLName xml.Name `xml:"package"`
Xmlns string `xml:"xmlns,attr"`
Metadata nuspeceMetadata `xml:"metadata"` Metadata nuspeceMetadata `xml:"metadata"`
} }
@ -162,23 +165,27 @@ func ParseNuspecMetaData(r io.Reader) (*Package, error) {
} }
packageType := DependencyPackage packageType := DependencyPackage
if p.Metadata.PackageTypes != nil {
for _, pt := range p.Metadata.PackageTypes.PackageType { for _, pt := range p.Metadata.PackageTypes.PackageType {
if pt.Name == "SymbolsPackage" { if pt.Name == "SymbolsPackage" {
packageType = SymbolsPackage packageType = SymbolsPackage
break break
} }
} }
}
m := &Metadata{ m := &Metadata{
Description: p.Metadata.Description, Description: p.Metadata.Description,
ReleaseNotes: p.Metadata.ReleaseNotes, ReleaseNotes: p.Metadata.ReleaseNotes,
Authors: p.Metadata.Authors, Authors: p.Metadata.Authors,
ProjectURL: p.Metadata.ProjectURL, ProjectURL: p.Metadata.ProjectURL,
RepositoryURL: p.Metadata.Repository.URL,
RequireLicenseAcceptance: p.Metadata.RequireLicenseAcceptance, RequireLicenseAcceptance: p.Metadata.RequireLicenseAcceptance,
Dependencies: make(map[string][]Dependency), Dependencies: make(map[string][]Dependency),
} }
if p.Metadata.Repository != nil {
m.RepositoryURL = p.Metadata.Repository.URL
}
if p.Metadata.Dependencies != nil {
for _, group := range p.Metadata.Dependencies.Group { for _, group := range p.Metadata.Dependencies.Group {
deps := make([]Dependency, 0, len(group.Dependency)) deps := make([]Dependency, 0, len(group.Dependency))
for _, dep := range group.Dependency { for _, dep := range group.Dependency {
@ -194,6 +201,7 @@ func ParseNuspecMetaData(r io.Reader) (*Package, error) {
m.Dependencies[group.TargetFramework] = deps m.Dependencies[group.TargetFramework] = deps
} }
} }
}
return &Package{ return &Package{
PackageType: packageType, PackageType: packageType,
ID: p.Metadata.ID, ID: p.Metadata.ID,
@ -224,29 +232,44 @@ func GenerateNuspec(pd *Package) any {
ID: pd.ID, ID: pd.ID,
Version: pd.Version, Version: pd.Version,
Authors: pd.Metadata.Authors, Authors: pd.Metadata.Authors,
Description: pd.Metadata.Description,
ProjectURL: pd.Metadata.ProjectURL, ProjectURL: pd.Metadata.ProjectURL,
Repository: nuspecRepository{URL: pd.Metadata.RepositoryURL},
RequireLicenseAcceptance: pd.Metadata.RequireLicenseAcceptance, RequireLicenseAcceptance: pd.Metadata.RequireLicenseAcceptance,
Dependencies: nuspecDependencies{ }
Group: make([]nuspecGroup, len(pd.Metadata.Dependencies)),
}, if pd.Metadata.RepositoryURL != "" {
m.Repository = &nuspecRepository{
URL: pd.Metadata.RepositoryURL,
}
}
groups := len(pd.Metadata.Dependencies)
if groups > 0 {
m.Dependencies = &nuspecDependencies{
Group: make([]nuspecGroup, 0, groups),
} }
for tgf, deps := range pd.Metadata.Dependencies { for tgf, deps := range pd.Metadata.Dependencies {
gDeps := make([]nuspecDependency, len(deps)) if len(deps) == 0 {
for i, dep := range deps { continue
gDeps[i] = nuspecDependency{ }
gDeps := make([]nuspecDependency, 0, len(deps))
for _, dep := range deps {
gDeps = append(gDeps, nuspecDependency{
ID: dep.ID, ID: dep.ID,
Version: dep.Version, Version: dep.Version,
})
} }
}
m.Dependencies.Group = append(m.Dependencies.Group, nuspecGroup{ m.Dependencies.Group = append(m.Dependencies.Group, nuspecGroup{
TargetFramework: tgf, TargetFramework: tgf,
Dependency: gDeps, Dependency: gDeps,
}) })
} }
}
return &nuspecPackage{ return &nuspecPackage{
Xmlns: "http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd",
Metadata: m, Metadata: m,
} }
} }