-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a custom MSBuild TaskItem when reporting compiler args.
The TaskItem implementation from MSBuild treats the ItemSpec as file path and tries to normalize the path separators. When running on non-windows machines this means changing '\' to '/'. However this breaks how double quotes are escaped in the compiler args. By using our own implemetation of TaskItem we can use the compiler args as the ItemSpec without any modification. Resolves #72014
- Loading branch information
Showing
2 changed files
with
84 additions
and
1 deletion.
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,83 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.Build.Framework; | ||
using System.Collections.Immutable; | ||
|
||
namespace Microsoft.CodeAnalysis.BuildTasks | ||
{ | ||
internal sealed class ArgsTaskItem : ITaskItem | ||
{ | ||
// This list is taken from src/Shared/Modifiers.cs in the dotnet/msbuild repo. | ||
private static readonly string[] WellKnownItemSpecMetadataNames = | ||
{ | ||
"FullPath", | ||
"RootDir", | ||
"Filename", | ||
"Extension", | ||
"RelativeDir", | ||
"Directory", | ||
"RecursiveDir", | ||
"Identity", | ||
"ModifiedTime", | ||
"CreatedTime", | ||
"AccessedTime", | ||
"DefiningProjectFullPath", | ||
"DefiningProjectDirectory", | ||
"DefiningProjectName", | ||
"DefiningProjectExtension", | ||
}; | ||
|
||
private readonly Dictionary<string, string> _metadata = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); | ||
|
||
public ArgsTaskItem(string itemSpec) | ||
{ | ||
ItemSpec = itemSpec; | ||
} | ||
|
||
public string ItemSpec { get; set; } | ||
|
||
// Implementation notes that we should include the built-in metadata names as well as our custom ones. | ||
public ICollection MetadataNames => _metadata.Keys.Concat(WellKnownItemSpecMetadataNames).ToImmutableArray(); | ||
|
||
// Implementation notes that we should include the built-in metadata names as well as our custom ones. | ||
public int MetadataCount => _metadata.Count + WellKnownItemSpecMetadataNames.Length; | ||
|
||
public IDictionary CloneCustomMetadata() | ||
{ | ||
return _metadata.ToImmutableDictionary(); | ||
} | ||
|
||
public void CopyMetadataTo(ITaskItem destinationItem) | ||
{ | ||
var destinationMetadataNames = destinationItem.MetadataNames.OfType<string>(); | ||
var metadataNamesToCopy = _metadata.Keys.Intersect(destinationMetadataNames, StringComparer.OrdinalIgnoreCase).ToArray(); | ||
|
||
foreach (var metadataName in metadataNamesToCopy) | ||
{ | ||
var metadataValue = _metadata[metadataName]; | ||
destinationItem.SetMetadata(metadataName, metadataValue); | ||
} | ||
} | ||
|
||
public string GetMetadata(string metadataName) | ||
{ | ||
return _metadata[metadataName]; | ||
} | ||
|
||
public void RemoveMetadata(string metadataName) | ||
{ | ||
_metadata.Remove(metadataName); | ||
} | ||
|
||
public void SetMetadata(string metadataName, string metadataValue) | ||
{ | ||
_metadata[metadataName] = metadataValue; | ||
} | ||
} | ||
} |
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