From d3d3649a68cb1fa589fdd987a6690dbd5d671f0d Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Sun, 17 Sep 2017 15:35:20 -0700 Subject: Initial code commit --- .../FileSearchHelperMethods.cs | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/WixToolset.BuildTasks/FileSearchHelperMethods.cs (limited to 'src/WixToolset.BuildTasks/FileSearchHelperMethods.cs') diff --git a/src/WixToolset.BuildTasks/FileSearchHelperMethods.cs b/src/WixToolset.BuildTasks/FileSearchHelperMethods.cs new file mode 100644 index 00000000..6cc804eb --- /dev/null +++ b/src/WixToolset.BuildTasks/FileSearchHelperMethods.cs @@ -0,0 +1,60 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. + +namespace WixToolset.BuildTasks +{ + using System; + using System.Collections.Generic; + using System.Diagnostics.CodeAnalysis; + using System.IO; + using System.Text; + using Microsoft.Build.Framework; + + /// + /// Contains helper methods on searching for files + /// + public static class FileSearchHelperMethods + { + /// + /// Searches for the existence of a file in multiple directories. + /// Search is satisfied if default file path is valid and exists. If not, + /// file name is extracted from default path and combined with each of the directories + /// looking to see if it exists. If not found, input default path is returned. + /// + /// Array of directories to look in, without filenames in them + /// Default path - to use if not found + /// File path if file found. Empty string if not found + public static string SearchFilePaths(string[] directories, string defaultFullPath) + { + if (String.IsNullOrEmpty(defaultFullPath)) + { + return String.Empty; + } + + if (File.Exists(defaultFullPath)) + { + return defaultFullPath; + } + + if (directories == null) + { + return string.Empty; + } + + string fileName = Path.GetFileName(defaultFullPath); + foreach (string currentPath in directories) + { + if (String.IsNullOrEmpty(currentPath) || String.IsNullOrEmpty(currentPath.Trim())) + { + continue; + } + + if (File.Exists(Path.Combine(currentPath, fileName))) + { + return Path.Combine(currentPath, fileName); + } + } + + return String.Empty; + } + } +} -- cgit v1.2.3-55-g6feb