mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-01-18 03:56:52 +02:00
feat: infer package name from go.mod (#3827)
If all other strategies fail, try to infer the `package_name` property from the `go.mod` file, using its last segment as the actual package name. This is not perfect, but usually this will only be used when running against a new project, with no git url, empty/default config, etc... so, in reality, it'll rarely be used. closes #3825 Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
parent
2920de7cec
commit
b6dd26c091
@ -3,6 +3,8 @@ package project
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/goreleaser/goreleaser/pkg/context"
|
||||
)
|
||||
@ -16,17 +18,40 @@ func (Pipe) String() string {
|
||||
|
||||
// Default set project defaults.
|
||||
func (Pipe) Default(ctx *context.Context) error {
|
||||
if ctx.Config.ProjectName == "" {
|
||||
switch {
|
||||
case ctx.Config.Release.GitHub.Name != "":
|
||||
ctx.Config.ProjectName = ctx.Config.Release.GitHub.Name
|
||||
case ctx.Config.Release.GitLab.Name != "":
|
||||
ctx.Config.ProjectName = ctx.Config.Release.GitLab.Name
|
||||
case ctx.Config.Release.Gitea.Name != "":
|
||||
ctx.Config.ProjectName = ctx.Config.Release.Gitea.Name
|
||||
default:
|
||||
return fmt.Errorf("couldn't guess project_name, please add it to your config")
|
||||
}
|
||||
}
|
||||
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(),
|
||||
} {
|
||||
if candidate == "" {
|
||||
continue
|
||||
}
|
||||
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 ""
|
||||
}
|
||||
|
||||
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])
|
||||
}
|
||||
|
@ -1,15 +1,18 @@
|
||||
package project
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/goreleaser/goreleaser/internal/testctx"
|
||||
"github.com/goreleaser/goreleaser/internal/testlib"
|
||||
"github.com/goreleaser/goreleaser/pkg/config"
|
||||
)
|
||||
|
||||
func TestCustomProjectName(t *testing.T) {
|
||||
_ = testlib.Mktmp(t)
|
||||
ctx := testctx.NewWithCfg(config.Project{
|
||||
ProjectName: "foo",
|
||||
Release: config.Release{
|
||||
@ -24,6 +27,7 @@ func TestCustomProjectName(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEmptyProjectName_DefaultsToGitHubRelease(t *testing.T) {
|
||||
_ = testlib.Mktmp(t)
|
||||
ctx := testctx.NewWithCfg(config.Project{
|
||||
Release: config.Release{
|
||||
GitHub: config.Repo{
|
||||
@ -37,6 +41,7 @@ func TestEmptyProjectName_DefaultsToGitHubRelease(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEmptyProjectName_DefaultsToGitLabRelease(t *testing.T) {
|
||||
_ = testlib.Mktmp(t)
|
||||
ctx := testctx.NewWithCfg(config.Project{
|
||||
Release: config.Release{
|
||||
GitLab: config.Repo{
|
||||
@ -50,6 +55,7 @@ func TestEmptyProjectName_DefaultsToGitLabRelease(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestEmptyProjectName_DefaultsToGiteaRelease(t *testing.T) {
|
||||
_ = testlib.Mktmp(t)
|
||||
ctx := testctx.NewWithCfg(config.Project{
|
||||
Release: config.Release{
|
||||
Gitea: config.Repo{
|
||||
@ -62,7 +68,16 @@ func TestEmptyProjectName_DefaultsToGiteaRelease(t *testing.T) {
|
||||
require.Equal(t, "bar", ctx.Config.ProjectName)
|
||||
}
|
||||
|
||||
func TestEmptyProjectName_DefaultsToGoModPath(t *testing.T) {
|
||||
_ = testlib.Mktmp(t)
|
||||
ctx := testctx.New()
|
||||
require.NoError(t, exec.Command("go", "mod", "init", "github.com/foo/bar").Run())
|
||||
require.NoError(t, Pipe{}.Default(ctx))
|
||||
require.Equal(t, "bar", ctx.Config.ProjectName)
|
||||
}
|
||||
|
||||
func TestEmptyProjectNameAndRelease(t *testing.T) {
|
||||
_ = testlib.Mktmp(t)
|
||||
ctx := testctx.NewWithCfg(config.Project{
|
||||
Release: config.Release{
|
||||
GitHub: config.Repo{},
|
||||
|
Loading…
x
Reference in New Issue
Block a user