-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect .NET SDK & VSLANG Custom Language Settings & Apply To MSBuild (#…
…8503) Fixes #1596 Changes Made SetConsoleUI now calls into a helper which sets the encoding to support non-en languages and checks if an environment variable exists to change the language to. Testing Setting DOTNET_CLI_UI_LANGUAGE=ja now changes msbuild correctly: image Doing a complicated build (aka building MSBuild) to use multiple threads shows other threads seem to use the same UI culture: image See that chcp remains the same after execution: image (Was set to 65001 temporarily but back to the original page before execution.) Notes Much of this code is a port of this code: dotnet/sdk#29755 There are some details about the code here. [!] In addition, it will introduce a breaking change for msbuild just like the SDK. The break is documented here for the sdk: dotnet/docs#34250
- Loading branch information
Showing
10 changed files
with
264 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// See https://aka.ms/new-console-template for more information | ||
Console.WriteLine("Hello, World!"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
using System.Security; | ||
using System.Text; | ||
|
||
namespace Microsoft.Build.CommandLine | ||
{ | ||
/// <summary> | ||
/// Ported from https://github.com/dotnet/sdk/blob/bcea1face15458814b8e53e8785b52ba464f6538/src/Cli/dotnet/AutomaticEncodingRestorer.cs. | ||
/// A program can change the encoding of the console which would affect other programs. | ||
/// We would prefer to have a pattern where the program does not affect encoding of other programs. | ||
/// Create this class in a function akin to Main and let it manage the console encoding resources to return it to the state before execution upon destruction. | ||
/// </summary> | ||
public class AutomaticEncodingRestorer : IDisposable | ||
{ | ||
private Encoding? _originalOutputEncoding = null; | ||
private Encoding? _originalInputEncoding = null; | ||
|
||
public AutomaticEncodingRestorer() | ||
{ | ||
try | ||
{ | ||
#if NET7_0_OR_GREATER | ||
if (OperatingSystem.IsIOS() || OperatingSystem.IsAndroid() || OperatingSystem.IsTvOS()) // Output + Input Encoding are unavailable on these platforms per docs, and they're only available past net 5. | ||
{ | ||
return; | ||
} | ||
#endif | ||
_originalOutputEncoding = Console.OutputEncoding; | ||
|
||
#if NET7_0_OR_GREATER | ||
if (OperatingSystem.IsBrowser()) // Input Encoding is also unavailable in this platform. (No concern for net472 as browser is unavailable.) | ||
{ | ||
return; | ||
} | ||
#endif | ||
_originalInputEncoding = Console.InputEncoding; | ||
} | ||
catch (Exception ex) when (ex is IOException || ex is SecurityException) | ||
{ | ||
// The encoding is unavailable. Do nothing. | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
try | ||
{ | ||
if (_originalOutputEncoding != null) | ||
{ | ||
Console.OutputEncoding = _originalOutputEncoding; | ||
} | ||
if (_originalInputEncoding != null) | ||
{ | ||
Console.InputEncoding = _originalInputEncoding; | ||
} | ||
} | ||
catch (Exception ex) when (ex is IOException || ex is SecurityException) | ||
{ | ||
// The encoding is unavailable. Do nothing. | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.