Exemplo n.º 1
0
        private static bool CheckHashesInFile(IEvfConsoleArgs evfConsoleArgs, string jsonPath,
                                              IReadOnlyDictionary <string, string> currentHashes)
        {
            Program.Logger.Information("Hash validation started.");

            var jsonFile = File.ReadAllText(jsonPath);

            if (!(JsonConvert.DeserializeObject(jsonFile, typeof(Dictionary <string, string>)) is
                  Dictionary <string, string> existingHashes))
            {
                Program.Logger.Error("Unable to deserialize the hash file {0} > ", jsonPath);
                return(false);
            }

            var errorCount = 0;

            foreach (var(key, value) in existingHashes)
            {
                if (!currentHashes.ContainsKey(key))
                {
                    errorCount++;
                    Program.Logger.Error("Unable to locate the file {0} in the folder {1}", key,
                                         evfConsoleArgs.EvolveFolderPath);
                    continue;
                }

                var correspondingCurrentHash = currentHashes[key];

                if (correspondingCurrentHash?.ToUpperInvariant() == value?.ToUpperInvariant())
                {
                    continue;
                }

                errorCount++;

                Program.Logger.Error(
                    "The file hash for {0} in the folder {1} changed unexpectedly.  CurrentHash : {2} , NewHash : {3}",
                    key, evfConsoleArgs.EvolveFolderPath, correspondingCurrentHash?.ToUpperInvariant(),
                    value?.ToUpperInvariant());
            }

            foreach (var(key, _) in currentHashes)
            {
                if (existingHashes.ContainsKey(key))
                {
                    continue;
                }

                errorCount++;
                Program.Logger.Error("Unable to locate the file {0} in the folder {1}", key,
                                     evfConsoleArgs.EvolveFolderPath);
            }

            Program.Logger.Information("Hash validation completed.");

            return(errorCount == 0);
        }
Exemplo n.º 2
0
        public static bool ChecksumFiles(IEvfConsoleArgs evfConsoleArgs)
        {
            var currentHashes = GetFileChecksumValuesInFolder(evfConsoleArgs.EvolveFolderPath);

            var destChecksumJsonPath   = Path.Combine(evfConsoleArgs.EvolveFolderPath, "_checksum.json");
            var sourceChecksumJsonPath = Path.Combine(evfConsoleArgs.EvolveFolderPath, "_source_checksum.json");

            if (File.Exists(destChecksumJsonPath) && !CheckHashesInFile(evfConsoleArgs, destChecksumJsonPath, currentHashes))
            {
                return(false);
            }
            if (!File.Exists(destChecksumJsonPath))
            {
                WriteHashesToFile(destChecksumJsonPath, currentHashes);
            }

            var existingHashes = new Dictionary <string, string>();

            if (File.Exists(sourceChecksumJsonPath))
            {
                var sourceFileJson = File.ReadAllText(sourceChecksumJsonPath);
                existingHashes =
                    JsonConvert.DeserializeObject(sourceFileJson, typeof(Dictionary <string, string>)) as
                    Dictionary <string, string>;
            }

            var sourceHashes = GetFileChecksumValuesInFolder(evfConsoleArgs.SourceFilesPath);

            var fileChanges = CheckForFileUpdates(existingHashes, sourceHashes);

            var nextVersionNumber = GetNextVersionNumber(currentHashes);

            Program.Logger.Information("The next version number will be {0}", nextVersionNumber);

            foreach (var fileChangeModel in fileChanges.FileChanges.OrderBy(f => f.Priority).ThenBy(f => f.FileName))
            {
                Program.Logger.Information("Will {2} > {0} | {1}", fileChangeModel.FileName, fileChangeModel.Priority,
                                           fileChangeModel.FileChangeType);
                nextVersionNumber = CopyFile(nextVersionNumber, fileChangeModel, evfConsoleArgs);
            }

            UpdateSourceHashes(evfConsoleArgs.SourceFilesPath, sourceChecksumJsonPath, existingHashes);

            if (fileChanges.FileChanges.Count > 0)
            {
                var updatedDestHashes = GetFileChecksumValuesInFolder(evfConsoleArgs.EvolveFolderPath);
                WriteHashesToFile(destChecksumJsonPath, updatedDestHashes);
                Program.Logger.Information("Updated destination path hash json file > {0}", destChecksumJsonPath);
            }
            else
            {
                Program.Logger.Information("No changes were detected.");
            }

            return(true);
        }
Exemplo n.º 3
0
        private static int CopyFile(int nextVersionNumber, FileChangeModel fileChangeModel,
                                    IEvfConsoleArgs evfConsoleArgs)
        {
            var sourceFile   = Path.Combine(evfConsoleArgs.SourceFilesPath, fileChangeModel.FileName);
            var destFileName = $"V{nextVersionNumber:0000}__{fileChangeModel.FileName}";
            var destFilePath = Path.Combine(evfConsoleArgs.EvolveFolderPath, destFileName);

            if (File.Exists(sourceFile))
            {
                Program.Logger.Verbose("Will copy {0} to {1}", sourceFile, destFilePath);
                File.Copy(sourceFile, destFilePath, false);
            }
            else
            {
                Program.Logger.Error("Unable to copy as the source file does not exist > {0} to {1}", sourceFile,
                                     destFilePath);
                return(nextVersionNumber);
            }

            return(nextVersionNumber + 1);
        }