Example #1
0
        public void WriteVariablesToDiskCache(GitVersionCacheKey cacheKey, VersionVariables variablesFromCache)
        {
            var cacheDir      = PrepareCacheDirectory();
            var cacheFileName = GetCacheFileName(cacheKey, cacheDir);

            variablesFromCache.FileName = cacheFileName;

            Dictionary <string, string> dictionary;

            using (log.IndentLog("Creating dictionary"))
            {
                dictionary = variablesFromCache.ToDictionary(x => x.Key, x => x.Value);
            }

            void WriteCacheOperation()
            {
                using var stream = fileSystem.OpenWrite(cacheFileName);
                using var sw     = new StreamWriter(stream);
                using (log.IndentLog("Storing version variables to cache file " + cacheFileName))
                {
                    var serializer = new Serializer();
                    serializer.Serialize(sw, dictionary);
                }
            }

            var retryOperation = new OperationWithExponentialBackoff <IOException>(new ThreadSleep(), log, WriteCacheOperation, maxRetries: 6);

            retryOperation.ExecuteAsync().Wait();
        }
Example #2
0
        public VersionVariables LoadVersionVariablesFromDiskCache(GitVersionCacheKey key)
        {
            using (log.IndentLog("Loading version variables from disk cache"))
            {
                var cacheDir = PrepareCacheDirectory();

                var cacheFileName = GetCacheFileName(key, cacheDir);
                if (!fileSystem.Exists(cacheFileName))
                {
                    log.Info("Cache file " + cacheFileName + " not found.");
                    return(null);
                }

                using (log.IndentLog("Deserializing version variables from cache file " + cacheFileName))
                {
                    try
                    {
                        var loadedVariables = VersionVariables.FromFile(cacheFileName, fileSystem);
                        return(loadedVariables);
                    }
                    catch (Exception ex)
                    {
                        log.Warning("Unable to read cache file " + cacheFileName + ", deleting it.");
                        log.Info(ex.ToString());
                        try
                        {
                            fileSystem.Delete(cacheFileName);
                        }
                        catch (Exception deleteEx)
                        {
                            log.Warning($"Unable to delete corrupted version cache file {cacheFileName}. Got {deleteEx.GetType().FullName} exception.");
                        }

                        return(null);
                    }
                }
            }
        }
Example #3
0
 private static string GetCacheFileName(GitVersionCacheKey key, string cacheDir)
 {
     return(Path.Combine(cacheDir, string.Concat(key.Value, ".yml")));
 }