From 42a43c6fc82a5d7d88fb667443ff1775da0ed483 Mon Sep 17 00:00:00 2001 From: Mike Norman Date: Thu, 6 Jul 2017 13:51:41 -0500 Subject: Refactored DocStat, plus added stub for comparereport command --- tools/DocStat/DocStat/ParallelXmlHelper.cs | 53 ++++++++--------- tools/DocStat/DocStat/comparefix.cs | 92 ++++++++++++++++-------------- tools/DocStat/DocStat/comparereport.cs | 51 +++++++++++++++++ 3 files changed, 126 insertions(+), 70 deletions(-) create mode 100644 tools/DocStat/DocStat/comparereport.cs (limited to 'tools') diff --git a/tools/DocStat/DocStat/ParallelXmlHelper.cs b/tools/DocStat/DocStat/ParallelXmlHelper.cs index 69f05ea0..bac32386 100644 --- a/tools/DocStat/DocStat/ParallelXmlHelper.cs +++ b/tools/DocStat/DocStat/ParallelXmlHelper.cs @@ -99,38 +99,39 @@ namespace DocStat }; } - public static XElement ParallelElement(XElement sourceElement, - string sourcePath, - string sourceRoot, - string refRoot, - HashSet refPaths) + public static string GetParallelFilePathFor(string pathToTypeToFix, + string rootOfReferenceFiles, + string rootOfFilesToFix, + Func referenceRootTransform = null, + Func referencePathTransform = null) { - string parallelPath = GetParallelFilePathFor(sourcePath, refRoot, sourceRoot); + + string fullFixPath = Path.GetFullPath(pathToTypeToFix); - // bail early if we can + string fullFixRoot = Path.GetFullPath(rootOfFilesToFix); - if (!File.Exists(parallelPath) || !refPaths.Contains(parallelPath)) - return null; + rootOfReferenceFiles = + null == referenceRootTransform ? rootOfReferenceFiles : referenceRootTransform(rootOfReferenceFiles); + string fullRefRoot = Path.GetFullPath(rootOfReferenceFiles); - FileAttributes attr = File.GetAttributes(parallelPath); - if ((attr & FileAttributes.Directory) == FileAttributes.Directory) - return null; + string fullReferencePath = fullFixPath.Replace(fullFixRoot, fullRefRoot); - XDocument refToSearch = XDocument.Load(parallelPath); - Console.WriteLine("Found parallel document"); - var toReturn = GetSelectorFor(sourceElement).Invoke(refToSearch); - Console.WriteLine("Got the parallel element"); - return toReturn; + fullReferencePath = + null == referencePathTransform ? fullReferencePath : referencePathTransform(fullReferencePath); + return fullReferencePath; } - public static string GetParallelFilePathFor(string pathToTypeToFix, - string rootOfFilesToUse, - string rootOfFilesToFix) - { - string fullFixPath = Path.GetFullPath(pathToTypeToFix); - string fullFixRoot = Path.GetFullPath(rootOfFilesToFix); - string fullRefRoot = Path.GetFullPath(rootOfFilesToUse); - return fullFixPath.Replace(fullFixRoot, fullRefRoot); - } + public static XDocument GetParallelXDocFor(string parallelFilePath, + HashSet refPaths = null) + { + + if (!File.Exists(parallelFilePath)) + return null; + + if ((null != refPaths) && !refPaths.Contains(parallelFilePath)) + return null; + + return XDocument.Load(parallelFilePath); + } } } diff --git a/tools/DocStat/DocStat/comparefix.cs b/tools/DocStat/DocStat/comparefix.cs index 5bfd8c58..3520a670 100644 --- a/tools/DocStat/DocStat/comparefix.cs +++ b/tools/DocStat/DocStat/comparefix.cs @@ -22,7 +22,7 @@ namespace DocStat ref processlist, ref pattern); // must have - string filesToUseDir = ""; + string filesToUseAsRefDir = ""; // should have bool doSummaries = true; bool doParameters = true; @@ -35,7 +35,7 @@ namespace DocStat var opts = new OptionSet { {"f|fix=", (f) => filesToFixDir = f}, - {"u|using=", (u) => filesToUseDir = u}, + {"u|using=", (u) => filesToUseAsRefDir = u}, {"s|summaries", (s) => doSummaries = s != null}, {"a|params", (p) => doParameters = p != null }, {"r|retvals", (r) => doReturns = r != null }, @@ -47,80 +47,84 @@ namespace DocStat extras = opts.Parse(extras); CommandUtils.ThrowOnFiniteExtras(extras); - if (String.IsNullOrEmpty(filesToUseDir)) + if (String.IsNullOrEmpty(filesToUseAsRefDir)) throw new ArgumentException("You must supply a parallel directory from which to source new content with '[u|using]'=."); - IEnumerable toFix = CommandUtils.GetFileList(processlist, omitlist, filesToFixDir, pattern); - HashSet toUse = new HashSet(CommandUtils.GetFileList("", "", filesToUseDir, "")); + IEnumerable filesToFix = CommandUtils.GetFileList(processlist, omitlist, filesToFixDir, pattern); + HashSet filesToUseAsReference = new HashSet(CommandUtils.GetFileList("", "", filesToUseAsRefDir, "")); - toFix = toFix.Where((f) => toUse.Contains(ParallelXmlHelper.GetParallelFilePathFor(f, filesToUseDir, filesToFixDir))); + filesToFix = + filesToFix.Where((f) => + filesToUseAsReference.Contains(ParallelXmlHelper.GetParallelFilePathFor(f, + filesToUseAsRefDir, + filesToFixDir))); - // closure for lexical brevity in loop below - Action Fix = (XElement e, string f) => - { - Console.WriteLine(e.Name); - ParallelXmlHelper.Fix(e, ParallelXmlHelper.ParallelElement(e, - f, - filesToFixDir, - filesToUseDir, - toUse)); - }; - foreach (var f in toFix) + foreach (var f in filesToFix) { - bool changed = false; - XDocument fixie = XDocument.Load(f); + XDocument currentRefXDoc = ParallelXmlHelper.GetParallelXDocFor( + ParallelXmlHelper.GetParallelFilePathFor(f, filesToUseAsRefDir, filesToFixDir), + filesToUseAsReference + ); + + if (null == currentRefXDoc) + continue; + + Action fix = + (XElement e) => ParallelXmlHelper.Fix(e, ParallelXmlHelper.GetSelectorFor(e).Invoke(currentRefXDoc)); + + bool changed = false; + XDocument currentXDocToFix = XDocument.Load(f); EventHandler SetTrueIfChanged = null; SetTrueIfChanged = - new EventHandler((sender, e) => { fixie.Changed -= SetTrueIfChanged; changed = true; }); - fixie.Changed += SetTrueIfChanged; + new EventHandler((sender, e) => { currentXDocToFix.Changed -= SetTrueIfChanged; changed = true; }); + currentXDocToFix.Changed += SetTrueIfChanged; // (1) Fix ype-level summary and remarks: - XElement typeSummaryToFix = fixie.Element("Type").Element("Docs").Element("summary"); - Fix(typeSummaryToFix, f); + XElement typeSummaryToFix = currentXDocToFix.Element("Type").Element("Docs").Element("summary"); + fix(typeSummaryToFix); - XElement typeRemarksToFix = fixie.Element("Type").Element("Docs").Element("remarks"); - Fix(typeRemarksToFix, f); + XElement typeRemarksToFix = currentXDocToFix.Element("Type").Element("Docs").Element("remarks"); + fix(typeRemarksToFix); - var members = fixie.Element("Type").Element("Members"); + var members = currentXDocToFix.Element("Type").Element("Members"); if (null != members) { + foreach (XElement m in members.Elements(). - Where((XElement e) => ParallelXmlHelper.ParallelElement(e, - f, - filesToFixDir, - filesToUseDir, - toUse) != null)) + Where((XElement e) => null != ParallelXmlHelper.GetSelectorFor(e).Invoke(currentRefXDoc))) { // (2) Fix summary, remarks, return values, parameters, and typeparams - XElement summary = m.Element("Docs").Element("summary"); - Fix(summary, f); + XElement docsElement = m.Element("Docs"); + + XElement summary = docsElement.Element("summary"); + fix(summary); - XElement remarks = m.Element("Docs").Element("remarks"); + XElement remarks = docsElement.Element("remarks"); if (null != remarks) - Fix(remarks, f); + fix(remarks); - XElement returns = m.Element("Docs").Element("returns"); + XElement returns = docsElement.Element("returns"); if (null != returns) - Fix(returns, f); + fix(returns); - if (m.Element("Docs").Elements("param").Any()) + if (docsElement.Elements("param").Any()) { - IEnumerable _params = m.Element("Docs").Elements("param"); + IEnumerable _params = docsElement.Elements("param"); foreach (XElement p in _params) { - Fix(p, f); + fix(p); } } - if (m.Element("Docs").Elements("typeparam").Any()) + if (docsElement.Elements("typeparam").Any()) { - IEnumerable typeparams = m.Element("Docs").Elements("typeparam"); + IEnumerable typeparams = docsElement.Elements("typeparam"); foreach (XElement p in typeparams) { - Fix(p, f); + fix(p); } } } @@ -128,7 +132,7 @@ namespace DocStat if (changed) { - CommandUtils.WriteXDocument(fixie, f); + CommandUtils.WriteXDocument(currentXDocToFix, f); } } diff --git a/tools/DocStat/DocStat/comparereport.cs b/tools/DocStat/DocStat/comparereport.cs new file mode 100644 index 00000000..768a8de8 --- /dev/null +++ b/tools/DocStat/DocStat/comparereport.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Xml.Linq; +using Mono.Options; + +namespace DocStat +{ + public class CompareReportCommand : ApiCommand + { + + public override void Run(IEnumerable args) + { + throw new NotImplementedException(); + + string updatedDir = ""; + string omitlist = ""; + string processlist = ""; + string pattern = ""; + + List extras = CommandUtils.ProcessFileArgs(args, + ref updatedDir, + ref omitlist, + ref processlist, + ref pattern); + + string oldFiles = ""; + bool typeOnly = false; + + + if (String.IsNullOrEmpty(oldFiles)) + throw new ArgumentException("You must supply a parallel directory from which to source new content with '[u|using]'=."); + + + IEnumerable updated = CommandUtils.GetFileList(processlist, omitlist, updatedDir, pattern); + HashSet filesToUseAsReference = new HashSet(CommandUtils.GetFileList("", "", oldFiles, "")); + + updated = + updated.Where((f) => + filesToUseAsReference.Contains(ParallelXmlHelper.GetParallelFilePathFor(f, + oldFiles, + updatedDir))); + + + + // For each member in the updated files, report if the old one isn't present + + } + } +} -- cgit v1.2.3