From f3b22a30e621737c729c1c00d12c278f58fe2856 Mon Sep 17 00:00:00 2001 From: SystemGlitch Date: Fri, 27 Aug 2021 14:19:56 +0200 Subject: [PATCH] Handle openapi for goyave v4 --- internal/inject/inject.go | 23 ++++++++++++++++++----- internal/inject/openapi.go | 7 ++++++- main.go | 2 +- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/internal/inject/inject.go b/internal/inject/inject.go index dc68d6d..6018f62 100644 --- a/internal/inject/inject.go +++ b/internal/inject/inject.go @@ -33,6 +33,15 @@ type FunctionCall struct { Value string } +// Dependency a library that needs to be imported for the planned injection. +type Dependency struct { + // Name of the dependency (e.g.: "goyave.dev/goyave/v4") + Name string + + // Version of the dependency (e.g.: "v4.0.0"). Leave empty for latest. + Version string +} + // Injector code injector for Goyave projects. Builds a temporary source file at // the project's root, build the project in plugin mode and return a Plugin instance. type Injector struct { @@ -50,7 +59,7 @@ type Injector struct { // Dependencies list of libraries that need to be imported // for the planned injection. These libraries will be added // automatically using "go get" and removed after the build is complete. - Dependencies []string + Dependencies []Dependency } // NewInjector create a new injector for the project in the given directory. @@ -120,7 +129,11 @@ func (i *Injector) build(output string) error { }() for _, d := range dependencies { - if err := i.executeCommand("go", "get", d); err != nil { + dep := d.Name + if d.Version != "" { + dep += "@" + d.Version + } + if err := i.executeCommand("go", "get", dep); err != nil { return err } } @@ -132,10 +145,10 @@ func (i *Injector) build(output string) error { return cmd.Run() } -func (i *Injector) getDependencies() []string { - dependencies := make([]string, 0, len(i.Dependencies)) +func (i *Injector) getDependencies() []Dependency { + dependencies := make([]Dependency, 0, len(i.Dependencies)) for _, d := range i.Dependencies { - if mod.FindDependency(i.ModFile, d) == nil { + if mod.FindDependency(i.ModFile, d.Name) == nil { dependencies = append(dependencies, d) } } diff --git a/internal/inject/openapi.go b/internal/inject/openapi.go index bbc52e1..404626a 100644 --- a/internal/inject/openapi.go +++ b/internal/inject/openapi.go @@ -1,6 +1,7 @@ package inject import ( + "github.com/Masterminds/semver" "goyave.dev/gyv/internal/stub" ) @@ -17,7 +18,11 @@ func OpenAPI3Generator(directory string) (func() ([]byte, error), error) { return nil, err } - injector.Dependencies = append(injector.Dependencies, "goyave.dev/openapi3") + libVersion := "" + if c, _ := semver.NewConstraint("< v4.0.0-rc1"); c.Check(injector.GoyaveVersion) { + libVersion = "v0.1.0" + } + injector.Dependencies = append(injector.Dependencies, Dependency{"goyave.dev/openapi3", libVersion}) injector.StubName = stub.InjectOpenAPI injector.StubData = stub.Data{"RouteRegistrerImportPath": ImportToString(call.Package)} diff --git a/main.go b/main.go index 5ed32fa..92aaac4 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,7 @@ import ( func buildRootCommand() *cobra.Command { gyv := &cobra.Command{ Use: "gyv", - Version: "0.2.1", // TODO use ldflags to set version at compile-time + Version: "0.2.2", // TODO use ldflags to set version at compile-time Short: "Productivity CLI for the Goyave framework", Long: `gyv productivity command-line interface for the Goyave framework. All commands can be run either in interactive mode or using POSIX flags.`,