├── .gitattributes ├── .gitignore ├── LangVersionFixer.sln ├── LangVersionFixer ├── App.config ├── LangVersionFixer.csproj ├── Program.cs └── Properties │ └── AssemblyInfo.cs └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.sln.docstates 8 | 9 | # Build results 10 | [Dd]ebug/ 11 | [Dd]ebugPublic/ 12 | [Rr]elease/ 13 | x64/ 14 | build/ 15 | bld/ 16 | [Bb]in/ 17 | [Oo]bj/ 18 | 19 | # Roslyn cache directories 20 | *.ide/ 21 | 22 | # MSTest test Results 23 | [Tt]est[Rr]esult*/ 24 | [Bb]uild[Ll]og.* 25 | 26 | #NUNIT 27 | *.VisualState.xml 28 | TestResult.xml 29 | 30 | # Build Results of an ATL Project 31 | [Dd]ebugPS/ 32 | [Rr]eleasePS/ 33 | dlldata.c 34 | 35 | *_i.c 36 | *_p.c 37 | *_i.h 38 | *.ilk 39 | *.meta 40 | *.obj 41 | *.pch 42 | *.pdb 43 | *.pgc 44 | *.pgd 45 | *.rsp 46 | *.sbr 47 | *.tlb 48 | *.tli 49 | *.tlh 50 | *.tmp 51 | *.tmp_proj 52 | *.log 53 | *.vspscc 54 | *.vssscc 55 | .builds 56 | *.pidb 57 | *.svclog 58 | *.scc 59 | 60 | # Chutzpah Test files 61 | _Chutzpah* 62 | 63 | # Visual C++ cache files 64 | ipch/ 65 | *.aps 66 | *.ncb 67 | *.opensdf 68 | *.sdf 69 | *.cachefile 70 | 71 | # Visual Studio profiler 72 | *.psess 73 | *.vsp 74 | *.vspx 75 | 76 | # TFS 2012 Local Workspace 77 | $tf/ 78 | 79 | # Guidance Automation Toolkit 80 | *.gpState 81 | 82 | # ReSharper is a .NET coding add-in 83 | _ReSharper*/ 84 | *.[Rr]e[Ss]harper 85 | *.DotSettings.user 86 | 87 | # JustCode is a .NET coding addin-in 88 | .JustCode 89 | 90 | # TeamCity is a build add-in 91 | _TeamCity* 92 | 93 | # DotCover is a Code Coverage Tool 94 | *.dotCover 95 | 96 | # NCrunch 97 | _NCrunch_* 98 | .*crunch*.local.xml 99 | 100 | # MightyMoose 101 | *.mm.* 102 | AutoTest.Net/ 103 | 104 | # Web workbench (sass) 105 | .sass-cache/ 106 | 107 | # Installshield output folder 108 | [Ee]xpress/ 109 | 110 | # DocProject is a documentation generator add-in 111 | DocProject/buildhelp/ 112 | DocProject/Help/*.HxT 113 | DocProject/Help/*.HxC 114 | DocProject/Help/*.hhc 115 | DocProject/Help/*.hhk 116 | DocProject/Help/*.hhp 117 | DocProject/Help/Html2 118 | DocProject/Help/html 119 | 120 | # Click-Once directory 121 | publish/ 122 | 123 | # Publish Web Output 124 | *.[Pp]ublish.xml 125 | *.azurePubxml 126 | ## TODO: Comment the next line if you want to checkin your 127 | ## web deploy settings but do note that will include unencrypted 128 | ## passwords 129 | #*.pubxml 130 | 131 | # NuGet Packages Directory 132 | packages/* 133 | ## TODO: If the tool you use requires repositories.config 134 | ## uncomment the next line 135 | #!packages/repositories.config 136 | 137 | # Enable "build/" folder in the NuGet Packages folder since 138 | # NuGet packages use it for MSBuild targets. 139 | # This line needs to be after the ignore of the build folder 140 | # (and the packages folder if the line above has been uncommented) 141 | !packages/build/ 142 | 143 | # Windows Azure Build Output 144 | csx/ 145 | *.build.csdef 146 | 147 | # Windows Store app package directory 148 | AppPackages/ 149 | 150 | # Others 151 | sql/ 152 | *.Cache 153 | ClientBin/ 154 | [Ss]tyle[Cc]op.* 155 | ~$* 156 | *~ 157 | *.dbmdl 158 | *.dbproj.schemaview 159 | *.pfx 160 | *.publishsettings 161 | node_modules/ 162 | bower_components/ 163 | 164 | # RIA/Silverlight projects 165 | Generated_Code/ 166 | 167 | # Backup & report files from converting an old project file 168 | # to a newer Visual Studio version. Backup files are not needed, 169 | # because we have git ;-) 170 | _UpgradeReport_Files/ 171 | Backup*/ 172 | UpgradeLog*.XML 173 | UpgradeLog*.htm 174 | 175 | # SQL Server files 176 | *.mdf 177 | *.ldf 178 | 179 | # Business Intelligence projects 180 | *.rdl.data 181 | *.bim.layout 182 | *.bim_*.settings 183 | 184 | # Microsoft Fakes 185 | FakesAssemblies/ 186 | 187 | # LightSwitch generated files 188 | GeneratedArtifacts/ 189 | _Pvt_Extensions/ 190 | ModelManifest.xml 191 | .vs/ 192 | -------------------------------------------------------------------------------- /LangVersionFixer.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.22823.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LangVersionFixer", "LangVersionFixer\LangVersionFixer.csproj", "{BA839F37-BCD2-418F-967E-AA49CC905ED6}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {BA839F37-BCD2-418F-967E-AA49CC905ED6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {BA839F37-BCD2-418F-967E-AA49CC905ED6}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {BA839F37-BCD2-418F-967E-AA49CC905ED6}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {BA839F37-BCD2-418F-967E-AA49CC905ED6}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /LangVersionFixer/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /LangVersionFixer/LangVersionFixer.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {BA839F37-BCD2-418F-967E-AA49CC905ED6} 8 | Exe 9 | Properties 10 | LangVersionFixer 11 | LangVersionFixer 12 | v4.5.2 13 | 512 14 | true 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 57 | -------------------------------------------------------------------------------- /LangVersionFixer/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Linq; 4 | using System.Xml; 5 | using System.Xml.Linq; 6 | 7 | namespace LangVersionFixer 8 | { 9 | public static class Program 10 | { 11 | public static int Main(string[] args) 12 | { 13 | if (args.Length < 2) 14 | { 15 | Console.WriteLine("Usage: LangVersionFixer []"); 16 | return -1; 17 | } 18 | 19 | var directoryPath = args[0]; 20 | var langVersion = args[1]; 21 | var cleanDocument = true; 22 | 23 | if (args.Length > 2) 24 | { 25 | if (!bool.TryParse(args[2], out cleanDocument)) 26 | { 27 | using (ConsoleColorScope.Start(ConsoleColor.Red)) 28 | { 29 | Console.WriteLine($"Could not convert '{args[2]}' to boolean."); 30 | return -1; 31 | } 32 | } 33 | } 34 | 35 | if (!double.TryParse(langVersion, out var _)) 36 | { 37 | if (!langVersion.Equals("default", StringComparison.OrdinalIgnoreCase) && 38 | !langVersion.Equals("latest", StringComparison.OrdinalIgnoreCase)) 39 | { 40 | using (ConsoleColorScope.Start(ConsoleColor.Red)) 41 | { 42 | Console.WriteLine($"'{langVersion}' is not a valid LangVersion parameter."); 43 | return -1; 44 | } 45 | } 46 | } 47 | 48 | var directory = new DirectoryInfo(directoryPath); 49 | 50 | if (!directory.Exists) 51 | { 52 | using (ConsoleColorScope.Start(ConsoleColor.Red)) 53 | { 54 | Console.WriteLine($"Folder '{directoryPath}' does not exist."); 55 | return -1; 56 | } 57 | } 58 | 59 | directory.FixLangVersion(langVersion, cleanDocument); 60 | 61 | using (ConsoleColorScope.Start(ConsoleColor.Green)) 62 | { 63 | Console.WriteLine($"Successfully set LangVersion to {langVersion} in all projects."); 64 | return 0; 65 | } 66 | } 67 | 68 | private static void FixLangVersion(this DirectoryInfo directory, string langVersion, bool cleanDocument = true) 69 | { 70 | XNamespace @namespace = "http://schemas.microsoft.com/developer/msbuild/2003"; 71 | 72 | var files = directory.EnumerateFiles("*.csproj", SearchOption.AllDirectories); 73 | 74 | Console.WriteLine($"Setting LangVersion to {langVersion} in all projects under {directory.FullName}..."); 75 | 76 | foreach (var file in files) 77 | { 78 | Console.WriteLine($"Processing {file.Name}."); 79 | 80 | var document = file.ReadXmlDocument(); 81 | 82 | var isNetCore = document.Root?.Attribute("Sdk") != null; 83 | 84 | document.AddLangVersionElement(isNetCore ? "" : @namespace, langVersion); 85 | 86 | if (cleanDocument) 87 | { 88 | document.CleanUpEmptyElements(); 89 | } 90 | 91 | var settings = new XmlWriterSettings 92 | { 93 | Indent = true, 94 | OmitXmlDeclaration = document.Declaration == null 95 | }; 96 | 97 | using (var writer = XmlWriter.Create(file.FullName, settings)) 98 | { 99 | document.Save(writer); 100 | } 101 | } 102 | } 103 | 104 | private static void AddLangVersionElement(this XContainer document, XNamespace @namespace, string langVersion) 105 | { 106 | var propertyGroups = document 107 | .Descendants(@namespace + "PropertyGroup") 108 | .ToList(); 109 | 110 | var langVersionElement = propertyGroups.Descendants(@namespace + "LangVersion").FirstOrDefault(); 111 | 112 | if (langVersionElement != null) 113 | { 114 | // If we already have an element, just set the value. 115 | langVersionElement.SetValue(langVersion); 116 | return; 117 | } 118 | 119 | var emptyPropertyGroup = propertyGroups.FirstOrDefault(x => !x.HasAttributes); 120 | 121 | var globalPropertyGroup = emptyPropertyGroup ?? propertyGroups.FirstOrDefault(); 122 | 123 | if (globalPropertyGroup == null) 124 | { 125 | document.Add(globalPropertyGroup = new XElement(@namespace + "PropertyGroup")); 126 | } 127 | 128 | var newElement = new XElement(@namespace + "LangVersion") 129 | { 130 | Value = langVersion 131 | }; 132 | 133 | globalPropertyGroup.Add(newElement); 134 | } 135 | 136 | private static void CleanUpEmptyElements(this XContainer document) 137 | { 138 | // Collapse start and end tags 139 | document.Descendants().Where(element => string.IsNullOrWhiteSpace(element.Value) && !element.HasElements).ToList().ForEach(x => x.RemoveNodes()); 140 | 141 | // Remove empty tags 142 | document.Descendants().Where(element => element.IsEmpty()).Remove(); 143 | } 144 | 145 | private static bool IsEmpty(this XElement element) 146 | { 147 | return element.HasNoValue() && !element.HasAttributes && !element.HasElements; 148 | } 149 | 150 | private static bool HasNoValue(this XElement element) 151 | { 152 | return element.IsEmpty || string.IsNullOrWhiteSpace(element.Value); 153 | } 154 | 155 | private static XDocument ReadXmlDocument(this FileSystemInfo file) 156 | { 157 | using (var readStream = File.OpenRead(file.FullName)) 158 | { 159 | return XDocument.Load(readStream); 160 | } 161 | } 162 | 163 | private class ConsoleColorScope : IDisposable 164 | { 165 | private ConsoleColorScope(ConsoleColor color) 166 | { 167 | Color = color; 168 | } 169 | 170 | private ConsoleColor Color { get; } 171 | 172 | public static IDisposable Start(ConsoleColor foregroundColor) 173 | { 174 | var scope = new ConsoleColorScope(Console.ForegroundColor); 175 | 176 | Console.ForegroundColor = foregroundColor; 177 | 178 | return scope; 179 | } 180 | 181 | public void Dispose() 182 | { 183 | Console.ForegroundColor = Color; 184 | } 185 | } 186 | } 187 | } 188 | -------------------------------------------------------------------------------- /LangVersionFixer/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.InteropServices; 3 | 4 | [assembly: AssemblyTitle("LangVersionFixer")] 5 | [assembly: AssemblyDescription("")] 6 | [assembly: AssemblyConfiguration("")] 7 | [assembly: AssemblyCompany("")] 8 | [assembly: AssemblyProduct("LangVersionFixer")] 9 | [assembly: AssemblyCopyright("Copyright © Kristian Hellang 2015")] 10 | [assembly: AssemblyTrademark("")] 11 | [assembly: AssemblyCulture("")] 12 | 13 | [assembly: ComVisible(false)] 14 | 15 | [assembly: Guid("ba839f37-bcd2-418f-967e-aa49cc905ed6")] 16 | 17 | [assembly: AssemblyVersion("1.5.0.0")] 18 | [assembly: AssemblyFileVersion("1.5.0.0")] 19 | [assembly: AssemblyInformationalVersion("1.5.0")] 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LangVersionFixer 2 | 3 | A tiny utility to set the `LangVersion` property of all *.csprojs in a folder 4 | 5 | ## Usage 6 | 7 | Download the latest release from [here](https://github.com/khellang/LangVersionFixer/releases/latest). 8 | 9 | Then run `LangVersionFixer `. 10 | 11 | ### Example 12 | 13 | `LangVersionFixer "C:\Code\AwesomeProject" 5` - Will set `LangVersion` to `5` for all `*.csproj` under `C:\Code\AwesomeProject`. 14 | --------------------------------------------------------------------------------