forked from stephenlyu/goqtuic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (83 loc) · 2.12 KB
/
main.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"flag"
"os"
"io/ioutil"
"path/filepath"
"strings"
"github.com/stephenlyu/goqtuic/parser"
"fmt"
)
func translateUIFile(uiFile string, destDir string, testGoFile string) error {
base := filepath.Base(uiFile)
destDir, _ = filepath.Abs(filepath.Clean(destDir))
goFile := filepath.Join(destDir, strings.Replace(base, ".", "_", -1) + ".go")
// 检查文件更新时间,如果go文件时间比ui文件晚,就不重新生成
goStat, err := os.Stat(goFile)
if err == nil {
uiStat, err := os.Stat(uiFile)
if err != nil {
return err
}
if goStat.ModTime().UnixNano() > uiStat.ModTime().UnixNano() {
return nil
}
}
fmt.Printf("Translating %s...\n", uiFile)
packageName := filepath.Base(destDir)
err, compiler := parser.NewCompiler(uiFile)
if err != nil {
return err
}
compiler.Parse()
err = compiler.GenerateCode(packageName, goFile)
if err != nil {
return err
}
if testGoFile != "" {
var genPackage string
goPath := filepath.Clean(os.Getenv("GOPATH"))
if goPath != "" {
sourcePath := filepath.Join(goPath, "src")
if strings.HasPrefix(destDir, sourcePath) {
genPackage = destDir[len(sourcePath):]
if genPackage[0] == filepath.Separator {
genPackage = genPackage[1:]
}
if genPackage[len(genPackage) - 1] == filepath.Separator {
genPackage = genPackage[:len(genPackage) - 1]
}
}
}
return compiler.GenerateTestCode(testGoFile, genPackage)
}
return nil
}
func main() {
uiFile := flag.String("ui-file", "ui", "QT Designer ui file or directory")
uiGoDir := flag.String("go-ui-dir", "uigen", "Generated ui go files directory")
testGoFile := flag.String("go-test-file", "", "Test go file path")
flag.Parse()
stat, err := os.Stat(*uiFile)
if err != nil {
panic(err)
}
if stat.IsDir() {
files, err := ioutil.ReadDir(*uiFile)
if err != nil {
panic(err)
}
for _, f := range files {
if f.IsDir() {
continue
}
if filepath.Ext(f.Name()) != ".ui" {
continue
}
filePath := filepath.Join(*uiFile, f.Name())
translateUIFile(filePath, *uiGoDir, "")
}
} else {
translateUIFile(*uiFile, *uiGoDir, *testGoFile)
}
}