/// <summary> /// Creates a file with unique temporary file name with a given extension in the specified folder. /// File is guaranteed to be unique. /// Extension may have an initial period. /// If folder is null, the temporary folder will be used. /// Caller must delete it when finished. /// May throw IOException. /// </summary> internal static string GetTemporaryFile(string directory, string extension, bool createFile = true) { ErrorUtilities.VerifyThrowArgumentLengthIfNotNull(directory, nameof(directory)); ErrorUtilities.VerifyThrowArgumentLength(extension, nameof(extension)); if (extension[0] != '.') { extension = '.' + extension; } try { directory ??= Path.GetTempPath(); Directory.CreateDirectory(directory); string file = Path.Combine(directory, $"tmp{Guid.NewGuid():N}{extension}"); ErrorUtilities.VerifyThrow(!FileSystems.Default.FileExists(file), "Guid should be unique"); if (createFile) { File.WriteAllText(file, string.Empty); } return(file); } catch (Exception ex) when(ExceptionHandling.IsIoRelatedException(ex)) { throw new IOException(ResourceUtilities.FormatResourceStringStripCodeAndKeyword("Shared.FailedCreatingTempFile", ex.Message), ex); } }
/// <summary> /// Convert a task item metadata to bool. Throw an exception if the string is badly formed and can't /// be converted. /// /// If the metadata is not found, then set metadataFound to false and then return false. /// </summary> /// <param name="item">The item that contains the metadata.</param> /// <param name="itemMetadataName">The name of the metadata.</param> /// <param name="metadataFound">Receives true if the metadata was found, false otherwise.</param> /// <returns>The resulting boolean value.</returns> internal static bool TryConvertItemMetadataToBool ( ITaskItem item, string itemMetadataName, out bool metadataFound ) { string metadataValue = item.GetMetadata(itemMetadataName); if (string.IsNullOrEmpty(metadataValue)) { metadataFound = false; return(false); } metadataFound = true; try { return(Microsoft.Build.Shared.ConversionUtilities.ConvertStringToBool(metadataValue)); } catch (System.ArgumentException e) { throw new ArgumentException(ResourceUtilities.FormatResourceStringStripCodeAndKeyword("General.InvalidAttributeMetadata", item.ItemSpec, itemMetadataName, metadataValue, "bool"), e); } }
/// <summary> /// Throws an InvalidProjectFileException using the given data. /// /// PERF WARNING: calling a method that takes a variable number of arguments /// is expensive, because memory is allocated for the array of arguments -- do /// not call this method repeatedly in performance-critical scenarios /// /// </summary> /// <param name="errorSubCategoryResourceName">The resource string for the /// error sub-category (can be null).</param> /// <param name="elementLocation">The <see cref="IElementLocation"/> of the element.</param> /// <param name="resourceName">The resource string for the error message.</param> /// <param name="args">Extra arguments for formatting the error message.</param> private static void ThrowInvalidProject ( string errorSubCategoryResourceName, IElementLocation elementLocation, string resourceName, params object[] args ) { ErrorUtilities.VerifyThrowInternalNull(elementLocation, nameof(elementLocation)); #if DEBUG if (errorSubCategoryResourceName != null) { ResourceUtilities.VerifyResourceStringExists(errorSubCategoryResourceName); } ResourceUtilities.VerifyResourceStringExists(resourceName); #endif string errorSubCategory = null; if (errorSubCategoryResourceName != null) { errorSubCategory = AssemblyResources.GetString(errorSubCategoryResourceName); } string errorCode; string helpKeyword; string message = ResourceUtilities.FormatResourceStringStripCodeAndKeyword(out errorCode, out helpKeyword, resourceName, args); throw new InvalidProjectFileException(elementLocation.File, elementLocation.Line, elementLocation.Column, 0 /* Unknown end line */, 0 /* Unknown end column */, message, errorSubCategory, errorCode, helpKeyword); }
/// <summary> /// This method is used to flag errors in the project file being processed. Do NOT use this method in place of /// ErrorUtilities.VerifyThrow(), because ErrorUtilities.VerifyThrow() is used to flag internal/programming errors. /// /// PERF WARNING: calling a method that takes a variable number of arguments is expensive, because memory is allocated for /// the array of arguments -- do not call this method repeatedly in performance-critical scenarios /// </summary> /// <param name="condition">The condition to check.</param> /// <param name="errorSubCategoryResourceName">The resource string for the error sub-category (can be null).</param> /// <param name="projectFile">The invalid project file.</param> /// <param name="innerException">The inner <see cref="Exception"/>.</param> /// <param name="resourceName">The resource string for the error message.</param> /// <param name="args">Extra arguments for formatting the error message.</param> internal static void VerifyThrowInvalidProjectFile ( bool condition, string errorSubCategoryResourceName, BuildEventFileInfo projectFile, Exception innerException, string resourceName, params object[] args ) { ErrorUtilities.VerifyThrow(projectFile != null, "Must specify the invalid project file. If project file is not available, use VerifyThrowInvalidProject() and pass in the XML node instead."); #if DEBUG if (errorSubCategoryResourceName != null) { ResourceUtilities.VerifyResourceStringExists(errorSubCategoryResourceName); } ResourceUtilities.VerifyResourceStringExists(resourceName); #endif if (!condition) { string errorSubCategory = null; if (errorSubCategoryResourceName != null) { errorSubCategory = AssemblyResources.GetString(errorSubCategoryResourceName); } string errorCode; string helpKeyword; string message = ResourceUtilities.FormatResourceStringStripCodeAndKeyword(out errorCode, out helpKeyword, resourceName, args); throw new InvalidProjectFileException(projectFile.File, projectFile.Line, projectFile.Column, projectFile.EndLine, projectFile.EndColumn, message, errorSubCategory, errorCode, helpKeyword, innerException); } }