go/pkg/release/publishers/chocolatey.go
Snider fdc108c69e
feat: git command, build improvements, and go fmt git-aware (#74)
* feat(go): make go fmt git-aware by default

- By default, only check changed Go files (modified, staged, untracked)
- Add --all flag to check all files (previous behaviour)
- Reduces noise when running fmt on large codebases

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* feat(build): minimal output by default, add missing i18n

- Default output now shows single line: "Success Built N artifacts (dir)"
- Add --verbose/-v flag to show full detailed output
- Add all missing i18n translations for build commands
- Errors still show failure reason in minimal mode

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* feat: add root-level `core git` command

- Create pkg/gitcmd with git workflow commands as root menu
- Export command builders from pkg/dev (AddCommitCommand, etc.)
- Commands available under both `core git` and `core dev` for compatibility
- Git commands: health, commit, push, pull, work, sync, apply
- GitHub orchestration stays in dev: issues, reviews, ci, impact

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* feat(qa): add docblock coverage checking

Implement docblock/docstring coverage analysis for Go code:
- New `core qa docblock` command to check coverage
- Shows compact file:line list when under threshold
- Integrate with `core go qa` as a default check
- Add --docblock-threshold flag (default 80%)

The checker uses Go AST parsing to find exported symbols
(functions, types, consts, vars) without documentation.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix: address CodeRabbit review feedback

- Fix doc comment: "status" → "health" in gitcmd package
- Implement --check flag for `core go fmt` (exits non-zero if files need formatting)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* docs: add docstrings for 100% coverage

Add documentation comments to all exported symbols:
- pkg/build: ProjectType constants
- pkg/cli: LogLevel, RenderStyle, TableStyle
- pkg/framework: ServiceFor, MustServiceFor, Core.Core
- pkg/git: GitError.Error, GitError.Unwrap
- pkg/i18n: Handler Match/Handle methods
- pkg/log: Level constants
- pkg/mcp: Tool input/output types
- pkg/php: Service constants, QA types, service methods
- pkg/process: ServiceError.Error
- pkg/repos: RepoType constants
- pkg/setup: ChangeType, ChangeCategory constants
- pkg/workspace: AddWorkspaceCommands

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* chore: standardize line endings to LF

Add .gitattributes to enforce LF line endings for all text files.
Normalize all existing files to use Unix-style line endings.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix: address CodeRabbit review feedback

- cmd_format.go: validate --check/--fix mutual exclusivity, capture stderr
- cmd_docblock.go: return error instead of os.Exit(1) for proper error handling

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix: address CodeRabbit review feedback (round 2)

- linuxkit.go: propagate state update errors, handle cmd.Wait() errors in waitForExit
- mcp.go: guard against empty old_string in editDiff to prevent runaway edits
- cmd_docblock.go: log parse errors instead of silently skipping

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 10:48:44 +00:00

278 lines
7.9 KiB
Go

// Package publishers provides release publishing implementations.
package publishers
import (
"bytes"
"context"
"embed"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
"github.com/host-uk/core/pkg/build"
"github.com/host-uk/core/pkg/i18n"
)
//go:embed templates/chocolatey/*.tmpl templates/chocolatey/tools/*.tmpl
var chocolateyTemplates embed.FS
// ChocolateyConfig holds Chocolatey-specific configuration.
type ChocolateyConfig struct {
// Package is the Chocolatey package name.
Package string
// Push determines whether to push to Chocolatey (false = generate only).
Push bool
// Official config for generating files for official repo PRs.
Official *OfficialConfig
}
// ChocolateyPublisher publishes releases to Chocolatey.
type ChocolateyPublisher struct{}
// NewChocolateyPublisher creates a new Chocolatey publisher.
func NewChocolateyPublisher() *ChocolateyPublisher {
return &ChocolateyPublisher{}
}
// Name returns the publisher's identifier.
func (p *ChocolateyPublisher) Name() string {
return "chocolatey"
}
// Publish publishes the release to Chocolatey.
func (p *ChocolateyPublisher) Publish(ctx context.Context, release *Release, pubCfg PublisherConfig, relCfg ReleaseConfig, dryRun bool) error {
cfg := p.parseConfig(pubCfg, relCfg)
repo := ""
if relCfg != nil {
repo = relCfg.GetRepository()
}
if repo == "" {
detectedRepo, err := detectRepository(release.ProjectDir)
if err != nil {
return fmt.Errorf("chocolatey.Publish: could not determine repository: %w", err)
}
repo = detectedRepo
}
projectName := ""
if relCfg != nil {
projectName = relCfg.GetProjectName()
}
if projectName == "" {
parts := strings.Split(repo, "/")
projectName = parts[len(parts)-1]
}
packageName := cfg.Package
if packageName == "" {
packageName = projectName
}
version := strings.TrimPrefix(release.Version, "v")
checksums := buildChecksumMap(release.Artifacts)
// Extract authors from repository
authors := strings.Split(repo, "/")[0]
data := chocolateyTemplateData{
PackageName: packageName,
Title: fmt.Sprintf("%s CLI", i18n.Title(projectName)),
Description: fmt.Sprintf("%s CLI", projectName),
Repository: repo,
Version: version,
License: "MIT",
BinaryName: projectName,
Authors: authors,
Tags: fmt.Sprintf("cli %s", projectName),
Checksums: checksums,
}
if dryRun {
return p.dryRunPublish(data, cfg)
}
return p.executePublish(ctx, release.ProjectDir, data, cfg)
}
type chocolateyTemplateData struct {
PackageName string
Title string
Description string
Repository string
Version string
License string
BinaryName string
Authors string
Tags string
Checksums ChecksumMap
}
func (p *ChocolateyPublisher) parseConfig(pubCfg PublisherConfig, relCfg ReleaseConfig) ChocolateyConfig {
cfg := ChocolateyConfig{
Push: false, // Default to generate only
}
if ext, ok := pubCfg.Extended.(map[string]any); ok {
if pkg, ok := ext["package"].(string); ok && pkg != "" {
cfg.Package = pkg
}
if push, ok := ext["push"].(bool); ok {
cfg.Push = push
}
if official, ok := ext["official"].(map[string]any); ok {
cfg.Official = &OfficialConfig{}
if enabled, ok := official["enabled"].(bool); ok {
cfg.Official.Enabled = enabled
}
if output, ok := official["output"].(string); ok {
cfg.Official.Output = output
}
}
}
return cfg
}
func (p *ChocolateyPublisher) dryRunPublish(data chocolateyTemplateData, cfg ChocolateyConfig) error {
fmt.Println()
fmt.Println("=== DRY RUN: Chocolatey Publish ===")
fmt.Println()
fmt.Printf("Package: %s\n", data.PackageName)
fmt.Printf("Version: %s\n", data.Version)
fmt.Printf("Push: %t\n", cfg.Push)
fmt.Printf("Repository: %s\n", data.Repository)
fmt.Println()
nuspec, err := p.renderTemplate("templates/chocolatey/package.nuspec.tmpl", data)
if err != nil {
return fmt.Errorf("chocolatey.dryRunPublish: %w", err)
}
fmt.Println("Generated package.nuspec:")
fmt.Println("---")
fmt.Println(nuspec)
fmt.Println("---")
fmt.Println()
install, err := p.renderTemplate("templates/chocolatey/tools/chocolateyinstall.ps1.tmpl", data)
if err != nil {
return fmt.Errorf("chocolatey.dryRunPublish: %w", err)
}
fmt.Println("Generated chocolateyinstall.ps1:")
fmt.Println("---")
fmt.Println(install)
fmt.Println("---")
fmt.Println()
if cfg.Push {
fmt.Println("Would push to Chocolatey community repo")
} else {
fmt.Println("Would generate package files only (push=false)")
}
fmt.Println()
fmt.Println("=== END DRY RUN ===")
return nil
}
func (p *ChocolateyPublisher) executePublish(ctx context.Context, projectDir string, data chocolateyTemplateData, cfg ChocolateyConfig) error {
nuspec, err := p.renderTemplate("templates/chocolatey/package.nuspec.tmpl", data)
if err != nil {
return fmt.Errorf("chocolatey.Publish: failed to render nuspec: %w", err)
}
install, err := p.renderTemplate("templates/chocolatey/tools/chocolateyinstall.ps1.tmpl", data)
if err != nil {
return fmt.Errorf("chocolatey.Publish: failed to render install script: %w", err)
}
// Create package directory
output := filepath.Join(projectDir, "dist", "chocolatey")
if cfg.Official != nil && cfg.Official.Enabled && cfg.Official.Output != "" {
output = cfg.Official.Output
if !filepath.IsAbs(output) {
output = filepath.Join(projectDir, output)
}
}
toolsDir := filepath.Join(output, "tools")
if err := os.MkdirAll(toolsDir, 0755); err != nil {
return fmt.Errorf("chocolatey.Publish: failed to create output directory: %w", err)
}
// Write files
nuspecPath := filepath.Join(output, fmt.Sprintf("%s.nuspec", data.PackageName))
if err := os.WriteFile(nuspecPath, []byte(nuspec), 0644); err != nil {
return fmt.Errorf("chocolatey.Publish: failed to write nuspec: %w", err)
}
installPath := filepath.Join(toolsDir, "chocolateyinstall.ps1")
if err := os.WriteFile(installPath, []byte(install), 0644); err != nil {
return fmt.Errorf("chocolatey.Publish: failed to write install script: %w", err)
}
fmt.Printf("Wrote Chocolatey package files: %s\n", output)
// Push to Chocolatey if configured
if cfg.Push {
if err := p.pushToChocolatey(ctx, output, data); err != nil {
return err
}
}
return nil
}
func (p *ChocolateyPublisher) pushToChocolatey(ctx context.Context, packageDir string, data chocolateyTemplateData) error {
// Check for CHOCOLATEY_API_KEY
apiKey := os.Getenv("CHOCOLATEY_API_KEY")
if apiKey == "" {
return fmt.Errorf("chocolatey.Publish: CHOCOLATEY_API_KEY environment variable is required for push")
}
// Pack the package
nupkgPath := filepath.Join(packageDir, fmt.Sprintf("%s.%s.nupkg", data.PackageName, data.Version))
cmd := exec.CommandContext(ctx, "choco", "pack", filepath.Join(packageDir, fmt.Sprintf("%s.nuspec", data.PackageName)), "-OutputDirectory", packageDir)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("chocolatey.Publish: choco pack failed: %w", err)
}
// Push the package
cmd = exec.CommandContext(ctx, "choco", "push", nupkgPath, "--source", "https://push.chocolatey.org/", "--api-key", apiKey)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("chocolatey.Publish: choco push failed: %w", err)
}
fmt.Printf("Published to Chocolatey: https://community.chocolatey.org/packages/%s\n", data.PackageName)
return nil
}
func (p *ChocolateyPublisher) renderTemplate(name string, data chocolateyTemplateData) (string, error) {
content, err := chocolateyTemplates.ReadFile(name)
if err != nil {
return "", fmt.Errorf("failed to read template %s: %w", name, err)
}
tmpl, err := template.New(filepath.Base(name)).Parse(string(content))
if err != nil {
return "", fmt.Errorf("failed to parse template %s: %w", name, err)
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
return "", fmt.Errorf("failed to execute template %s: %w", name, err)
}
return buf.String(), nil
}
// Ensure build package is used
var _ = build.Artifact{}