public static void AddNode(this IEnumerable <RefsNode> refsNodes, RefsNode node, bool optional = false) { if (refsNodes == null) { throw new ArgumentNullException($"{nameof(refsNodes)} cannot be null for {nameof(AddNode)}."); } if (node == null) { throw new ArgumentNullException($"{nameof(node)} cannot be null for {nameof(AddNode)}."); } RefsNode previousNode = null; foreach (var rootNode in refsNodes.ToArray()) { } }
/// <summary> /// /// </summary> /// <param name="preferAlias"></param> /// <returns></returns> /// <exception cref="InvalidOperationException">Thrown when the file doesn't have a valid path in the tree.</exception> public string GetFilename(bool preferAlias = false) { if (preferAlias && !string.IsNullOrEmpty(Alias)) { return(Alias); } if (!string.IsNullOrEmpty(Filename)) { return(Filename); } if (!string.IsNullOrEmpty(RelativePath)) { return(Path.GetFileName(RelativePath)); } string name = File; RefsNode next = Parent; while (!(name.Contains(Path.DirectorySeparatorChar) || name.Contains(Path.AltDirectorySeparatorChar))) { if (next is LeafNode leaf) { name = leaf.LeafData + name; next = next.Parent; } else { break; } } //if (name.Contains(Path.DirectorySeparatorChar) || name.Contains(Path.AltDirectorySeparatorChar)) //{ try { Filename = Path.GetFileName(name); return(Filename); } catch (ArgumentException) { } //} throw new InvalidOperationException($"FileNode '{File}' doesn't appear to be a valid path"); }
public override bool InsertReference(FileEntry fileEntry, bool optional = false) { if (base.InsertReference(fileEntry, optional)) { return(true); } CommandNode newCommand; RefsNode currentNode = this; CommandNode.CommandType commandType = fileEntry.PathSource == CommandNode.PromptSourceTag ? CommandNode.CommandType.Prompt : CommandNode.CommandType.From; Add(new CommandNode(CommandNode.CommandType.EmptyLine, null)); if (optional) { newCommand = new CommandNode(CommandNode.CommandType.OptionalBlock, null); Add(newCommand); currentNode = newCommand; } newCommand = new CommandNode(commandType, fileEntry.PathSource); currentNode.Add(newCommand); return(newCommand.InsertReference(fileEntry, optional)); }
public RootNode ReadFile() { if (!FileExists) { return(null); } RootNode rootNode = new RootNode(); CommandNode currentCommand = null; RefsNode currentNode = null; string depsFile = File.ReadAllText(_refsFilePath); var lineNo = 0; int currentLevel; int nextLevel; string[] allLines = depsFile.Split(new[] { Environment.NewLine, "\n", "\r" }, StringSplitOptions.None); string currentLine = allLines[0]; string nextLine; for (int i = 0; i < allLines.Length; i++) { if (i + 1 < allLines.Length) { nextLine = allLines[i + 1]; } else { nextLine = string.Empty; } var currentParts = currentLine.Split('"'); var nextParts = nextLine?.Split('"') ?? Array.Empty <string>(); var path = currentParts.Last(); currentLevel = currentParts.Length - 2; nextLevel = Math.Max(nextParts.Length - 2, 0); if (path.StartsWith("::") || path == string.Empty) { // pseudo-command if (currentNode is CommandNode currentCmd && currentCmd.Command == CommandNode.CommandType.OptionalBlock) { currentNode = new CommandNode(currentLine); currentCommand.Add(currentNode); } else if (path.StartsWith("::endopt")) { while (currentNode.Parent != null) { currentNode = currentNode.Parent; if (currentNode is CommandNode commandNode && commandNode.Command == CommandNode.CommandType.OptionalBlock) { currentNode = currentNode.Parent; break; } } } else { currentCommand = new CommandNode(currentLine); currentNode = currentCommand; rootNode.Add(currentCommand); } currentLine = nextLine; continue; } if (currentLevel < nextLevel) { LeafNode toAdd = new LeafNode(currentLine); currentNode.Add(toAdd); currentNode = toAdd; } else if (currentLevel == nextLevel) { currentNode.Add(new FileNode(currentLine)); } else if (currentLevel > nextLevel) { currentNode.Add(new FileNode(currentLine)); while (currentNode.NodeDepth > nextLevel) { currentNode = currentNode.Parent; } } lineNo++; currentLine = nextLine; }
protected override void SetParent(RefsNode newParent) { throw new NotSupportedException("RootNodes cannot have parents."); }