From 03834f4cf2ec7d1b7ac02c76e551b281ba4a2116 Mon Sep 17 00:00:00 2001 From: Jeff Lindsay Date: Wed, 30 Aug 2023 10:44:49 -0500 Subject: [PATCH] generate: add findstruct tool (#209) * generate: add findstruct tool * docs: fix findstruct example * generate: add build ignore to tools --- docs/generation.md | 6 ++++ generate/tools/findstruct.go | 60 ++++++++++++++++++++++++++++++++++++ generate/tools/genmod.go | 2 ++ 3 files changed, 68 insertions(+) create mode 100644 generate/tools/findstruct.go diff --git a/docs/generation.md b/docs/generation.md index 63ea80f8..21119a23 100644 --- a/docs/generation.md +++ b/docs/generation.md @@ -219,3 +219,9 @@ Finds type symbol(s) in symbolsdb with the given type name. Can be used with `jq Re-generates frameworks that have been generated (have .gen.go files). `./generate/tools/regen.sh macos` + +### findstruct [struct-name] + +Looks through XCode SDK headers for a struct definition. Helpful to fill in struct definitions. + +`go run ./generate/tools/findstruct.go CLLocationCoordinate2D` diff --git a/generate/tools/findstruct.go b/generate/tools/findstruct.go new file mode 100644 index 00000000..2a849a93 --- /dev/null +++ b/generate/tools/findstruct.go @@ -0,0 +1,60 @@ +//go:build ignore + +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "os/exec" + "path/filepath" + "regexp" + "strings" +) + +func main() { + if len(os.Args) < 2 { + fmt.Println("Usage: findstruct ") + return + } + + cmd := exec.Command("xcrun", "--show-sdk-path") + b, err := cmd.CombinedOutput() + if err != nil { + log.Fatal(err) + } + + dir := fmt.Sprintf("%s/System/Library/Frameworks", strings.TrimSpace(string(b))) + structName := os.Args[1] + + // Naive regex to find C struct definitions + // It won't capture complex scenarios or structs split across lines. + re := regexp.MustCompile(`(?s)struct\s+` + structName + `\s+\{.*?\};`) + + filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if !info.IsDir() && (filepath.Ext(path) == ".h" || filepath.Ext(path) == ".c") { + file, err := os.Open(path) + if err != nil { + return err + } + defer file.Close() + + var content string + scanner := bufio.NewScanner(file) + for scanner.Scan() { + content += scanner.Text() + "\n" + } + + matches := re.FindAllString(content, -1) + for _, match := range matches { + fmt.Println(match) + } + } + return nil + }) +} diff --git a/generate/tools/genmod.go b/generate/tools/genmod.go index 8707d7f8..e261fac3 100644 --- a/generate/tools/genmod.go +++ b/generate/tools/genmod.go @@ -1,3 +1,5 @@ +//go:build ignore + package main import (