diff --git a/experiments/windowsconstgen.go b/experiments/windowsconstgen.go index a1411aa..213747d 100644 --- a/experiments/windowsconstgen.go +++ b/experiments/windowsconstgen.go @@ -8,6 +8,7 @@ import ( "go/token" "go/ast" "go/parser" + "sort" ) func getPackage(path string) (pkg *ast.Package) { @@ -68,6 +69,11 @@ func gatherNames(pkg *ast.Package) { } } +func preamble(pkg string) string { + return "// autogenerated by windowsconstgen; do not edit\n" + + "package " + pkg + "\n" +} + func main() { if len(os.Args) != 2 { panic("usage: " + os.Args[0] + " path") @@ -84,7 +90,25 @@ func main() { panic(s) } + // keep sorted for git + consts := make([]string, 0, len(known)) + for ident, kind := range known { - fmt.Printf("%-30s %s\n", ident, kind) + if kind == "const" || kind == "var" { + consts = append(consts, ident) + } } + sort.Strings(consts) + + fmt.Printf("%s" + + "import \"fmt\"\n" + + "// #include \n" + + "import \"C\"\n" + + "func main() {\n" + + " fmt.Println(%q)\n", + preamble("main"), preamble("ui")) + for _, ident := range consts { + fmt.Printf(" fmt.Println(\"const %s =\", C.%s)\n", ident, ident[1:]) + } + fmt.Printf("}\n") }