-
Notifications
You must be signed in to change notification settings - Fork 0
/
LuaRocks.lua
1045 lines (880 loc) · 41.2 KB
/
LuaRocks.lua
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- (c) 2023 Propagram. MIT Licensed.
local zbs_lua_version = string.match(tostring(_VERSION), "%d%.%d")
local luarocks_config = ide:GetConfig().luarocks or {}
local luarocks_version
local luarocks_path = luarocks_config.path or (ide.osname == "Windows" and "luarocks.exe" or "luarocks")
local luarocks_panel = "luarocksPanel"
local luarocks_variables = luarocks_config.variables or {}
local debug = luarocks_config.debug
local lua_dir, lua_version, lua_incdir
local luarocks_dir
local zerobrane_path, dir_separator = string.match(ide:GetRootPath(), "^(.+)([/\\])")
zerobrane_path = zerobrane_path .. dir_separator
local packages_path = zerobrane_path .. "packages" .. dir_separator
local packages_cache = {}
local project_path
local control
local current_page
local onTabLoad = {}
local LoadPackages
local PagesResultsLabel = {}
local function print(...)
if debug then
ide:Print(...)
end
end
print("ZBS Lua", zbs_lua_version)
local function urlencode(url)
url = url:gsub("\n", "\r\n")
url = url:gsub("([^%w ])", function(c)
return string.format("%%%02X", string.byte(c))
end)
url = url:gsub(" ", "+")
return url
end
wx.wxGetApp():Connect(wx.wxEVT_HTML_TAG_HANDLER, function(object)
local tag = object.HtmlTag:GetParam("tag")
if tag == "a" then
local href = object.HtmlTag:GetParam("href")
local text = object.HtmlTag:GetParam("text")
local parent = object:GetHtmlParser():GetWindowInterface():GetHTMLWindow()
local link = wx.wxHyperlinkCtrl(parent, wx.wxID_ANY, text, href, wx.wxDefaultPosition, wx.wxDefaultSize)
local widget = wx.wxHtmlWidgetCell(link, 0)
object.HtmlParser:OpenContainer():InsertCell(widget)
object:SetParseInnerCalled(false)
end
end)
local bg, fg
local function luarocks(cmd, ok_callback, spec, no_lua, no_shell, old_lua_dir, old_lua_version)
if no_lua then
cmd = luarocks_path .." " .. cmd
else
local args = cmd
cmd = luarocks_path ..
" --lua-dir=\"" .. (old_lua_dir or lua_dir):gsub("\\$", "") .. "\"" ..
" --lua-version=\"" .. (old_lua_version or lua_version):gsub("\\$", "") .. "\""
if lua_incdir then
cmd = cmd ..
" LUA_INCDIR=\"" .. lua_incdir:gsub("\\$", "") .. "\""
end
if luarocks_config.server then
cmd = luarocks_path ..
" --server=\"" .. luarocks_config.server .. "\""
end
for key, value in pairs(luarocks_variables) do
if type(key) == "string" then
cmd = cmd .. " " .. key:gsub("\"", "") .. "=\"" .. value:gsub("\"", ""):gsub("\\$", "") .. "\""
end
end
if spec == 0 then -- Projects Modules
cmd = cmd ..
" --tree=\"" .. (project_path:gsub("\"", "") .. (luarocks_config.directory or "lua_modules")):gsub("\\$", "") .. "\""
spec = project_path
elseif spec == 1 and not luarocks_config.global then -- System/User Modules
cmd = cmd ..
" --local"
elseif spec == 2 then -- IDE Packages
cmd = cmd ..
" --tree=\"" .. packages_path:gsub("\"", ""):gsub("\\$", "") .. "\""
spec = packages_path
end
cmd = cmd .." " .. args
end
-- sudo git config --global url."https://github.com/".insteadOf git://github.com/
local shell_cmd = 'bash -c "%s"' -- do not use %q
if ide.osname == "Windows" then
shell_cmd = 'cmd /c "%s"' -- do not use %q
end
if not no_shell then
cmd = shell_cmd:format(cmd:gsub('"', '""'))
end
local results = {}
-- CommandLineRun(cmd,wdir,tooutput,nohide,stringcallback,uid,endcallback)
return CommandLineRun(cmd, type(spec) == "string" and spec or nil, true, false, function(result)
if type(result) == "string" then
table.insert(results, result)
end
end, cmd, function(pid)
local out = table.concat(results):match("^[\n\r\f%s\t]*(.-)[%s\t\n\r\f]*$")
return ok_callback and ok_callback(out, pid)
end)
end
local onInterpreterLoad = function(self, interpreter, callback, not_reload)
if interpreter.fexepath then
lua_dir = string.match(interpreter.fexepath(), "^(.+)([/\\])")
end
if interpreter.luaversion then
lua_version = interpreter.luaversion
if type(lua_version) == "number" then
lua_version = lua_version + 0.01 -- fix 5.2999999999999998
end
lua_version = string.match(tostring(lua_version), "^(%d%.%d)")
end
print("lua interpreter: ", lua_dir)
print("lua version: ", lua_version)
if not luarocks_variables.LUA_INCDIR then
local pid = luarocks("config variables.LUA_INCDIR", function(result)
if result == "" or result:lower():match("error") or result == packages_path:gsub("\"", ""):gsub("\\$", "") then
-- Lua 5.X header files not found
-- This package will create a false 'lua.h' so that Luarocks does not report an error during the installation of pure Lua libraries.
local major, minor = lua_version:match("^(%d)%.(%d)")
local lua_h = io.open(packages_path .. "lua.h", "w")
lua_h:write(string.format("LUA_VERSION_NUM %s0%s", major, minor))
lua_h:close()
if result ~= packages_path:gsub("\"", ""):gsub("\\$", "") then
luarocks("config variables.LUA_INCDIR \"" .. packages_path:gsub("\"", ""):gsub("\\$", "") .. "\"", function()
print("variables.LUA_INCDIR: ", packages_path)
end, nil, true)
end
lua_incdir = packages_path
else
lua_incdir = result
end
if type(callback) == "function" then
print("run callback")
callback()
end
print("lua incdir: ", lua_incdir)
end, nil , true)
if not pid and type(callback) == "function" then
print("run callback (no pid)")
callback()
end
end
if current_page and onTabLoad[current_page] and not not_reload then
onTabLoad[current_page]()
end
end
-- IDE Packages luarocks command hack
local function luarocks_ide(cmd, callback)
control:Enable(false)
local lua_version = lua_version -- scope this var
local old_lua_dir = lua_dir
local old_lua_version = lua_version
if lua_version ~= tostring(zbs_lua_version) then
-- Temporarily change the interpreter version to zbs_lua_version
onInterpreterLoad(nil, {
luaversion = tostring(zbs_lua_version)
}, nil, true)
old_lua_version = tostring(zbs_lua_version)
end
local lua_modules_path = "/share/lua/" .. old_lua_version
local lib_modules_path = "/lib/lua/" .. old_lua_version
local rocks_subdir = "/lib/luarocks/rocks-" .. old_lua_version
local empty = ide.osname == "Windows" and '"./"' or "''"
luarocks("config lua_modules_path " .. empty, function()
luarocks("config lib_modules_path " .. empty, function()
luarocks("config rocks_subdir " .. empty, function()
luarocks(cmd, function(result)
luarocks("config lua_modules_path \"" .. lua_modules_path .. "\"", function()
luarocks("config lib_modules_path \"" .. lib_modules_path .. "\"", function()
luarocks("config rocks_subdir \"" .. rocks_subdir .. "\"", function()
if lua_version ~= tostring(zbs_lua_version) then
-- Revert to the current interpreter version.
onInterpreterLoad(nil, {
luaversion = lua_version
}, nil, true)
end
control:Enable(true)
return callback and callback(result)
end, 2, nil, nil, old_lua_dir, old_lua_version)
end, 2, nil, nil, old_lua_dir, old_lua_version)
end, 2, nil, nil, old_lua_dir, old_lua_version)
end, 2, nil, nil, old_lua_dir, old_lua_version)
end, 2, nil, nil, old_lua_dir, old_lua_version)
end, 2, nil, nil, old_lua_dir, old_lua_version)
end, 2, nil, nil, old_lua_dir, old_lua_version)
end
local page_image_list = wx.wxImageList(24, 24)
page_image_list:Add(wx.wxArtProvider.GetBitmap(wx.wxART_FOLDER_OPEN, wx.wxART_OTHER, wx.wxSize(-1, -1)))
page_image_list:Add(wx.wxArtProvider.GetBitmap(wx.wxART_GO_HOME, wx.wxART_TOOLBAR, wx.wxSize(24, 24)))
page_image_list:Add(wx.wxArtProvider.GetBitmap(wx.wxART_FILE_SAVE, wx.wxART_TOOLBAR, wx.wxSize(24, 24)))
page_image_list:Add(wx.wxArtProvider.GetBitmap(wx.wxART_INFORMATION, wx.wxART_TOOLBAR, wx.wxSize(24, 24)))
local image_list = wx.wxImageList(16, 16)
image_list:Add(wx.wxArtProvider.GetBitmap(wx.wxART_LIST_VIEW, wx.wxART_TOOLBAR, wx.wxSize(16, 16)))
image_list:Add(wx.wxArtProvider.GetBitmap(wx.wxART_FIND_AND_REPLACE, wx.wxART_TOOLBAR, wx.wxSize(16, 16)))
image_list:Add(wx.wxArtProvider.GetBitmap(wx.wxART_GO_DOWN, wx.wxART_TOOLBAR, wx.wxSize(16, 16)))
image_list:Add(wx.wxArtProvider.GetBitmap(wx.wxART_HELP, wx.wxART_TOOLBAR, wx.wxSize(16, 16)))
image_list:Add(wx.wxArtProvider.GetBitmap(wx.wxART_REDO, wx.wxART_TOOLBAR, wx.wxSize(16, 16)))
image_list:Add(wx.wxArtProvider.GetBitmap(wx.wxART_DELETE, wx.wxART_TOOLBAR, wx.wxSize(16, 16)))
image_list:Add(wx.wxArtProvider.GetBitmap(wx.wxART_PASTE, wx.wxART_TOOLBAR, wx.wxSize(16, 16)))
local function create_html(html)
if not html then
return "<html></html>"
end
html = html:gsub("&", "&")
html = html:gsub("<", "<")
html = html:gsub(">", ">")
html = html:gsub("\n", "<br/>\n")
html = html:gsub("(.-)%s%-%s([^<]+)", function(title, summary)
return "<h5>" .. title .. "</h5><p>" .. summary .. "</p><hr/>"
end)
html = html:gsub("(https://[^%s\n]+)", function(link)
return "<lua tag='a' href='" .. link .. "' text='" .. link .. "'>"
end)
html = html:gsub("\n([A-Za-z0-9%s]-):", function(title)
return "<br/><b>" .. title .. ":</b> "
end)
return [[
<html>
<head>
<title>LuaRocks ZeroBrane Package</title>
</head>
<body>
]] .. html .. [[
</body>
</html>
]]
end
local function create_tab(parent, page, tab)
local panel = wx.wxPanel(parent, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
local sizer = wx.wxBoxSizer(wx.wxVERTICAL)
local list = wx.wxListBox(panel, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
panel:SetBackgroundColour(panel:GetBackgroundColour()) -- fix panel colour
if ide.osname == "Windows" and fg and bg then
list:SetBackgroundColour(bg) -- fix selected item colour on Linux
list:SetForegroundColour(fg)
end
local box = wx.wxStaticBoxSizer(wx.wxVERTICAL, panel, page == 2 and "Selected package:" or "Selected module:")
box:GetStaticBox():Enable(false)
local image = wx.wxArtProvider.GetBitmap(wx.wxART_FIND, wx.wxART_BUTTON, wx.wxSize(16, 16))
local details
local results_label
local toolbar
if tab == 2 then --> Download
local search_panel = wx.wxPanel(panel, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
local search_sizer = wx.wxBoxSizer(wx.wxHORIZONTAL)
search_panel:SetSizer(search_sizer)
local search_label = wx.wxStaticText(panel, wx.wxID_ANY, page == 2 and "Filter remote packages:" or "Search remote modules:")
-- search_label:SetForegroundColour(fg)
local search = wx.wxTextCtrl(search_panel, wx.wxID_ANY, "", wx.wxDefaultPosition, wx.wxDefaultSize, wx.wxTE_PROCESS_ENTER)
if ide.osname == "Windows" and fg and bg then
search:SetBackgroundColour(bg) -- fix selected item colour on Linux
search:SetForegroundColour(fg)
end
local search_button = wx.wxBitmapButton(search_panel, wx.wxID_ANY, image, wx.wxDefaultPosition, wx.wxDefaultSize)
local event = function(empty)
local cmd
local value = search:GetValue()
if empty == true then
value = ""
end
value = string.lower(value)
value = value:match("^[%s\t]*(.-)[%s\t]*$")
value = value:gsub("[^a-zA-Z_%-0-9]", "")
if page ~= 2 and value == "" then
results_label:SetLabel("No results for '" .. search:GetValue() .. "'")
return
end
local parse_results = function(out)
print("packages results: ", out)
if page == 2 and lua_version ~= tostring(zbs_lua_version) then -- IDE Packages
-- Revert to the current interpreter version.
onInterpreterLoad(nil, {
luaversion = lua_version
}, nil, true)
end
if out:lower():match("^warning: failed searching manifest") or out:lower():match("error: lua 5%.1 interpreter not found") then
results_label:SetLabel("No results. Press the button above and try again.")
return
end
if page == 2 and out ~= "" then -- IDE packages
packages_cache[value] = out
end
local items = {}
string.gsub(out, "([^\n\r\f%s\t]+)[%s\t]+[0-9%.%-]+[%s\t]+rockspec", function(item)
if page == 2 then
item = string.sub(item, #(luarocks_config.package_prefix or "zerobranepackage-") + 1)
end
item = string.match(item, "^[\n\r\f%s\t]*(.-)[\n\r\f%s\t]*$")
if item == "" then
return
end
if not items[item] then
items[#items + 1] = item
end
items[item] = true
end)
list:Clear()
list:InsertItems(items, list:GetCount())
if empty == true or search:GetValue() == "" then
if #items == 0 then
results_label:SetLabel("No results")
elseif #items == 1 then
results_label:SetLabel(#items .. " result:")
else
results_label:SetLabel(#items .. " results:")
end
else
if #items == 0 then
results_label:SetLabel("No results for '" .. search:GetValue() .. "'")
elseif #items == 1 then
results_label:SetLabel(#items .. " result for '" .. search:GetValue() .. "':")
else
results_label:SetLabel(#items .. " results for '" .. search:GetValue() .. "':")
end
end
end
results_label:SetLabel("Searching...")
if page == 0 then -- Project Modules
cmd = "search " .. value .. " --porcelain"
elseif page == 1 then -- System Modules
cmd = "search " .. value .. " --porcelain"
elseif page == 2 then -- IDE Packages
cmd = "search " .. (luarocks_config.package_prefix or "zerobranepackage-") .. value .. " --porcelain"
else
print("page not found:", page)
return
end
if packages_cache[value] then
parse_results(packages_cache[value])
return
end
if page == 2 then
luarocks(cmd, parse_results, nil, nil, nil, nil, tostring(zbs_lua_version))
else
luarocks(cmd, parse_results)
end
end
if page == 2 then
LoadPackages = event
end
search_button:Connect(wx.wxEVT_BUTTON, event)
search:Connect(wx.wxEVT_TEXT_ENTER, event)
search_sizer:Add(search, 2, wx.wxEXPAND+wx.wxALL, 0)
search_sizer:Add(search_button, 0, wx.wxEXPAND+wx.wxRIGHT + wx.wxBOTTOM + wx.wxTOP, 0)
sizer:Add(search_label, 0, wx.wxEXPAND+wx.wxALL, 4)
sizer:Add(search_panel, 0, wx.wxEXPAND+wx.wxALL, 0)
elseif tab == 1 then --> Installed
onTabLoad[page] = function()
list:Clear()
details:SetPage(create_html())
box:GetStaticBox():Enable(false)
-- toolbar:Enable(false)
results_label:SetLabel("Loading...")
if page == 0 then --> Project Modules
luarocks("list --porcelain", function(result)
local items = {}
string.gsub(result, "([^\n\r\f%s\t]+)[%s\t]+[0-9%.%-]+[%s\t]+installed", function(item)
item = string.match(item, "^[\n\r\f%s\t]*(.-)[\n\r\f%s\t]*$")
if item == "" then
return
end
if not items[item] then
items[#items + 1] = item
end
items[item] = true
end)
list:Clear()
list:InsertItems(items, list:GetCount())
if #items == 0 then
results_label:SetLabel("No modules found (Lua " .. lua_version .. ")")
elseif #items == 1 then
results_label:SetLabel(#items .. " module found (Lua " .. lua_version .. ")")
else
results_label:SetLabel(#items .." modules found (Lua " .. lua_version .. ")")
end
end, 0)
elseif page == 1 then --> System Modules
luarocks("list --porcelain", function(result)
local items = {}
string.gsub(result, "([^\n\r\f%s\t]+)[%s\t]+[0-9%.%-]+[%s\t]+installed", function(item)
item = string.match(item, "^[\n\r\f%s\t]*(.-)[\n\r\f%s\t]*$")
if item == "" then
return
end
if not items[item] then
items[#items + 1] = item
end
items[item] = true
end)
list:Clear()
list:InsertItems(items, list:GetCount())
if #items == 0 then
results_label:SetLabel("No modules found (Lua " .. lua_version .. ")")
elseif #items == 1 then
results_label:SetLabel(#items .. " module found (Lua " .. lua_version .. ")")
else
results_label:SetLabel(#items .." modules found (Lua " .. lua_version .. ")")
end
end, 1)
elseif page == 2 then --> IDE Packages
luarocks_ide("list --porcelain", function(result)
local items = {}
string.gsub(result, "([^\n\r\f%s\t]+)[%s\t]+[0-9%.%-]+[%s\t]+installed", function(item)
local start, final = string.find(item, (luarocks_config.package_prefix or "zerobranepackage-"), 0, true)
if start == 1 then
item = string.sub(item, final + 1)
item = string.match(item, "^[\n\r\f%s\t]*(.-)[\n\r\f%s\t]*$")
if item == "" then
return
end
if not items[item] then
items[#items + 1] = item
end
items[item] = true
end
end)
list:Clear()
list:InsertItems(items, list:GetCount())
if #items == 0 then
results_label:SetLabel("No packages found (ZBS Lua " .. zbs_lua_version .. ")")
elseif #items == 1 then
results_label:SetLabel(#items .. " package found (ZBS Lua " .. zbs_lua_version .. ")")
else
results_label:SetLabel(#items .. " packages found (ZBS Lua " .. zbs_lua_version .. ")")
end
end)
else
print("page not found:", page)
return
end
if page == 2 then
LoadPackages(true)
end
end
end
results_label = wx.wxStaticText(panel, wx.wxID_ANY, tab == 2 and "Results:" or (page == 2 and "Packages:" or "Modules:"))
sizer:Add(results_label, 0, wx.wxEXPAND+wx.wxALL, 4)
if tab == 1 then --> Installed
PagesResultsLabel[page] = results_label
end
sizer:Add(list, 10, wx.wxEXPAND+wx.wxALL, 0)
sizer:Add(box, 1, wx.wxEXPAND + wx.wxLEFT + wx.wxRIGHT + wx.wxBOTTOM, 0)
panel:SetSizer(sizer)
if tab == 1 then --> Installed
details = wx.wxLuaHtmlWindow(box:GetStaticBox(), wx.wxID_ANY, wx.wxDefaultPosition, wx.wxSize(0, 100))
details:SetPage(create_html())
box:Add(details, 10, wx.wxEXPAND+wx.wxALL, 0)
end
toolbar = wx.wxToolBar(box:GetStaticBox(), wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize, wx.wxTB_NODIVIDER)
if tab == 1 then --> Installed
toolbar:AddTool(wx.wxID_ANY, "&Update", image_list:GetBitmap(4), page == 2 and "Update package" or "Update module") --> 0
toolbar:AddSeparator() --> 1
toolbar:AddTool(wx.wxID_ANY, "&Homepage", image_list:GetBitmap(3), page == 2 and "Package homepage" or "Module homepage") --> 2
toolbar:AddSeparator() --> 3
toolbar:AddTool(wx.wxID_ANY, "&Remove", image_list:GetBitmap(5), page == 2 and "Remove package" or "Remove module") --> 4
if page ~= 2 then
toolbar:AddSeparator() --> 5
toolbar:AddTool(wx.wxID_ANY, "&Paths", image_list:GetBitmap(6), "Insert modules' paths in the editor") --> 6
end
elseif tab ==2 then --> Download
toolbar:AddTool(wx.wxID_ANY, "&Install", image_list:GetBitmap(2), page == 2 and "Install package" or "Install module") --> 0
toolbar:AddSeparator() --> 1
toolbar:AddTool(wx.wxID_ANY, "&Query", image_list:GetBitmap(3), page == 2 and "Query package" or "Query module") --> 2
end
toolbar:Connect(wx.wxEVT_COMMAND_MENU_SELECTED, function(event)
local tool = toolbar:GetToolPos(event:GetId())
if tool == wx.wxNOT_FOUND then
return
end
print("tool:", tool, page, tab)
local selection = list:GetSelection()
if selection == wx.wxNOT_FOUND then
return
end
local item = list:GetString(selection)
if tab == 1 then --> Installed
if tool == 0 then --> Update
PagesResultsLabel[page]:SetLabel("Checking '" .. item .. "' for updates...")
if page == 0 then --> Project modules
luarocks("install " .. item, function(result)
if string.match(result, "now installed") then
wx.wxMessageDialog(ide:GetProjectNotebook(), "The module '" .. item .. "' has been successfully updated in 'Project Modules'.", TR("LuaRocks"), wx.wxICON_INFORMATION+wx.wxOK):ShowModal()
end
print(result)
onTabLoad[page]()
end, 0)
elseif page == 1 then --> System modules
luarocks("install " .. item, function(result)
if string.match(result, "now installed") then
wx.wxMessageDialog(ide:GetProjectNotebook(), "The module '" .. item .. "' has been successfully updated in '" .. (not luarocks_config.global and "User" or "System" ) .. " Modules'.", TR("LuaRocks"), wx.wxICON_INFORMATION+wx.wxOK):ShowModal()
end
print(result)
onTabLoad[page]()
end, 1)
elseif page == 2 then --> IDE packages
luarocks_ide("install " .. (luarocks_config.package_prefix or "zerobranepackage-") .. item, function(result)
if string.match(result, "now installed") then
wx.wxMessageDialog(ide:GetProjectNotebook(), "The package '" .. item .. "' has been successfully updated. Restart ZeroBrane Studio to apply the changes.", TR("LuaRocks"), wx.wxICON_INFORMATION+wx.wxOK):ShowModal()
end
print(result)
onTabLoad[page]()
end)
end
elseif tool == 2 then --> Homepage
if page == 0 then --> Project modules
luarocks("show " .. item .. " --porcelain", function(result)
local homepage = string.match(result, "\nhomepage[%s\t]+([^\n%s]+)")
if homepage then
wx.wxLaunchDefaultBrowser(homepage, 0)
else
print("no homepage found")
end
end, 0)
elseif page == 1 then --> System modules
luarocks("show " .. item .. " --porcelain", function(result)
print(result)
local homepage = string.match(result, "\nhomepage[%s\t]+([^\n%s]+)")
if homepage then
wx.wxLaunchDefaultBrowser(homepage, 0)
else
print("no homepage found")
end
end, 1)
elseif page == 2 then --> IDE packages
luarocks_ide("show " .. (luarocks_config.package_prefix or "zerobranepackage-") .. item .. " --porcelain", function(result)
local homepage = string.match(result, "\nhomepage[%s\t]+([^\n%s]+)")
if homepage then
wx.wxLaunchDefaultBrowser(homepage, 0)
else
print("no homepage found")
end
end)
end
elseif tool == 4 then --> Remove
if wx.wxMessageDialog(ide:GetProjectNotebook(), page == 2 and "Are you sure you want to remove the package '" .. item .. "'?" or "Are you sure you want to remove the module '" .. item .. "'?", TR("LuaRocks"), wx.wxICON_QUESTION+wx.wxOK+wx.wxCANCEL):ShowModal() == wx.wxID_OK then
if page == 0 then --> Project modules
luarocks("remove " .. item, function(result)
if string.match(result, "Removal successful") then
wx.wxMessageDialog(ide:GetProjectNotebook(), "The module '" .. item .. "' has been successfully removed in 'Project Modules'.", TR("LuaRocks"), wx.wxICON_INFORMATION+wx.wxOK):ShowModal()
end
print(result)
onTabLoad[page]()
end, 0)
elseif page == 1 then --> System modules
luarocks("remove " .. item, function(result)
if string.match(result, "Removal successful") then
wx.wxMessageDialog(ide:GetProjectNotebook(), "The module '" .. item .. "' has been successfully removed in '" .. (not luarocks_config.global and "User" or "System" ) .. " Modules'.", TR("LuaRocks"), wx.wxICON_INFORMATION+wx.wxOK):ShowModal()
end
print(result)
onTabLoad[page]()
end, 1)
elseif page == 2 then --> IDE packages
luarocks_ide("remove " .. (luarocks_config.package_prefix or "zerobranepackage-") .. item, function(result)
if string.match(result, "Removal successful") then
wx.wxMessageDialog(ide:GetProjectNotebook(), "The package '" .. item .. "' has been successfully removed. Restart ZeroBrane Studio to apply the changes.", TR("LuaRocks"), wx.wxICON_INFORMATION+wx.wxOK):ShowModal()
end
print(result)
onTabLoad[page]()
end)
end
end
elseif tool == 6 and page ~= 2 then --> Paths
if page == 0 then --> Projects Modules
local ed = ide:GetEditor()
if ed then
ed:AddText([[
package.path = package.path .. ";lua_modules/share/lua/]] .. lua_version .. [[/?.lua;lua_modules/share/lua/]] .. lua_version .. [[/?/init.lua"
package.cpath = package.cpath .. ";lua_modules/lib/lua/]] .. lua_version .. [[/?.]] .. (ide.osname == "Windows" and "dll" or "so") .. [["
]])
end
elseif page == 1 then --> System Modules
luarocks("path", function(result)
print("path: ", result)
local lua_path = string.gsub(string.match(result, "LUA_PATH=([^\n\r\f]+)"), "\\", "\\\\")
print("lua_path: ", lua_path)
local lua_path_1, lua_path_2 = string.match(lua_path, "([^;]+);([^;]+)")
local lua_cpath = string.gsub(string.match(result, "LUA_CPATH=([^\n\r\f]+)"), "\\", "\\\\")
print("lua_cpath: ", lua_cpath)
local lua_cpath_1 = string.match(lua_cpath, "([^;]+);")
local ed = ide:GetEditor()
if ed then
ed:AddText([[
package.path = package.path .. ";]] .. lua_path_1 .. [[;]] .. lua_path_2 .. [["
package.cpath = package.cpath .. ";]] .. lua_cpath_1 .. [["
]])
end
end, 1)
end
end
elseif tab ==2 then --> Download
if tool == 0 then --> Install
PagesResultsLabel[page]:SetLabel("Installing '" .. item .. "'...")
parent:SetSelection(0)
if page == 0 then --> Project modules
luarocks("install " .. item, function(result)
if string.match(string.lower(result), "build error:") then
if wx.wxMessageDialog(ide:GetProjectNotebook(), "Build error. It is necessary to install tools to compile the code. Do you want to open the instructions on how to proceed?", TR("LuaRocks"), wx.wxICON_QUESTION+wx.wxOK+wx.wxCANCEL):ShowModal() == wx.wxID_OK then
wx.wxLaunchDefaultBrowser("https://github.com/luarocks/luarocks/wiki/Using-LuaRocks#using-a-c-compiler", 0)
end
elseif string.match(result, "'git' program not found") then
if wx.wxMessageDialog(ide:GetProjectNotebook(), "The 'git' program was not found for installation. Would you like to download it now from the official website?", TR("LuaRocks"), wx.wxICON_QUESTION+wx.wxOK+wx.wxCANCEL):ShowModal() == wx.wxID_OK then
wx.wxLaunchDefaultBrowser("https://git-scm.com/downloads", 0)
end
elseif string.match(result, "now installed") then
wx.wxMessageDialog(ide:GetProjectNotebook(), "The module '" .. item .. "' has been successfully installed in 'Project Modules'.", TR("LuaRocks"), wx.wxICON_INFORMATION+wx.wxOK):ShowModal()
end
print(result)
onTabLoad[page]()
end, 0)
elseif page == 1 then --> System modules
luarocks("install " .. item, function(result)
if string.match(string.lower(result), "build error:") then
if wx.wxMessageDialog(ide:GetProjectNotebook(), "Build error. It is necessary to install tools to compile the code. Do you want to open the instructions on how to proceed?", TR("LuaRocks"), wx.wxICON_QUESTION+wx.wxOK+wx.wxCANCEL):ShowModal() == wx.wxID_OK then
wx.wxLaunchDefaultBrowser("https://github.com/luarocks/luarocks/wiki/Using-LuaRocks#using-a-c-compiler", 0)
end
elseif string.match(result, "'git' program not found") then
if wx.wxMessageDialog(ide:GetProjectNotebook(), "The 'git' program was not found for installation. Would you like to download it now from the official website?", TR("LuaRocks"), wx.wxICON_QUESTION+wx.wxOK+wx.wxCANCEL):ShowModal() == wx.wxID_OK then
wx.wxLaunchDefaultBrowser("https://git-scm.com/downloads", 0)
end
elseif string.match(result, "now installed") then
wx.wxMessageDialog(ide:GetProjectNotebook(), "The module '" .. item .. "' has been successfully installed in '" .. (not luarocks_config.global and "User" or "System" ) .. " Modules'.", TR("LuaRocks"), wx.wxICON_INFORMATION+wx.wxOK):ShowModal()
end
print(result)
onTabLoad[page]()
end, 1)
elseif page == 2 then --> IDE packages
luarocks_ide("install " .. (luarocks_config.package_prefix or "zerobranepackage-") .. item, function(result)
if string.match(string.lower(result), "build error:") then
if wx.wxMessageDialog(ide:GetProjectNotebook(), "Build error. It is necessary to install tools to compile the code. Do you want to open the instructions on how to proceed?", TR("LuaRocks"), wx.wxICON_QUESTION+wx.wxOK+wx.wxCANCEL):ShowModal() == wx.wxID_OK then
wx.wxLaunchDefaultBrowser("https://github.com/luarocks/luarocks/wiki/Using-LuaRocks#using-a-c-compiler", 0)
end
elseif string.match(result, "'git' program not found") then
if wx.wxMessageDialog(ide:GetProjectNotebook(), "The 'git' program was not found for installation. Would you like to download it now from the official website?", TR("LuaRocks"), wx.wxICON_QUESTION+wx.wxOK+wx.wxCANCEL):ShowModal() == wx.wxID_OK then
wx.wxLaunchDefaultBrowser("https://git-scm.com/downloads", 0)
end
elseif string.match(result, "now installed") then
wx.wxMessageDialog(ide:GetProjectNotebook(), "The package '" .. item .. "' has been successfully installed. Restart ZeroBrane Studio to apply the changes.", TR("LuaRocks"), wx.wxICON_INFORMATION+wx.wxOK):ShowModal()
end
print(result)
onTabLoad[page]()
end)
end
elseif tool == 2 then --> Query
if page == 2 then --> IDE packages
wx.wxLaunchDefaultBrowser("https://luarocks.org/search?q=" .. urlencode((luarocks_config.package_prefix or "zerobranepackage-") .. item), 0)
else
wx.wxLaunchDefaultBrowser("https://luarocks.org/search?q=" .. urlencode(item), 0)
end
end
end
end)
toolbar:Realize()
box:Add(toolbar, 0, wx.wxEXPAND+wx.wxALL, 2)
list:Connect(wx.wxEVT_LISTBOX, function(object)
local selection = object:GetSelection()
if selection == wx.wxNOT_FOUND then
return
end
local item = object:GetString(selection)
print("item:", item)
box:GetStaticBox():Enable(true)
-- toolbar:Enable(true)
if tab == 1 then -- Installed
if page == 0 then --> Project modules
luarocks("show " .. item, function(result)
details:SetPage(create_html(result))
end, 0)
elseif page == 1 then --> System modules
luarocks("show " .. item, function(result)
details:SetPage(create_html(result))
end, 1)
elseif page == 2 then --> IDE packages
luarocks_ide("show " .. (luarocks_config.package_prefix or "zerobranepackage-") .. item, function(result)
details:SetPage(create_html(result))
end)
end
end
end)
return panel
end
local function create_page(parent, page)
local panel = wx.wxPanel(parent, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
local sizer = wx.wxBoxSizer(wx.wxVERTICAL)
panel:SetBackgroundColour(panel:GetBackgroundColour()) -- fix background colour
panel:SetForegroundColour(panel:GetForegroundColour()) -- fix foreground colour
local subcontrol = wx.wxToolbook(panel, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
subcontrol:SetBackgroundColour(subcontrol:GetBackgroundColour()) -- fix background colour
subcontrol:SetForegroundColour(subcontrol:GetForegroundColour()) -- fix foreground colour
sizer:Add(subcontrol, 0, wx.wxEXPAND+wx.wxALL, 0)
subcontrol:SetImageList(image_list)
subcontrol:AddPage(create_tab(subcontrol, page, 1), "Installed", true, 0)
subcontrol:AddPage(create_tab(subcontrol, page, 2), "Download", false, 1)
panel:SetSizer(sizer)
return panel
end
local function create_about(parent)
local panel = wx.wxPanel(parent, wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
local sizer = wx.wxBoxSizer(wx.wxVERTICAL)
panel:SetBackgroundColour(panel:GetBackgroundColour()) -- fix background colour
panel:SetForegroundColour(panel:GetForegroundColour()) -- fix foreground colour
local subcontrol = wx.wxLuaHtmlWindow(panel)
subcontrol:SetPage([[
<html>
<head>
<title>About LuaRocks ZeroBrane Package</title>
</head>
<body>
<h3>LuaRocks ZeroBrane Package</h3>
<p><b>LuaRocks</b> is the package manager for Lua modules.</p>
<p>It allows you to create and install Lua modules as self-contained packages called <i>rocks</i>. You can download and install LuaRocks on Unix and Windows. <lua tag="a" href="https://luarocks.org/#quick-start" text="Get started"></p>
<p>LuaRocks is free software and uses the same license as Lua.</p>
<hr>
<p>Submit your fixes, issues, and feature requests to help improve this project!</p>
<p>
<lua tag="a" href="https://github.com/Propagram/ZeroBranePackage-LuaRocks" text="https://github.com/Propagram/ZeroBranePackage-LuaRocks">
</p>
</body>
</html>
]])
sizer:Add(subcontrol, 1, wx.wxEXPAND+wx.wxALL, 0)
panel:SetSizer(sizer)
return panel
end
local w, h = 200,200
local conf = function(panel)
panel:Dock():MinSize(w,-1):BestSize(w,-1):FloatingSize(w,h)
end
local luarocks_panel_id = ID("LuaRocksPanel.LuaRocksPanelView")
local function MenuItem()
local menu = ide:GetMenuBar():GetMenu(ide:GetMenuBar():FindMenu(TR("&View")))
menu:InsertCheckItem(4, luarocks_panel_id, TR("LuaRocks Window")..KSC(luarocks_panel_id))
menu:Connect(luarocks_panel_id, wx.wxEVT_COMMAND_MENU_SELECTED, function (_)
local uimgr = ide:GetUIManager()
uimgr:GetPane(luarocks_panel):Show(not uimgr:GetPane(luarocks_panel):IsShown())
uimgr:Update()
end)
ide:GetMainFrame():Connect(luarocks_panel_id, wx.wxEVT_UPDATE_UI, function(event)
local pane = ide:GetUIManager():GetPane(luarocks_panel)
menu:Enable(event:GetId(), pane:IsOk()) -- disable if doesn't exist
menu:Check(event:GetId(), pane:IsOk() and pane:IsShown())
end)
end
local function success()
if type(ide:GetConfig().styles) == "table" and ide:GetConfig().styles.text then
bg = wx.wxColour(unpack(ide:GetConfig().styles.text.bg))
fg = wx.wxColour(unpack(ide:GetConfig().styles.text.fg))
end
control = wx.wxListbook(ide:GetProjectNotebook(), wx.wxID_ANY, wx.wxDefaultPosition, wx.wxSize(200,200))
control:SetImageList(page_image_list)
control:SetBackgroundColour(control:GetBackgroundColour()) -- fix background colour
control:SetForegroundColour(control:GetForegroundColour()) -- fix foreground colour
if fg and bg then
control:GetListView():SetBackgroundColour(bg)
control:GetListView():SetForegroundColour(fg)
end
control:Connect(wx.wxEVT_LISTBOOK_PAGE_CHANGED, function(object)
local page = object:GetSelection()
if page == wx.wxNOT_FOUND then
return
end
current_page = page
if onTabLoad[page] then
onTabLoad[page]()
end
end)
control:AddPage(create_page(control, 0), "Project Modules", true, 0)
control:AddPage(create_page(control, 1), (not luarocks_config.global and "User" or "System" ) .. " Modules", false, 1)
control:AddPage(create_page(control, 2), "IDE Packages", false, 2)
-- control:AddPage(CreateBookPage(control, 3), "Tools", false, 2)
control:AddPage(create_about(control), "About", false, 3)
ide:AddPanelFlex(ide:GetProjectNotebook(), control, luarocks_panel, TR("LuaRocks"), conf)
MenuItem()
end
local function failure()
local panel = wx.wxPanel(ide:GetProjectNotebook(), wx.wxID_ANY, wx.wxDefaultPosition, wx.wxDefaultSize)
local sizer = wx.wxBoxSizer(wx.wxVERTICAL)
panel:SetBackgroundColour(panel:GetBackgroundColour()) -- fix background colour
panel:SetForegroundColour(panel:GetForegroundColour()) -- fix foreground colour
local subcontrol = wx.wxLuaHtmlWindow(panel)
sizer:Add(subcontrol, 1, wx.wxEXPAND+wx.wxALL, 0)
panel:SetSizer(sizer)
if ide.osname == "Windows" then
subcontrol:SetPage([[
<html>
<head>
<title>LuaRocks ZeroBrane Package</title>
</head>
<body>
<h3>LuaRocks ZeroBrane Package</h3>
<hr>
<p><b>Could not find "LuaRocks.exe" on your system PATH.</b><br/><br/>Download or compile the executable and place it in the same folder as this package <i>(]] .. packages_path .. [[luarocks.exe)</i>, or edit the settings (Edit Menu/Preferences) and provide the executable location as follows:<br/><br/><i>luarocks = {<br/> path = "C:/path/to/luarocks.exe"<br/>}</i></p>
<br/><hr>
<p>Submit your fixes, issues, and feature requests to help improve this project!</p>
<p><lua tag="a" href="https://github.com/Propagram/ZeroBranePackage-LuaRocks" text="https://github.com/Propagram/ZeroBranePackage-LuaRocks">
</p>
</body>
</html>
]])
else
subcontrol:SetPage([[
<html>
<head>
<title>LuaRocks ZeroBrane Package</title>
</head>
<body>
<h3>LuaRocks ZeroBrane Package</h3>
<hr>
<p><b>Could not find "LuaRocks" on your system PATH.</b><br/><br/>Download and build the source code and place it in the same folder as this package <i>(]] .. packages_path .. [[luarocks)</i>, or edit the settings (Edit Menu/Preferences) and provide the binary location as follows:<br/><br/><i>luarocks = {<br/> path = "/path/to/luarocks"<br/>}</i></p>
<br/><hr>
<p>Submit your fixes, issues, and feature requests to help improve this project!</p>
<p><lua tag="a" href="https://github.com/Propagram/ZeroBranePackage-LuaRocks" text="https://github.com/Propagram/ZeroBranePackage-LuaRocks">
</p>
</body>
</html>
]])
end
ide:AddPanelFlex(ide:GetProjectNotebook(), panel, luarocks_panel, "LuaRocks", conf)
MenuItem()
end
return {
name = "LuaRocks ZeroBrane Package",
description = "Search, install, and manage ZeroBrane Packages and Modules from LuaRocks directly in your favorite IDE!",
author = "Evandro C.",
version = 0.14,
onRegister = function(self)
local pid
local user_dir = ide:GetPackagePath():match("^(.+)[/\\]"):match("^(.+)[/\\]"):match("^(.+)[/\\]") .. dir_separator
if not wx.wxDirExists(user_dir .. ".luarocks") then
wx.wxMkdir(user_dir .. ".luarocks")
end
print("user_dir: ", user_dir)
luarocks_dir = user_dir .. ".luarocks" .. dir_separator
if not wx.wxDirExists(user_dir .. ".zbstudio") then
wx.wxMkdir(user_dir .. ".zbstudio")
end
if not wx.wxDirExists(user_dir .. ".zbstudio" .. dir_separator .. "packages") then
wx.wxMkdir(user_dir .. ".zbstudio" .. dir_separator .. "packages")
end