internal static StateFileBase DeserializeCache(string stateFile, TaskLoggingHelper log, Type requiredReturnType) { StateFileBase o = null; try { if (((stateFile == null) || (stateFile.Length <= 0)) || !File.Exists(stateFile)) { return o; } using (FileStream stream = new FileStream(stateFile, FileMode.Open)) { object obj2 = new BinaryFormatter().Deserialize(stream); o = obj2 as StateFileBase; if ((o == null) && (obj2 != null)) { log.LogMessageFromResources("General.CouldNotReadStateFileMessage", new object[] { stateFile, log.FormatResourceString("General.IncompatibleStateFileType", new object[0]) }); } if ((o != null) && !requiredReturnType.IsInstanceOfType(o)) { log.LogWarningWithCodeFromResources("General.CouldNotReadStateFile", new object[] { stateFile, log.FormatResourceString("General.IncompatibleStateFileType", new object[0]) }); o = null; } } } catch (Exception exception) { log.LogWarningWithCodeFromResources("General.CouldNotReadStateFile", new object[] { stateFile, exception.Message }); } return o; }
private string GetTypeLibId(TaskLoggingHelper log) { if (this.taskItem != null) { return this.taskItem.ItemSpec; } return log.FormatResourceString("ResolveComReference.TypeLibAttrId", new object[] { this.attr.guid, this.attr.wMajorVerNum, this.attr.wMinorVerNum }); }
public void TestFormatResourceString2 () { tlh = new TaskLoggingHelper (task); tlh.FormatResourceString ("MessageResource1"); }
public void TestFormatResourceString1 () { tlh = new TaskLoggingHelper (task); tlh.FormatResourceString (null); }
/// <summary> /// Given a log and a resource string, acquires the text of that resource string and /// compares it to the log. Assert fails if the log contain the desired string. /// </summary> /// <param name="e">The MockEngine that contains the log we're checking</param> /// <param name="log">The TaskLoggingHelper that we use to load the string resource</param> /// <param name="errorResource">The name of the resource string to check the log for</param> /// <param name="args">Arguments needed to format the resource string properly</param> private void VerifyLogDoesNotContainResource(MockEngine e, TaskLoggingHelper log, string messageResource, params object[] args) { string message = log.FormatResourceString(messageResource, args); e.AssertLogDoesntContain(message); }
internal static bool GetTypeLibNameForTypeLibAttrs(TaskLoggingHelper log, System.Runtime.InteropServices.ComTypes.TYPELIBATTR typeLibAttr, out string typeLibName) { bool flag; typeLibName = ""; ITypeLib typeLib = null; try { try { System.Runtime.InteropServices.ComTypes.TYPELIBATTR typelibattr = typeLibAttr; typeLib = (ITypeLib) Microsoft.Build.Tasks.NativeMethods.LoadRegTypeLib(ref typelibattr.guid, typelibattr.wMajorVerNum, typelibattr.wMinorVerNum, typelibattr.lcid); } catch (COMException exception) { log.LogWarningWithCodeFromResources("ResolveComReference.CannotLoadTypeLib", new object[] { typeLibAttr.guid, typeLibAttr.wMajorVerNum, typeLibAttr.wMinorVerNum, exception.Message }); return false; } string typeLibId = log.FormatResourceString("ResolveComReference.TypeLibAttrId", new object[] { typeLibAttr.guid.ToString(), typeLibAttr.wMajorVerNum, typeLibAttr.wMinorVerNum }); flag = GetTypeLibNameForITypeLib(log, typeLib, typeLibId, out typeLibName); } finally { if (typeLib != null) { Marshal.ReleaseComObject(typeLib); } } return flag; }
/// <summary> /// Reads the specified file from disk into a StateFileBase derived object. /// </summary> /// <param name="stateFile"></param> /// <returns></returns> static internal StateFileBase DeserializeCache(string stateFile, TaskLoggingHelper log, Type requiredReturnType) { StateFileBase retVal = null; // First, we read the cache from disk if one exists, or if one does not exist // then we create one. try { if (!string.IsNullOrEmpty(stateFile) && File.Exists(stateFile)) { using (FileStream s = new FileStream(stateFile, FileMode.Open)) { BinaryFormatter formatter = new BinaryFormatter(); object deserializedObject = formatter.Deserialize(s); retVal = deserializedObject as StateFileBase; // If the deserialized object is null then there would be no cast error but retVal would still be null // only log the message if there would have been a cast error if (retVal == null && deserializedObject != null) { // When upgrading to Visual Studio 2008 and running the build for the first time the resource cache files are replaced which causes a cast error due // to a new version number on the tasks class. "Unable to cast object of type 'Microsoft.Build.Tasks.SystemState' to type 'Microsoft.Build.Tasks.StateFileBaseā. // If there is an invalid cast, a message rather than a warning should be emitted. log.LogMessageFromResources("General.CouldNotReadStateFileMessage", stateFile, log.FormatResourceString("General.IncompatibleStateFileType")); } if ((retVal != null) && (!requiredReturnType.IsInstanceOfType(retVal))) { log.LogWarningWithCodeFromResources("General.CouldNotReadStateFile", stateFile, log.FormatResourceString("General.IncompatibleStateFileType")); retVal = null; } // If we get back a valid object and internals were changed, things are likely to be null. Check the version before we use it. if (retVal != null && retVal._serializedVersion != CurrentSerializationVersion) { log.LogMessageFromResources("General.CouldNotReadStateFileMessage", stateFile, log.FormatResourceString("General.IncompatibleStateFileType")); retVal = null; } } } } catch (Exception e) { if (ExceptionHandling.IsCriticalException(e)) { throw; } // The deserialization process seems like it can throw just about // any exception imaginable. Catch them all here. // Not being able to deserialize the cache is not an error, but we let the user know anyway. // Don't want to hold up processing just because we couldn't read the file. log.LogWarningWithCodeFromResources("General.CouldNotReadStateFile", stateFile, e.Message); } return retVal; }
/// <summary> /// Given a log and a resource string, acquires the text of that resource string and /// compares it to the log. Assert fails if the log contains the desired string. /// </summary> /// <param name="e">The MockEngine that contains the log we're checking</param> /// <param name="log">The TaskLoggingHelper that we use to load the string resource</param> /// <param name="errorResource">The name of the resource string to check the log for</param> /// <param name="args">Arguments needed to format the resource string properly</param> internal static void VerifyLogDoesNotContainErrorFromResource(MockEngine e, TaskLoggingHelper log, string errorResource, params object[] args) { string errorMessage = log.FormatResourceString(errorResource, args); e.AssertLogDoesntContain(errorMessage); }
/* * Method: GetTypeLibNameForTypeLibAttrs * * Gets the name of given type library. */ internal static bool GetTypeLibNameForTypeLibAttrs(TaskLoggingHelper log, bool silent, TYPELIBATTR typeLibAttr, out string typeLibName) { typeLibName = ""; ITypeLib typeLib = null; try { // load our type library try { TYPELIBATTR attr = typeLibAttr; typeLib = (ITypeLib)NativeMethods.LoadRegTypeLib(ref attr.guid, attr.wMajorVerNum, attr.wMinorVerNum, attr.lcid); } catch (COMException ex) { if (!silent) { log.LogWarningWithCodeFromResources("ResolveComReference.CannotLoadTypeLib", typeLibAttr.guid, typeLibAttr.wMajorVerNum, typeLibAttr.wMinorVerNum, ex.Message); } return false; } string typeLibId = log.FormatResourceString("ResolveComReference.TypeLibAttrId", typeLibAttr.guid.ToString(), typeLibAttr.wMajorVerNum, typeLibAttr.wMinorVerNum); return GetTypeLibNameForITypeLib(log, silent, typeLib, typeLibId, out typeLibName); } finally { if (typeLib != null) Marshal.ReleaseComObject(typeLib); } }
/// <summary> /// A unique id string of this reference, it's either the item spec or (in the case of a dependency ref) /// guid and version from typelib attributes /// </summary> private string GetTypeLibId(TaskLoggingHelper log) { if (taskItem != null) { return taskItem.ItemSpec; } else { return log.FormatResourceString("ResolveComReference.TypeLibAttrId", attr.guid, attr.wMajorVerNum, attr.wMinorVerNum); } }