Skip to content

Commit

Permalink
Reformatted all code.
Browse files Browse the repository at this point in the history
Even if you still don't like it at least you don't like all of it now instead of just parts of it.
  • Loading branch information
CADbloke committed Nov 3, 2015
1 parent 31a5a8d commit 2754e71
Show file tree
Hide file tree
Showing 11 changed files with 386 additions and 177 deletions.
104 changes: 68 additions & 36 deletions CodeLinker/DestinationProjLinker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,26 @@ internal class DestinationProjLinker
internal DestinationProjLinker(string destProj)
{
DestProjAbsolutePath = PathMaker.MakeAbsolutePathFromPossibleRelativePathOrDieTrying(null, destProj);
DestProjDirectory = Path.GetDirectoryName(DestProjAbsolutePath) ?? "";
DestProjDirectory = Path.GetDirectoryName(DestProjAbsolutePath) ?? "";

if (string.IsNullOrEmpty(DestProjAbsolutePath))
{
Linker.Crash("ERROR: No destProjFileAbsolutePath. That's a bug.");
}

try { destProjXml = new DestinationProjXml(DestProjAbsolutePath); }
try
{
destProjXml = new DestinationProjXml(DestProjAbsolutePath);
}
catch (Exception e)
{
Linker.Crash(e, "DestinationProjLinker CTOR (1 param) loading destination XML from " + DestProjAbsolutePath);
}

if (destProjXml.RootXelement == null || !destProjXml.RootXelement.Elements().Any())
{
Linker.Crash("ERROR: Bad Destination Proj file at " + DestProjAbsolutePath);
}

SourceProjList = new List<string>();
ExclusionsList = new List<string>();
Expand All @@ -64,19 +71,26 @@ internal DestinationProjLinker(string destProj)
}

if (line.ToLower().Trim().StartsWith(Settings.ExcludePlaceholderLowerCase))
{ ExclusionsList.Add(line.ToLower().Replace(Settings.ExcludePlaceholderLowerCase, "").Trim().ToLower()); }
{
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()); }
{
InclusionsList.Add(line.ToLower().Replace(Settings.IncludePlaceholderLowerCase, "").Trim().ToLower());
}
}
if (InclusionsList == null || !InclusionsList.Any())
{
InclusionsList.Add("*"); // default wildcard match will include everything.
}
if (InclusionsList == null || !InclusionsList.Any()) { InclusionsList.Add("*"); }
}


/// <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)
internal DestinationProjLinker(string sourceProj, string destProj) : this(destProj)
{
SourceProjList = new List<string> {PathMaker.MakeAbsolutePathFromPossibleRelativePathOrDieTrying(null, sourceProj)};
}
Expand All @@ -103,10 +117,12 @@ internal void LinkCode()
: Path.Combine(DestProjDirectory, sourcePath);

string sourceProjDirectory = Path.GetDirectoryName(sourceProjAbsolutePath);

string destDirectoryForRelativePath = DestProjDirectory.EndsWith("\\")
? DestProjDirectory
: DestProjDirectory + "\\";
string linkRelativeSource = PathMaker.MakeRelativePath(destDirectoryForRelativePath , sourceProjAbsolutePath);

string linkRelativeSource = PathMaker.MakeRelativePath(destDirectoryForRelativePath, sourceProjAbsolutePath);

SourceProjParser sourceProjParser = new SourceProjParser(sourceProjAbsolutePath);

Expand All @@ -122,30 +138,34 @@ internal void LinkCode()
foreach (XElement sourceItem in sourceItemGroup.Elements())
{
string sourceElementName = sourceItem.Name.LocalName;
if (Settings.ItemElementsToSkip.Contains(sourceElementName.ToLower())) { continue; }

if (Settings.ItemElementsToSkip.Contains(sourceElementName.ToLower()))
{
continue;
}

XAttribute attrib = sourceItem.Attribute("Include") ?? sourceItem.Attribute("Exclude");

if (attrib != null)
{
string originalSourcePath = attrib.Value;
string originalSourcePath = attrib.Value;
string trimmedOriginalSourcePath = originalSourcePath.Trim().ToLower();

IEnumerable<string> exclude = ExclusionsList
.Where(x => Operators.LikeString(trimmedOriginalSourcePath, x, CompareMethod.Text)).ToList(); // OW my eyes!
IEnumerable<string> exclude =
ExclusionsList.Where(x => Operators.LikeString(trimmedOriginalSourcePath, x, CompareMethod.Text)).ToList(); // OW my eyes!

if ( exclude.Any() )
if (exclude.Any())
{
Log.WriteLine( "Excluded: " + originalSourcePath + Environment.NewLine +
" from: " + sourceProjAbsolutePath + Environment.NewLine +
"because you said to Exclude: " + exclude.FirstOrDefault() + Environment.NewLine);
Log.WriteLine("Excluded: " + originalSourcePath + Environment.NewLine +
" from: " + sourceProjAbsolutePath + Environment.NewLine +
"because you said to Exclude: " + exclude.FirstOrDefault() + Environment.NewLine);
continue;
}

List<string> include = InclusionsList
.Where(i => Operators.LikeString(trimmedOriginalSourcePath, i, CompareMethod.Text)).ToList(); // OW my eyes!
List<string> include =
InclusionsList.Where(i => Operators.LikeString(trimmedOriginalSourcePath, i, CompareMethod.Text)).ToList(); // OW my eyes!

if ( !InclusionsList.Any() || include.Any() )
if (!InclusionsList.Any() || include.Any())
{
if (!PathMaker.IsAbsolutePath(originalSourcePath))
{
Expand All @@ -154,63 +174,75 @@ internal void LinkCode()
{
string sourceFileName = Path.GetFileName(originalSourcePath); // wildcards blow up Path.GetFullPath()
string originalFolder = originalSourcePath;
if (!string.IsNullOrEmpty(sourceFileName)) originalFolder = originalSourcePath.Replace(sourceFileName, "");

if (!string.IsNullOrEmpty(sourceFileName))
{
originalFolder = originalSourcePath.Replace(sourceFileName, "");
}
sourceAbsolutePath = Path.GetFullPath(sourceProjDirectory + "\\" + originalFolder) + sourceFileName;
}
catch (Exception e) {
catch (Exception e)
{
Linker.Crash(e, "Recycling. GetFullPath: " + sourceProjDirectory + "\\" + originalSourcePath);
}

string relativePathFromDestination = PathMaker.MakeRelativePath(DestProjDirectory + "\\", sourceAbsolutePath);

if (!Settings.ItemElementsDoNotMakeRelativePath.Contains(sourceElementName.ToLower()))
{
attrib.Value = relativePathFromDestination;
}
}

IEnumerable<XElement> links = sourceItem.Descendants(Settings.MSBuild + "Link");
if (!(links.Any() || Settings.ItemElementsDoNotBreakLink.Contains(sourceElementName.ToLower()))) // Folders, mostly

if (!(links.Any() || Settings.ItemElementsDoNotBreakLink.Contains(sourceElementName.ToLower()))) // Folders, mostly
{
XElement linkElement = new XElement(Settings.MSBuild + "Link", originalSourcePath);
sourceItem.Add(linkElement);
}
newLinkedItemGroup.Add(sourceItem);
codezLinked++;

}
else
{
Log.WriteLine( "Excluded: " + originalSourcePath + Environment.NewLine +
" from: " + sourceProjAbsolutePath + Environment.NewLine +
"because it did not match anything on the Include: list " + Environment.NewLine);
Log.WriteLine("Excluded: " + originalSourcePath + Environment.NewLine +
" from: " + sourceProjAbsolutePath + Environment.NewLine +
"because it did not match anything on the Include: list " + Environment.NewLine);
}

}
}

if (newLinkedItemGroup.HasElements) { destProjXml.EndPlaceHolder.AddBeforeSelf(newLinkedItemGroup); }
if (newLinkedItemGroup.HasElements)
{
destProjXml.EndPlaceHolder.AddBeforeSelf(newLinkedItemGroup);
}
}
destProjXml.EndPlaceHolder.AddBeforeSelf(new XComment("End LinkCodez from " + linkRelativeSource+ Environment.NewLine +
"Linked " + codezLinked + " codez."));
destProjXml.EndPlaceHolder.AddBeforeSelf(new XComment("End LinkCodez from " + linkRelativeSource + Environment.NewLine +
"Linked " + codezLinked + " codez."));
totalCodezLinked += codezLinked;

destProjXml.Keepers.RemoveAll(k => k.Attribute("Include").Value.Contains(sourceProjDirectory));
}
catch (Exception e) {
catch (Exception e)
{
Linker.Crash(e, "Recycling " + sourcePath + " to " + DestProjAbsolutePath);
}
}


destProjXml.EndPlaceHolder.AddBeforeSelf(new XComment("End of Linked Code" + Environment.NewLine +
"See CodeLinkerLog.txt for details. CodeLinker by " + Settings.SourceCodeUrl + " "));
"See CodeLinkerLog.txt for details. CodeLinker by " + Settings.SourceCodeUrl + " "));

if (oldXml != destProjXml.ReadLinkedXml())
{
destProjXml.Save();
Log.WriteLine("Linked " + totalCodezLinked + " codez from " + SourceProjList.Count + " source Project(s).");
}
else Log.WriteLine("No changes, didn't save.");
else
{
Log.WriteLine("No changes, didn't save.");
}

Log.WriteLine("----------------------------");
Log.WriteLine();
Expand Down
73 changes: 46 additions & 27 deletions CodeLinker/DestinationProjXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,42 @@ internal class DestinationProjXml
internal XComment EndPlaceHolder;
internal List<XElement> ItemGroups;
internal XElement RootXelement;
internal string OldLinkedXml;
internal List<XElement> Keepers;
internal string OldLinkedXml; // To see if this has changed and needs to be resaved
internal List<XElement> Keepers; // These belong outside the Link Zone


internal DestinationProjXml(string destProj)
{
DestProjAbsolutePath = PathMaker.MakeAbsolutePathFromPossibleRelativePathOrDieTrying(null, destProj);
DestProjDirectory = Path.GetDirectoryName(DestProjAbsolutePath) ?? "";
DestProjDirectory = Path.GetDirectoryName(DestProjAbsolutePath) ?? "";

try
{
DestProjXdoc = XDocument.Load(DestProjAbsolutePath);
RootXelement = DestProjXdoc.Element(Settings.MSBuild + "Project");
ItemGroups = RootXelement?.Elements(Settings.MSBuild + "ItemGroup").ToList();
ItemGroups = RootXelement?.Elements(Settings.MSBuild + "ItemGroup").ToList();
}
catch (Exception e)
{ Linker.Crash(e, "Crash: DestProjXml CTOR loading destination XML from " + DestProjAbsolutePath); }
{
Linker.Crash(e, "Crash: DestProjXml CTOR loading destination XML from " + DestProjAbsolutePath);
}

if (RootXelement == null)
{ Linker.Crash("Crash: No MSBuild Namespace in " + DestProjAbsolutePath); }
{
Linker.Crash("Crash: No MSBuild Namespace in " + DestProjAbsolutePath);
}

StartPlaceHolder = FindCommentOrCrashIfDuplicatesFound(Settings.StartPlaceholderComment);
EndPlaceHolder = FindCommentOrCrashIfDuplicatesFound(Settings.EndPlaceholderComment);
EndPlaceHolder = FindCommentOrCrashIfDuplicatesFound(Settings.EndPlaceholderComment);

if (StartPlaceHolder == null && RootXelement != null)
{
XElement lastItemGroup = ItemGroups?.Last();
lastItemGroup?.AddAfterSelf(new XComment( Settings.EndPlaceholderComment ));
lastItemGroup?.AddAfterSelf(new XComment( Settings.StartPlaceholderComment ));
StartPlaceHolder = FindCommentOrCrashIfDuplicatesFound(Settings.StartPlaceholderComment);
EndPlaceHolder = FindCommentOrCrashIfDuplicatesFound(Settings.EndPlaceholderComment);
}
{
XElement lastItemGroup = ItemGroups?.Last();
lastItemGroup?.AddAfterSelf(new XComment(Settings.EndPlaceholderComment));
lastItemGroup?.AddAfterSelf(new XComment(Settings.StartPlaceholderComment));
StartPlaceHolder = FindCommentOrCrashIfDuplicatesFound(Settings.StartPlaceholderComment);
EndPlaceHolder = FindCommentOrCrashIfDuplicatesFound(Settings.EndPlaceholderComment);
}

OldLinkedXml = ReadLinkedXml();
Keepers = new List<XElement>();
Expand All @@ -72,8 +76,6 @@ internal void ClearOldLinkedCode()
{
string oldLinkedXml = "<root>" + OldLinkedXml + "</root>";



if (oldLinkedXml.Contains("ItemGroup"))
{
XElement keeperElements = XElement.Parse(oldLinkedXml); // http://stackoverflow.com/a/11644640/492
Expand All @@ -82,13 +84,12 @@ internal void ClearOldLinkedCode()
{
XAttribute xAttribute = descendant.Attribute("Include");
if (xAttribute != null && !xAttribute.Value.StartsWith(".."))
{
Keepers.Add(descendant); // keep stray code that is not a relative link. VS *may* have added it here.
}
}


}


if (StartPlaceHolder != null && EndPlaceHolder != null && StartPlaceHolder.IsBefore(EndPlaceHolder))
{
XNode startNode = StartPlaceHolder;
Expand All @@ -99,11 +100,19 @@ internal void ClearOldLinkedCode()
}

foreach (XElement itemGroup in ItemGroups)
if (itemGroup.IsEmpty || (!itemGroup.Descendants().Any() && string.IsNullOrEmpty(itemGroup.Value))) itemGroup.Remove();
{
if (itemGroup.IsEmpty || (!itemGroup.Descendants().Any() && string.IsNullOrEmpty(itemGroup.Value)))
{
itemGroup.Remove();
}
}

ItemGroups = RootXelement?.Elements(Settings.MSBuild + "ItemGroup").ToList();
}
catch (Exception e) { Linker.Crash(e, "Bad Proj No ItemGroups: " + DestProjAbsolutePath); }
catch (Exception e)
{
Linker.Crash(e, "Bad Proj No ItemGroups: " + DestProjAbsolutePath);
}
}
Log.WriteLine("ok.");
}
Expand Down Expand Up @@ -136,7 +145,9 @@ private XComment FindCommentOrCrashIfDuplicatesFound(string commentStartsWith)
List<XComment> placeholders = comments.Where(c => c.Value.ToLower().Trim().StartsWith(commentStartsWith.ToLower())).ToList();

if (placeholders.Count > 1)
{
Linker.Crash("ERROR: " + DestProjAbsolutePath + " has " + placeholders.Count + " XML comments with " + commentStartsWith);
}

return placeholders.FirstOrDefault();
}
Expand All @@ -148,17 +159,24 @@ internal void ClearAllExistingCodeExceptExplicitlyLinked()
{
ItemGroups = new List<XElement>();
ItemGroups.AddRange(RootXelement.Elements(Settings.MSBuild + "ItemGroup").Select(elements => elements));
if (ItemGroups.Count == 0) Log.WriteLine("Curious: " + DestProjAbsolutePath + " contains no ItemGroups. No Codez?");
if (ItemGroups.Count == 0)
{
Log.WriteLine("Curious: " + DestProjAbsolutePath + " contains no ItemGroups. No Codez?");
}

if (ItemGroups != null)
{
foreach (XElement itemGroup in ItemGroups)
{
itemGroup.Elements().Where(i => !Settings.ItemElementsToSkip.Contains(i.Name.LocalName.ToLower()) &&
(i.Attribute("Include") != null) &&
i.Attribute("Link") == null).Remove();
itemGroup.Elements()
.Where(i => !Settings.ItemElementsToSkip.Contains(i.Name.LocalName.ToLower()) &&
(i.Attribute("Include") != null) && i.Attribute("Link") == null)
.Remove();

if (itemGroup.IsEmpty || (!itemGroup.Descendants().Any() && string.IsNullOrEmpty(itemGroup.Value))) itemGroup.Remove();
if (itemGroup.IsEmpty || (!itemGroup.Descendants().Any() && string.IsNullOrEmpty(itemGroup.Value)))
{
itemGroup.Remove();
}
}
ItemGroups = RootXelement?.Elements(Settings.MSBuild + "ItemGroup").ToList();
Log.WriteLine("Removed old Code from: " + DestProjAbsolutePath);
Expand Down Expand Up @@ -190,14 +208,15 @@ internal void AddSource(string source)
Log.WriteLine("Added Source: " + source);
}

/// <summary> Saves the Project and also preserves any <c>Keepers</c> - code that needs rescuing from the Link Zone. </summary>
internal void Save()
{
if (Keepers.Any())
{
XElement newItemGroup = new XElement(Settings.MSBuild + "ItemGroup");
foreach (XElement keeper in Keepers)
{
newItemGroup.Add(keeper);
newItemGroup.Add(keeper);
Log.WriteLine("Rescued: " + keeper.ToString().Replace("xmlns=\"" + Settings.MSBuild.ToString() + "\"", ""));
}
EndPlaceHolder.AddAfterSelf(newItemGroup); // move the keepers out of the LinkCodez zone.
Expand Down
Loading

0 comments on commit 2754e71

Please sign in to comment.