コード例 #1
0
        /// <summary>
        /// Writes the contents of this object out to the specified file.
        /// TODO: once all derived classes from StateFileBase adopt new serialization, we shall consider to mode this into base class
        /// </summary>
        internal void SerializeCacheByTranslator(string stateFile, TaskLoggingHelper log)
        {
            try
            {
                if (!string.IsNullOrEmpty(stateFile))
                {
                    if (FileSystems.Default.FileExists(stateFile))
                    {
                        File.Delete(stateFile);
                    }

                    using var s = new FileStream(stateFile, FileMode.CreateNew);
                    var translator = BinaryTranslator.GetWriteTranslator(s);

                    // write file signature
                    translator.Writer.Write(TranslateContractSignature);
                    translator.Writer.Write(TranslateContractVersion);

                    Translate(translator);
                    isDirty = false;
                }
            }
            catch (Exception e) when(!ExceptionHandling.NotExpectedSerializationException(e))
            {
                // Not being able to serialize 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.CouldNotWriteStateFile", stateFile, e.Message);
            }
        }
コード例 #2
0
        public void VerifyAssemblyNameExSerializationWithRemappedFromByTranslator()
        {
            AssemblyNameExtension assemblyNameOriginal = new AssemblyNameExtension("System.Xml, Version=10.0.0.0, Culture=en, PublicKeyToken=b03f5f7f11d50a3a");
            AssemblyNameExtension assemblyRemappedFrom = new AssemblyNameExtension("System.Xml, Version=9.0.0.0, Culture=en, PublicKeyToken=b03f5f7f11d50a3a");

            assemblyRemappedFrom.MarkImmutable();
            assemblyNameOriginal.AddRemappedAssemblyName(assemblyRemappedFrom);
            assemblyNameOriginal.RemappedFromEnumerator.Count().ShouldBe(1);

            AssemblyNameExtension assemblyNameDeserialized = null;

            MemoryStream serializationStream = new MemoryStream();
            ITranslator  writeTranslator     = BinaryTranslator.GetWriteTranslator(serializationStream);

            writeTranslator.Translate(ref assemblyNameOriginal, (ITranslator t) => new AssemblyNameExtension(t));

            serializationStream.Seek(0, SeekOrigin.Begin);
            ITranslator readTranslator = BinaryTranslator.GetReadTranslator(serializationStream, null);

            readTranslator.Translate(ref assemblyNameDeserialized, (ITranslator t) => new AssemblyNameExtension(t));

            assemblyNameDeserialized.Equals(assemblyNameOriginal).ShouldBeTrue();
            assemblyNameDeserialized.RemappedFromEnumerator.Count().ShouldBe(1);
            assemblyNameDeserialized.RemappedFromEnumerator.First().ShouldBe(assemblyRemappedFrom);
        }
コード例 #3
0
        internal virtual void SerializeCache(string stateFile, TaskLoggingHelper log)
        {
            try
            {
                if (!string.IsNullOrEmpty(stateFile))
                {
                    if (FileSystems.Default.FileExists(stateFile))
                    {
                        File.Delete(stateFile);
                    }

                    using (var s = new FileStream(stateFile, FileMode.CreateNew))
                    {
                        var translator = BinaryTranslator.GetWriteTranslator(s);
                        translator.Translate(ref _serializedVersion);
                        Translate(translator);
                    }
                }
            }
            // If there was a problem writing the file (like it's read-only or locked on disk, for
            // example), then eat the exception and log a warning.  Otherwise, rethrow.
            catch (Exception e) when(!ExceptionHandling.NotExpectedSerializationException(e))
            {
                // Not being able to serialize 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.CouldNotWriteStateFile", stateFile, e.Message);
            }
        }
コード例 #4
0
        public void TestSerializationMode()
        {
            MemoryStream stream     = new MemoryStream();
            ITranslator  translator = BinaryTranslator.GetReadTranslator(stream, null);

            Assert.Equal(TranslationDirection.ReadFromStream, translator.Mode);

            translator = BinaryTranslator.GetWriteTranslator(stream);
            Assert.Equal(TranslationDirection.WriteToStream, translator.Mode);
        }
コード例 #5
0
        public void VerifyAssemblyNameExSerializationByTranslator(string assemblyName)
        {
            AssemblyNameExtension assemblyNameOriginal     = new AssemblyNameExtension(assemblyName);
            AssemblyNameExtension assemblyNameDeserialized = null;

            MemoryStream serializationStream = new MemoryStream();
            ITranslator  writeTranslator     = BinaryTranslator.GetWriteTranslator(serializationStream);

            writeTranslator.Translate(ref assemblyNameOriginal, (ITranslator t) => new AssemblyNameExtension(t));

            serializationStream.Seek(0, SeekOrigin.Begin);
            ITranslator readTranslator = BinaryTranslator.GetReadTranslator(serializationStream, null);

            readTranslator.Translate(ref assemblyNameDeserialized, (ITranslator t) => new AssemblyNameExtension(t));

            assemblyNameDeserialized.ShouldBe(assemblyNameOriginal);
        }
コード例 #6
0
        public static string SerializeCaches(IConfigCache configCache, IResultsCache resultsCache, string outputCacheFile)
        {
            ErrorUtilities.VerifyThrowInternalNull(outputCacheFile, nameof(outputCacheFile));

            try
            {
                if (string.IsNullOrWhiteSpace(outputCacheFile))
                {
                    return(ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword("EmptyOutputCacheFile"));
                }

                var fullPath = FileUtilities.NormalizePath(outputCacheFile);

                Directory.CreateDirectory(Path.GetDirectoryName(fullPath));

                // Use FileStream constructor (File.OpenWrite should not be used as it doesn't reset the length of the file!)
                using (var fileStream = new FileStream(fullPath, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    var translator = BinaryTranslator.GetWriteTranslator(fileStream);

                    ConfigCache  configCacheToSerialize  = null;
                    ResultsCache resultsCacheToSerialize = null;

                    switch (configCache)
                    {
                    case ConfigCache asConfigCache:
                        configCacheToSerialize = asConfigCache;
                        break;

                    case ConfigCacheWithOverride configCacheWithOverride:
                        configCacheToSerialize = configCacheWithOverride.CurrentCache;
                        break;

                    default:
                        ErrorUtilities.ThrowInternalErrorUnreachable();
                        break;
                    }

                    switch (resultsCache)
                    {
                    case ResultsCache asResultsCache:
                        resultsCacheToSerialize = asResultsCache;
                        break;

                    case ResultsCacheWithOverride resultsCacheWithOverride:
                        resultsCacheToSerialize = resultsCacheWithOverride.CurrentCache;
                        break;

                    default:
                        ErrorUtilities.ThrowInternalErrorUnreachable();
                        break;
                    }

                    translator.Translate(ref configCacheToSerialize);
                    translator.Translate(ref resultsCacheToSerialize);
                }
            }
            catch (Exception e)
            {
                return(ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword("ErrorWritingCacheFile", outputCacheFile, e.Message));
            }

            return(null);
        }
コード例 #7
0
 /// <summary>
 /// Gets a serializer used to write data.  Note that only one such serializer may be used from this class at a time.
 /// </summary>
 internal static ITranslator GetWriteTranslator()
 {
     s_serializationStream = new MemoryStream();
     return(BinaryTranslator.GetWriteTranslator(s_serializationStream));
 }
コード例 #8
0
ファイル: TaskTranslatorHelpers.cs プロジェクト: xen2/msbuild
 private ITranslator GetWriteTranslator()
 {
     _serializationStream = new MemoryStream();
     return(BinaryTranslator.GetWriteTranslator(_serializationStream));
 }