/
profile-setup.go
277 lines (239 loc) · 6.93 KB
/
profile-setup.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package cmd
import (
"bufio"
"fmt"
"os"
fp "path/filepath"
"runtime"
"strings"
"github.com/go-qamel/qamel/internal/config"
"github.com/spf13/cobra"
)
func profileSetupCmd() *cobra.Command {
return &cobra.Command{
Use: "setup [profileName]",
Short: "Set up a new or existing profile",
Args: cobra.MaximumNArgs(1),
Run: profileSetupHandler,
}
}
func profileSetupHandler(cmd *cobra.Command, args []string) {
// Get profile name
profileName := "default"
if len(args) > 0 {
profileName = args[0]
}
// Define input reader
reader := bufio.NewReader(os.Stdin)
// Fetch target OS
cBlueBold.Println("Thanks for using qamel, QML's binding for Go.")
fmt.Println()
fmt.Println("Please specify the target OS for this profile. " +
`Possible values are "windows", "linux" and "darwin". ` + "\n" +
"Keep it empty to use your system OS.")
fmt.Println()
cCyanBold.Printf("Target OS (default %s) : ", runtime.GOOS)
targetOS, _ := reader.ReadString('\n')
targetOS = strings.TrimSpace(targetOS)
if targetOS == "" {
targetOS = runtime.GOOS
}
switch targetOS {
case "linux", "windows", "darwin":
default:
cRedBold.Printf("OS %s is not supported\n", targetOS)
os.Exit(1)
}
// Fetch target architecture
fmt.Println()
fmt.Println("Please specify the target architecture for this profile. " +
`Possible values are "386" and "amd64".` + "\n" +
"Keep it empty to use your system architecture.")
fmt.Println()
cCyanBold.Printf("Target arch (default %s) : ", runtime.GOARCH)
targetArch, _ := reader.ReadString('\n')
targetArch = strings.TrimSpace(targetArch)
if targetArch == "" {
targetArch = runtime.GOARCH
}
switch targetArch {
case "386", "amd64":
default:
cRedBold.Printf("Architecture %s is not supported\n", targetArch)
os.Exit(1)
}
// Fetch build mode
fmt.Println()
fmt.Println("Please specify whether this profile used to build static or shared app.")
fmt.Println()
cCyanBold.Print("Use static mode (y/n, default n) : ")
staticMode, _ := reader.ReadString('\n')
staticMode = strings.TrimSpace(staticMode)
staticMode = strings.ToLower(staticMode)
if staticMode == "" {
staticMode = "n"
}
if staticMode != "y" && staticMode != "n" {
cRedBold.Println("Input value is not valid")
os.Exit(1)
}
// Fetch path to Qt's directory and tools
fmt.Println()
fmt.Println("Please specify the *full path* to your Qt's tools directory.")
fmt.Println("This might be different depending on your platform or your target. " +
"For example, in Linux with Qt 5.11.1, " +
"the tools are located in $HOME/Qt5.11.1/5.11.1/gcc_64/bin/")
fmt.Println()
cCyanBold.Print("Qt tools dir : ")
qtDir, _ := reader.ReadString('\n')
qtDir = strings.TrimSpace(qtDir)
if !dirExists(qtDir) {
cRedBold.Println("The specified directory does not exist")
os.Exit(1)
}
// Make sure qmake, moc, and rcc is exists
qmakePath := fp.Join(qtDir, "qmake")
mocPath := fp.Join(qtDir, "moc")
rccPath := fp.Join(qtDir, "rcc")
if runtime.GOOS == "windows" {
qmakePath += ".exe"
mocPath += ".exe"
rccPath += ".exe"
}
qmakeExists := fileExists(qmakePath)
mocExists := fileExists(mocPath)
rccExists := fileExists(rccPath)
cCyanBold.Print("qmake : ")
if qmakeExists {
cGreen.Println("found")
} else {
cRed.Println("not found")
}
cCyanBold.Print("moc : ")
if mocExists {
cGreen.Println("found")
} else {
cRed.Println("not found")
}
cCyanBold.Print("rcc : ")
if rccExists {
cGreen.Println("found")
} else {
cRed.Println("not found")
}
if !qmakeExists || !mocExists || !rccExists {
fmt.Println()
fmt.Println("Unable to find some of the tools. Please specify the *full path* to it manually.")
fmt.Println()
if !qmakeExists {
cCyanBold.Print("Path to qmake : ")
qmakePath, _ = reader.ReadString('\n')
qmakePath = strings.TrimSpace(qmakePath)
if !fileExists(qmakePath) {
cRedBold.Println("The specified path does not exist")
os.Exit(1)
}
}
if !mocExists {
cCyanBold.Print("Path to moc : ")
mocPath, _ = reader.ReadString('\n')
mocPath = strings.TrimSpace(mocPath)
if !fileExists(mocPath) {
cRedBold.Println("The specified path does not exist")
os.Exit(1)
}
}
if !rccExists {
cCyanBold.Print("Path to rcc : ")
rccPath, _ = reader.ReadString('\n')
rccPath = strings.TrimSpace(rccPath)
if !fileExists(rccPath) {
cRedBold.Println("The specified path does not exist")
os.Exit(1)
}
}
}
// Fetch custom C and C++ compiler
defaultGcc := "gcc"
defaultGxx := "g++"
defaultObjdump := "objdump"
if runtime.GOOS == "windows" {
defaultGcc += ".exe"
defaultGxx += ".exe"
defaultObjdump += ".exe"
}
fmt.Println()
fmt.Println("Please specify the *full path* to your compiler.")
fmt.Println("Keep it empty to use the default compiler on your system.")
fmt.Println()
cCyanBold.Printf("C compiler (default %s) : ", defaultGcc)
gccPath, _ := reader.ReadString('\n')
gccPath = strings.TrimSpace(gccPath)
if gccPath == "" {
gccPath = defaultGcc
}
cCyanBold.Printf("C++ compiler (default %s) : ", defaultGxx)
gxxPath, _ := reader.ReadString('\n')
gxxPath = strings.TrimSpace(gxxPath)
if gxxPath == "" {
gxxPath = defaultGxx
}
// If Windows shared, specify path to objdump, which used to fetch dependencies
objdumpPath := ""
if targetOS == "windows" && staticMode != "y" {
cCyanBold.Printf("Objdump (default %s) : ", defaultObjdump)
objdumpPath, _ = reader.ReadString('\n')
objdumpPath = strings.TrimSpace(objdumpPath)
if objdumpPath == "" {
objdumpPath = defaultObjdump
}
}
// If Windows, specify path to windres
// which will be used to create icon for the executable
windresPath := ""
if targetOS == "windows" {
fmt.Println()
fmt.Println("Since you are targeting Windows, you might want to set icon for your executable file. " +
"To do so, please specify the *full path* to windres on your system. " +
"It's usually located in the directory where MinGW is installed.")
fmt.Println()
cCyanBold.Print("Path to windres : ")
windresPath, _ = reader.ReadString('\n')
windresPath = strings.TrimSpace(windresPath)
if !fileExists(windresPath) {
cRedBold.Println("The specified path does not exist")
os.Exit(1)
}
}
// Save config file
fmt.Printf("Saving profile %s...", profileName)
profiles, err := config.LoadProfiles(configPath)
if err != nil {
fmt.Println()
cRedBold.Println("Failed to save the profile:", err)
os.Exit(1)
}
profiles[profileName] = config.Profile{
OS: targetOS,
Arch: targetArch,
Static: staticMode == "y",
Qmake: qmakePath,
Moc: mocPath,
Rcc: rccPath,
Gcc: gccPath,
Gxx: gxxPath,
Windres: windresPath,
Objdump: objdumpPath,
}
err = config.SaveProfiles(configPath, profiles)
if err != nil {
fmt.Println()
cRedBold.Println("Failed to save the profile:", err)
os.Exit(1)
}
cGreen.Println("done")
// Setup finished
fmt.Println()
cBlueBold.Println("Setup finished.")
cBlueBold.Println("Now you can get started on your own QML app.")
}