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

generate: add findstruct tool #209

Merged
merged 3 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
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
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
Loading