Skip to content

Commit

Permalink
Some cleanup. Add new, empty PropertyGroup if none exist.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristian Hellang committed Jul 20, 2017
1 parent a217134 commit af17a1a
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions LangVersionFixer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,21 @@ public static int Main(string[] args)

var directoryPath = args[0];
var langVersion = args[1];

var cleanDocument = true;

if (args.Length > 2)
{
var parseOk = Boolean.TryParse(args[2], out cleanDocument);
if (!parseOk)
if (!bool.TryParse(args[2], out cleanDocument))
{
using (ConsoleColorScope.Start(ConsoleColor.Red))
{
Console.WriteLine($"Could not convert '{args[2].ToString()}' to boolean.");
Console.WriteLine($"Could not convert '{args[2]}' to boolean.");
return -1;
}
}
}

int parsedLangVersion;
if (!int.TryParse(langVersion, out parsedLangVersion))
if (!int.TryParse(langVersion, out int parsedLangVersion))
{
if (!langVersion.Equals("default", StringComparison.OrdinalIgnoreCase))
{
Expand All @@ -57,7 +55,7 @@ public static int Main(string[] args)
}
}

directory.FixLangVersion(langVersion, cleanDocument);
directory.FixLangVersion(parsedLangVersion, cleanDocument);

using (ConsoleColorScope.Start(ConsoleColor.Green))
{
Expand All @@ -66,7 +64,7 @@ public static int Main(string[] args)
}
}

private static void FixLangVersion(this DirectoryInfo directory, string langVersion, bool cleanDocument = true)
private static void FixLangVersion(this DirectoryInfo directory, int langVersion, bool cleanDocument = true)
{
XNamespace @namespace = "http://schemas.microsoft.com/developer/msbuild/2003";

Expand Down Expand Up @@ -96,7 +94,7 @@ private static void FixLangVersion(this DirectoryInfo directory, string langVers
}
}

private static void AddLangVersionElement(this XContainer document, XNamespace @namespace, string langVersion)
private static void AddLangVersionElement(this XContainer document, XNamespace @namespace, int langVersion)
{
var propertyGroups = document
.Descendants(@namespace + "PropertyGroup")
Expand All @@ -117,15 +115,15 @@ private static void AddLangVersionElement(this XContainer document, XNamespace @

if (globalPropertyGroup == null)
{
using (ConsoleColorScope.Start(ConsoleColor.Yellow))
{
Console.WriteLine("No PropertyGroup element found in file.");
}
document.Add(globalPropertyGroup = new XElement(@namespace + "PropertyGroup"));
}
else

var newElement = new XElement(@namespace + "LangVersion")
{
globalPropertyGroup.Add(new XElement(@namespace + "LangVersion") { Value = langVersion });
}
Value = langVersion.ToString()
};

globalPropertyGroup.Add(newElement);
}

private static void CleanUpEmptyElements(this XContainer document)
Expand Down Expand Up @@ -179,4 +177,4 @@ public void Dispose()
}
}
}
}
}

0 comments on commit af17a1a

Please sign in to comment.