From 467365dcbbcb0ea1bbfb72b4c1b952ff07354c74 Mon Sep 17 00:00:00 2001 From: Andy Chien Date: Fri, 23 Feb 2018 18:14:24 -0800 Subject: [PATCH] platform dependent implementation of check_if_bin_exists and check_if_file_exists --- lib/dtutils/file.lua | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/lib/dtutils/file.lua b/lib/dtutils/file.lua index bc215e3..22cd5e5 100644 --- a/lib/dtutils/file.lua +++ b/lib/dtutils/file.lua @@ -51,16 +51,17 @@ dtutils_file.libdoc.functions["check_if_bin_exists"] = { } function dtutils_file.check_if_bin_exists(bin) - local cmd - if package.config:sub(1,1) == '/' then - cmd = 'which ' + local result + if (dt.configuration.running_os == 'linux') then + result = os.execute("which "..bin) else - cmd = 'where ' + result = dtutils_file.check_if_file_exists(bin) end - local result = os.execute(cmd .. bin) + if not result then result = false end + return result end @@ -200,11 +201,26 @@ dtutils_file.libdoc.functions["check_if_file_exists"] = { } function dtutils_file.check_if_file_exists(filepath) - local file = io.open(filepath, "r") - local result = false - if file then - result = true - file:close() + local result + if (dt.configuration.running_os == 'windows') then + filepath = string.gsub(filepath, '[\\/]+', '\\') + result = os.execute('if exist "'..filepath..'" (cmd /c exit 0) else (cmd /c exit 1)') + if not result then + result = false + end + elseif (dt.configuration.running_os == "linux") then + result = os.execute('test -e ' .. filepath) + if not result then + result = false + end + else + local file = io.open(filepath, "r") + if file then + result = true + file:close() + else + result = false + end end return result @@ -400,4 +416,4 @@ function dtutils_file.create_unique_filename(filepath) return filepath end -return dtutils_file +return dtutils_file \ No newline at end of file