Skip to content

Commit

Permalink
Merge pull request #257 from qor/qor_view_path_in_gomod
Browse files Browse the repository at this point in the history
check pkg/mod, then check gopath/src
  • Loading branch information
raven-chen authored Jun 18, 2021
2 parents 99728c8 + e665b4f commit 422fa7d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ adm.GetRouter().PrintRoutes()
QOR was developed before go mod was introduced. So it still support go path while finding its template files. The priority is

1. check vendor, if not found
2. check $GOPATH/src/github.com/qor/admin/views, if still not found
3. load view path from $GOPATH/pkg/mod/github.com/qor/[email protected]/views. the version would be detected automatically by you go.sum file
2. check $GOPATH/pkg/mod/github.com/qor/[email protected]/views. the version would be detected automatically by your go.mod file, if still not found
3. load view path from $GOPATH/src/github.com/qor/admin/views


So if you want to use the template under the pkg/mod, make sure $GOPATH/src/github.com/qor/admin is absent.

Expand Down
13 changes: 8 additions & 5 deletions admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package admin

import (
"html/template"
"log"
"path/filepath"
"reflect"
"strings"

"github.com/jinzhu/gorm"
"github.com/jinzhu/inflection"
Expand Down Expand Up @@ -109,18 +109,21 @@ func (admin *Admin) SetAssetFS(assetFS assetfs.Interface) {

// RegisterViewPath register view path for admin
func (admin *Admin) RegisterViewPath(pth string) {
if admin.AssetFS.RegisterPath(filepath.Join(utils.AppRoot, "vendor", pth)) != nil {
var err error
if err = admin.AssetFS.RegisterPath(filepath.Join(utils.AppRoot, "vendor", pth)); err != nil {
for _, gopath := range utils.GOPATH() {
if admin.AssetFS.RegisterPath(filepath.Join(gopath, "src", pth)) == nil {
if err = admin.AssetFS.RegisterPath(filepath.Join(gopath, getDepVersionFromMod(pth))); err == nil {
break
}

pth = strings.TrimSuffix(pth, "/views")
if admin.AssetFS.RegisterPath(filepath.Join(gopath, getDepVersionFromMod(pth), "views")) == nil {
if err = admin.AssetFS.RegisterPath(filepath.Join(gopath, "src", pth)); err == nil {
break
}
}
}
if err != nil {
log.Printf("RegisterViewPathError: %s %s!", pth, err.Error())
}
}

// RegisterMetaConfigor register configor for a kind, it will be called when register those kind of metas
Expand Down
18 changes: 13 additions & 5 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package admin
import (
"html/template"
"io/ioutil"
"log"
"path/filepath"
"reflect"
"strings"
Expand Down Expand Up @@ -40,20 +41,23 @@ type I18n interface {
// RegisterViewPath register view path for all assetfs
func RegisterViewPath(pth string) {
globalViewPaths = append(globalViewPaths, pth)

var err error
for _, assetFS := range globalAssetFSes {
if assetFS.RegisterPath(filepath.Join(utils.AppRoot, "vendor", pth)) != nil {
if err = assetFS.RegisterPath(filepath.Join(utils.AppRoot, "vendor", pth)); err != nil {
for _, gopath := range utils.GOPATH() {
if assetFS.RegisterPath(filepath.Join(gopath, "src", pth)) == nil {
if err = assetFS.RegisterPath(filepath.Join(gopath, getDepVersionFromMod(pth))); err == nil {
break
}
pth = strings.TrimSuffix(pth, "/views")
if assetFS.RegisterPath(filepath.Join(gopath, getDepVersionFromMod(pth), "views")) == nil {

if err = assetFS.RegisterPath(filepath.Join(gopath, "src", pth)); err == nil {
break
}
}
}
}
if err != nil {
log.Printf("RegisterViewPathError: %s %s!", pth, err.Error())
}
}

func equal(a, b interface{}) bool {
Expand All @@ -73,5 +77,9 @@ func getDepVersionFromMod(pth string) string {
}
}

if strings.LastIndex(pth, "/") == -1 {
return pth
}

return getDepVersionFromMod(pth[:strings.LastIndex(pth, "/")]) + pth[strings.LastIndex(pth, "/"):]
}
5 changes: 2 additions & 3 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package admin

import (
"fmt"
"strings"
"testing"
)

Expand All @@ -23,10 +22,10 @@ func TestGetDepVersionFromMod(t *testing.T) {
{view: "github.com/qor/publish2/views", want: "pkg/mod/github.com/qor/[email protected]/views"},
{view: "github.com/qor/media/media_library/views", want: "pkg/mod/github.com/qor/[email protected]/media_library/views"},
{view: "github.com/qor/i18n/exchange_actions/views", want: "pkg/mod/github.com/qor/[email protected]/exchange_actions/views"},
{view: "no/unknown/nonexistent", want: "no/unknown/nonexistent"},
}
for _, v := range cases {
pth := strings.TrimSuffix(v.view, "/views")
if got := getDepVersionFromMod(pth) + "/views"; v.want != got {
if got := getDepVersionFromMod(v.view); v.want != got {
t.Errorf("GetDepVersionFromMod-viewpath: %v, want: %v, got: %v", v.view, v.want, got)
} else {
fmt.Println(got)
Expand Down

0 comments on commit 422fa7d

Please sign in to comment.