1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-11-06 09:09:29 +02:00

refactor(winget): improve winget code

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
Carlos Alexandro Becker
2024-01-21 23:09:32 -03:00
parent 2489da3a96
commit bfa9e7fd17

View File

@@ -211,73 +211,9 @@ func (p Pipe) doRun(ctx *context.Context, winget config.Winget, cl client.Releas
return err
}
var deps []PackageDependency
for _, dep := range winget.Dependencies {
if err := tp.ApplyAll(&dep.MinimumVersion, &dep.PackageIdentifier); err != nil {
return err
}
deps = append(deps, PackageDependency{
PackageIdentifier: dep.PackageIdentifier,
MinimumVersion: dep.MinimumVersion,
})
}
installer := Installer{
PackageIdentifier: winget.PackageIdentifier,
PackageVersion: ctx.Version,
InstallerLocale: defaultLocale,
InstallerType: "zip",
Commands: []string{},
ReleaseDate: ctx.Date.Format(time.DateOnly),
Installers: []InstallerItem{},
ManifestType: "installer",
ManifestVersion: manifestVersion,
Dependencies: Dependencies{
PackageDependencies: deps,
},
}
var amd64Count, i386count, zipCount, binaryCount int
for _, archive := range archives {
sha256, err := archive.Checksum("sha256")
if err != nil {
return err
}
url, err := tmpl.New(ctx).WithArtifact(archive).Apply(winget.URLTemplate)
if err != nil {
return err
}
item := InstallerItem{
Architecture: fromGoArch[archive.Goarch],
InstallerURL: url,
InstallerSha256: sha256,
UpgradeBehavior: "uninstallPrevious",
}
if archive.Format() == "zip" {
zipCount++
installer.InstallerType = "zip"
item.NestedInstallerType = "portable"
item.NestedInstallerFiles = installerItemFilesFor(*archive)
} else {
binaryCount++
installer.InstallerType = "portable"
installer.Commands = []string{winget.Name}
}
installer.Installers = append(installer.Installers, item)
switch archive.Goarch {
case "386":
i386count++
case "amd64":
amd64Count++
}
}
if binaryCount > 0 && zipCount > 0 {
return errMixedFormats
}
if i386count > 1 || amd64Count > 1 {
return errMultipleArchives
installer, err := makeInstaller(ctx, winget, archives)
if err != nil {
return err
}
if err := createYAML(ctx, winget, installer, artifact.WingetInstaller); err != nil {
@@ -460,3 +396,77 @@ func installerItemFilesFor(archive artifact.Artifact) []InstallerItemFile {
}
return files
}
func makeInstaller(ctx *context.Context, winget config.Winget, archives []*artifact.Artifact) (Installer, error) {
tp := tmpl.New(ctx)
var deps []PackageDependency
for _, dep := range winget.Dependencies {
if err := tp.ApplyAll(&dep.MinimumVersion, &dep.PackageIdentifier); err != nil {
return Installer{}, err
}
deps = append(deps, PackageDependency{
PackageIdentifier: dep.PackageIdentifier,
MinimumVersion: dep.MinimumVersion,
})
}
installer := Installer{
PackageIdentifier: winget.PackageIdentifier,
PackageVersion: ctx.Version,
InstallerLocale: defaultLocale,
InstallerType: "zip",
Commands: []string{},
ReleaseDate: ctx.Date.Format(time.DateOnly),
Installers: []InstallerItem{},
ManifestType: "installer",
ManifestVersion: manifestVersion,
Dependencies: Dependencies{
PackageDependencies: deps,
},
}
var amd64Count, i386count, zipCount, binaryCount int
for _, archive := range archives {
sha256, err := archive.Checksum("sha256")
if err != nil {
return Installer{}, err
}
url, err := tmpl.New(ctx).WithArtifact(archive).Apply(winget.URLTemplate)
if err != nil {
return Installer{}, err
}
item := InstallerItem{
Architecture: fromGoArch[archive.Goarch],
InstallerURL: url,
InstallerSha256: sha256,
UpgradeBehavior: "uninstallPrevious",
}
if archive.Format() == "zip" {
zipCount++
installer.InstallerType = "zip"
item.NestedInstallerType = "portable"
item.NestedInstallerFiles = installerItemFilesFor(*archive)
} else {
binaryCount++
installer.InstallerType = "portable"
installer.Commands = []string{winget.Name}
}
installer.Installers = append(installer.Installers, item)
switch archive.Goarch {
case "386":
i386count++
case "amd64":
amd64Count++
}
}
if binaryCount > 0 && zipCount > 0 {
return Installer{}, errMixedFormats
}
if i386count > 1 || amd64Count > 1 {
return Installer{}, errMultipleArchives
}
return installer, nil
}