-
Notifications
You must be signed in to change notification settings - Fork 0
/
fpm-gui.tcl
348 lines (287 loc) · 9.85 KB
/
fpm-gui.tcl
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# fpm-gui.tcl --
# Humble beginnings of a GUI for fpm
#
# I may need to use TWAPI to suppress the spurious windows that the compiler produces:
# https://twapi.magicsplat.com/v4.5/process.html#create_process
#
#
#if { $::tcl_platform(platform) eq "windows" } {
# console show
#}
set fpmGuiVersion 0.2
# Default settings --
set startDir "~/fpm"
set tomlFile "~/fpm/fpm-registry/registry.toml"
if { $::tcl_platform(platform) eq "windows" } {
set extension ".exe"
} else {
set extension ""
}
set fpmCommand [file join [file dirname $::argv0] .. fpm$extension]
set installDir "~/fpm-packages/lib"
set checkoutDir "~/fpm-packages/src"
set registryUrl "https://github.com/fortran-lang/fpm-registry"
set registryDir "~/fpm"
set fpmInformation "https://github.com/fortran-lang/fpm"
set compiler "gfortran"
set compilerProfile "release"
set profileName fpm-gui.profile
puts [glob *]
if { [file exists $profileName] } {
source $profileName
puts "profile loaded"
}
# Note: not entirely robust ...
foreach var [list startDir tomlFile fpmCommand installDir checkoutDir registryDir] {
if { [string range [set $var] 0 1] eq "~/" } {
set $var [file join $env(HOME) [string range [set $var] 2 end]]
}
}
# loadRegistry --
# Read the list of packages
#
# Arguments:
# tomlfile Name of the toml file to be loaded
#
# Result:
# Dictionary of the packages and their Internet location
#
proc loadRegistry {tomlfile} {
#
# Make sure we have a registry file
#
getRegistry $::registryUrl $::registryDir
#
# Read it to fill the list of packages
#
if { ! [file exist $tomlfile] } {
return [dict create]
}
set packageList [dict create]
set infile [open $tomlfile]
while { [gets $infile line] >= 0 } {
if { [string index $line 0] == "\[" } {
set name [string range $line 1 end-1]
gets $infile line
set line [string trim $line]
if { [string index $line 0] == "\"" } {
set url [lindex [split $line \"] 3]
} else {
set url [lindex [split $line \"] 1]
}
dict append packageList $name $url
}
}
close $infile
return $packageList
}
# runCommand --
# Run a command and show the output in the text window
#
# Arguments:
# cmdtype What command to run
#
proc runCommand {cmdtype} {
global checkoutDir
global installDir
global fpmCommand
global selectedPackage
global packageList
global compiler
global compilerProfile
set url [dict get $packageList $selectedPackage]
set workDir [file tail $url]
if { $::tcl_platform(platform) eq "windows" } {
set run [file join [file dirname [file dirname $::argv0]] run.bat]
} else {
set run ""
}
.textw.text insert end $run\n
switch -- $cmdtype {
"retrieve" {
cd $checkoutDir
if { ! [file exists $workDir] } {
set cmds [list "git clone $url.git"]
} else {
cd [file join $checkoutDir $workDir]
set cmds [list "git fetch" "git merge --ff-only"]
}
}
"build" {
cd [file join $checkoutDir $workDir]
set cmds [list [string map {/ "\\\\"} "$run $fpmCommand build --compiler $compiler --profile $compilerProfile"]]
}
"install" {
cd [file join $checkoutDir $workDir]
set cmds [list [string map {/ "\\\\"} "$run $fpmCommand install --prefix $installDir "]]
}
}
.textw.text insert end "Package: $selectedPackage -- location: $url\n\n" pkg
foreach cmd $cmds {
.textw.text insert end ">> $cmd -- [pwd]\n"
set infile [open "|$cmd 2>@1" "r"]
fconfigure $infile -buffering line
fileevent $infile readable [list getInput $infile]
vwait ::finish
}
.textw.text insert end "\nDone\n" pkg
.textw.text see end
}
# getInput --
# Get the text that an external program writes to stdout/stderr
#
# Arguments:
# channel Channel to the external program
#
# Returns:
# Nothing
#
proc getInput {channel} {
if { [gets $channel line] >= 0 } {
#puts $logfile $line
.textw.text insert end "$line\n"
.textw.text see end
} elseif { [eof $channel] } {
catch {
close $channel
cd $::startDir
set ::finish 1
}
}
}
# showPkgInformation --
# Show information on the selected package by opening the web page
#
# Arguments:
# specific Show specific information or general (fpm project)
#
# Note:
# For the moment this works on Windows only
proc showPkgInformation {specific} {
global selectedPackage
global packageList
global fpmInformation
set outfile [open "index.html" w]
if { $specific } {
set url [dict get $packageList $selectedPackage]
} else {
set url $fpmInformation
}
puts $outfile [string map [list URL $url] {
<html>
<head>
<title>Package information</title>
<meta http-equiv="refresh" content="0; url=URL" />
</head>
<body>
<p>Information can be found here at <a href="URL">the package's home page</a></p>
</body>}]
close $outfile
if { $::tcl_platform(platform) eq "windows" } {
exec cmd /c index.html &
} else {
exec xdg-open index.html
}
}
# stopProgram --
# Stop the program (for now: simply quit)
#
# Arguments:
# None
#
proc stopProgram {} {
exit
}
# mainWindow --
# Set up the main window
#
# Arguments:
# packageNames List of package names
#
proc mainWindow {packageNames} {
wm title . "fpm - Fortran package manager"
wm protocol . WM_DELETE_WINDOW {stopProgram}
catch { wm iconbitmap . [file join [file dirname $::argv0] "favicon.ico"] }
menu .menuBar -tearoff 0
menu .menuBar.file -tearoff 0
menu .menuBar.options -tearoff 0
menu .menuBar.help -tearoff 0
.menuBar add cascade -label "File" -underline 0 -menu .menuBar.file
.menuBar add cascade -label "Options" -underline 0 -menu .menuBar.options
.menuBar add cascade -label "Help" -underline 0 -menu .menuBar.help
menu .menuBar.file.menu -tearoff 0
menu .menuBar.options.menu -tearoff 0
.menuBar.file add command -label "New" -command "newProfile" -underline 0
.menuBar.file add command -label "Open" -command "openProfile" -underline 0
.menuBar.file add separator
.menuBar.file add command -label "Save" -command [list saveProfile 0] -underline 0 -accelerator "Ctrl-S"
.menuBar.file add command -label "Save as ..." -command [list saveProfile 1]
.menuBar.file add separator
.menuBar.file add command -label "Exit" -command "stopProgram" -underline 0 \
-accelerator "Alt-F4"
.menuBar.options add command -label "Directories" -command "selectDirectories" -underline 0
.menuBar.options add command -label "Compiling" -command "selectCompileOptions" -underline 0
.menuBar.help add command -label "Information" -command "showInformation" -underline 0
.menuBar.file add separator
.menuBar.help add command -label "About" -command "showAboutBox" -underline 0
ttk::frame .packages
grid [::ttk::label .packages.label -text "Available packages:"] \
[::ttk::combobox .packages.pkglist -width 30 -textvariable selectedPackage -values $packageNames] -sticky news -pady 3 -padx 3
grid .packages -sticky news
ttk::frame .buttons
grid [::ttk::button .buttons.retrieve -text "Retrieve" -command [list runCommand retrieve]] \
[::ttk::button .buttons.build -text "Build" -command [list runCommand build]] \
[::ttk::button .buttons.install -text "Install" -command [list runCommand install]] \
[::ttk::button .buttons.info -text "Information" -command [list showPkgInformation 1]] -sticky news -padx 3
grid .buttons -sticky news
grid [::ttk::label .empty1 -text ""] -sticky news
grid [::ttk::label .output -text "Output of commands:"] -sticky news -pady 3 -padx 3
ttk::frame .textw
grid .textw - -sticky news -padx 3
ttk::scrollbar .textw.scrollx -orient horiz -command ".textw.text xview"
ttk::scrollbar .textw.scrolly -command ".textw.text yview"
text .textw.text -yscrollcommand ".textw.scrolly set" \
-xscrollcommand ".textw.scrollx set" \
-font "courier 10" -wrap none \
-foreground black
grid .textw.text .textw.scrolly
grid .textw.scrollx x
grid .textw.text -sticky news
grid .textw.scrolly -sticky ns
grid .textw.scrollx -sticky ew
grid rowconfigure .textw 2 -weight 1
grid columnconfigure .textw 0 -weight 1
.textw.text configure -wrap none
. configure -menu .menuBar
.textw.text tag configure pkg -foreground blue
.textw.text insert end \
"Start up complete:
Checkout directory: $::checkoutDir
Installation directory: $::installDir\n
Compiler: $::compiler
Compiler profile: $::compilerProfile\n
Number of registered packages: [llength $::packageNames]\n"
}
# main --
#
#set tomlfile "../fpm-registry/registry.toml"
#set fpmCommand "../fpm/fpm.exe"
# TODO: necessary to set it to something else?
set startDir [file dirname $::argv0]
source [file join $startDir fpm-prepare.tcl]
source [file join $startDir fpm-menu.tcl]
set packageList [loadRegistry $tomlFile]
#
# Make sure the directories for checking out and installation exist
#
checkDirectories $checkoutDir $installDir
#
# Set up the main window
#
set packageNames [dict keys $packageList]
set selectedPackage [lindex $packageList 0]
#foreach p [dict keys $packageList] {
# #puts "$p -- [dict get $list $p]"
# lappend packageNames [dict get $packageList $p]
#}
mainWindow $packageNames