Esempio n. 1
0
        public FolderVersionEntry CreateDiff(IEnumerable <string> currentFilesList)
        {
            FolderVersionEntry[] currentHistory = ReadCurrentHistory();

            var lastFilesVersion = new List <FileHistoryEntry>();

            foreach (var historyEntry in currentHistory)
            {
                lastFilesVersion.AddRange(historyEntry.Files.Where(f => f.EditType == FileHistoryType.Added));

                foreach (FileHistoryEntry deletedEntry in historyEntry.Files.Where(fv => fv.EditType == FileHistoryType.Deleted))
                {
                    FileHistoryEntry entryToRemove = lastFilesVersion.FirstOrDefault(fv => fv.Path == deletedEntry.Path);
                    if (entryToRemove != null)
                    {
                        lastFilesVersion.Remove(entryToRemove);
                    }
                    else
                    {
                        //delete entry without added file, an error?
                    }
                }
            }

            FolderVersionEntry currentVersion = new FolderVersionEntry();

            List <FileHistoryEntry> touchedEntries = new List <FileHistoryEntry>();

            foreach (string currentFilePath in currentFilesList)
            {
                string           currentFileHash = hashService.ComputeFileHash(currentFilePath);
                FileHistoryEntry fileLastVersion = lastFilesVersion.FirstOrDefault(f => f.Path == currentFilePath);

                if (fileLastVersion != null)
                {
                    if (fileLastVersion.Hash != currentFileHash)
                    {
                        currentVersion.AddEntry(currentFilePath, currentFileHash, FileHistoryType.Modified);
                    }

                    touchedEntries.Add(fileLastVersion);
                }
                else
                {
                    currentVersion.AddEntry(currentFilePath, currentFileHash, FileHistoryType.Added);
                }
            }

            foreach (FileHistoryEntry deletedEntry in lastFilesVersion.Except(touchedEntries))
            {
                currentVersion.AddEntry(deletedEntry.Path, string.Empty, FileHistoryType.Deleted);
            }

            SaveHistory(currentVersion);

            return(currentVersion);
        }
Esempio n. 2
0
        protected async override Task RunCommandAsync()
        {
            PackageConfiguration config = null;

            if (!configService.TryGetConfig(out config))
            {
                uiService.AddMessage("No config inited, can not compute diff");
                return;
            }

            string newTag = await GetTagName(config);

            Dictionary <string, string> actualPathToHashMap = new Dictionary <string, string>();

            foreach (var filePath in fileService.GetWorkingDirectoryFiles(config.ExcludePaths))
            {
                actualPathToHashMap.Add(filePath, hashService.ComputeFileHash(filePath));
            }

            Dictionary <string, string> tagPathToHashMap =
                await onlineStoreService.GetPackageFilesAtVersionAsync(config.Name, config.Tag);

            bool hasChanges =
                tagPathToHashMap.Keys.Except(actualPathToHashMap.Keys).Any() ||
                actualPathToHashMap.Keys.Except(tagPathToHashMap.Keys).Any();

            if (!hasChanges)
            {
                foreach (var pathToHashMap in tagPathToHashMap)
                {
                    if (actualPathToHashMap[pathToHashMap.Key] != pathToHashMap.Value)
                    {
                        hasChanges = true;
                        break;
                    }
                }
            }

            if (!hasChanges)
            {
                uiService.AddMessage("No difference with previous version. Can not add tag");
                return;
            }

            FolderVersionEntry folderVersion = versioningService.CreateDiff(tagPathToHashMap, actualPathToHashMap);

            ConsoleColor?statusToColor(FileHistoryType status)
            {
                switch (status)
                {
                case FileHistoryType.Added:
                    return(ConsoleColor.Green);

                case FileHistoryType.Modified:
                    return(ConsoleColor.Yellow);

                case FileHistoryType.Deleted:
                    return(ConsoleColor.Red);

                default:
                    return(null);
                }
            }

            uiService.AddMessage($"Created {config.Name}@{newTag}");
            uiService.AddMessage("List of changes:");
            foreach (FileHistoryEntry fileHistoryEntry in folderVersion.Files)
            {
                uiService.AddMessage(
                    message: $"\t[{fileHistoryEntry.EditType.ToString().ToLower()}]\t{Path.GetFileName(fileHistoryEntry.Path)}",
                    color: statusToColor(fileHistoryEntry.EditType));
            }

            configService.SetTag(newTag);

            await onlineStoreService.PushPackageAsync($"{config.Name}@{newTag}", folderVersion);
        }