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 --- src/WixToolset.Core/AppCommon.cs | 145 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 src/WixToolset.Core/AppCommon.cs (limited to 'src/WixToolset.Core/AppCommon.cs') diff --git a/src/WixToolset.Core/AppCommon.cs b/src/WixToolset.Core/AppCommon.cs new file mode 100644 index 00000000..30ec7771 --- /dev/null +++ b/src/WixToolset.Core/AppCommon.cs @@ -0,0 +1,145 @@ +// 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 +{ + using System; + using System.Collections.Specialized; + using System.Configuration; + using System.Globalization; + using System.IO; + using System.Reflection; + using System.Text; + using System.Threading; + using WixToolset.Data; + + /// + /// Common utilities for Wix applications. + /// + public static class AppCommon + { + /// + /// Read the configuration file (*.exe.config). + /// + /// Extensions to load. + public static void ReadConfiguration(StringCollection extensions) + { + if (null == extensions) + { + throw new ArgumentNullException("extensions"); + } + +#if REDO_IN_NETCORE + // Don't use the default AppSettings reader because + // the tool may be called from within another process. + // Instead, read the .exe.config file from the tool location. + string toolPath = (new System.Uri(Assembly.GetCallingAssembly().CodeBase)).LocalPath; + Configuration config = ConfigurationManager.OpenExeConfiguration(toolPath); + if (config.HasFile) + { + KeyValueConfigurationElement configVal = config.AppSettings.Settings["extensions"]; + if (configVal != null) + { + string extensionTypes = configVal.Value; + foreach (string extensionType in extensionTypes.Split(";".ToCharArray())) + { + extensions.Add(extensionType); + } + } + } +#endif + } + + /// + /// Gets a unique temporary location or uses the provided temporary location. + /// + /// Optional temporary location to use. + /// Temporary location. + public static string GetTempLocation(string tempLocation = null) + { + if (String.IsNullOrEmpty(tempLocation)) + { + tempLocation = Environment.GetEnvironmentVariable("WIX_TEMP") ?? Path.GetTempPath(); + + do + { + tempLocation = Path.Combine(tempLocation, DateTime.Now.ToString("wixyyMMddTHHmmssffff")); + } while (Directory.Exists(tempLocation)); + } + + return tempLocation; + } + + /// + /// Delete a directory with retries and best-effort cleanup. + /// + /// The directory to delete. + /// The message handler. + /// True if all files were deleted, false otherwise. + public static bool DeleteDirectory(string path, IMessageHandler messageHandler) + { + return Common.DeleteTempFiles(path, messageHandler); + } + + /// + /// Prepares the console for localization. + /// + public static void PrepareConsoleForLocalization() + { + Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentUICulture.GetConsoleFallbackUICulture(); + if ((Console.OutputEncoding.CodePage != Encoding.UTF8.CodePage) && + (Console.OutputEncoding.CodePage != Thread.CurrentThread.CurrentUICulture.TextInfo.OEMCodePage) && + (Console.OutputEncoding.CodePage != Thread.CurrentThread.CurrentUICulture.TextInfo.ANSICodePage)) + { + Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US"); + } + } + + /// + /// Handler for display message events. + /// + /// Sender of message. + /// Event arguments containing message to display. + public static void ConsoleDisplayMessage(object sender, DisplayEventArgs e) + { + switch (e.Level) + { + case MessageLevel.Warning: + case MessageLevel.Error: + Console.Error.WriteLine(e.Message); + break; + default: + Console.WriteLine(e.Message); + break; + } + } + + /// + /// Creates and returns the string for CreatingApplication field (MSI Summary Information Stream). + /// + /// It reads the AssemblyProductAttribute and AssemblyVersionAttribute of executing assembly + /// and builds the CreatingApplication string of the form "[ProductName] ([ProductVersion])". + /// Returns value for PID_APPNAME." + public static string GetCreatingApplicationString() + { + Assembly assembly = Assembly.GetExecutingAssembly(); + return WixDistribution.ReplacePlaceholders("[AssemblyProduct] ([FileVersion])", assembly); + } + + /// + /// Displays help message header on Console for caller tool. + /// + public static void DisplayToolHeader() + { + Assembly assembly = Assembly.GetCallingAssembly(); + Console.WriteLine(WixDistribution.ReplacePlaceholders(WixDistributionSpecificStrings.ToolsetHelpHeader, assembly)); + } + + /// + /// Displays help message header on Console for caller tool. + /// + public static void DisplayToolFooter() + { + Console.Write(WixDistribution.ReplacePlaceholders(WixDistributionSpecificStrings.ToolsetHelpFooter, null)); + } + } +} -- cgit v1.2.3-55-g6feb