private static void LoadFormatDataHelper( ExtendedTypeDefinition formatData, PSPropertyExpressionFactory expressionFactory, List <XmlLoaderLoggerEntry> logEntries, ref bool success, PSSnapInTypeAndFormatErrors file, TypeInfoDataBase db, bool isBuiltInFormatData, bool isForHelp) { using (TypeInfoDataBaseLoader loader = new TypeInfoDataBaseLoader()) { if (!loader.LoadFormattingData(formatData, db, expressionFactory, isBuiltInFormatData, isForHelp)) { success = false; } foreach (XmlLoaderLoggerEntry entry in loader.LogEntries) { // filter in only errors from the current file... if (entry.entryType == XmlLoaderLoggerEntry.EntryType.Error) { string mshsnapinMessage = StringUtil.Format(FormatAndOutXmlLoadingStrings.MshSnapinQualifiedError, file.PSSnapinName, entry.message); file.Errors.Add(mshsnapinMessage); } } // now aggregate the entries... logEntries.AddRange(loader.LogEntries); } }
internal TypeInfoDataBaseManager(IEnumerable <string> formatFiles, bool isShared, AuthorizationManager authorizationManager, PSHost host) { this.databaseLock = new object(); this.updateDatabaseLock = new object(); this.formatFileList = new List <string>(); Collection <PSSnapInTypeAndFormatErrors> files = new Collection <PSSnapInTypeAndFormatErrors>(); Collection <string> loadErrors = new Collection <string>(); foreach (string str in formatFiles) { if (string.IsNullOrEmpty(str) || !Path.IsPathRooted(str)) { throw PSTraceSource.NewArgumentException("formatFiles", "FormatAndOutXmlLoadingStrings", "FormatFileNotRooted", new object[] { str }); } PSSnapInTypeAndFormatErrors item = new PSSnapInTypeAndFormatErrors(string.Empty, str) { Errors = loadErrors }; files.Add(item); this.formatFileList.Add(str); } MshExpressionFactory expressionFactory = new MshExpressionFactory(); List <XmlLoaderLoggerEntry> logEntries = null; this.LoadFromFile(files, expressionFactory, true, authorizationManager, host, false, out logEntries); this.isShared = isShared; if (loadErrors.Count > 0) { throw new FormatTableLoadException(loadErrors); } }
/// <summary> /// Update the current formattable with the existing formatFileList. /// New files might have been added using Add() or Files might /// have been removed using Remove. /// </summary> /// <param name="authorizationManager"> /// Authorization manager to perform signature checks before reading ps1xml files (or null of no checks are needed) /// </param> /// <param name="host"> /// Host passed to <paramref name="authorizationManager"/>. Can be null if no interactive questions should be asked. /// </param> internal void Update(AuthorizationManager authorizationManager, PSHost host) { if (DisableFormatTableUpdates) { return; } if (isShared) { throw PSTraceSource.NewInvalidOperationException(FormatAndOutXmlLoadingStrings.SharedFormatTableCannotBeUpdated); } Collection <PSSnapInTypeAndFormatErrors> filesToLoad = new Collection <PSSnapInTypeAndFormatErrors>(); lock (_formatFileList) { foreach (string formatFile in _formatFileList) { PSSnapInTypeAndFormatErrors fileToLoad = new PSSnapInTypeAndFormatErrors(string.Empty, formatFile); filesToLoad.Add(fileToLoad); } } UpdateDataBase(filesToLoad, authorizationManager, host, false); }
/// <summary> /// Update a shared formatting database with formatData of 'ExtendedTypeDefinition' type. /// This method should only be called from the FormatTable, where are shared formatting /// database is created. /// </summary> /// <param name="formatData"> /// The format data to update the database /// </param> /// <param name="shouldPrepend"> /// Specify the order in which the format data will be loaded /// </param> internal void AddFormatData(IEnumerable <ExtendedTypeDefinition> formatData, bool shouldPrepend) { Diagnostics.Assert(isShared, "this method should only be called from FormatTable to update a shared database"); Collection <PSSnapInTypeAndFormatErrors> filesToLoad = new Collection <PSSnapInTypeAndFormatErrors>(); ConcurrentBag <string> errors = new ConcurrentBag <string>(); if (shouldPrepend) { foreach (ExtendedTypeDefinition typeDefinition in formatData) { PSSnapInTypeAndFormatErrors entryToLoad = new PSSnapInTypeAndFormatErrors(string.Empty, typeDefinition); entryToLoad.Errors = errors; filesToLoad.Add(entryToLoad); } // check if the passed in formatData is empty if (filesToLoad.Count == 0) { return; } } lock (_formatFileList) { foreach (string formatFile in _formatFileList) { PSSnapInTypeAndFormatErrors fileToLoad = new PSSnapInTypeAndFormatErrors(string.Empty, formatFile); fileToLoad.Errors = errors; filesToLoad.Add(fileToLoad); } } if (!shouldPrepend) { foreach (ExtendedTypeDefinition typeDefinition in formatData) { PSSnapInTypeAndFormatErrors entryToLoad = new PSSnapInTypeAndFormatErrors(string.Empty, typeDefinition); entryToLoad.Errors = errors; filesToLoad.Add(entryToLoad); } // check if the passed in formatData is empty if (filesToLoad.Count == _formatFileList.Count) { return; } } PSPropertyExpressionFactory expressionFactory = new PSPropertyExpressionFactory(); List <XmlLoaderLoggerEntry> logEntries = null; // load the formatting data LoadFromFile(filesToLoad, expressionFactory, false, null, null, false, out logEntries); // check to see if there are any errors loading the format files if (errors.Count > 0) { throw new FormatTableLoadException(errors); } }
internal void AddFormatData(IEnumerable <ExtendedTypeDefinition> formatData, bool shouldPrepend) { Collection <PSSnapInTypeAndFormatErrors> files = new Collection <PSSnapInTypeAndFormatErrors>(); Collection <string> loadErrors = new Collection <string>(); if (shouldPrepend) { foreach (ExtendedTypeDefinition definition in formatData) { PSSnapInTypeAndFormatErrors item = new PSSnapInTypeAndFormatErrors(string.Empty, definition) { Errors = loadErrors }; files.Add(item); } if (files.Count == 0) { return; } } lock (this.formatFileList) { foreach (string str in this.formatFileList) { PSSnapInTypeAndFormatErrors errors2 = new PSSnapInTypeAndFormatErrors(string.Empty, str) { Errors = loadErrors }; files.Add(errors2); } } if (!shouldPrepend) { foreach (ExtendedTypeDefinition definition2 in formatData) { PSSnapInTypeAndFormatErrors errors3 = new PSSnapInTypeAndFormatErrors(string.Empty, definition2) { Errors = loadErrors }; files.Add(errors3); } if (files.Count == this.formatFileList.Count) { return; } } MshExpressionFactory expressionFactory = new MshExpressionFactory(); List <XmlLoaderLoggerEntry> logEntries = null; this.LoadFromFile(files, expressionFactory, false, null, null, false, out logEntries); if (loadErrors.Count > 0) { throw new FormatTableLoadException(loadErrors); } }
private static void ProcessBuiltinFormatViewDefinitions( IEnumerable <ExtendedTypeDefinition> views, TypeInfoDataBase db, PSPropertyExpressionFactory expressionFactory, PSSnapInTypeAndFormatErrors file, List <XmlLoaderLoggerEntry> logEntries, bool isForHelp, ref bool success) { foreach (var v in views) { LoadFormatDataHelper(v, expressionFactory, logEntries, ref success, file, db, isBuiltInFormatData: true, isForHelp: isForHelp); } }
internal void Update(AuthorizationManager authorizationManager, PSHost host) { if (this.isShared) { throw TypeInfoDataBaseManager.tracer.NewInvalidOperationException("FormatAndOut.XmlLoading", "SharedFormattableCannotBeUpdated"); } Collection <PSSnapInTypeAndFormatErrors> mshsnapins = new Collection <PSSnapInTypeAndFormatErrors>(); lock (this.formatFileList) { foreach (string formatFile in this.formatFileList) { PSSnapInTypeAndFormatErrors typeAndFormatErrors = new PSSnapInTypeAndFormatErrors(string.Empty, formatFile); mshsnapins.Add(typeAndFormatErrors); } } this.UpdateDataBase(mshsnapins, authorizationManager, host); }
internal void Update(AuthorizationManager authorizationManager, PSHost host) { if (!this.DisableFormatTableUpdates) { if (this.isShared) { throw PSTraceSource.NewInvalidOperationException("FormatAndOutXmlLoadingStrings", "SharedFormatTableCannotBeUpdated", new object[0]); } Collection <PSSnapInTypeAndFormatErrors> mshsnapins = new Collection <PSSnapInTypeAndFormatErrors>(); lock (this.formatFileList) { foreach (string str in this.formatFileList) { PSSnapInTypeAndFormatErrors item = new PSSnapInTypeAndFormatErrors(string.Empty, str); mshsnapins.Add(item); } } this.UpdateDataBase(mshsnapins, authorizationManager, host, false); } }
private static bool ProcessBuiltin( PSSnapInTypeAndFormatErrors file, TypeInfoDataBase db, PSPropertyExpressionFactory expressionFactory, List <XmlLoaderLoggerEntry> logEntries, ref bool success) { if (s_builtinGenerators == null) { var builtInGenerators = new Dictionary <string, Tuple <bool, TypeGenerator> >(StringComparer.OrdinalIgnoreCase); var psHome = Utils.DefaultPowerShellAppBase; builtInGenerators.Add(Path.Combine(psHome, "Certificate.format.ps1xml"), GetBuiltin(false, Certificate_Format_Ps1Xml.GetFormatData)); builtInGenerators.Add(Path.Combine(psHome, "Diagnostics.Format.ps1xml"), GetBuiltin(false, Diagnostics_Format_Ps1Xml.GetFormatData)); builtInGenerators.Add(Path.Combine(psHome, "DotNetTypes.format.ps1xml"), GetBuiltin(false, DotNetTypes_Format_Ps1Xml.GetFormatData)); builtInGenerators.Add(Path.Combine(psHome, "Event.Format.ps1xml"), GetBuiltin(false, Event_Format_Ps1Xml.GetFormatData)); builtInGenerators.Add(Path.Combine(psHome, "FileSystem.format.ps1xml"), GetBuiltin(false, FileSystem_Format_Ps1Xml.GetFormatData)); builtInGenerators.Add(Path.Combine(psHome, "Help.format.ps1xml"), GetBuiltin(true, Help_Format_Ps1Xml.GetFormatData)); builtInGenerators.Add(Path.Combine(psHome, "HelpV3.format.ps1xml"), GetBuiltin(true, HelpV3_Format_Ps1Xml.GetFormatData)); builtInGenerators.Add(Path.Combine(psHome, "PowerShellCore.format.ps1xml"), GetBuiltin(false, PowerShellCore_Format_Ps1Xml.GetFormatData)); builtInGenerators.Add(Path.Combine(psHome, "PowerShellTrace.format.ps1xml"), GetBuiltin(false, PowerShellTrace_Format_Ps1Xml.GetFormatData)); builtInGenerators.Add(Path.Combine(psHome, "Registry.format.ps1xml"), GetBuiltin(false, Registry_Format_Ps1Xml.GetFormatData)); builtInGenerators.Add(Path.Combine(psHome, "WSMan.Format.ps1xml"), GetBuiltin(false, WSMan_Format_Ps1Xml.GetFormatData)); Interlocked.CompareExchange(ref s_builtinGenerators, builtInGenerators, null); } Tuple <bool, TypeGenerator> generator; if (!s_builtinGenerators.TryGetValue(file.FullPath, out generator)) { return(false); } ProcessBuiltinFormatViewDefinitions(generator.Item2(), db, expressionFactory, file, logEntries, generator.Item1, ref success); return(true); }
/// <summary> /// /// </summary> /// <param name="formatFiles"></param> /// <param name="isShared"></param> /// <param name="authorizationManager"> /// Authorization manager to perform signature checks before reading ps1xml files (or null of no checks are needed) /// </param> /// <param name="host"> /// Host passed to <paramref name="authorizationManager"/>. Can be null if no interactive questions should be asked. /// </param> /// <exception cref="ArgumentNullException" /> /// <exception cref="ArgumentException"> /// 1. FormatFile is not rooted. /// </exception> /// <exception cref="FormatTableLoadException"> /// 1. There were errors loading Formattable. Look in the Errors property to get /// detailed error messages. /// </exception> internal TypeInfoDataBaseManager( IEnumerable <string> formatFiles, bool isShared, AuthorizationManager authorizationManager, PSHost host) { _formatFileList = new List <string>(); Collection <PSSnapInTypeAndFormatErrors> filesToLoad = new Collection <PSSnapInTypeAndFormatErrors>(); ConcurrentBag <string> errors = new ConcurrentBag <string>(); foreach (string formatFile in formatFiles) { if (string.IsNullOrEmpty(formatFile) || (!Path.IsPathRooted(formatFile))) { throw PSTraceSource.NewArgumentException("formatFiles", FormatAndOutXmlLoadingStrings.FormatFileNotRooted, formatFile); } PSSnapInTypeAndFormatErrors fileToLoad = new PSSnapInTypeAndFormatErrors(string.Empty, formatFile); fileToLoad.Errors = errors; filesToLoad.Add(fileToLoad); _formatFileList.Add(formatFile); } PSPropertyExpressionFactory expressionFactory = new PSPropertyExpressionFactory(); List <XmlLoaderLoggerEntry> logEntries = null; // load the files LoadFromFile(filesToLoad, expressionFactory, true, authorizationManager, host, false, out logEntries); this.isShared = isShared; // check to see if there are any errors loading the format files if (errors.Count > 0) { throw new FormatTableLoadException(errors); } }