Skip to content

Commit

Permalink
generate-gnostic: support goimports local prefix
Browse files Browse the repository at this point in the history
Signed-off-by: Koichi Shiraishi <[email protected]>
  • Loading branch information
zchee committed Sep 26, 2021
1 parent 7a88755 commit 1cca2a0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
20 changes: 14 additions & 6 deletions generate-gnostic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import (
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"runtime"
"strings"

"golang.org/x/tools/imports"

"github.com/google/gnostic/jsonschema"
)

Expand Down Expand Up @@ -199,13 +199,21 @@ func generateOpenAPIModel(version string) error {
log.Printf("Generating compiler support code")
compiler := cc.GenerateCompiler(goPackageName, License, packageImports)
goFileName := projectRoot + directoryName + "/" + filename + ".go"
err = ioutil.WriteFile(goFileName, []byte(compiler), 0644)

// format the compiler
log.Printf("Formatting compiler support code")
imports.LocalPrefix = "github.com/google/gnostic"
data, err := imports.Process(goFileName, []byte(compiler), &imports.Options{
TabWidth: 8,
TabIndent: true,
Comments: true,
Fragment: true,
})
if err != nil {
return err
}
// format the compiler
log.Printf("Formatting compiler support code")
return exec.Command(runtime.GOROOT()+"/bin/gofmt", "-w", goFileName).Run()

return ioutil.WriteFile(goFileName, []byte(data), 0644)
}

func usage() string {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ require (
github.com/golang/protobuf v1.5.2
github.com/kr/pretty v0.2.0 // indirect
github.com/stoewer/go-strcase v1.2.0
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/genproto v0.0.0-20201019141844-1ed22bb0c154
google.golang.org/protobuf v1.26.0
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15
Expand Down
4 changes: 3 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135 h1:5Beo0mZN8dRzgrMMkDp0jc8YXQKx9DiJ2k1dkvGsn5A=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
Expand Down

0 comments on commit 1cca2a0

Please sign in to comment.