Skip to content

Commit d2c6955

Browse files
committed
lib/dtutils/file - reworked executable path management so that
executable_manager can see new executables in case they need the user to find the path and assign it. Reworked check_if_bin_exists to use the preference if it exists to try and cut down on the "flickering windows" problem on windows.
1 parent 5727b2d commit d2c6955

File tree

1 file changed

+91
-4
lines changed

1 file changed

+91
-4
lines changed

lib/dtutils/file.lua

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ end
265265
]]
266266

267267
local function _check_path_for_wine_bin(path)
268-
local result = false
268+
local result = false
269269

270270
if string.len(path) > 0 then
271271
-- check for windows executable to run under wine
@@ -294,7 +294,7 @@ local function _check_path_for_bin(bin)
294294
path = bin
295295
else
296296
path = dtutils_file.get_executable_path_preference(bin)
297-
-- reset path preference is the returned preference is a directory
297+
-- reset path preference if the returned preference is a directory
298298
if dtutils_file.test_file(path, "d") then
299299
dtutils_file.set_executable_path_preference(bin, "")
300300
path = nil
@@ -318,6 +318,19 @@ local function _check_path_for_bin(bin)
318318
return result
319319
end
320320

321+
local function _check_preferences_for_bin(bin)
322+
323+
local result = dtutils_file.get_executable_path_preference(bin)
324+
325+
if result:len() == 0 then
326+
result = false
327+
end
328+
329+
return result
330+
end
331+
332+
333+
321334
--[[
322335
local function to the old check_if_bin_exists functionality
323336
on windows in order to decrease the amount of windows being
@@ -385,12 +398,22 @@ function dtutils_file.check_if_bin_exists(bin)
385398
result = _old_check_if_bin_exists(bin)
386399
else
387400

388-
result = _check_path_for_bin(bin)
401+
result = _check_preferences_for_bin(bin)
402+
403+
if not result then
404+
result = _check_path_for_bin(bin)
405+
end
389406

390407
if not result then
391408
result = _search_for_bin(bin)
392409
end
393410
end
411+
412+
if not result then
413+
-- make an entry so that executable_manager can be used to fix it
414+
dtutils_file.set_executable_path_preference(bin, "")
415+
end
416+
394417
return result
395418
end
396419

@@ -674,6 +697,62 @@ function dtutils_file.create_unique_filename(filepath)
674697
return filepath
675698
end
676699

700+
-- sorted list of executable names
701+
dtutils_file.known_executables = {}
702+
703+
-- executable name/path pairs
704+
dtutils_file.executable_paths = {}
705+
706+
-- initialized flag
707+
dtutils_file.executables_initialized = false
708+
709+
local function _find_known_executable(name)
710+
local found = false
711+
for _, bin in ipairs(dtutils_file.known_executables) do
712+
if name == bin then
713+
found = true
714+
end
715+
end
716+
717+
return found
718+
end
719+
720+
local function _get_known_executables()
721+
-- read the known executables preference and populate the tables
722+
723+
local executables = dt.preferences.read("executable_manager", "known_executables", "string")
724+
725+
if executables then
726+
local names = du.split(executables, ",")
727+
728+
for _, name in ipairs(names) do
729+
table.insert(dtutils_file.known_executables, name)
730+
dtutils_file.executable_paths[name] = dtutils_file.get_executable_path_preference(name)
731+
end
732+
733+
table.sort(dtutils_file.known_executables)
734+
735+
dtutils_file.executables_initialized = true
736+
end
737+
end
738+
739+
local function _save_known_executables()
740+
-- write the preference with the known executable names
741+
local pref = du.join(dtutils_file.known_executables, ",")
742+
dt.preferences.write("executable_manager", "known_executables", "string", pref)
743+
end
744+
745+
local function _add_known_executable(name)
746+
-- add a known executable to the table
747+
if not dtutils_file.executables_initialized then
748+
_get_known_executables()
749+
end
750+
751+
table.insert(dtutils_file.known_executables, name)
752+
table.sort(dtutils_file.known_executables)
753+
_save_known_executables()
754+
end
755+
677756

678757
dtutils_file.libdoc.functions["set_executable_path_preference"] = {
679758
Name = [[set_executable_path_preference]],
@@ -696,6 +775,10 @@ dtutils_file.libdoc.functions["set_executable_path_preference"] = {
696775

697776
function dtutils_file.set_executable_path_preference(executable, path)
698777
dt.preferences.write("executable_paths", executable, "string", path)
778+
dtutils_file.executable_paths[executable] = path
779+
if not _find_known_executable(executable) then
780+
_add_known_executable(executable)
781+
end
699782
end
700783

701784

@@ -718,7 +801,11 @@ dtutils_file.libdoc.functions["get_executable_path_preference"] = {
718801
}
719802

720803
function dtutils_file.get_executable_path_preference(executable)
721-
return dt.preferences.read("executable_paths", executable, "string")
804+
if dtutils_file.executables_initialized then
805+
return dtutils_file.executable_paths(executable)
806+
else
807+
return dt.preferences.read("executable_paths", executable, "string")
808+
end
722809
end
723810

724811

0 commit comments

Comments
 (0)