/// <summary> /// Validates a project file against the given schema. If no schema is given, validates /// against the default schema /// </summary> /// <param name="projectFile">Path of the file to validate.</param> /// <param name="schemaFile">Can be null.</param> /// <param name="binPath">Path to the framework directory where the default schema for /// this ToolsVersion can be found.</param> /// <returns>True if the project was successfully validated against the given schema, false otherwise</returns> internal static void VerifyProjectSchema ( string projectFile, string schemaFile, string binPath ) { ErrorUtilities.VerifyThrowArgumentNull(projectFile, "projectFile"); ErrorUtilities.VerifyThrowArgumentNull(binPath, "binPath"); if ((schemaFile == null) || (schemaFile.Length == 0)) { schemaFile = Path.Combine(binPath, "Microsoft.Build.xsd"); } if (FileSystems.Default.FileExists(schemaFile)) { // Print the schema file we're using, particularly since it can vary // according to the toolset being used Console.WriteLine(AssemblyResources.GetString("SchemaFileLocation"), schemaFile); } else { // If we've gotten to this point, there is no schema to validate against -- just exit. InitializationException.Throw ( ResourceUtilities.FormatResourceString("SchemaNotFoundErrorWithFile", schemaFile), null /* No associated command line switch */ ); } ProjectSchemaValidationHandler validationHandler = new ProjectSchemaValidationHandler(); validationHandler.VerifyProjectSchema(projectFile, schemaFile); }
/// <summary> /// Given the parameters passed in, builds an error message and throws an /// InitializationException with that message. /// </summary> private static void ThrowInitializationExceptionWithResource ( string projectFile, int fileLine, int fileEndLine, int fileColumn, int fileEndColumn, string resourceName, params object[] args ) { InitializationException.Throw ( BuildStringFromResource ( projectFile, fileLine, fileEndLine, fileColumn, fileEndColumn, resourceName, args ), null /* No associated command line switch */ ); }
/// <summary> /// This method takes in a list of file name extensions to ignore. It will then validate the extensions /// to make sure they start with a period, have atleast one character after the period and do not contain /// any invalid path chars or wild cards /// </summary> /// <param name="projectsExtensionsToIgnore"></param> /// <returns></returns> private static Dictionary <string, object> ValidateExtensions(string[] projectsExtensionsToIgnore) { // Dictionary to contain the list of files which match the extensions to ignore. We are using a dictionary for fast lookups of extensions to ignore Dictionary <string, object> extensionsToIgnoreDictionary = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase); // Go through each of the extensions to ignore and add them as a key in the dictionary if (projectsExtensionsToIgnore != null && projectsExtensionsToIgnore.Length > 0) { string extension = null; foreach (string extensionToIgnore in projectsExtensionsToIgnore) { // Use the path get extension method to figure out if there is an extension in the passed // in extension to ignore. Will return null or empty if there is no extension in the string try { // Use GetExtension to parse the extension from the extensionToIgnore extension = Path.GetExtension(extensionToIgnore); } catch (ArgumentException) { // There has been an invalid char in the extensionToIgnore InitializationException.Throw("InvalidExtensionToIgnore", extensionToIgnore, null, false); } // if null or empty is returned that means that there was not extension able to be parsed from the string InitializationException.VerifyThrow(!string.IsNullOrEmpty(extension), "InvalidExtensionToIgnore", extensionToIgnore); // There has to be more than a . passed in as the extension InitializationException.VerifyThrow(extension.Length >= 2, "InvalidExtensionToIgnore", extensionToIgnore); // The parsed extension does not match the passed in extension, this means that there were // some other chars before the last extension if (string.Compare(extension, extensionToIgnore, StringComparison.OrdinalIgnoreCase) != 0) { InitializationException.Throw("InvalidExtensionToIgnore", extensionToIgnore, null, false); } // Make sure that no wild cards are in the string because for now we dont allow wild card extensions if (extensionToIgnore.IndexOfAny(s_wildcards) > -1) { InitializationException.Throw("InvalidExtensionToIgnore", extensionToIgnore, null, false); } ; if (!extensionsToIgnoreDictionary.ContainsKey(extensionToIgnore)) { extensionsToIgnoreDictionary.Add(extensionToIgnore, null); } } } return(extensionsToIgnoreDictionary); }
internal static void VerifyThrow(bool condition, string messageResourceName, string invalidSwitch, params object[] args) { if (!condition) { string errorMessage = AssemblyResources.GetString(messageResourceName); ErrorUtilities.VerifyThrow(errorMessage != null, "The resource string must exist."); errorMessage = ResourceUtilities.FormatString(errorMessage, args); InitializationException.Throw(errorMessage, invalidSwitch); } }
/// <summary> /// Throws an exception if any errors were found while parsing the command-line. /// </summary> internal void ThrowErrors() { if (HaveErrors()) { if (_isParameterError) { InitializationException.Throw(_errorMessage, _badCommandLineArg, _innerException, false); } else { CommandLineSwitchException.Throw(_errorMessage, _badCommandLineArg); } } }
internal void ThrowErrors() { if (this.HaveErrors()) { if (this.isParameterError) { InitializationException.Throw(this.errorMessage, this.badCommandLineArg, this.innerException, false); } else { CommandLineSwitchException.Throw(this.errorMessage, this.badCommandLineArg); } } }
/// <summary> /// Throws the exception using the given exception context. /// </summary> /// <param name="messageResourceName"></param> /// <param name="invalidSwitch"></param> /// <param name="e"></param> /// <param name="showStackTrace"></param> internal static void Throw(string messageResourceName, string invalidSwitch, Exception e, bool showStackTrace) { string errorMessage = AssemblyResources.GetString(messageResourceName); ErrorUtilities.VerifyThrow(errorMessage != null, "The resource string must exist."); if (showStackTrace) { errorMessage += Environment.NewLine + e.ToString(); } else { // the exception message can contain a format item i.e. "{0}" to hold the given exception's message errorMessage = ResourceUtilities.FormatString(errorMessage, ((e == null) ? String.Empty : e.Message)); } InitializationException.Throw(errorMessage, invalidSwitch); }
internal static void VerifyProjectSchema(string projectFile, string schemaFile, string binPath) { Microsoft.Build.Shared.ErrorUtilities.VerifyThrowArgumentNull(projectFile, "projectFile"); Microsoft.Build.Shared.ErrorUtilities.VerifyThrowArgumentNull(binPath, "binPath"); if ((schemaFile == null) || (schemaFile.Length == 0)) { schemaFile = Path.Combine(binPath, "Microsoft.Build.xsd"); } if (File.Exists(schemaFile)) { Console.WriteLine(Microsoft.Build.Shared.AssemblyResources.GetString("SchemaFileLocation"), schemaFile); } else { InitializationException.Throw(Microsoft.Build.Shared.ResourceUtilities.FormatResourceString("SchemaNotFoundErrorWithFile", new object[] { schemaFile }), null); } new ProjectSchemaValidationHandler().VerifyProjectSchema(projectFile, schemaFile); }