2018-03-12 08:42:31 -03:00
|
|
|
// Package project sets "high level" defaults related to the project.
|
|
|
|
package project
|
|
|
|
|
2020-03-04 16:03:05 -03:00
|
|
|
import (
|
|
|
|
"fmt"
|
2023-03-03 09:50:02 -03:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2020-03-04 16:03:05 -03:00
|
|
|
|
2023-10-14 18:59:07 -03:00
|
|
|
"github.com/goreleaser/goreleaser/internal/git"
|
2020-03-04 16:03:05 -03:00
|
|
|
"github.com/goreleaser/goreleaser/pkg/context"
|
|
|
|
)
|
2018-03-12 08:42:31 -03:00
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// Pipe implemens defaulter to set the project name.
|
2018-03-12 08:42:31 -03:00
|
|
|
type Pipe struct{}
|
|
|
|
|
|
|
|
func (Pipe) String() string {
|
|
|
|
return "project name"
|
|
|
|
}
|
|
|
|
|
2020-05-26 00:48:10 -03:00
|
|
|
// Default set project defaults.
|
2018-03-12 08:42:31 -03:00
|
|
|
func (Pipe) Default(ctx *context.Context) error {
|
2023-03-03 09:50:02 -03:00
|
|
|
if ctx.Config.ProjectName != "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, candidate := range []string{
|
|
|
|
ctx.Config.Release.GitHub.Name,
|
|
|
|
ctx.Config.Release.GitLab.Name,
|
|
|
|
ctx.Config.Release.Gitea.Name,
|
|
|
|
moduleName(),
|
2023-10-14 18:59:07 -03:00
|
|
|
gitRemote(ctx),
|
2023-03-03 09:50:02 -03:00
|
|
|
} {
|
|
|
|
if candidate == "" {
|
|
|
|
continue
|
2020-02-26 12:38:07 +00:00
|
|
|
}
|
2023-03-03 09:50:02 -03:00
|
|
|
ctx.Config.ProjectName = candidate
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("couldn't guess project_name, please add it to your config")
|
|
|
|
}
|
|
|
|
|
|
|
|
func moduleName() string {
|
|
|
|
bts, err := exec.Command("go", "list", "-m").CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
2018-03-12 08:42:31 -03:00
|
|
|
}
|
2023-03-03 09:50:02 -03:00
|
|
|
|
|
|
|
mod := strings.TrimSpace(string(bts))
|
|
|
|
|
|
|
|
// this is the default module used when go runs without a go module.
|
|
|
|
// https://pkg.go.dev/cmd/go@master#hdr-Package_lists_and_patterns
|
|
|
|
if mod == "command-line-arguments" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(mod, "/")
|
|
|
|
return strings.TrimSpace(parts[len(parts)-1])
|
2018-03-12 08:42:31 -03:00
|
|
|
}
|
2023-10-14 18:59:07 -03:00
|
|
|
|
|
|
|
func gitRemote(ctx *context.Context) string {
|
|
|
|
repo, err := git.ExtractRepoFromConfig(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if err := repo.CheckSCM(); err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return repo.Name
|
|
|
|
}
|