mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Source files keep their SPDX-License-Identifier and Hanzo copyright line; the Gitea, Gogs and LFS Test Server copyrights they used to repeat now live once in NOTICE, alongside LICENSE. The header lint accepts any copyright line above the SPDX line, so it no longer demands an upstream one. Co-authored-by: Hanzo Dev <[email protected]>
112 lines
2.9 KiB
Go
112 lines
2.9 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package maven
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"io"
|
|
|
|
"github.com/hanzoai/git/modules/util"
|
|
"github.com/hanzoai/git/modules/validation"
|
|
|
|
"golang.org/x/net/html/charset"
|
|
)
|
|
|
|
// Metadata represents the metadata of a Maven package
|
|
type Metadata struct {
|
|
GroupID string `json:"group_id,omitempty"`
|
|
ArtifactID string `json:"artifact_id,omitempty"`
|
|
Name string `json:"name,omitempty"`
|
|
Description string `json:"description,omitempty"`
|
|
ProjectURL string `json:"project_url,omitempty"`
|
|
Licenses []string `json:"licenses,omitempty"`
|
|
Dependencies []*Dependency `json:"dependencies,omitempty"`
|
|
}
|
|
|
|
// Dependency represents a dependency of a Maven package
|
|
type Dependency struct {
|
|
GroupID string `json:"group_id,omitempty"`
|
|
ArtifactID string `json:"artifact_id,omitempty"`
|
|
Version string `json:"version,omitempty"`
|
|
}
|
|
|
|
type pomStruct struct {
|
|
XMLName xml.Name `xml:"project"`
|
|
|
|
Parent struct {
|
|
GroupID string `xml:"groupId"`
|
|
ArtifactID string `xml:"artifactId"`
|
|
Version string `xml:"version"`
|
|
} `xml:"parent"`
|
|
|
|
GroupID string `xml:"groupId"`
|
|
ArtifactID string `xml:"artifactId"`
|
|
Version string `xml:"version"`
|
|
Name string `xml:"name"`
|
|
Description string `xml:"description"`
|
|
URL string `xml:"url"`
|
|
|
|
Licenses []struct {
|
|
Name string `xml:"name"`
|
|
URL string `xml:"url"`
|
|
Distribution string `xml:"distribution"`
|
|
} `xml:"licenses>license"`
|
|
|
|
Dependencies []struct {
|
|
GroupID string `xml:"groupId"`
|
|
ArtifactID string `xml:"artifactId"`
|
|
Version string `xml:"version"`
|
|
Scope string `xml:"scope"`
|
|
} `xml:"dependencies>dependency"`
|
|
}
|
|
|
|
// ParsePackageMetaData parses the metadata of a pom file
|
|
func ParsePackageMetaData(r io.Reader) (*Metadata, error) {
|
|
var pom pomStruct
|
|
|
|
dec := xml.NewDecoder(r)
|
|
dec.CharsetReader = charset.NewReaderLabel
|
|
if err := dec.Decode(&pom); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if !validation.IsValidURL(pom.URL) {
|
|
pom.URL = ""
|
|
}
|
|
|
|
licenses := make([]string, 0, len(pom.Licenses))
|
|
for _, l := range pom.Licenses {
|
|
if l.Name != "" {
|
|
licenses = append(licenses, l.Name)
|
|
}
|
|
}
|
|
|
|
dependencies := make([]*Dependency, 0, len(pom.Dependencies))
|
|
for _, d := range pom.Dependencies {
|
|
dependencies = append(dependencies, &Dependency{
|
|
GroupID: d.GroupID,
|
|
ArtifactID: d.ArtifactID,
|
|
Version: d.Version,
|
|
})
|
|
}
|
|
|
|
pomGroupID := pom.GroupID
|
|
if pomGroupID == "" {
|
|
// the current module could inherit parent: https://maven.apache.org/pom.html#Inheritance
|
|
pomGroupID = pom.Parent.GroupID
|
|
}
|
|
if pomGroupID == "" {
|
|
return nil, util.ErrInvalidArgument
|
|
}
|
|
return &Metadata{
|
|
GroupID: pomGroupID,
|
|
ArtifactID: pom.ArtifactID,
|
|
Name: pom.Name,
|
|
Description: pom.Description,
|
|
ProjectURL: pom.URL,
|
|
Licenses: licenses,
|
|
Dependencies: dependencies,
|
|
}, nil
|
|
}
|