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 {
@ -98,19 +99,21 @@ type nuspecDependencies struct {
} }
type nuspeceMetadata struct { 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,10 +165,12 @@ func ParseNuspecMetaData(r io.Reader) (*Package, error) {
} }
packageType := DependencyPackage packageType := DependencyPackage
for _, pt := range p.Metadata.PackageTypes.PackageType { if p.Metadata.PackageTypes != nil {
if pt.Name == "SymbolsPackage" { for _, pt := range p.Metadata.PackageTypes.PackageType {
packageType = SymbolsPackage if pt.Name == "SymbolsPackage" {
break packageType = SymbolsPackage
break
}
} }
} }
@ -174,24 +179,27 @@ func ParseNuspecMetaData(r io.Reader) (*Package, error) {
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 {
for _, group := range p.Metadata.Dependencies.Group { m.RepositoryURL = p.Metadata.Repository.URL
deps := make([]Dependency, 0, len(group.Dependency)) }
for _, dep := range group.Dependency { if p.Metadata.Dependencies != nil {
if dep.ID == "" || dep.Version == "" { for _, group := range p.Metadata.Dependencies.Group {
continue deps := make([]Dependency, 0, len(group.Dependency))
for _, dep := range group.Dependency {
if dep.ID == "" || dep.Version == "" {
continue
}
deps = append(deps, Dependency{
ID: dep.ID,
Version: dep.Version,
})
}
if len(deps) > 0 {
m.Dependencies[group.TargetFramework] = deps
} }
deps = append(deps, Dependency{
ID: dep.ID,
Version: dep.Version,
})
}
if len(deps) > 0 {
m.Dependencies[group.TargetFramework] = deps
} }
} }
return &Package{ return &Package{
@ -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)),
},
} }
for tgf, deps := range pd.Metadata.Dependencies { if pd.Metadata.RepositoryURL != "" {
gDeps := make([]nuspecDependency, len(deps)) m.Repository = &nuspecRepository{
for i, dep := range deps { URL: pd.Metadata.RepositoryURL,
gDeps[i] = nuspecDependency{ }
ID: dep.ID, }
Version: dep.Version,
} groups := len(pd.Metadata.Dependencies)
if groups > 0 {
m.Dependencies = &nuspecDependencies{
Group: make([]nuspecGroup, 0, groups),
}
for tgf, deps := range pd.Metadata.Dependencies {
if len(deps) == 0 {
continue
}
gDeps := make([]nuspecDependency, 0, len(deps))
for _, dep := range deps {
gDeps = append(gDeps, nuspecDependency{
ID: dep.ID,
Version: dep.Version,
})
}
m.Dependencies.Group = append(m.Dependencies.Group, nuspecGroup{
TargetFramework: tgf,
Dependency: gDeps,
})
} }
m.Dependencies.Group = append(m.Dependencies.Group, nuspecGroup{
TargetFramework: tgf,
Dependency: gDeps,
})
} }
return &nuspecPackage{ return &nuspecPackage{
Xmlns: "http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd",
Metadata: m, Metadata: m,
} }
} }