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

Enable go.mod support #34

Merged
merged 4 commits into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
22 changes: 7 additions & 15 deletions convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,14 @@ type EnumValue struct {
}

func NewConvertPlugin(output, backend, frontend Config) plugin.Plugin {
return &ConvertPlugin{Output: output, Backend: backend, Frontend: frontend}
return &ConvertPlugin{Output: output, Backend: backend, Frontend: frontend, rootImportPath: getRootImportPath()}
}

type ConvertPlugin struct {
Output Config
Backend Config
Frontend Config
Output Config
Backend Config
Frontend Config
rootImportPath string
}

type Config struct {
Expand All @@ -148,15 +149,6 @@ func copyConfig(cfg config.Config) *config.Config {
return &cfg
}

func getGoImportFromFile(dir string) string {
longPath, err := filepath.Abs(dir)
if err != nil {
fmt.Println("error while trying to convert folder to gopath", err)
}
// src/Users/.../go/src/gitlab.com/.../app/backend/graphql_models
return strings.TrimPrefix(pathRegex.FindString(longPath), "src/")
}

func GetModelsWithInformation(enums []*Enum, cfg *config.Config, boilerModels []*BoilerModel) []*Model {

// get models based on the schema and sqlboiler structs
Expand All @@ -180,11 +172,11 @@ func (m *ConvertPlugin) MutateConfig(originalCfg *config.Config) error {
b := &ModelBuild{
PackageName: m.Output.PackageName,
Backend: Config{
Directory: getGoImportFromFile(m.Backend.Directory),
Directory: path.Join(m.rootImportPath, m.Backend.Directory),
PackageName: m.Backend.PackageName,
},
Frontend: Config{
Directory: getGoImportFromFile(m.Frontend.Directory),
Directory: path.Join(m.rootImportPath, m.Frontend.Directory),
PackageName: m.Frontend.PackageName,
},
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ require (
github.com/pkg/errors v0.9.1
github.com/vektah/gqlparser/v2 v2.0.1
github.com/web-ridge/go-pluralize v0.1.5
golang.org/x/mod v0.3.0 // indirect
golang.org/x/tools v0.0.0-20200507205054-480da3ebd79c
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand Down
98 changes: 98 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package gqlgen_sqlboiler

import (
"fmt"
"golang.org/x/mod/modfile"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
)

func getRootImportPath() string {
importPath, err := rootImportPath()
if err != nil {
fmt.Printf("error while getting root import path %v", err)
return ""
}
return importPath
}

func getGoImportFromFile(dir string) string {
dir = strings.TrimPrefix(dir, "/")
importPath, err := rootImportPath()
if err != nil {
fmt.Printf("error while getting root import path %v", err)
return ""
}
return path.Join(importPath, dir)
}

func rootImportPath() (string, error) {
projectPath, err := getWorkingPath()
if err != nil {
// TODO: adhering to your original error handling
// should consider doing something here rather than continuing
// since this step occurs during generation, panicing or fatal error should be okay
return "", fmt.Errorf("error while getting working directory %w", err)
}
if hasGoMod(projectPath) {
modulePath, err := getModulePath(projectPath)
if err != nil {
// TODO: adhering to your original error handling
// should consider doing something here rather than continuing
// since this step occurs during generation, panicing or fatal error should be okay
return "", fmt.Errorf("error while getting module path %w", err)
}
return modulePath, nil
}

return gopathImport(projectPath), nil
}
func getProjectPath(dir string) (string, error) {
longPath, err := filepath.Abs(dir)
if err != nil {
return "", fmt.Errorf("error while trying to convert folder to gopath %w", err)
}
return strings.TrimSuffix(longPath, dir), nil
}

// getWorkingPath gets the current working directory
func getWorkingPath() (string, error) {
wd, err := os.Getwd()
if err != nil {
return "", err
}
return wd, nil
}
func hasGoMod(projectPath string) bool {
filePath := path.Join(projectPath, "go.mod")
return fileExists(filePath)
}

func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}

func getModulePath(projectPath string) (string, error) {
filePath := path.Join(projectPath, "go.mod")
file, err := ioutil.ReadFile(filePath)
if err != nil {
return "", fmt.Errorf("error while trying to read go mods path %w", err)
}

modPath := modfile.ModulePath(file)
if modPath == "" {
return "", fmt.Errorf("could not determine mod path \n")
}
return modPath, nil
}

func gopathImport(dir string) string {
return strings.TrimPrefix(pathRegex.FindString(dir), "src/")
}
29 changes: 29 additions & 0 deletions helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package gqlgen_sqlboiler

import "testing"

func Test_gopathImport(t *testing.T) {
type args struct {
dir string
}
tests := []struct {
name string
args args
want string
}{
{
name: "in GOPATH",
args: args{
dir: "/Users/someonefamous/go/src/github.com/someonefamous/famous-project",
},
want: "github.com/someonefamous/famous-project",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := gopathImport(tt.args.dir); got != tt.want {
t.Errorf("gopathImport() = %v, want %v", got, tt.want)
}
})
}
}
18 changes: 10 additions & 8 deletions resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gqlgen_sqlboiler
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"

Expand All @@ -16,14 +17,15 @@ import (
)

func NewResolverPlugin(output, backend, frontend Config, authImport string) plugin.Plugin {
return &ResolverPlugin{output: output, backend: backend, frontend: frontend, authImport: authImport}
return &ResolverPlugin{output: output, backend: backend, frontend: frontend, authImport: authImport, rootImportPath: getRootImportPath()}
}

type ResolverPlugin struct {
output Config
backend Config
frontend Config
authImport string
output Config
backend Config
frontend Config
authImport string
rootImportPath string
}

var _ plugin.CodeGenerator = &ResolverPlugin{}
Expand Down Expand Up @@ -61,16 +63,16 @@ func (m *ResolverPlugin) generateSingleFile(data *codegen.Data, models []*Model,

file.imports = append(file.imports, Import{
Alias: ".",
ImportPath: getGoImportFromFile(m.output.Directory),
ImportPath: path.Join(m.rootImportPath, m.output.Directory),
})

file.imports = append(file.imports, Import{
Alias: "dm",
ImportPath: getGoImportFromFile(m.backend.Directory),
ImportPath: path.Join(m.rootImportPath, m.backend.Directory),
})
file.imports = append(file.imports, Import{
Alias: "fm",
ImportPath: getGoImportFromFile(m.frontend.Directory),
ImportPath: path.Join(m.rootImportPath, m.frontend.Directory),
})

if m.authImport != "" {
Expand Down