Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds alias for the kebab cases of the original targets. #441

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 33 additions & 1 deletion mage/main.go
Expand Up @@ -609,6 +609,38 @@ func Compile(goos, goarch, ldflags, magePath, goCmd, compileTo string, gofiles [
return nil
}

var (
matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
)

// camelToKebab turns a camelcase string into a kebab case one.
func camelToKebab(s string) string {
ks := matchFirstCap.ReplaceAllString(s, "${1}-${2}")
ks = matchAllCap.ReplaceAllString(ks, "${1}-${2}")
return strings.ToLower(ks)
}

// backfillKebabAliases backfill the existing alias with those infered from camelcase targets.
func backfillKebabAliases(aliases map[string]*parse.Function, funcs []*parse.Function) map[string]*parse.Function {
newAliases := aliases
if newAliases == nil {
newAliases = make(map[string]*parse.Function)
}
for _, f := range funcs {
kebabAlias := camelToKebab(f.Name)
if kebabAlias == f.Name {
continue
}

if _, ok := newAliases[f.Name]; !ok {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't replace existing aliases.

newAliases[kebabAlias] = f
}
}

return newAliases
}

// GenerateMainfile generates the mage mainfile at path.
func GenerateMainfile(binaryName, path string, info *parse.PkgInfo) error {
debug.Println("Creating mainfile at", path)
Expand All @@ -621,7 +653,7 @@ func GenerateMainfile(binaryName, path string, info *parse.PkgInfo) error {
data := mainfileTemplateData{
Description: info.Description,
Funcs: info.Funcs,
Aliases: info.Aliases,
Aliases: backfillKebabAliases(info.Aliases, info.Funcs),
Imports: info.Imports,
BinaryName: binaryName,
}
Expand Down
46 changes: 46 additions & 0 deletions mage/main_test.go
Expand Up @@ -1731,6 +1731,24 @@ func TestNamespaceDefault(t *testing.T) {
func TestAliasToImport(t *testing.T) {
}

func TestAliasToKebabCase(t *testing.T) {
stdout := &bytes.Buffer{}
inv := Invocation{
Dir: "./testdata/kebabcase",
Stderr: io.Discard,
Stdout: stdout,
Args: []string{"fooBar", "foo-bar"},
}
code := Invoke(inv)
if code != 0 {
t.Fatalf("expected 0, but got %v", code)
}
expected := "FooBar\nFooBar\n"
if stdout.String() != expected {
t.Fatalf("expected %q, but got %q", expected, stdout.String())
}
}

func TestWrongDependency(t *testing.T) {
stderr := &bytes.Buffer{}
inv := Invocation{
Expand Down Expand Up @@ -1801,3 +1819,31 @@ func fileData(file string) (exeType, archSize, error) {
}
return -1, -1, fmt.Errorf("unrecognized executable format")
}

func TestCamelToKebab(t *testing.T) {
testCases := []struct {
camelCaseWord string
kebabWord string
}{
{
camelCaseWord: "FizzBazz",
kebabWord: "fizz-bazz",
},
{
camelCaseWord: "C",
kebabWord: "c",
},
{
camelCaseWord: "DoSomeHTTPStuff",
kebabWord: "do-some-http-stuff",
},
}

for _, tCase := range testCases {
t.Run(tCase.camelCaseWord, func(t *testing.T) {
if want, have := tCase.kebabWord, camelToKebab(tCase.camelCaseWord); want != have {
t.Errorf("unexpected transformation, want %q, have %q", want, have)
}
})
}
}
15 changes: 15 additions & 0 deletions mage/testdata/kebabcase/magefile.go
@@ -0,0 +1,15 @@
//go:build mage
// +build mage

package main

import (
"fmt"
)

// No default so we can check the list().

// Prints out 'FooBar'.
func FooBar() {
fmt.Println("FooBar")
}