Skip to content

Commit

Permalink
generate: add findstruct tool (#209)
Browse files Browse the repository at this point in the history
* generate: add findstruct tool

* docs: fix findstruct example

* generate: add build ignore to tools
  • Loading branch information
progrium authored Aug 30, 2023
1 parent f68ee9a commit 03834f4
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/generation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
60 changes: 60 additions & 0 deletions generate/tools/findstruct.go
Original file line number Diff line number Diff line change
@@ -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 <struct_name>")
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
})
}
2 changes: 2 additions & 0 deletions generate/tools/genmod.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

package main

import (
Expand Down

0 comments on commit 03834f4

Please sign in to comment.