Skip to content

Commit b893383

Browse files
committed
[file.lua] Added comments for local functions. Added more API
documentation for check_if_bin_exists. Added save executable path preference to _search_for_nix_bin() so that we can save a little time on subsequent runs once an executable is found.
1 parent ddcf3ed commit b893383

File tree

1 file changed

+49
-3
lines changed

1 file changed

+49
-3
lines changed

lib/dtutils/file.lua

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ end
5959

6060
local function _is_windows_executable(path)
6161
local result = false
62+
dt.print_log("in -is_windows_executable")
6263
if dtutils_file.test_file(path, "f") then
6364
if string.match(path, ".exe$") or string.match(path, ".EXE$") or
6465
string.match(path, ".com$") or string.match(path, ".COM$") or
@@ -142,12 +143,22 @@ function dtutils_file.test_file(path, test)
142143
return engine(cmdstring)
143144
end
144145

146+
--[[
147+
local function to return a case insensitive pattern for matching
148+
i.e. gimp becomes [Gg][Ii][Mm][Pp] which should match any capitalization
149+
of gimp.
150+
]]
151+
145152
local function _case_insensitive_pattern(pattern)
146153
return pattern:gsub("(.)", function(letter)
147154
return string.format("[%s$s]", letter:lower(), letter:upper())
148155
end)
149156
end
150157

158+
--[[
159+
local function to search windows for an executable
160+
]]
161+
151162
local function _search_for_bin_windows(bin)
152163
local result = false
153164
-- use where on path
@@ -175,24 +186,33 @@ local function _search_for_bin_windows(bin)
175186
return result
176187
end
177188

189+
--[[
190+
local function to search *nix systems for an executable
191+
]]
192+
178193
local function _search_for_bin_nix(bin)
179194
local result = false
180195
local p = io.popen("which " .. bin)
181196
local output = p:read("*a")
182197
p:close()
183198
if string.len(output) > 0 then
184-
local spath = dtutils_file.sanitize_filename(output:sub(1,-2))
199+
local spath = dtutils_file.sanitize_filename(output:sub(1, -2))
185200
if dtutils_file.test_file(spath, "f") and dtutils_file.test_file(spath, "x") then
201+
dtutils_file.set_executable_path_preference(bin, spath)
186202
result = spath
187203
end
188204
end
189205
return result
190206
end
191207

208+
--[[
209+
local function to search macos systems for an executable
210+
]]
211+
192212
local function _search_for_bin_macos(bin)
193213
local result = false
194214

195-
result = _search_for_bin_nix(bin)
215+
result = _search_for_bin_nix(bin) -- see if it's in the path
196216

197217
if not result then
198218
local search_start = "/Applications"
@@ -218,6 +238,11 @@ local function _search_for_bin_macos(bin)
218238
return result
219239
end
220240

241+
--[[
242+
local function to provide a generic search call that can be
243+
split into operating system specific calls
244+
]]
245+
221246
local function _search_for_bin(bin)
222247
local result = false
223248

@@ -235,6 +260,11 @@ local function _search_for_bin(bin)
235260
return result
236261
end
237262

263+
--[[
264+
local function to check if an executable path is
265+
a windows executable on linux or macos, thus requiring wine to run
266+
]]
267+
238268
local function _check_path_for_wine_bin(path)
239269
local result = false
240270

@@ -249,6 +279,12 @@ local function _check_path_for_wine_bin(path)
249279
return result
250280
end
251281

282+
--[[
283+
local function to check if an executable path is
284+
a valid executable. Some generic checks are done before
285+
system specific checks are done.
286+
]]
287+
252288
local function _check_path_for_bin(bin)
253289
local result = false
254290
local path = nil
@@ -283,6 +319,12 @@ local function _check_path_for_bin(bin)
283319
return result
284320
end
285321

322+
--[[
323+
local function to the old check_if_bin_exists functionality
324+
on windows in order to decrease the amount of windows being
325+
created and destroyed by system calls.
326+
]]
327+
286328
local function _old_check_if_bin_exists(bin) -- only run on windows if preference checked
287329
local result = false
288330
local path = nil
@@ -325,7 +367,10 @@ dtutils_file.libdoc.functions["check_if_bin_exists"] = {
325367
Return_Value = [[result - string - the sanitized path of the binary, false if not found]],
326368
Limitations = [[If more than one executable that satisfies the search results is found, the
327369
wrong one may be returned. If the wrong value is returned, the user can still specify the
328-
correct execuable using tools/executable_manager.]],
370+
correct execuable using tools/executable_manager. Most packages are well behaved with the
371+
notiable exception being GIMP on windows. Depending on the packager there are multiple
372+
gimp executables, often with version numbers. In this case, the user needs to specify
373+
the location of the correct executable using executable_manager.]],
329374
Example = [[]],
330375
See_Also = [[executable_manager]],
331376
Reference = [[]],
@@ -334,6 +379,7 @@ dtutils_file.libdoc.functions["check_if_bin_exists"] = {
334379
}
335380

336381
function dtutils_file.check_if_bin_exists(bin)
382+
dt.print_log("in check_if_bin_exists")
337383
local result = false
338384

339385
if dt.configuration.running_os == "windows" and dt.preferences.read("dtutils.file", "use_old_check_if_bin_exists", "bool") then

0 commit comments

Comments
 (0)