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

Look harder for goimports #11

Merged
merged 2 commits into from
Mar 28, 2023
Merged
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
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")
}