-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update implicit global usings feature
* Undo changes to VB projects * For C#, projects use the `Using` itemgroup to generate global usings. Support aliasing and static imports with attributes. * Use ImplicitUsings to determine if SDK-specific namespaces are imported by default.
- Loading branch information
Showing
21 changed files
with
350 additions
and
310 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
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
117 changes: 117 additions & 0 deletions
117
src/Tasks/Microsoft.NET.Build.Tasks/GenerateGlobalUsings.cs
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,117 @@ | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using Microsoft.Build.Framework; | ||
|
||
namespace Microsoft.NET.Build.Tasks | ||
{ | ||
public sealed class GenerateGlobalUsings : TaskBase | ||
{ | ||
[Required] | ||
public ITaskItem[] Usings { get; set; } | ||
|
||
[Output] | ||
public string[] Lines { get; set; } | ||
|
||
protected override void ExecuteCore() | ||
{ | ||
if (Usings.Length == 0) | ||
{ | ||
Lines = Array.Empty<string>(); | ||
return; | ||
} | ||
|
||
var usings = Usings.Select(UsingInfo.Read) | ||
.OrderBy(static k => k, UsingInfoComparer.Instance) | ||
.Distinct(UsingInfoComparer.Instance); | ||
|
||
var lines = new string[Usings.Length + 1]; | ||
lines[0] = "// <auto-generated/>"; | ||
|
||
var index = 1; | ||
var lineBuilder = new StringBuilder(); | ||
foreach (var @using in usings) | ||
{ | ||
lineBuilder.Clear(); | ||
lineBuilder.Append("global using "); | ||
|
||
if (@using.Static) | ||
{ | ||
lineBuilder.Append("static "); | ||
} | ||
|
||
if (!string.IsNullOrEmpty(@using.Alias)) | ||
{ | ||
lineBuilder.Append(@using.Alias) | ||
.Append(" = "); | ||
} | ||
|
||
lineBuilder.Append("global::") | ||
.Append(@using.Namespace) | ||
.Append(';'); | ||
|
||
lines[index++] = lineBuilder.ToString(); | ||
} | ||
|
||
Lines = lines; | ||
} | ||
|
||
private readonly struct UsingInfo | ||
{ | ||
public static UsingInfo Read(ITaskItem taskItem) | ||
{ | ||
return new UsingInfo( | ||
taskItem.ItemSpec, | ||
taskItem.GetBooleanMetadata("Static") == true, | ||
taskItem.GetMetadata("Alias")); | ||
} | ||
|
||
private UsingInfo(string @namespace, bool @static, string alias) | ||
{ | ||
Namespace = @namespace; | ||
Static = @static; | ||
Alias = alias; | ||
} | ||
|
||
public string Namespace { get; } | ||
public bool Static { get; } | ||
public string Alias { get; } | ||
} | ||
|
||
private sealed class UsingInfoComparer : IComparer<UsingInfo>, IEqualityComparer<UsingInfo> | ||
{ | ||
public static readonly UsingInfoComparer Instance = new(); | ||
|
||
public int Compare(UsingInfo x, UsingInfo y) | ||
{ | ||
var @static = x.Static.CompareTo(y.Static); | ||
if (@static != 0) | ||
{ | ||
return @static; | ||
} | ||
|
||
var alias = x.Alias.CompareTo(y.Alias); | ||
if (alias != 0) | ||
{ | ||
return alias; | ||
} | ||
|
||
return StringComparer.Ordinal.Compare(x.Namespace, y.Namespace); | ||
} | ||
|
||
public bool Equals(UsingInfo x, UsingInfo y) | ||
{ | ||
return Compare(x, y) == 0; | ||
} | ||
|
||
public int GetHashCode(UsingInfo obj) | ||
{ | ||
return StringComparer.Ordinal.GetHashCode(obj.Namespace); | ||
} | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.GenerateGlobalUsings.targets
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,57 @@ | ||
<!-- | ||
*********************************************************************************************** | ||
Microsoft.NET.GenerateGlobalUsings.targets | ||
WARNING: DO NOT MODIFY this file unless you are knowledgeable about MSBuild and have | ||
created a backup copy. Incorrect changes to this file will make it | ||
impossible to load or build your projects from the command-line or the IDE. | ||
Copyright (c) .NET Foundation. All rights reserved. | ||
*********************************************************************************************** | ||
--> | ||
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<!-- | ||
============================================================ | ||
GenerateGlobalUsings | ||
Generates implicit namespace imports source to intermediate directory for C# projects | ||
============================================================ | ||
--> | ||
<UsingTask TaskName="GenerateGlobalUsings" AssemblyFile="$(MicrosoftNETBuildTasksAssembly)" /> | ||
|
||
<PropertyGroup> | ||
<GeneratedGlobalUsingsFile Condition="'$(GeneratedGlobalUsingsFile)' ==''">$(IntermediateOutputPath)$(MSBuildProjectName).GlobalUsings.g$(DefaultLanguageSourceExtension)</GeneratedGlobalUsingsFile> | ||
</PropertyGroup> | ||
|
||
<!-- | ||
Note that this must run before every invocation of CoreCompile to ensure that all compiler | ||
runs see the generated global usings. There is at least one scenario involving Xaml | ||
where CoreCompile is invoked without other potential hooks such as Compile or CoreBuild, | ||
etc., so we hook directly on to CoreCompile. Furthermore, we must run *after* | ||
PrepareForBuild to ensure that the intermediate directory has been created. | ||
Targets that generate Compile items are also expected to run before | ||
BeforeCompile targets (common targets convention). | ||
--> | ||
<Target Name="GenerateGlobalUsings" | ||
BeforeTargets="BeforeCompile;CoreCompile" | ||
AfterTargets="PrepareForBuild" | ||
Condition="@(Using->Count()) != 0"> | ||
|
||
<GenerateGlobalUsings Usings="@(Using)"> | ||
<Output TaskParameter="Lines" ItemName="_GlobalUsingLines" /> | ||
</GenerateGlobalUsings> | ||
|
||
<WriteLinesToFile | ||
File="$(GeneratedGlobalUsingsFile)" | ||
Lines="@(_GlobalUsingLines)" | ||
Overwrite="true" | ||
WriteOnlyWhenDifferent="true" /> | ||
|
||
<ItemGroup> | ||
<Compile Include="$(GeneratedGlobalUsingsFile)" /> | ||
<FileWrites Include="$(GeneratedGlobalUsingsFile)" /> | ||
</ItemGroup> | ||
</Target> | ||
|
||
</Project> |
57 changes: 0 additions & 57 deletions
57
.../Microsoft.NET.Build.Tasks/targets/Microsoft.NET.GenerateImplicitNamespaceImports.targets
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.