No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SdkStyleProjectGeneration.cs 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Unity Technologies.
  3. * Copyright (c) Microsoft Corporation. All rights reserved.
  4. * Licensed under the MIT License. See License.txt in the project root for license information.
  5. *--------------------------------------------------------------------------------------------*/
  6. using System;
  7. using System.IO;
  8. using System.Text;
  9. using UnityEditor.Compilation;
  10. using UnityEngine;
  11. namespace Microsoft.Unity.VisualStudio.Editor
  12. {
  13. internal class SdkStyleProjectGeneration : ProjectGeneration
  14. {
  15. internal override string StyleName => "SDK";
  16. internal class SdkStyleAssemblyNameProvider : AssemblyNameProvider
  17. {
  18. // disable PlayerGeneration with SdkStyle projects
  19. internal override ProjectGenerationFlag ProjectGenerationFlagImpl => base.ProjectGenerationFlagImpl & ~ProjectGenerationFlag.PlayerAssemblies;
  20. }
  21. public SdkStyleProjectGeneration() : base(
  22. Directory.GetParent(Application.dataPath)?.FullName,
  23. new SdkStyleAssemblyNameProvider(),
  24. new FileIOProvider(),
  25. new GUIDProvider())
  26. {
  27. }
  28. internal static readonly string[] SupportedCapabilities = new string[]
  29. {
  30. "Unity",
  31. };
  32. internal static readonly string[] UnsupportedCapabilities = new string[]
  33. {
  34. "LaunchProfiles",
  35. "SharedProjectReferences",
  36. "ReferenceManagerSharedProjects",
  37. "ProjectReferences",
  38. "ReferenceManagerProjects",
  39. "COMReferences",
  40. "ReferenceManagerCOM",
  41. "AssemblyReferences",
  42. "ReferenceManagerAssemblies",
  43. };
  44. internal override void GetProjectHeader(ProjectProperties properties, out StringBuilder headerBuilder)
  45. {
  46. headerBuilder = new StringBuilder();
  47. headerBuilder.Append(@"<Project ToolsVersion=""Current"">").Append(k_WindowsNewline);
  48. headerBuilder.Append(@" <!-- Generated file, do not modify, your changes will be overwritten (use AssetPostprocessor.OnGeneratedCSProject) -->").Append(k_WindowsNewline);
  49. // Prevent circular dependency issues see https://github.com/microsoft/vscode-dotnettools/issues/401
  50. // We need a dedicated subfolder for each project in obj, else depending on the build order, nuget cache files could be overwritten
  51. // We need to do this before common.props, else we'll have a MSB3539 The value of the property "BaseIntermediateOutputPath" was modified after it was used by MSBuild
  52. headerBuilder.Append(@" <PropertyGroup>").Append(k_WindowsNewline);
  53. headerBuilder.Append($" <BaseIntermediateOutputPath>{@"Temp\obj\$(Configuration)\$(MSBuildProjectName)".NormalizePathSeparators()}</BaseIntermediateOutputPath>").Append(k_WindowsNewline);
  54. headerBuilder.Append(@" <IntermediateOutputPath>$(BaseIntermediateOutputPath)</IntermediateOutputPath>").Append(k_WindowsNewline);
  55. headerBuilder.Append(@" </PropertyGroup>").Append(k_WindowsNewline);
  56. // Supported capabilities
  57. GetCapabilityBlock(headerBuilder, "Sdk.props", "Include", SupportedCapabilities);
  58. headerBuilder.Append(@" <PropertyGroup>").Append(k_WindowsNewline);
  59. headerBuilder.Append(@" <GenerateAssemblyInfo>false</GenerateAssemblyInfo>").Append(k_WindowsNewline);
  60. headerBuilder.Append(@" <EnableDefaultItems>false</EnableDefaultItems>").Append(k_WindowsNewline);
  61. headerBuilder.Append(@" <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>").Append(k_WindowsNewline);
  62. headerBuilder.Append(@" <LangVersion>").Append(properties.LangVersion).Append(@"</LangVersion>").Append(k_WindowsNewline);
  63. headerBuilder.Append(@" <Configurations>Debug;Release</Configurations>").Append(k_WindowsNewline);
  64. headerBuilder.Append(@" <Configuration Condition="" '$(Configuration)' == '' "">Debug</Configuration>").Append(k_WindowsNewline);
  65. headerBuilder.Append(@" <Platform Condition="" '$(Platform)' == '' "">AnyCPU</Platform>").Append(k_WindowsNewline);
  66. headerBuilder.Append(@" <RootNamespace>").Append(properties.RootNamespace).Append(@"</RootNamespace>").Append(k_WindowsNewline);
  67. headerBuilder.Append(@" <OutputType>Library</OutputType>").Append(k_WindowsNewline);
  68. headerBuilder.Append(@" <AppDesignerFolder>Properties</AppDesignerFolder>").Append(k_WindowsNewline);
  69. headerBuilder.Append(@" <AssemblyName>").Append(properties.AssemblyName).Append(@"</AssemblyName>").Append(k_WindowsNewline);
  70. // In the end, given we use NoConfig/NoStdLib (see below), hardcoding the target framework version will have no impact, even when targeting netstandard/net48 from Unity.
  71. // But with SDK style we use netstandard2.1 (net471 for legacy), so 3rd party tools will not fail to work when .NETFW reference assemblies are not installed.
  72. // Unity already selected proper API surface through referenced DLLs for us.
  73. headerBuilder.Append(@" <TargetFramework>netstandard2.1</TargetFramework>").Append(k_WindowsNewline);
  74. headerBuilder.Append(@" <BaseDirectory>.</BaseDirectory>").Append(k_WindowsNewline);
  75. headerBuilder.Append(@" </PropertyGroup>").Append(k_WindowsNewline);
  76. GetProjectHeaderConfigurations(properties, headerBuilder);
  77. // Explicit references
  78. headerBuilder.Append(@" <PropertyGroup>").Append(k_WindowsNewline);
  79. headerBuilder.Append(@" <NoStandardLibraries>true</NoStandardLibraries>").Append(k_WindowsNewline);
  80. headerBuilder.Append(@" <NoStdLib>true</NoStdLib>").Append(k_WindowsNewline);
  81. headerBuilder.Append(@" <NoConfig>true</NoConfig>").Append(k_WindowsNewline);
  82. headerBuilder.Append(@" <DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>").Append(k_WindowsNewline);
  83. headerBuilder.Append(@" <MSBuildWarningsAsMessages>MSB3277</MSBuildWarningsAsMessages>").Append(k_WindowsNewline);
  84. headerBuilder.Append(@" </PropertyGroup>").Append(k_WindowsNewline);
  85. GetProjectHeaderVstuFlavoring(properties, headerBuilder, false);
  86. GetProjectHeaderAnalyzers(properties, headerBuilder);
  87. }
  88. internal override void AppendProjectReference(Assembly assembly, Assembly reference, StringBuilder projectBuilder)
  89. {
  90. // If the current assembly is a Player project, we want to project-reference the corresponding Player project
  91. var referenceName = m_AssemblyNameProvider.GetAssemblyName(assembly.outputPath, reference.name);
  92. projectBuilder.Append(@" <ProjectReference Include=""").Append(referenceName).Append(GetProjectExtension()).Append(@""" />").Append(k_WindowsNewline);
  93. }
  94. internal override void GetProjectFooter(StringBuilder footerBuilder)
  95. {
  96. // Unsupported capabilities
  97. GetCapabilityBlock(footerBuilder, "Sdk.targets", "Remove", UnsupportedCapabilities);
  98. footerBuilder.Append("</Project>").Append(k_WindowsNewline);
  99. }
  100. internal static void GetCapabilityBlock(StringBuilder footerBuilder, string import, string attribute, string[] capabilities)
  101. {
  102. footerBuilder.Append($@" <Import Project=""{import}"" Sdk=""Microsoft.NET.Sdk"" />").Append(k_WindowsNewline);
  103. footerBuilder.Append(@" <ItemGroup>").Append(k_WindowsNewline);
  104. foreach (var capability in capabilities)
  105. {
  106. footerBuilder.Append($@" <ProjectCapability {attribute}=""{capability}"" />").Append(k_WindowsNewline);
  107. }
  108. footerBuilder.Append(@" </ItemGroup>").Append(k_WindowsNewline);
  109. }
  110. }
  111. }