Skip to content

Commit

Permalink
Add LangVersion to enable C# 8 on VS versions from September 2019+
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamTheCoder committed Mar 19, 2020
1 parent e317095 commit e87fef1
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 5 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

* Avoid extra newlines in doc comments [#334](https://github.com/icsharpcode/CodeConverter/pull/334)
* Avoid duplicate generated constructors [#543](https://github.com/icsharpcode/CodeConverter/issues/543)
* Comparisons of value/generic types to Nothing now convert to the correct corresponding C# operator
* Comparisons of value/generic types to Nothing now convert to the correct corresponding C# 8 operator
* Resources are now correctly namespaced after conversion [#540](https://github.com/icsharpcode/CodeConverter/issues/540)
* LangVersion is now set to "Latest" in csproj

### C# -> VB

Expand Down
4 changes: 4 additions & 0 deletions CodeConverter/CSharp/VBToCSConversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public string PostTransformProjectFile(string xml)
xml = new Regex(@"(<ItemGroup>)(\s*)").Replace(xml, "$1$2<Reference Include=\"Microsoft.VisualBasic\" />$2", 1);
}

if (!Regex.IsMatch(xml, @"<\s*LangVersion\s*>")) {
xml = new Regex(@"(\s*)(</\s*PropertyGroup\s*>)").Replace(xml, $"$1 <LangVersion>{_vbToCsProjectContentsConverter.LanguageVersion}</LangVersion>$1$2", 1);
}

// TODO Find API to, or parse project file sections to remove "<DefineDebug>true</DefineDebug>" + "<DefineTrace>true</DefineTrace>"
// Then add them to the define constants in the same section, or create one if necessary.

Expand Down
4 changes: 2 additions & 2 deletions CodeConverter/CSharp/VBToCSProjectContentsConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.VisualBasic;
using LanguageVersion = Microsoft.CodeAnalysis.CSharp.LanguageVersion;
using LangVersion = Microsoft.CodeAnalysis.CSharp.LanguageVersion;
using System;
using System.Xml.Linq;
using System.IO;
Expand Down Expand Up @@ -46,7 +46,7 @@ public async Task InitializeSourceAsync(Project project)
Project = await project.WithRenamedMergedMyNamespace(_cancellationToken);
}

string IProjectContentsConverter.LanguageVersion { get { return LanguageVersion.Default.ToDisplayString(); } }
public string LanguageVersion { get { return LangVersion.Latest.ToDisplayString(); } }

public Project Project { get; private set; }

Expand Down
4 changes: 2 additions & 2 deletions CodeConverter/Shared/ProjectConversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ public static ConversionResult ConvertProjectFile(Project project,
}

private static (string Find, string Replace, bool FirstOnly) ChangeLanguageVersionRegex(string languageVersion) {
return (Find: new Regex(@"<\s*LangVersion>(\d|\D)*</LangVersion\s*>").ToString(), Replace: $"<LangVersion>{languageVersion}</LangVersion>", FirstOnly: true);
return (Find: new Regex(@"<\s*LangVersion\s*>[^<]*</\s*LangVersion\s*>").ToString(), Replace: $"<LangVersion>{languageVersion}</LangVersion>", FirstOnly: true);
}

private static (string Find, string Replace, bool FirstOnly) ChangeRootNamespaceRegex(string rootNamespace) {
return (Find: new Regex(@"<\s*RootNamespace>(\d|\D)*</RootNamespace\s*>").ToString(), Replace: $"<RootNamespace>{rootNamespace}</RootNamespace>", FirstOnly: true);
return (Find: new Regex(@"<\s*RootNamespace\s*>([^<]*)</\s*RootNamespace\s*>").ToString(), Replace: $"<RootNamespace>{rootNamespace}</RootNamespace>", FirstOnly: true);
}

private static (string Find, string Replace, bool FirstOnly) AddCompiledItemsRegexFromRelativePaths(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
<DefaultItemExcludes>$(DefaultItemExcludes);$(ProjectDir)**\*.vb</DefaultItemExcludes>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<RootNamespace>ConsoleApp4</RootNamespace>
<TargetFramework>netcoreapp3.0</TargetFramework>
<DefaultItemExcludes>$(DefaultItemExcludes);$(ProjectDir)**\*.vb</DefaultItemExcludes>
<LangVersion>latest</LangVersion>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
<DefaultItemExcludes>$(DefaultItemExcludes);$(ProjectDir)**\*.vb</DefaultItemExcludes>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<RootNamespace>VbNetStandardLib</RootNamespace>
<TargetFramework>netstandard2.0</TargetFramework>
<DefaultItemExcludes>$(DefaultItemExcludes);$(ProjectDir)**\*.vb</DefaultItemExcludes>
<LangVersion>latest</LangVersion>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<DefaultItemExcludes>$(DefaultItemExcludes);$(ProjectDir)**\*.vb</DefaultItemExcludes>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down

0 comments on commit e87fef1

Please sign in to comment.