-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Update the logic of custom culture support #11607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
YuliiaKovalova
merged 13 commits into
dotnet:vs17.14
from
YuliiaKovalova:dev/ykovalova/fix_custom_culture
Apr 2, 2025
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e51b29f
add feature flag for custom culture
YuliiaKovalova a12f91b
add test framework
YuliiaKovalova b768170
cover the flag with tests
YuliiaKovalova f4cc173
exclude the test from core scenarious
YuliiaKovalova 24184b6
update test data
YuliiaKovalova 0f94c2b
undo extra change
YuliiaKovalova 1ab77dd
add bootstrap full bits population for windows core scenarious
YuliiaKovalova 863babe
fix review comments
YuliiaKovalova 68101ae
cleanup the description
YuliiaKovalova a50210f
substitute MSBUILDENABLECUSTOMCULTURES env var -> EnableCustomCultur…
YuliiaKovalova eaa2e60
bump the version
YuliiaKovalova b94e2fb
final tweaks
YuliiaKovalova ad9033e
Merge branch 'dev/ykovalova/fix_custom_culture' of https://github.com…
YuliiaKovalova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,42 @@ | ||
| # MSBuild Custom Cultures Support | ||
|
|
||
| ## Overview | ||
|
|
||
| The `EnableCustomCulture` property provides an opt-in mechanism for handling custom culture-specific resources in MSBuild projects. This feature allows for greater control over which directories are treated as culture-specific resources during the build process. | ||
|
|
||
| ## Purpose | ||
|
|
||
| In some projects, directory names that match culture name patterns might not actually be culture resources. This can cause issues with resource compilation and deployment. This feature flag enables: | ||
|
|
||
| 1. Control over whether custom culture detection is enabled | ||
| 2. Fine-grained configuration of which directories should be excluded from culture-specific resource processing | ||
YuliiaKovalova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ## Usage | ||
|
|
||
| ### Enabling the Feature | ||
|
|
||
| To enable the custom cultures feature, set the `EnableCustomCulture` property `true`. | ||
|
|
||
| ```xml | ||
| <PropertyGroup> | ||
| <EnableCustomCulture>true</EnableCustomCulture> | ||
| </PropertyGroup> | ||
| ``` | ||
|
|
||
| ### Excluding Specific Directories | ||
|
|
||
| When the feature is enabled, you can specify directories that should not be treated as culture-specific resources using the `NonCultureResourceDirectories` property: | ||
|
|
||
| ```xml | ||
| <PropertyGroup> | ||
| <NonCultureResourceDirectories>long;hash;temp</NonCultureResourceDirectories> | ||
| </PropertyGroup> | ||
| ``` | ||
|
|
||
| In this example, directories named "long", "hash", or "temp" will not be processed as culture-specific resources and the assemblied inside of them will be skipped, even if their names match culture naming patterns. Globbing is not supported. | ||
|
|
||
| ## Additional Notes | ||
|
|
||
| - This feature does not affect the standard resource handling for well-known cultures. | ||
| - The feature is designed to be backward compatible - existing projects without the feature flag will behave the same as before. | ||
| - Performance impact is minimal, as the exclusion check happens only during the resource discovery phase of the build. | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
62 changes: 62 additions & 0 deletions
62
src/MSBuild.Bootstrap.Utils/Tasks/LocateVisualStudioTask.cs
This file contains hidden or 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,62 @@ | ||
| // 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.Text; | ||
| using Microsoft.Build.Framework; | ||
| using Microsoft.Build.Utilities; | ||
|
|
||
| namespace MSBuild.Bootstrap.Utils.Tasks | ||
| { | ||
| public class LocateVisualStudioTask : ToolTask | ||
| { | ||
| private readonly StringBuilder _standardOutput = new(); | ||
|
|
||
| [Output] | ||
| public string VsInstallPath { get; set; } | ||
|
|
||
| protected override string ToolName => "vswhere.exe"; | ||
|
|
||
| protected override string GenerateFullPathToTool() | ||
| { | ||
| string programFilesX86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86); | ||
| string vsWherePath = Path.Combine(programFilesX86, "Microsoft Visual Studio", "Installer", ToolName); | ||
|
|
||
|
|
||
| return vsWherePath; | ||
| } | ||
|
|
||
| protected override string GenerateCommandLineCommands() => "-latest -prerelease -property installationPath"; | ||
|
|
||
| public override bool Execute() | ||
| { | ||
| if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
| { | ||
| Log.LogMessage(MessageImportance.High, "Not running on Windows. Skipping Visual Studio detection."); | ||
| return true; | ||
| } | ||
|
|
||
| _ = ExecuteTool(GenerateFullPathToTool(), string.Empty, GenerateCommandLineCommands()); | ||
|
|
||
| if (!Log.HasLoggedErrors) | ||
| { | ||
| VsInstallPath = _standardOutput.ToString().Trim(); | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| // Override to capture standard output | ||
| protected override void LogEventsFromTextOutput(string singleLine, MessageImportance messageImportance) | ||
| { | ||
| if (!string.IsNullOrWhiteSpace(singleLine)) | ||
| { | ||
| _ = _standardOutput.AppendLine(singleLine); | ||
| } | ||
|
|
||
| base.LogEventsFromTextOutput(singleLine, messageImportance); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
89 changes: 89 additions & 0 deletions
89
src/Tasks.UnitTests/ResolveAssemblyReference_CustomCultureTests.cs
This file contains hidden or 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,89 @@ | ||
| // 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 Microsoft.Build.UnitTests; | ||
| using Microsoft.Build.UnitTests.Shared; | ||
| using Shouldly; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Build.Tasks.UnitTests | ||
| { | ||
| /// <summary> | ||
| /// Unit tests for the ResolveAssemblyReference task. | ||
| /// </summary> | ||
| public class ResolveAssemblyReference_CustomCultureTests | ||
| { | ||
| private static string TestAssetsRootPath { get; } = Path.Combine( | ||
| Path.GetDirectoryName(typeof(AddToWin32Manifest_Tests).Assembly.Location) ?? AppContext.BaseDirectory, | ||
| "TestResources", | ||
| "CustomCulture"); | ||
|
|
||
| [WindowsOnlyTheory] | ||
| [InlineData(true, "", true, true)] | ||
| [InlineData(false)] | ||
| [InlineData(true, "yue", false, true)] | ||
| [InlineData(false, "yue", false, true)] | ||
| [InlineData(true, "euy", true)] | ||
| [InlineData(true, "yue;euy")] | ||
| [InlineData(true, "euy;yue")] | ||
| public void E2EScenarioTests(bool enableCustomCulture, string customCultureExclusions = "", bool isYueCultureExpected = false, bool isEuyCultureExpected = false) | ||
| { | ||
| using (TestEnvironment env = TestEnvironment.Create()) | ||
| { | ||
| // Set up project paths | ||
| var testAssetsPath = TestAssetsRootPath; | ||
| var solutionFolder = env.CreateFolder(); | ||
| var solutionPath = solutionFolder.Path; | ||
|
|
||
| // Create and configure ProjectB | ||
| var projectBName = "ProjectB.csproj"; | ||
| var projBOutputPath = env.CreateFolder().Path; | ||
| var projectBFolder = Path.Combine(solutionPath, projectBName); | ||
| Directory.CreateDirectory(projectBFolder); | ||
| var projBContent = File.ReadAllText(Path.Combine(testAssetsPath, projectBName)) | ||
| .Replace("OutputPathPlaceholder", projBOutputPath) | ||
| .Replace("NonCultureResourceDirectoriesPlaceholder", customCultureExclusions) | ||
| .Replace("EnableCustomCulturePlaceholder", enableCustomCulture.ToString()); | ||
| env.CreateFile(Path.Combine(projectBFolder, projectBName), projBContent); | ||
|
|
||
| // Copy ProjectA files to test solution folder | ||
| CopyTestAsset(testAssetsPath, "ProjectA.csproj", solutionPath); | ||
| CopyTestAsset(testAssetsPath, "Test.resx", solutionPath); | ||
| CopyTestAsset(testAssetsPath, "Test.yue.resx", solutionPath); | ||
| CopyTestAsset(testAssetsPath, "Test.euy.resx", solutionPath); | ||
|
|
||
| env.SetCurrentDirectory(projectBFolder); | ||
| var output = RunnerUtilities.ExecBootstrapedMSBuild("-restore", out bool buildSucceeded); | ||
|
|
||
| buildSucceeded.ShouldBeTrue($"MSBuild should complete successfully. Build output: {output}"); | ||
|
|
||
| var yueCultureResourceDll = Path.Combine(projBOutputPath, "yue", "ProjectA.resources.dll"); | ||
| AssertCustomCulture(isYueCultureExpected, "yue", yueCultureResourceDll); | ||
|
|
||
| var euyCultureResourceDll = Path.Combine(projBOutputPath, "euy", "ProjectA.resources.dll"); | ||
| AssertCustomCulture(isEuyCultureExpected, "euy", euyCultureResourceDll); | ||
| } | ||
|
|
||
| void AssertCustomCulture(bool isCultureExpectedToExist, string customCultureName, string cultureResourcePath) | ||
| { | ||
| if (enableCustomCulture && isCultureExpectedToExist) | ||
| { | ||
| File.Exists(cultureResourcePath).ShouldBeTrue($"Expected '{customCultureName}' resource DLL not found at: {cultureResourcePath}"); | ||
| } | ||
| else | ||
| { | ||
| File.Exists(cultureResourcePath).ShouldBeFalse($"Unexpected '{customCultureName}' culture DLL was found at: {cultureResourcePath}"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void CopyTestAsset(string sourceFolder, string fileName, string destinationFolder) | ||
| { | ||
| var sourcePath = Path.Combine(sourceFolder, fileName); | ||
|
|
||
| File.Copy(sourcePath, Path.Combine(destinationFolder, fileName)); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.