Esempio n. 1
0
        public static ImportNode PathToTree(string path, RepositoryNode target = null)
        {
            var attributes = File.GetAttributes(path);
            var filename   = Path.GetFileNameWithoutExtension(path);
            var tree       = new ImportNode()
            {
                Path = path, Name = filename
            };

            if (attributes.HasFlag(FileAttributes.Directory))
            {
                tree.Type = EntryType.Folder;

                RepositoryNode folderNode = null;
                target?.TryGetFolder(filename, out folderNode);

                foreach (var file in Directory.GetFiles(path))
                {
                    var newNode = PathToTree(file, folderNode);

                    tree.SubNodes.Add(newNode);

                    if (newNode.FileExistsInConfig)
                    {
                        tree.TotalConflicts++;
                    }
                }
            }
            else
            {
                tree.Type = EntryType.Template;

                RepositoryNode fileNode = null;
                target?.TryGetFile(filename, out fileNode);

                if (fileNode != null)
                {
                    tree.FileExistsInConfig = true;
                    tree.TotalConflicts++;
                }
            }

            return(tree);
        }
Esempio n. 2
0
        private void ImportTreeToConfig(ImportNode tree, RepositoryNode target, OverwriteAction action)
        {
            foreach (var entry in tree.SubNodes)
            {
                if (entry.Type == EntryType.Folder)
                {
                    if (!target.TryGetFolder(entry.Name, out var folderNode))
                    {
                        folderNode = target.AddItem(EntryType.Folder, entry.Name);
                    }

                    ImportTreeToConfig(entry, folderNode, action);
                }
                else if (entry.Type == EntryType.Template)
                {
                    if (target.TryGetFile(entry.Name, out var fileNode))
                    {
                        switch (action)
                        {
                        case OverwriteAction.OverwriteAll:
                            fileNode.Content = File.ReadAllText(entry.Path);
                            break;

                        case OverwriteAction.SkipAll:
                            break;

                        case OverwriteAction.PromptEvery:
                            // TODO: prompt user for file overwriting
                            break;
                        }
                    }
                    else
                    {
                        target.AddItem(EntryType.Template, entry.Name, File.ReadAllText(entry.Path));
                    }
                }
            }
        }