-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Update implicit global usings feature #19599
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) | ||
pranavkm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .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); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| 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> |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,30 @@ Copyright (c) .NET Foundation. All rights reserved. | |
| *********************************************************************************************** | ||
| --> | ||
| <Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
| <ItemGroup Condition=" '$(DisableImplicitNamespaceImports)' != 'true' and '$(TargetFrameworkIdentifier)' == '.NETFramework'"> | ||
pranavkm marked this conversation as resolved.
Show resolved
Hide resolved
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May I please get a clarification why VB's implicit imports are now hidden behind This has broken dotnet/winforms#5437
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this may be the culprit, which needs to be undone:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this might have been missed. @pranavkm?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does the screen shot show "net6.0-windows" if this is a " .NET 5.0 VB app"?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The issue that @RussKie is running in to is because of an Arcade change - dotnet/arcade#7745. That said, I missed undoing one of the VB changes that is covered as part of #19840 |
||
| <!-- These namespaces are present in 2.0 Framework assemblies --> | ||
| <Import Include="Microsoft.VisualBasic" /> | ||
| <Import Include="System" /> | ||
| <Import Include="System.Collections" /> | ||
| <Import Include="System.Collections.Generic" /> | ||
| <Import Include="System.Data" /> | ||
| <Import Include="System.Diagnostics" /> | ||
| <!-- These namespaces are introduced in 3.5 Framework assemblies --> | ||
| <Import Include="System.Linq" Condition=" '$(_TargetFrameworkVersionWithoutV)' >= '3.5' "/> | ||
| <Import Include="System.Xml.Linq" Condition=" '$(_TargetFrameworkVersionWithoutV)' >= '3.5' "/> | ||
| <!-- This namespace is introduced in 4.0 Framework assemblies --> | ||
| <Import Include="System.Threading.Tasks" Condition=" '$(_TargetFrameworkVersionWithoutV)' >= '4.0' "/> | ||
| </ItemGroup> | ||
| <ItemGroup Condition=" '$(DisableImplicitNamespaceImports)' != 'true' and '$(_IsNETCoreOrNETStandard)' == 'true'"> | ||
| <Import Include="Microsoft.VisualBasic" /> | ||
| <Import Include="System" /> | ||
| <Import Include="System.Collections" /> | ||
| <Import Include="System.Collections.Generic" /> | ||
| <Import Include="System.Diagnostics" /> | ||
| <Import Include="System.Linq" /> | ||
| <Import Include="System.Xml.Linq" /> | ||
| <Import Include="System.Threading.Tasks" /> | ||
| </ItemGroup> | ||
| <PropertyGroup Condition="'$(DisableImplicitConfigurationDefines)' != 'true' and '$(Configuration)' != ''"> | ||
| <ImplicitConfigurationDefine>$(Configuration.ToUpperInvariant())</ImplicitConfigurationDefine> | ||
|
|
||
|
|
||




Uh oh!
There was an error while loading. Please reload this page.