Skip to content

Commit

Permalink
Merge pull request #11 from asmaloney/find-goimports
Browse files Browse the repository at this point in the history
Look harder for goimports
  • Loading branch information
bbredesen committed Mar 28, 2023
2 parents 9040c56 + 850d4a7 commit c8fa475
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"go/build"
"io"
"io/fs"
"io/ioutil"
Expand Down Expand Up @@ -141,6 +142,13 @@ func main() {

vk1_0.Resolve(globalTypes, globalValues)

goimportsPath, err := findGoimports()
if err != nil {
logrus.
WithField("error", err.Error()).
Error("Could not find goimports")
}

commandCount := 0

for tc, reg := range vk1_0.FilterByCategory() {
Expand All @@ -151,7 +159,7 @@ func main() {
reg.ResolvedTypes["VK_DEFINE_HANDLE"].PushValue(globalValues["VK_NULL_HANDLE"])
}

printCategory(tc, reg, nil, 0)
printCategory(tc, reg, nil, 0, goimportsPath)
if tc == def.CatCommand {
commandCount += len(reg.ResolvedTypes)
}
Expand All @@ -166,8 +174,8 @@ func main() {
pf := plat.GeneratePlatformFeatures()
pf.Resolve(globalTypes, globalValues)

for tc, reg := range pf.FilterByCategory() {
printCategory(tc, reg, plat, commandCount)
for tc, reg := range pf.FilterByCategory() {
printCategory(tc, reg, plat, commandCount, goimportsPath)
if tc == def.CatCommand {
commandCount += len(reg.ResolvedTypes)
}
Expand All @@ -180,7 +188,7 @@ func main() {

const fileHeader string = "// Code generated by go-vk from %s at %s. DO NOT EDIT.\n\npackage vk\n\n" // fix doc/issue-1

func printCategory(tc def.TypeCategory, fc *feat.Feature, platform *feat.Platform, startingCount int) {
func printCategory(tc def.TypeCategory, fc *feat.Feature, platform *feat.Platform, startingCount int, goimportsPath string) {
if tc == def.CatInclude {
return
}
Expand Down Expand Up @@ -247,7 +255,7 @@ func printCategory(tc def.TypeCategory, fc *feat.Feature, platform *feat.Platfor

logrus.WithField("file", filename+".go").Info("Running goimports")

cmd := exec.Command("goimports", "-w", outpath)
cmd := exec.Command(goimportsPath, "-w", outpath)
e := &strings.Builder{}
cmd.Stderr = e

Expand Down Expand Up @@ -348,5 +356,27 @@ func copyStaticFiles() {
if err != nil {
panic(err)
}
}

func findGoimports() (path string, err error) {
// goimports is probably in GOPATH which may not be in the user's PATH
goPath := os.Getenv("GOPATH")
if goPath == "" {
// If GOPATH is not set, use go's default
goPath = build.Default.GOPATH
}

// There may be multiple paths, so split and add "/bin" to each
paths := strings.Split(goPath, string(os.PathListSeparator))
goPath = ""
for _, path := range paths {
goPath += fmt.Sprintf("%s%sbin%s", path, string(os.PathSeparator), string(os.PathListSeparator))
}

// Add PATH paths to the end
goPath += os.Getenv("PATH")

os.Setenv("PATH", goPath)

return exec.LookPath("goimports")
}

0 comments on commit c8fa475

Please sign in to comment.