You've already forked goreleaser
mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-07-17 01:42:37 +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:
committed by
GitHub
parent
2920de7cec
commit
b6dd26c091
@ -3,6 +3,8 @@ package project
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/goreleaser/goreleaser/pkg/context"
|
"github.com/goreleaser/goreleaser/pkg/context"
|
||||||
)
|
)
|
||||||
@ -16,17 +18,40 @@ func (Pipe) String() string {
|
|||||||
|
|
||||||
// Default set project defaults.
|
// Default set project defaults.
|
||||||
func (Pipe) Default(ctx *context.Context) error {
|
func (Pipe) Default(ctx *context.Context) error {
|
||||||
if ctx.Config.ProjectName == "" {
|
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")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
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
|
package project
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os/exec"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/goreleaser/goreleaser/internal/testctx"
|
"github.com/goreleaser/goreleaser/internal/testctx"
|
||||||
|
"github.com/goreleaser/goreleaser/internal/testlib"
|
||||||
"github.com/goreleaser/goreleaser/pkg/config"
|
"github.com/goreleaser/goreleaser/pkg/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCustomProjectName(t *testing.T) {
|
func TestCustomProjectName(t *testing.T) {
|
||||||
|
_ = testlib.Mktmp(t)
|
||||||
ctx := testctx.NewWithCfg(config.Project{
|
ctx := testctx.NewWithCfg(config.Project{
|
||||||
ProjectName: "foo",
|
ProjectName: "foo",
|
||||||
Release: config.Release{
|
Release: config.Release{
|
||||||
@ -24,6 +27,7 @@ func TestCustomProjectName(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEmptyProjectName_DefaultsToGitHubRelease(t *testing.T) {
|
func TestEmptyProjectName_DefaultsToGitHubRelease(t *testing.T) {
|
||||||
|
_ = testlib.Mktmp(t)
|
||||||
ctx := testctx.NewWithCfg(config.Project{
|
ctx := testctx.NewWithCfg(config.Project{
|
||||||
Release: config.Release{
|
Release: config.Release{
|
||||||
GitHub: config.Repo{
|
GitHub: config.Repo{
|
||||||
@ -37,6 +41,7 @@ func TestEmptyProjectName_DefaultsToGitHubRelease(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEmptyProjectName_DefaultsToGitLabRelease(t *testing.T) {
|
func TestEmptyProjectName_DefaultsToGitLabRelease(t *testing.T) {
|
||||||
|
_ = testlib.Mktmp(t)
|
||||||
ctx := testctx.NewWithCfg(config.Project{
|
ctx := testctx.NewWithCfg(config.Project{
|
||||||
Release: config.Release{
|
Release: config.Release{
|
||||||
GitLab: config.Repo{
|
GitLab: config.Repo{
|
||||||
@ -50,6 +55,7 @@ func TestEmptyProjectName_DefaultsToGitLabRelease(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEmptyProjectName_DefaultsToGiteaRelease(t *testing.T) {
|
func TestEmptyProjectName_DefaultsToGiteaRelease(t *testing.T) {
|
||||||
|
_ = testlib.Mktmp(t)
|
||||||
ctx := testctx.NewWithCfg(config.Project{
|
ctx := testctx.NewWithCfg(config.Project{
|
||||||
Release: config.Release{
|
Release: config.Release{
|
||||||
Gitea: config.Repo{
|
Gitea: config.Repo{
|
||||||
@ -62,7 +68,16 @@ func TestEmptyProjectName_DefaultsToGiteaRelease(t *testing.T) {
|
|||||||
require.Equal(t, "bar", ctx.Config.ProjectName)
|
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) {
|
func TestEmptyProjectNameAndRelease(t *testing.T) {
|
||||||
|
_ = testlib.Mktmp(t)
|
||||||
ctx := testctx.NewWithCfg(config.Project{
|
ctx := testctx.NewWithCfg(config.Project{
|
||||||
Release: config.Release{
|
Release: config.Release{
|
||||||
GitHub: config.Repo{},
|
GitHub: config.Repo{},
|
||||||
|
Reference in New Issue
Block a user