From 5d57347730a1278f89bfabba2c7acec96e2e208b Mon Sep 17 00:00:00 2001 From: Andy Chien Date: Thu, 22 Feb 2018 16:01:54 -0800 Subject: [PATCH 1/2] fixed a couple dtutils_file functions for windows platform --- lib/dtutils/file.lua | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/dtutils/file.lua b/lib/dtutils/file.lua index 617234b..bc215e3 100644 --- a/lib/dtutils/file.lua +++ b/lib/dtutils/file.lua @@ -51,7 +51,13 @@ dtutils_file.libdoc.functions["check_if_bin_exists"] = { } function dtutils_file.check_if_bin_exists(bin) - local result = os.execute("which " .. bin) + local cmd + if package.config:sub(1,1) == '/' then + cmd = 'which ' + else + cmd = 'where ' + end + local result = os.execute(cmd .. bin) if not result then result = false end @@ -194,10 +200,13 @@ dtutils_file.libdoc.functions["check_if_file_exists"] = { } function dtutils_file.check_if_file_exists(filepath) - local result = os.execute("test -e " .. filepath) - if not result then - result = false + local file = io.open(filepath, "r") + local result = false + if file then + result = true + file:close() end + return result end @@ -384,7 +393,7 @@ function dtutils_file.create_unique_filename(filepath) while dtutils_file.check_if_file_exists(filepath) do filepath = dtutils_file.filename_increment(filepath) -- limit to 99 more exports of the original export - if string.match(dtfileutils.get_basename(filepath), "_(d-)$") == "99" then + if string.match(dtutils_file.get_basename(filepath), "_(d-)$") == "99" then break end end From 467365dcbbcb0ea1bbfb72b4c1b952ff07354c74 Mon Sep 17 00:00:00 2001 From: Andy Chien Date: Fri, 23 Feb 2018 18:14:24 -0800 Subject: [PATCH 2/2] 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