Skip to content

Commit 03834f4

Browse files
authored
generate: add findstruct tool (#209)
* generate: add findstruct tool * docs: fix findstruct example * generate: add build ignore to tools
1 parent f68ee9a commit 03834f4

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

docs/generation.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,9 @@ Finds type symbol(s) in symbolsdb with the given type name. Can be used with `jq
219219
Re-generates frameworks that have been generated (have .gen.go files).
220220

221221
`./generate/tools/regen.sh macos`
222+
223+
### findstruct [struct-name]
224+
225+
Looks through XCode SDK headers for a struct definition. Helpful to fill in struct definitions.
226+
227+
`go run ./generate/tools/findstruct.go CLLocationCoordinate2D`

generate/tools/findstruct.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//go:build ignore
2+
3+
package main
4+
5+
import (
6+
"bufio"
7+
"fmt"
8+
"log"
9+
"os"
10+
"os/exec"
11+
"path/filepath"
12+
"regexp"
13+
"strings"
14+
)
15+
16+
func main() {
17+
if len(os.Args) < 2 {
18+
fmt.Println("Usage: findstruct <struct_name>")
19+
return
20+
}
21+
22+
cmd := exec.Command("xcrun", "--show-sdk-path")
23+
b, err := cmd.CombinedOutput()
24+
if err != nil {
25+
log.Fatal(err)
26+
}
27+
28+
dir := fmt.Sprintf("%s/System/Library/Frameworks", strings.TrimSpace(string(b)))
29+
structName := os.Args[1]
30+
31+
// Naive regex to find C struct definitions
32+
// It won't capture complex scenarios or structs split across lines.
33+
re := regexp.MustCompile(`(?s)struct\s+` + structName + `\s+\{.*?\};`)
34+
35+
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
36+
if err != nil {
37+
return err
38+
}
39+
40+
if !info.IsDir() && (filepath.Ext(path) == ".h" || filepath.Ext(path) == ".c") {
41+
file, err := os.Open(path)
42+
if err != nil {
43+
return err
44+
}
45+
defer file.Close()
46+
47+
var content string
48+
scanner := bufio.NewScanner(file)
49+
for scanner.Scan() {
50+
content += scanner.Text() + "\n"
51+
}
52+
53+
matches := re.FindAllString(content, -1)
54+
for _, match := range matches {
55+
fmt.Println(match)
56+
}
57+
}
58+
return nil
59+
})
60+
}

generate/tools/genmod.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build ignore
2+
13
package main
24

35
import (

0 commit comments

Comments
 (0)