github.com/goreleaser/goreleaser@v1.25.1/internal/pipe/winget/template.go (about)

     1  package winget
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"github.com/caarlos0/log"
    10  	"github.com/goreleaser/goreleaser/internal/artifact"
    11  	"github.com/goreleaser/goreleaser/internal/yaml"
    12  	"github.com/goreleaser/goreleaser/pkg/config"
    13  	"github.com/goreleaser/goreleaser/pkg/context"
    14  )
    15  
    16  func createYAML(ctx *context.Context, winget config.Winget, in any, tp artifact.Type) error {
    17  	versionContent, err := yaml.Marshal(in)
    18  	if err != nil {
    19  		return err
    20  	}
    21  
    22  	filename := winget.PackageIdentifier + extFor(tp)
    23  	path := filepath.Join(ctx.Config.Dist, "winget", winget.Path, filename)
    24  	if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
    25  		return err
    26  	}
    27  
    28  	log.WithField("path", path).Info("writing")
    29  	if err := os.WriteFile(path, []byte(strings.Join([]string{
    30  		generatedHeader,
    31  		langserverLineFor(tp),
    32  		string(versionContent),
    33  	}, "\n")), 0o644); err != nil { //nolint: gosec
    34  		return fmt.Errorf("failed to write winget version: %w", err)
    35  	}
    36  
    37  	ctx.Artifacts.Add(&artifact.Artifact{
    38  		Name: filename,
    39  		Path: path,
    40  		Type: tp,
    41  		Extra: map[string]interface{}{
    42  			wingetConfigExtra: winget,
    43  			artifact.ExtraID:  winget.Name,
    44  		},
    45  	})
    46  
    47  	return nil
    48  }
    49  
    50  const (
    51  	manifestVersion         = "1.6.0"
    52  	versionLangServer       = "# yaml-language-server: $schema=https://aka.ms/winget-manifest.version.1.6.0.schema.json"
    53  	installerLangServer     = "# yaml-language-server: $schema=https://aka.ms/winget-manifest.installer.1.6.0.schema.json"
    54  	defaultLocaleLangServer = "# yaml-language-server: $schema=https://aka.ms/winget-manifest.defaultLocale.1.6.0.schema.json"
    55  	defaultLocale           = "en-US"
    56  	generatedHeader         = `# This file was generated by GoReleaser. DO NOT EDIT.`
    57  )
    58  
    59  // nolint: tagliatelle
    60  type Version struct {
    61  	PackageIdentifier string `yaml:"PackageIdentifier,omitempty"`
    62  	PackageVersion    string `yaml:"PackageVersion,omitempty"`
    63  	DefaultLocale     string `yaml:"DefaultLocale,omitempty"`
    64  	ManifestType      string `yaml:"ManifestType,omitempty"`
    65  	ManifestVersion   string `yaml:"ManifestVersion,omitempty"`
    66  }
    67  
    68  // nolint: tagliatelle
    69  type InstallerItemFile struct {
    70  	RelativeFilePath     string `yaml:"RelativeFilePath,omitempty"`
    71  	PortableCommandAlias string `yaml:"PortableCommandAlias,omitempty"`
    72  }
    73  
    74  // nolint: tagliatelle
    75  type InstallerItem struct {
    76  	Architecture         string              `yaml:"Architecture,omitempty"`
    77  	NestedInstallerType  string              `yaml:"NestedInstallerType,omitempty"`
    78  	NestedInstallerFiles []InstallerItemFile `yaml:"NestedInstallerFiles,omitempty"`
    79  	InstallerURL         string              `yaml:"InstallerUrl,omitempty"`
    80  	InstallerSha256      string              `yaml:"InstallerSha256,omitempty"`
    81  	UpgradeBehavior      string              `yaml:"UpgradeBehavior,omitempty"`
    82  }
    83  
    84  // nolint: tagliatelle
    85  type Installer struct {
    86  	PackageIdentifier string          `yaml:"PackageIdentifier,omitempty"`
    87  	PackageVersion    string          `yaml:"PackageVersion,omitempty"`
    88  	InstallerLocale   string          `yaml:"InstallerLocale,omitempty"`
    89  	InstallerType     string          `yaml:"InstallerType,omitempty"`
    90  	Commands          []string        `yaml:"Commands,omitempty"`
    91  	ReleaseDate       string          `yaml:"ReleaseDate,omitempty"`
    92  	Installers        []InstallerItem `yaml:"Installers,omitempty"`
    93  	ManifestType      string          `yaml:"ManifestType,omitempty"`
    94  	ManifestVersion   string          `yaml:"ManifestVersion,omitempty"`
    95  	Dependencies      Dependencies    `yaml:"Dependencies,omitempty"`
    96  }
    97  
    98  // nolint: tagliatelle
    99  type Dependencies struct {
   100  	PackageDependencies []PackageDependency `yaml:"PackageDependencies,omitempty"`
   101  }
   102  
   103  // nolint: tagliatelle
   104  type PackageDependency struct {
   105  	PackageIdentifier string `yaml:"PackageIdentifier"`
   106  	MinimumVersion    string `yaml:"MinimumVersion,omitempty"`
   107  }
   108  
   109  // nolint: tagliatelle
   110  type Locale struct {
   111  	PackageIdentifier   string   `yaml:"PackageIdentifier,omitempty"`
   112  	PackageVersion      string   `yaml:"PackageVersion,omitempty"`
   113  	PackageLocale       string   `yaml:"PackageLocale,omitempty"`
   114  	Publisher           string   `yaml:"Publisher,omitempty"`
   115  	PublisherURL        string   `yaml:"PublisherUrl,omitempty"`
   116  	PublisherSupportURL string   `yaml:"PublisherSupportUrl,omitempty"`
   117  	Author              string   `yaml:"Author,omitempty"`
   118  	PackageName         string   `yaml:"PackageName,omitempty"`
   119  	PackageURL          string   `yaml:"PackageUrl,omitempty"`
   120  	License             string   `yaml:"License,omitempty"`
   121  	LicenseURL          string   `yaml:"LicenseUrl,omitempty"`
   122  	Copyright           string   `yaml:"Copyright,omitempty"`
   123  	CopyrightURL        string   `yaml:"CopyrightUrl,omitempty"`
   124  	ShortDescription    string   `yaml:"ShortDescription,omitempty"`
   125  	Description         string   `yaml:"Description,omitempty"`
   126  	Moniker             string   `yaml:"Moniker,omitempty"`
   127  	Tags                []string `yaml:"Tags,omitempty"`
   128  	ReleaseNotes        string   `yaml:"ReleaseNotes,omitempty"`
   129  	ReleaseNotesURL     string   `yaml:"ReleaseNotesUrl,omitempty"`
   130  	ManifestType        string   `yaml:"ManifestType,omitempty"`
   131  	ManifestVersion     string   `yaml:"ManifestVersion,omitempty"`
   132  }
   133  
   134  var fromGoArch = map[string]string{
   135  	"amd64": "x64",
   136  	"386":   "x86",
   137  	"arm64": "arm64",
   138  }