-
Notifications
You must be signed in to change notification settings - Fork 63
/
generate_objcbridge.go
82 lines (65 loc) · 1.78 KB
/
generate_objcbridge.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//go:build generate
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
"text/template"
)
const objcbridgeH = `void SetMyApplicationDelegate();`
const objcbridgeM = `#import <Cocoa/Cocoa.h>
// Forward declaration of the Go function to be called from C
extern void GetOpeningFilepath(char* str);
@interface MyApplicationDelegate : NSObject <NSApplicationDelegate>
@end
@implementation MyApplicationDelegate
- (BOOL)applicationSupportsSecureRestorableState:(NSApplication *)app {
return YES;
}
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename {
const char *utf8String = [filename UTF8String];
char *cStr = strdup(utf8String);
GetOpeningFilepath(cStr);
return YES;
}
@end
void SetMyApplicationDelegate() {
NSApplication *app = [NSApplication sharedApplication];
app.delegate = [[MyApplicationDelegate alloc] init];
[app activateIgnoringOtherApps:YES]; // make application foreground
}`
func main() {
if runtime.GOOS != "darwin" {
return
}
generateFile(filepath.Join("editor", "objcbridge.h"), objcbridgeH)
generateFile(filepath.Join("editor", "objcbridge.m"), objcbridgeM)
}
func generateFile(filename, content string) {
if _, err := os.Stat(filename); err == nil {
err = os.Remove(filename)
if err != nil {
fmt.Println("Error removing existing file:", err)
return
}
} else if !os.IsNotExist(err) {
fmt.Println("Error checking file existence:", err)
return
}
file, err := os.Create(filename)
if err != nil {
fmt.Println("Error creating file:", err)
return
}
defer file.Close()
tmpl, err := template.New("file").Parse(content)
if err != nil {
fmt.Println("Error parsing template:", err)
return
}
err = tmpl.Execute(file, nil)
if err != nil {
fmt.Println("Error executing template:", err)
}
}