Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Extracting module name from go.mod (#1354)
Browse files Browse the repository at this point in the history
* V0.12.7 (#1349)

* Update Bootstrap version

https://nvd.nist.gov/vuln/detail/CVE-2018-14041

* update Dockerfile and Makefile; removed go.*

* updated to use release

* version bump: v0.12.7-rc.1

* generated goreleaser

* version bump: v0.12.7-beta.1

* fixed releaser template

* generated goreleaser

* fixed broken deps

* fixed a few lint issues

* clean up packr files before gometalinter sees them

* fixes CloseNotify lint issue

* touch router_test

* use the development image

* fixes appveyor

* compile in sqlite

* removed stderr from plugin check

* updated deps

* removed Gopkg.toml

* fixes #750

* package logo.svg into binary

* version bump: v0.12.7

* generated goreleaser

* fixed tap

* generated goreleaser

* fixes broken Dockerfile.build on master

* extracting module from go.mod

* removing start and end from regexp to capture the module name

* modifying comment

* fixing go.mod

* fixing dockerfgile

* vadding version.go changes

* fixing merges pending

* fixing missing conflicts

* adding tests for the modules package name

* adding other 2 tests

* adding testss for empty go.mod
  • Loading branch information
paganotoni authored and markbates committed Oct 8, 2018
1 parent 2365c0c commit 1cd0d04
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 11 deletions.
31 changes: 31 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Code generated by github.com/gobuffalo/release. DO NOT EDIT.
# Edit .goreleaser.yml.plush instead

builds:
-
goos:
- darwin
- linux
- windows
env:
- CGO_ENABLED=0
main: ./buffalo/main.go

checksum:
name_template: 'checksums.txt'

snapshot:
name_template: "{{ .Tag }}-next"

changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'

brew:
github:
owner: gobuffalo
name: homebrew-tap

3 changes: 3 additions & 0 deletions generators/assets/standard/a_standard-packr.go

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e h1:MZM7FHLqUHYI0Y/mQAt
github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041 h1:llrF3Fs4018ePo4+G/HV/uQUqEI1HMDjCeOf2V6puPc=
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ=
github.com/shurcooL/graphql v0.0.0-20180514000029-62c9ce094e75/go.mod h1:AuYgA5Kyo4c7HfUmvRGs/6rGlMMV/6B1bVnB9JxJEEg=
github.com/shurcooL/highlight_diff v0.0.0-20170515013008-09bb4053de1b h1:vYEG87HxbU6dXj5npkeulCS96Dtz5xg3jcfCgpcvbIw=
github.com/shurcooL/highlight_diff v0.0.0-20170515013008-09bb4053de1b/go.mod h1:ZpfEhSmds4ytuByIcDnOLkTHGUI6KNqRNPDLHDk+mUU=
github.com/shurcooL/highlight_go v0.0.0-20170515013102-78fb10f4a5f8 h1:xLQlo0Ghg8zBaQi+tjpK+z/WLjbg/BhAWP9pYgqo/LQ=
Expand Down
40 changes: 30 additions & 10 deletions meta/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"runtime"
"strings"

Expand Down Expand Up @@ -71,16 +72,7 @@ func New(root string) App {

// Gather meta data
name := inflect.Name(filepath.Base(root))

pp := envy.CurrentPackage()
if filepath.Base(pp) != string(name) {
pp = path.Join(pp, string(name))
}
if modsOn {
if !strings.HasPrefix(pwd, filepath.Join(envy.GoPath(), "src")) {
pp = name.String()
}
}
pp := resolvePackageName(name, pwd, modsOn)

app := App{
Pwd: pwd,
Expand Down Expand Up @@ -130,6 +122,34 @@ func New(root string) App {
return app
}

func resolvePackageName(name inflect.Name, pwd string, modsOn bool) string {
result := envy.CurrentPackage()

if filepath.Base(result) != string(name) {
result = path.Join(result, string(name))
}

if modsOn {
if !strings.HasPrefix(pwd, filepath.Join(envy.GoPath(), "src")) {
result = name.String()
}

//Extract package from go.mod
if f, err := os.Open(filepath.Join(pwd, "go.mod")); err == nil {
if s, err := ioutil.ReadAll(f); err == nil {
re := regexp.MustCompile("module (.*)")
res := re.FindAllStringSubmatch(string(s), 1)

if len(res) == 1 && len(res[0]) == 2 {
result = res[0][1]
}
}
}
}

return result
}

// ResolveSymlinks takes a path and gets the pointed path
// if the original one is a symlink.
func ResolveSymlinks(p string) string {
Expand Down
42 changes: 42 additions & 0 deletions meta/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package meta

import (
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/gobuffalo/envy"
"github.com/stretchr/testify/require"
)

func Test_ModulesPackageName(t *testing.T) {
r := require.New(t)
tmp := os.TempDir()
modsOn = true

r.NoError(os.Chdir(tmp))

tcases := []struct {
Content string
PackageName string
}{
{"module github.com/wawandco/zekito", "github.com/wawandco/zekito"},
{"module zekito", "zekito"},
{"module gopkg.in/some/friday.v2", "gopkg.in/some/friday.v2"},
{"", "zekito"},
}

for _, tcase := range tcases {
envy.Set("GOPATH", tmp)

t.Run(tcase.Content, func(st *testing.T) {
r := require.New(st)

r.NoError(ioutil.WriteFile("go.mod", []byte(tcase.Content), 0644))

a := New(filepath.Join(tmp, "zekito"))
r.Equal(tcase.PackageName, a.PackagePkg)
})
}
}

0 comments on commit 1cd0d04

Please sign in to comment.