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

Commit

Permalink
Handle openapi for goyave v4
Browse files Browse the repository at this point in the history
  • Loading branch information
System-Glitch committed Aug 27, 2021
1 parent 0e1223a commit f3b22a3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
23 changes: 18 additions & 5 deletions internal/inject/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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)
}
}
Expand Down
7 changes: 6 additions & 1 deletion internal/inject/openapi.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package inject

import (
"github.com/Masterminds/semver"
"goyave.dev/gyv/internal/stub"
)

Expand All @@ -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)}

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.`,
Expand Down

0 comments on commit f3b22a3

Please sign in to comment.