/// <summary> <c> sourceProj </c> here overrides any sources specified in the <c> destProj </c> </summary> /// <param name="sourceProj"> Absolute or Relative path of Source <c> Proj </c>. </param> /// <param name="destProj"> Absolute or Relative path of Destination <c> Proj </c>. </param> internal DestinationProjLinker(string sourceProj, string destProj) : this(destProj) { SourceProjList = new List <string> { PathMaker.MakeAbsolutePathFromPossibleRelativePathOrDieTrying(null, sourceProj) }; }
private static List <string> GetProjectsFromFolders(string rootFolder, bool doSubDirectories = false) { string destinationsDirectory = rootFolder; if (!PathMaker.IsAbsolutePath(rootFolder)) { destinationsDirectory = PathMaker.MakeAbsolutePathFromPossibleRelativePathOrDieTrying(null, rootFolder); } if (Directory.Exists(destinationsDirectory)) { try { var destProjFiles = new List <string>(); SearchOption includeSubs = doSubDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; destProjFiles.AddRange(Directory.GetFiles(destinationsDirectory, "*.csproj", includeSubs)); destProjFiles.AddRange(Directory.GetFiles(destinationsDirectory, "*.vbproj", includeSubs)); return(destProjFiles); } catch (Exception e) { Crash(e, "Getting Projects in " + rootFolder + " didn't work. Bad name?"); } } return(new List <string>()); // because complain no return path }
/// <summary> Source <c>Proj</c> is specified in the destination <c>Proj</c> XML comment placeholder. </summary> /// <param name="destProj"> Absolute path of destination <c>Proj</c>. </param> internal DestinationProjLinker(string destProj) { DestProjAbsolutePath = PathMaker.MakeAbsolutePathFromPossibleRelativePathOrDieTrying(null, destProj); DestProjDirectory = Path.GetDirectoryName(DestProjAbsolutePath) ?? ""; if (string.IsNullOrEmpty(DestProjAbsolutePath)) { App.Crash("ERROR: No destProjFileAbsolutePath. That's a bug."); } try { destProjXml = new DestinationProjXml(DestProjAbsolutePath); } catch (Exception e) { App.Crash(e, "DestinationProjLinker CTOR (1 param) loading destination XML from " + DestProjAbsolutePath); } if (destProjXml.RootXelement == null || !destProjXml.RootXelement.Elements().Any()) { App.Crash("ERROR: Bad Destination Proj file at " + DestProjAbsolutePath); } SourceProjList = new List <string>(); ExclusionsList = new List <string>(); InclusionsList = new List <string>(); foreach (string line in destProjXml.StartPlaceHolder.Value.Split(new[] { "\r\n", "\n", Environment.NewLine }, StringSplitOptions.None).ToList()) { if (line.ToLower().Trim().StartsWith(Settings.SourcePlaceholderLowerCase)) { string sourceInXml = line.ToLower().Replace(Settings.SourcePlaceholderLowerCase, "").Replace("-->", "").Trim(); string absoluteSource = PathMaker.MakeAbsolutePathFromPossibleRelativePathOrDieTrying(DestProjDirectory, sourceInXml); SourceProjList.Add(absoluteSource); } if (line.ToLower().Trim().StartsWith(Settings.ExcludePlaceholderLowerCase)) { ExclusionsList.Add(line.ToLower().Replace(Settings.ExcludePlaceholderLowerCase, "").Trim().ToLower()); } if (line.ToLower().Trim().StartsWith(Settings.IncludePlaceholderLowerCase)) { InclusionsList.Add(line.ToLower().Replace(Settings.IncludePlaceholderLowerCase, "").Trim().ToLower()); } } if (InclusionsList == null || !InclusionsList.Any()) { InclusionsList.Add("*"); // default wildcard match will include everything. } }
internal DestinationProjXml(string destProjAbsolutePath) { try { DestProjAbsolutePath = PathMaker.MakeAbsolutePathFromPossibleRelativePathOrDieTrying(null, destProjAbsolutePath); DestProjDirectory = Path.GetDirectoryName(DestProjAbsolutePath) ?? ""; DestProjXdoc = XDocument.Load(DestProjAbsolutePath); RootXelement = DestProjXdoc.Element(Settings.MSBuild + "Project"); ItemGroups = RootXelement?.Elements(Settings.MSBuild + "ItemGroup").ToList(); } catch (Exception e) { App.Crash(e, $"Crash: DestProjXml CTOR loading destination XML from {DestProjAbsolutePath}"); } if (RootXelement == null) { App.Crash($"Crash: No MSBuild Namespace in {DestProjAbsolutePath}"); } StartPlaceHolder = FindStartOrEndCommentOrCrashIfDuplicatesFound(Settings.StartPlaceholderComment); EndPlaceHolder = FindStartOrEndCommentOrCrashIfDuplicatesFound(Settings.EndPlaceholderComment); if (StartPlaceHolder == null && RootXelement != null) { try { XElement lastItemGroup = ItemGroups?.Last(); lastItemGroup?.AddAfterSelf(new XComment(Settings.EndPlaceholderComment)); lastItemGroup?.AddAfterSelf(new XComment(Settings.StartPlaceholderComment)); StartPlaceHolder = FindStartOrEndCommentOrCrashIfDuplicatesFound(Settings.StartPlaceholderComment); EndPlaceHolder = FindStartOrEndCommentOrCrashIfDuplicatesFound(Settings.EndPlaceholderComment); Log.WriteLine("No placeholders in destination Project. Added them", ConsoleColor.Yellow); } catch (Exception e) { App.Crash(e, $"Crash: DestProjXmladding placeholders to destination XML in {DestProjAbsolutePath}"); } } OldLinkedXml = ReadLinkedXml(); Keepers = new List <XElement>(); }
/// <summary> Source <c> Proj </c> is specified in the destination <c> Proj </c> XML comment placeholder. </summary> /// <param name="destProj"> Absolute path of destination <c> Proj </c>. </param> internal DestinationProjLinker(string destProj) { DestProjAbsolutePath = PathMaker.MakeAbsolutePathFromPossibleRelativePathOrDieTrying(null, destProj); DestProjDirectory = Path.GetDirectoryName(DestProjAbsolutePath) ?? ""; if (string.IsNullOrEmpty(DestProjAbsolutePath)) { App.Crash("ERROR: No destProjFileAbsolutePath. That's a bug."); } try { destProjXml = new DestinationProjXml(DestProjAbsolutePath); if (destProjXml.RootXelement == null || !destProjXml.RootXelement.Elements().Any()) { App.Crash($"ERROR: Bad Destination Proj file at {DestProjAbsolutePath}. Root elemental null?: {destProjXml?.RootXelement == null}" + $", Root elements count: {destProjXml.RootXelement?.Elements()?.Count()}"); } } catch (Exception e) { App.Crash(e, "DestinationProjLinker CTOR (1 param) loading destination XML from " + DestProjAbsolutePath); } SourceProjList = new List <string>(); ExclusionsList = new List <string>(); InclusionsList = new List <string>(); foreach (string line in destProjXml.StartPlaceHolder.Value.Split(new[] { "\r\n", "\n", Environment.NewLine }, StringSplitOptions.None).ToList()) { try { if (line.ToLower().Trim().StartsWith(Settings.SourcePlaceholderLowerCase, StringComparison.Ordinal)) { string sourceInXml = line.Replace(Settings.SourcePlaceholderLowerCase, "") .Replace(Settings.SourcePlaceholder, "") .Replace("-->", "").Replace("\"", "").Trim(); string absoluteSource = PathMaker.MakeAbsolutePathFromPossibleRelativePathOrDieTrying(DestProjDirectory, sourceInXml); SourceProjList.Add(absoluteSource); } else if (line.ToLower().Trim().StartsWith(Settings.ExcludePlaceholderLowerCase, StringComparison.Ordinal)) { ExclusionsList.Add(line.ToLower().Replace(Settings.ExcludePlaceholderLowerCase, "").Trim()); } else if (line.ToLower().Trim().StartsWith(Settings.IncludePlaceholderLowerCase, StringComparison.Ordinal)) { InclusionsList.Add(line.ToLower().Replace(Settings.IncludePlaceholderLowerCase, "").Trim()); } } catch (Exception e) { App.Crash(e, $"broke parsing the Code Linker placeholder at: {line}"); } } foreach (string exclusion in ExclusionsList) { Log.WriteLine("exclusion: " + exclusion, ConsoleColor.DarkYellow); } foreach (string inclusion in InclusionsList) { Log.WriteLine("inclusion: " + inclusion, ConsoleColor.White, ConsoleColor.DarkGreen); } if (!InclusionsList.Any()) { InclusionsList.Add("*"); // default wildcard match will include everything. } }
public static void ParseCommands(string[] args) { linkers.Clear(); // System.Diagnostics.Debugger.Launch(); // to find teh bugs load this in Visual Studio and uncomment the start of this line. int argsCount = args.Length; if (argsCount == 0) { Log.WriteLine("No Args given so Help Text Displayed.", ConsoleColor.Red); Log.WriteLine(); Help.Write(); Finish(); } List <string> argsList = args.Select(a => a.Replace(@"""", "")).ToList(); if (argsList.Contains("/?")) { Help.Write(); Log.WriteLine("User asked For Help. Hope I helped.", ConsoleColor.Green); Finish(); } bool doSubDirectories = argsList.Contains("/s", StringComparer.CurrentCultureIgnoreCase); bool createSubDirectories = !argsList.Contains("/nosub", StringComparer.CurrentCultureIgnoreCase); Log.WriteToConsole = !argsList.Contains("/stfu", StringComparer.CurrentCultureIgnoreCase); NoConfirm = argsList.Contains("/noconfirm", StringComparer.CurrentCultureIgnoreCase); if (argsList.Contains("/abs", StringComparer.CurrentCultureIgnoreCase)) { PathMaker.UseRelativePaths = false; } string prefixArg = args.FirstOrDefault(arg => arg.StartsWith("/prefix:", StringComparison.OrdinalIgnoreCase)); if (prefixArg?.Any() ?? false) { string prefix = prefixArg.Substring(8).Replace("\"", ""); DestinationProjLinker.LinkPrefix = prefix; } if (!string.IsNullOrEmpty(argsList[0])) { if (argsList[0].IsaCsOrVbProjFile()) { if (argsCount > 1 && args[1].IsaCsOrVbProjFile()) { Log.WriteLine("Queueing Code Link from: " + argsList[0], ConsoleColor.Cyan); Log.WriteLine(" to: " + argsList[1], ConsoleColor.Cyan); Log.WriteLine(); linkers.Add(new DestinationProjLinker(argsList[0], argsList[1])); } else { Log.WriteLine("Queueing Code Link to: " + argsList[0] + ". Source TBA.", ConsoleColor.Cyan); linkers.Add(new DestinationProjLinker(argsList[0])); } } else if (argsList[0].ToLower() == "strip") { if (argsCount > 1) { if (args[1].IsaCsOrVbProjFile()) { var destinationProjXml = new DestinationProjXml(args[1]); destinationProjXml.ClearOldLinkedCode(); destinationProjXml.Save(); Finish("Stripped all code from " + args[1]); } else { try { List <string> destProjFiles = GetProjectsFromFolders(argsList[1], doSubDirectories); foreach (string destProjFile in destProjFiles) { Log.WriteLine("Stripping Code from: " + destProjFile + ". ", ConsoleColor.Yellow); var destinationProjXml = new DestinationProjXml(destProjFile); destinationProjXml.ClearOldLinkedCode(); destinationProjXml.Save(); } } catch (Exception e) { Crash(e, "Stripping Code from Folder: " + args[1] + " didn't work. Bad name?"); } Finish("Stripped all code"); } } } else if (argsList[0].ToLower() == "new") { if (argsCount > 2) { if (args[1].IsaCsOrVbProjFile()) { string sourcePath = PathMaker.MakeAbsolutePathFromPossibleRelativePathOrDieTrying(null, args[1]); try { ProjectMaker.NewProject(sourcePath, args[2], createSubDirectories); } catch (Exception e) { Crash(e, "Linking " + args[1] + " to " + args[2] + " didn't work. Bad name?"); } Finish("Linked " + " from " + args[1] + " to " + args[2]); } else { try { List <string> sourceProjFiles = GetProjectsFromFolders(argsList[1], doSubDirectories); foreach (string sourceProjFile in sourceProjFiles) { if (sourceProjFile.IsaCsOrVbProjFile()) { string sourcePath = PathMaker.MakeAbsolutePathFromPossibleRelativePathOrDieTrying(null, sourceProjFile); try { ProjectMaker.NewProject(sourcePath, args[2], createSubDirectories); } catch (Exception e) { Crash(e, "Linking " + sourceProjFile + " to " + args[2] + " didn't work. Bad name?"); } Log.WriteLine("Linked " + " from " + sourceProjFile + " to " + args[2], ConsoleColor.Green); } else { Log.WriteLine("ERROR: " + sourceProjFile + " is not a project file. Cannot Link it.", ConsoleColor.Green); } } } catch (Exception e) { Crash(e, "Linking Projects from Folder: " + args[1] + " didn't work. Bad name?"); } Finish("Linked Projects"); } } } // /NewProject else // vanilla Link command with a folder { try { List <string> destProjFiles = GetProjectsFromFolders(argsList[0], doSubDirectories); foreach (string destProjFile in destProjFiles) { Log.WriteLine("Queueing Code Link to: " + destProjFile + ". Source TBA.", ConsoleColor.Cyan); linkers.Add(new DestinationProjLinker(destProjFile)); } } catch (Exception e) { Crash(e, "Queueing Code Link didn't work. Bad file name?"); } } if (!linkers.Any()) { string errorMessage = "I got nuthin. Your Args made no sense to me." + Environment.NewLine; foreach (string arg in args) { errorMessage += arg + Environment.NewLine; } Crash(errorMessage); } foreach (DestinationProjLinker destinationProjLinker in linkers) { destinationProjLinker.LinkCode(); } } }