Skip to content

Commit 4c536b4

Browse files
committed
checkpoint
1 parent 55ced3e commit 4c536b4

File tree

1 file changed

+52
-9
lines changed

1 file changed

+52
-9
lines changed

lib/dtutils/file.lua

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,35 +57,78 @@ dtutils_file.libdoc.functions["check_if_bin_exists"] = {
5757
Copyright = [[]],
5858
}
5959

60-
function dtutils_file.check_if_bin_exists(bin)
60+
local function _check_if_bin_exists_windows(bin)
6161
local result = false
6262
local path = nil
6363

64-
if string.match(bin, "/") or string.match(bin, "\\") then
64+
if string.match(bin, "\\") then
6565
path = bin
6666
else
6767
path = dtutils_file.get_executable_path_preference(bin)
6868
end
6969

70-
if string.len(path) > 0 then
70+
if (string.match(path, ".exe$") or string.match(path, ".EXE$")) or
71+
(string.match(path, ".com$") or string.match(path, ".COM$")) or
72+
(string.match(path, ".bat$") or string.match(path, ".BAT$")) or
73+
(string.match(path, ".cmd$") or string.match(path, ".CMD$")) then
7174
if dtutils_file.check_if_file_exists(path) then
72-
if (string.match(path, ".exe$") or string.match(path, ".EXE$")) and dt.configuration.running_os ~= "windows" then
73-
result = dtutils_file.sanitize_filename("wine " .. path)
74-
else
75+
result = dtutils_file.sanitize_filename(path)
76+
end
77+
end
78+
return result
79+
end
80+
81+
-- check_if_bin_exists for unix like systems (linux, macos)
82+
local function _check_if_bin_exists_nix(bin)
83+
local result = false
84+
local path = nil
85+
86+
if string.match(bin, "/") then
87+
path = bin
88+
else
89+
path = dtutils_file.get_executable_path_preference(bin)
90+
end
91+
92+
if path then dt.print_log("path is " .. path) end
93+
94+
if string.len(path) > 0 then
95+
-- check for windows executable to run under wine
96+
if string.match(path, ".exe$") or string.match(path, ".EXE$") then
97+
if dtutils_file.check_if_file_exists(path) then
7598
result = dtutils_file.sanitize_filename(path)
7699
end
100+
else
101+
if dtutils_file.check_if_file_exists(path) then
102+
local spath = dtutils_file.sanitize_filename(path)
103+
-- check that it's an executable file
104+
if os.execute("test -f " .. spath .. " && test -x " .. spath) then
105+
result = spath
106+
end
107+
end
77108
end
78-
elseif dt.configuration.running_os == "linux" then
109+
end
110+
if not result then
79111
local p = io.popen("which " .. bin)
80112
local output = p:read("*a")
81113
p:close()
82114
if string.len(output) > 0 then
83-
result = dtutils_file.sanitize_filename(output:sub(1,-2))
115+
local spath = dtutils_file.sanitize_filename(output:sub(1,-2))
116+
if os.execute("test -f " .. spath .. " && test -x " .. spath) then
117+
result = spath
118+
end
84119
end
85120
end
86121
return result
87122
end
88123

124+
function dtutils_file.check_if_bin_exists(bin)
125+
if dt.configuration.running_os == "windows" then
126+
return _check_if_bin_exists_windows(bin)
127+
else
128+
return _check_if_bin_exists_nix(bin)
129+
end
130+
end
131+
89132
dtutils_file.libdoc.functions["split_filepath"] = {
90133
Name = [[split_filepath]],
91134
Synopsis = [[split a filepath into parts]],
@@ -542,7 +585,7 @@ function dtutils_file.executable_path_widget(executables)
542585
is_directory = false,
543586
changed_callback = function(self)
544587
if dtutils_file.check_if_bin_exists(self.value) then
545-
dtutils_file.set_executable_path_preference(executable, self.value)
588+
dtutils_file.set_executable_path_preference(executable, dtutils_file.check_if_bin_exists(self.value))
546589
end
547590
end}
548591
)

0 commit comments

Comments
 (0)