From af17a1a8c1e44f3ddf3c28cbc9df7d20cfcfb03c Mon Sep 17 00:00:00 2001 From: Kristian Hellang Date: Thu, 20 Jul 2017 16:18:42 +0200 Subject: [PATCH] Some cleanup. Add new, empty PropertyGroup if none exist. --- LangVersionFixer/Program.cs | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/LangVersionFixer/Program.cs b/LangVersionFixer/Program.cs index 8157044..a71fcaa 100644 --- a/LangVersionFixer/Program.cs +++ b/LangVersionFixer/Program.cs @@ -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)) { @@ -57,7 +55,7 @@ public static int Main(string[] args) } } - directory.FixLangVersion(langVersion, cleanDocument); + directory.FixLangVersion(parsedLangVersion, cleanDocument); using (ConsoleColorScope.Start(ConsoleColor.Green)) { @@ -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"; @@ -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") @@ -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) @@ -179,4 +177,4 @@ public void Dispose() } } } -} \ No newline at end of file +}