forked from go-qamel/qamel
/
deps-qml.go
210 lines (175 loc) · 4.87 KB
/
deps-qml.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package generator
import (
"bufio"
"fmt"
"os"
fp "path/filepath"
"regexp"
"strings"
"github.com/MrKich/qamel/internal/config"
)
var rxQmlImport = regexp.MustCompile(`^import\s+(Qt\S+)\s(\d)+.*$`)
// copyQmlDependencies copies QML dependencies to output dir
func copyQmlDependencies(qtQmlDir string, profile config.Profile, projectDir, outputDir string) error {
// Get list of dir to check
var dirToCheck []string
projectResDir := fp.Join(projectDir, "res")
if dirExists(projectResDir) {
dirToCheck = append(dirToCheck, projectResDir)
}
// Get list of QML dependencies
mapDependencies := map[string]struct{}{}
dirAlreadyChecked := map[string]struct{}{}
for {
// Keep checking until there are no directory left
if len(dirToCheck) == 0 {
break
}
// If this dir has been checked before, skip
dirPath := dirToCheck[0]
if _, checked := dirAlreadyChecked[dirPath]; checked {
dirToCheck = dirToCheck[1:]
continue
}
// Fetch qml dependencies from the directory
qmlDeps, err := getQmlDependenciesFromDir(qtQmlDir, dirPath)
if err != nil {
return err
}
for _, qmlDep := range qmlDeps {
mapDependencies[qmlDep] = struct{}{}
dirToCheck = append(dirToCheck, qmlDep)
}
// Save this dir as been already checked, then move to next
dirAlreadyChecked[dirPath] = struct{}{}
dirToCheck = dirToCheck[1:]
}
// Copy all dependency libs to output dir
dstQmlDir := outputDir
if profile.OS != "windows" {
dstQmlDir = fp.Join(dstQmlDir, "qml")
}
for qmlPath := range mapDependencies {
parentExists := false
qmlPathParts := fp.SplitList(qmlPath)
for i := 0; i <= len(qmlPathParts)-1; i++ {
parentPath := fp.Join(qmlPathParts[:i]...)
if _, exist := mapDependencies[parentPath]; exist {
parentExists = true
break
}
}
if parentExists {
continue
}
dirName, err := fp.Rel(qtQmlDir, qmlPath)
if err != nil {
return err
}
err = copyDir(qmlPath, fp.Join(dstQmlDir, dirName), func(path string, info os.FileInfo) bool {
fileExt := fp.Ext(info.Name())
if fileExt == ".qmlc" || fileExt == ".jsc" {
return true
}
if strings.HasSuffix(info.Name(), "d.dll") {
tmpPath := strings.TrimSuffix(path, "d.dll")
tmpPath += ".dll"
if fileExists(tmpPath) {
return true
}
}
return false
})
if err != nil {
return err
}
}
return nil
}
// getQmlDependenciesFromDir gets all QML imports from *.qml files
// inside specified directory
func getQmlDependenciesFromDir(qtQmlDir string, srcDir string) ([]string, error) {
// Make sure dir existst
if !dirExists(srcDir) {
return nil, fmt.Errorf("directory %s doesn't exist", srcDir)
}
// Fetch each QML file inside the specified dir
var qmlFiles []string
fp.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() && fp.Ext(info.Name()) == ".qml" {
qmlFiles = append(qmlFiles, path)
}
return nil
})
// Get QML dependencies from each file
mapDependencies := map[string]struct{}{}
for _, qmlFile := range qmlFiles {
deps, err := getQmlDependenciesFromFile(qtQmlDir, qmlFile)
if err != nil {
return nil, err
}
for _, dep := range deps {
mapDependencies[dep] = struct{}{}
}
}
// Convert map to arrays
var results []string
for depName := range mapDependencies {
results = append(results, depName)
}
return results, nil
}
// getQmlDependenciesFromFile gets all QML imports from *.qml files
// from the specified file
func getQmlDependenciesFromFile(qtQmlDir string, qmlFilePath string) ([]string, error) {
// Open QML file
qmlFile, err := os.Open(qmlFilePath)
if err != nil {
return nil, err
}
defer qmlFile.Close()
// Read each line from the file
var results []string
scanner := bufio.NewScanner(qmlFile)
for scanner.Scan() {
// Use regex to find import line
line := scanner.Text()
line = strings.TrimSpace(line)
matches := rxQmlImport.FindStringSubmatch(line)
// If regex doesn't match, skip
if len(matches) != 3 {
continue
}
// Find possible directory for that QML import from Qt's QML dir
// For example, "import QtQuick.Controls 2.11" might means the QML files located in :
// - qtQmlDir/QtQuick/Controls.2
// - qtQmlDir/QtQuick.2/Controls
// Or the version number might be not used afterall
// - qtQmlDir/QtQuick/Controls
name := matches[1]
nameParts := strings.Split(name, ".")
possibleDir := ""
version := "." + matches[2]
for i := len(nameParts) - 1; i >= 0; i-- {
tmp := make([]string, len(nameParts))
copy(tmp, nameParts)
tmp[i] += version
tmpDir := fp.Join(tmp...)
tmpDir = fp.Join(qtQmlDir, tmpDir)
if dirExists(tmpDir) {
possibleDir = tmpDir
break
}
}
if possibleDir != "" {
results = append(results, possibleDir)
} else {
possibleDir := fp.Join(nameParts...)
possibleDir = fp.Join(qtQmlDir, possibleDir)
if dirExists(possibleDir) {
results = append(results, possibleDir)
}
}
}
return results, nil
}