diff --git a/src/MSIdentityScaffolding/Microsoft.DotNet.MSIdentity/CodeReaderWriter/ProjectModifier.cs b/src/MSIdentityScaffolding/Microsoft.DotNet.MSIdentity/CodeReaderWriter/ProjectModifier.cs index c24c0b314..2293adcf0 100644 --- a/src/MSIdentityScaffolding/Microsoft.DotNet.MSIdentity/CodeReaderWriter/ProjectModifier.cs +++ b/src/MSIdentityScaffolding/Microsoft.DotNet.MSIdentity/CodeReaderWriter/ProjectModifier.cs @@ -6,7 +6,6 @@ using System.Text; using System.Text.Json; using System.Threading.Tasks; -using Microsoft.Build.Locator; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -64,12 +63,6 @@ public async Task AddAuthCodeAsync() return; } - // Initialize Microsoft.Build assemblies - if (!MSBuildLocator.IsRegistered) - { - MSBuildLocator.RegisterDefaults(); - } - // Initialize CodeAnalysis.Project wrapper CodeAnalysis.Project project = await CodeAnalysisHelper.LoadCodeAnalysisProjectAsync(_toolOptions.ProjectFilePath, _files); if (project is null) diff --git a/src/MSIdentityScaffolding/Microsoft.DotNet.MSIdentity/Microsoft.DotNet.MSIdentity.csproj b/src/MSIdentityScaffolding/Microsoft.DotNet.MSIdentity/Microsoft.DotNet.MSIdentity.csproj index 374408f88..cbfe47e40 100644 --- a/src/MSIdentityScaffolding/Microsoft.DotNet.MSIdentity/Microsoft.DotNet.MSIdentity.csproj +++ b/src/MSIdentityScaffolding/Microsoft.DotNet.MSIdentity/Microsoft.DotNet.MSIdentity.csproj @@ -37,7 +37,6 @@ - diff --git a/src/Scaffolding/VS.Web.CG.Design/CodeGenCommandExecutor.cs b/src/Scaffolding/VS.Web.CG.Design/CodeGenCommandExecutor.cs index 69b4fef03..a02ceefee 100644 --- a/src/Scaffolding/VS.Web.CG.Design/CodeGenCommandExecutor.cs +++ b/src/Scaffolding/VS.Web.CG.Design/CodeGenCommandExecutor.cs @@ -74,8 +74,7 @@ private void AddFrameworkServices(ServiceProvider serviceProvider, IProjectConte { var applicationInfo = new ApplicationInfo( projectInformation.ProjectName, - Path.GetDirectoryName(projectInformation.ProjectFullPath), - new RoslynWorkspaceHelper(projectInformation.ProjectFullPath)); + Path.GetDirectoryName(projectInformation.ProjectFullPath)); serviceProvider.Add(projectInformation); serviceProvider.Add(applicationInfo); serviceProvider.Add(new DefaultAssemblyLoadContext()); diff --git a/src/Scaffolding/VS.Web.CG.Design/VS.Web.CG.Design.csproj b/src/Scaffolding/VS.Web.CG.Design/VS.Web.CG.Design.csproj index 66de5c712..a9f70c0d4 100644 --- a/src/Scaffolding/VS.Web.CG.Design/VS.Web.CG.Design.csproj +++ b/src/Scaffolding/VS.Web.CG.Design/VS.Web.CG.Design.csproj @@ -14,7 +14,6 @@ - project.AssemblyName == _projectContext.AssemblyName) @@ -382,7 +377,7 @@ private async Task GenerateNewDbContextAndRegisterProgramFile(ModelType programT } // Create a new Context _logger.LogMessage(string.Format(MessageStrings.GeneratingDbContext, _dbContextFullTypeName)); - bool nullabledEnabled = "enable".Equals(applicationInfo?.WorkspaceHelper?.GetMsBuildProperty("Nullable"), StringComparison.OrdinalIgnoreCase); + bool nullabledEnabled = "enable".Equals(_projectContext.Nullable, StringComparison.OrdinalIgnoreCase); bool useTopLevelsStatements = await ProjectModifierHelper.IsUsingTopLevelStatements(_modelTypesLocator); var dbContextTemplateModel = new NewDbContextTemplateModel(_dbContextFullTypeName, _modelTypeSymbol, programType, nullabledEnabled); _dbContextSyntaxTree = await _dbContextEditorServices.AddNewContext(dbContextTemplateModel); @@ -451,7 +446,7 @@ private async Task GenerateNewDbContextAndRegister(ModelType startupType, ModelT } // Create a new Context _logger.LogMessage(string.Format(MessageStrings.GeneratingDbContext, _dbContextFullTypeName)); - bool nullabledEnabled = "enable".Equals(applicationInfo?.WorkspaceHelper?.GetMsBuildProperty("Nullable"), StringComparison.OrdinalIgnoreCase); + bool nullabledEnabled = "enable".Equals(_projectContext.Nullable, StringComparison.OrdinalIgnoreCase); var dbContextTemplateModel = new NewDbContextTemplateModel(_dbContextFullTypeName, _modelTypeSymbol, programType, nullabledEnabled); _dbContextSyntaxTree = await _dbContextEditorServices.AddNewContext(dbContextTemplateModel); diff --git a/src/Scaffolding/VS.Web.CG.Msbuild/ProjectContextWriter.cs b/src/Scaffolding/VS.Web.CG.Msbuild/ProjectContextWriter.cs index 02c6949ed..767ec1d98 100644 --- a/src/Scaffolding/VS.Web.CG.Msbuild/ProjectContextWriter.cs +++ b/src/Scaffolding/VS.Web.CG.Msbuild/ProjectContextWriter.cs @@ -83,6 +83,9 @@ public class ProjectContextWriter : Build.Utilities.Task //not required as it might not get a value (fails if required and value not present). public string GeneratedImplicitNamespaceImportFile { get; set; } + + //not required as it might not get a value (fails if required and value not present). + public string Nullable { get; set; } #endregion public override bool Execute() @@ -108,7 +111,8 @@ public override bool Execute() TargetDirectory = this.TargetDirectory, TargetFramework = this.TargetFramework, TargetFrameworkMoniker = this.TargetFrameworkMoniker, - GeneratedImplicitNamespaceImportFile = this.GeneratedImplicitNamespaceImportFile + GeneratedImplicitNamespaceImportFile = this.GeneratedImplicitNamespaceImportFile, + Nullable = this.Nullable }; var projectReferences = msBuildContext.ProjectReferences; diff --git a/src/Scaffolding/VS.Web.CG.Msbuild/Target/build/Microsoft.VisualStudio.Web.CodeGeneration.Tools.targets b/src/Scaffolding/VS.Web.CG.Msbuild/Target/build/Microsoft.VisualStudio.Web.CodeGeneration.Tools.targets index 6304dc765..79ccd2da1 100644 --- a/src/Scaffolding/VS.Web.CG.Msbuild/Target/build/Microsoft.VisualStudio.Web.CodeGeneration.Tools.targets +++ b/src/Scaffolding/VS.Web.CG.Msbuild/Target/build/Microsoft.VisualStudio.Web.CodeGeneration.Tools.targets @@ -48,6 +48,7 @@ Outputs the Project Information needed for CodeGeneration to a file. ProjectRuntimeConfigFileName="$(ProjectRuntimeConfigFileName)" ProjectAssetsFile="$(ProjectAssetsFile)" GeneratedImplicitNamespaceImportFile = "$(GeneratedImplicitNamespaceImportFile)" + Nullable = "$(Nullable)" /> diff --git a/src/Scaffolding/VS.Web.CG.Mvc/Controller/ControllerWithContextGenerator.cs b/src/Scaffolding/VS.Web.CG.Mvc/Controller/ControllerWithContextGenerator.cs index bb0b5b037..239d6fa59 100644 --- a/src/Scaffolding/VS.Web.CG.Mvc/Controller/ControllerWithContextGenerator.cs +++ b/src/Scaffolding/VS.Web.CG.Mvc/Controller/ControllerWithContextGenerator.cs @@ -6,7 +6,6 @@ using System.Diagnostics.Contracts; using System.IO; using System.Threading.Tasks; -using Microsoft.Build.Locator; using Microsoft.DotNet.Scaffolding.Shared; using Microsoft.DotNet.Scaffolding.Shared.Project; using Microsoft.DotNet.Scaffolding.Shared.ProjectModel; @@ -81,10 +80,6 @@ public override async Task Generate(CommandLineGeneratorModel controllerGenerato controllerGeneratorModel.ControllerName = modelTypeAndContextModel.ModelType.Name + Constants.ControllerSuffix; } - if (!MSBuildLocator.IsRegistered) - { - MSBuildLocator.RegisterDefaults(); - } var namespaceName = string.IsNullOrEmpty(controllerGeneratorModel.ControllerNamespace) ? GetDefaultControllerNamespace(controllerGeneratorModel.RelativeFolderPath) : controllerGeneratorModel.ControllerNamespace; @@ -95,7 +90,7 @@ public override async Task Generate(CommandLineGeneratorModel controllerGenerato UseAsync = controllerGeneratorModel.UseAsync, // This is no longer used for controllers with context. ControllerNamespace = namespaceName, ModelMetadata = modelTypeAndContextModel.ContextProcessingResult.ModelMetadata, - NullableEnabled = "enable".Equals(ApplicationInfo?.WorkspaceHelper?.GetMsBuildProperty("Nullable"), StringComparison.OrdinalIgnoreCase) + NullableEnabled = "enable".Equals(ProjectContext.Nullable, StringComparison.OrdinalIgnoreCase) }; await CodeGeneratorActionsService.AddFileFromTemplateAsync(outputPath, GetTemplateName(controllerGeneratorModel), TemplateFolders, templateModel); diff --git a/src/Scaffolding/VS.Web.CG.Mvc/Minimal Api/MinimalApiGenerator.cs b/src/Scaffolding/VS.Web.CG.Mvc/Minimal Api/MinimalApiGenerator.cs index 20e5f6905..b07965a24 100644 --- a/src/Scaffolding/VS.Web.CG.Mvc/Minimal Api/MinimalApiGenerator.cs +++ b/src/Scaffolding/VS.Web.CG.Mvc/Minimal Api/MinimalApiGenerator.cs @@ -6,7 +6,6 @@ using System.Reflection; using System.Text.Json; using System.Threading.Tasks; -using Microsoft.Build.Locator; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; @@ -75,11 +74,6 @@ public async Task GenerateCode(MinimalApiGeneratorCommandLineModel model) ModelTypesLocator, areaName : string.Empty); - if (!MSBuildLocator.IsRegistered) - { - MSBuildLocator.RegisterDefaults(); - } - if (!string.IsNullOrEmpty(modelTypeAndContextModel.DbContextFullName) && CalledFromCommandline) { EFValidationUtil.ValidateEFDependencies(ProjectContext.PackageDependencies, useSqlite: model.UseSqlite); @@ -90,7 +84,7 @@ public async Task GenerateCode(MinimalApiGeneratorCommandLineModel model) EndpointsName = model.EndpintsClassName, EndpointsNamespace = namespaceName, ModelMetadata = modelTypeAndContextModel.ContextProcessingResult?.ModelMetadata, - NullableEnabled = "enable".Equals(AppInfo?.WorkspaceHelper?.GetMsBuildProperty("Nullable"), StringComparison.OrdinalIgnoreCase), + NullableEnabled = "enable".Equals(ProjectContext?.Nullable, StringComparison.OrdinalIgnoreCase), OpenAPI = model.OpenApi, MethodName = $"Map{modelTypeAndContextModel.ModelType.Name}Endpoints", UseSqlite = model.UseSqlite diff --git a/src/Scaffolding/VS.Web.CG.Mvc/RazorPage/RazorPageGeneratorTemplateModel.cs b/src/Scaffolding/VS.Web.CG.Mvc/RazorPage/RazorPageGeneratorTemplateModel.cs index 84dede036..4ec5fa969 100644 --- a/src/Scaffolding/VS.Web.CG.Mvc/RazorPage/RazorPageGeneratorTemplateModel.cs +++ b/src/Scaffolding/VS.Web.CG.Mvc/RazorPage/RazorPageGeneratorTemplateModel.cs @@ -1,4 +1,4 @@ -// Copyright (c) .NET Foundation. All rights reserved. +// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; @@ -31,7 +31,7 @@ public class RazorPageGeneratorTemplateModel public IModelMetadata ModelMetadata { get; set; } public string JQueryVersion { get; set; } - public bool NullableEnabled { get; set; } = false; + public string NullableEnabled { get; set; } } } diff --git a/src/Scaffolding/VS.Web.CG.Mvc/RazorPage/RazorPageScaffolderBase.cs b/src/Scaffolding/VS.Web.CG.Mvc/RazorPage/RazorPageScaffolderBase.cs index 7fb990a0f..7ca9496d4 100644 --- a/src/Scaffolding/VS.Web.CG.Mvc/RazorPage/RazorPageScaffolderBase.cs +++ b/src/Scaffolding/VS.Web.CG.Mvc/RazorPage/RazorPageScaffolderBase.cs @@ -6,7 +6,6 @@ using System.IO; using System.Linq; using System.Threading.Tasks; -using Microsoft.Build.Locator; using Microsoft.DotNet.Scaffolding.Shared; using Microsoft.DotNet.Scaffolding.Shared.ProjectModel; using Microsoft.VisualStudio.Web.CodeGeneration; @@ -252,7 +251,7 @@ protected RazorPageGeneratorTemplateModel GetRazorPageViewGeneratorTemplateModel JQueryVersion = "1.10.2", //Todo BootstrapVersion = razorGeneratorModel.BootstrapVersion, ContentVersion = DetermineContentVersion(razorGeneratorModel), - NullableEnabled = "enable".Equals(ApplicationInfo?.WorkspaceHelper?.GetMsBuildProperty("Nullable"), StringComparison.OrdinalIgnoreCase) + NullableEnabled = _projectContext.Nullable }; return templateModel; @@ -272,11 +271,6 @@ protected RazorPageWithContextTemplateModel GetRazorPageWithContextTemplateModel razorGeneratorModel.BootstrapVersion = RazorPageScaffolderBase.DefaultBootstrapVersion; } - if (!MSBuildLocator.IsRegistered) - { - MSBuildLocator.RegisterDefaults(); - } - RazorPageWithContextTemplateModel2 templateModel = new RazorPageWithContextTemplateModel2(modelTypeAndContextModel.ModelType, modelTypeAndContextModel.DbContextFullName) { NamespaceName = namespaceName, @@ -295,7 +289,7 @@ protected RazorPageWithContextTemplateModel GetRazorPageWithContextTemplateModel BootstrapVersion = razorGeneratorModel.BootstrapVersion, ContentVersion = DetermineContentVersion(razorGeneratorModel), UseSqlite = razorGeneratorModel.UseSqlite, - NullableEnabled = "enable".Equals(ApplicationInfo?.WorkspaceHelper?.GetMsBuildProperty("Nullable"), StringComparison.OrdinalIgnoreCase) + NullableEnabled = _projectContext.Nullable }; return templateModel; diff --git a/src/Scaffolding/VS.Web.CG.Mvc/View/ViewGeneratorTemplateModel.cs b/src/Scaffolding/VS.Web.CG.Mvc/View/ViewGeneratorTemplateModel.cs index 42c340d6b..c0b1ef3fa 100644 --- a/src/Scaffolding/VS.Web.CG.Mvc/View/ViewGeneratorTemplateModel.cs +++ b/src/Scaffolding/VS.Web.CG.Mvc/View/ViewGeneratorTemplateModel.cs @@ -25,6 +25,6 @@ public class ViewGeneratorTemplateModel public IModelMetadata ModelMetadata { get; set; } public string JQueryVersion { get; set; } - public bool NullableEnabled { get; set; } + public string NullableEnabled { get; set; } } } diff --git a/src/Scaffolding/VS.Web.CG.Mvc/View/ViewScaffolderBase.cs b/src/Scaffolding/VS.Web.CG.Mvc/View/ViewScaffolderBase.cs index 32c0910df..aba0c55ac 100644 --- a/src/Scaffolding/VS.Web.CG.Mvc/View/ViewScaffolderBase.cs +++ b/src/Scaffolding/VS.Web.CG.Mvc/View/ViewScaffolderBase.cs @@ -6,7 +6,6 @@ using System.IO; using System.Linq; using System.Threading.Tasks; -using Microsoft.Build.Locator; using Microsoft.DotNet.Scaffolding.Shared; using Microsoft.DotNet.Scaffolding.Shared.ProjectModel; using Microsoft.VisualStudio.Web.CodeGeneration.DotNet; @@ -168,10 +167,6 @@ protected ViewGeneratorTemplateModel GetViewGeneratorTemplateModel(ViewGenerator bool isLayoutSelected = !viewGeneratorModel.PartialView && (viewGeneratorModel.UseDefaultLayout || !String.IsNullOrEmpty(viewGeneratorModel.LayoutPage)); - if (!MSBuildLocator.IsRegistered) - { - MSBuildLocator.RegisterDefaults(); - } ViewGeneratorTemplateModel templateModel = new ViewGeneratorTemplateModel() { ViewDataTypeName = modelTypeAndContextModel?.ModelType?.FullName, @@ -183,7 +178,7 @@ protected ViewGeneratorTemplateModel GetViewGeneratorTemplateModel(ViewGenerator ReferenceScriptLibraries = viewGeneratorModel.ReferenceScriptLibraries, ModelMetadata = modelTypeAndContextModel?.ContextProcessingResult?.ModelMetadata, JQueryVersion = "1.10.2", //Todo, - NullableEnabled = "enable".Equals(ApplicationInfo?.WorkspaceHelper?.GetMsBuildProperty("Nullable"), StringComparison.OrdinalIgnoreCase) + NullableEnabled = _projectContext.Nullable }; return templateModel; diff --git a/src/Scaffolding/VS.Web.CG.Utils/DotNet/ApplicationInfo.cs b/src/Scaffolding/VS.Web.CG.Utils/DotNet/ApplicationInfo.cs index 41bdec93e..60b228782 100644 --- a/src/Scaffolding/VS.Web.CG.Utils/DotNet/ApplicationInfo.cs +++ b/src/Scaffolding/VS.Web.CG.Utils/DotNet/ApplicationInfo.cs @@ -8,13 +8,13 @@ namespace Microsoft.VisualStudio.Web.CodeGeneration.DotNet { public class ApplicationInfo : IApplicationInfo { - public ApplicationInfo(string appName, string appBasePath, RoslynWorkspaceHelper workspaceHelper) - : this(appName, appBasePath, "Debug", workspaceHelper) + public ApplicationInfo(string appName, string appBasePath) + : this(appName, appBasePath, "Debug") { } - public ApplicationInfo(string appName, string appBasePath, string appConfiguration, RoslynWorkspaceHelper workspaceHelper) + public ApplicationInfo(string appName, string appBasePath, string appConfiguration) { if (appName == null) { @@ -31,7 +31,6 @@ public ApplicationInfo(string appName, string appBasePath, string appConfigurati ApplicationName = appName; ApplicationBasePath = appBasePath; ApplicationConfiguration = appConfiguration; - WorkspaceHelper = workspaceHelper; } public string ApplicationBasePath @@ -48,10 +47,5 @@ public string ApplicationConfiguration { get; private set; } - - public RoslynWorkspaceHelper WorkspaceHelper - { - get; private set; - } } } diff --git a/src/Scaffolding/VS.Web.CG.Utils/DotNet/IApplicationInfo.cs b/src/Scaffolding/VS.Web.CG.Utils/DotNet/IApplicationInfo.cs index 96b336a16..790e2d954 100644 --- a/src/Scaffolding/VS.Web.CG.Utils/DotNet/IApplicationInfo.cs +++ b/src/Scaffolding/VS.Web.CG.Utils/DotNet/IApplicationInfo.cs @@ -9,6 +9,5 @@ public interface IApplicationInfo string ApplicationBasePath { get; } string ApplicationName { get; } string ApplicationConfiguration { get; } - RoslynWorkspaceHelper WorkspaceHelper { get; } } } diff --git a/src/Scaffolding/VS.Web.CG.Utils/VS.Web.CG.Utils.csproj b/src/Scaffolding/VS.Web.CG.Utils/VS.Web.CG.Utils.csproj index 714c7c1d7..530974ac0 100644 --- a/src/Scaffolding/VS.Web.CG.Utils/VS.Web.CG.Utils.csproj +++ b/src/Scaffolding/VS.Web.CG.Utils/VS.Web.CG.Utils.csproj @@ -1,4 +1,4 @@ - + Contains utilities used by ASP.NET Core Code Generation packages. @@ -20,8 +20,7 @@ - - + - + \ No newline at end of file diff --git a/src/Scaffolding/VS.Web.CG.Utils/Workspaces/RoslynWorkspace.cs b/src/Scaffolding/VS.Web.CG.Utils/Workspaces/RoslynWorkspace.cs index 9cedd0950..06181a523 100644 --- a/src/Scaffolding/VS.Web.CG.Utils/Workspaces/RoslynWorkspace.cs +++ b/src/Scaffolding/VS.Web.CG.Utils/Workspaces/RoslynWorkspace.cs @@ -7,7 +7,6 @@ using System.Linq; using System.Reflection.PortableExecutable; using System.Text; -using Microsoft.Build.Locator; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Host.Mef; using Microsoft.CodeAnalysis.Text; @@ -34,12 +33,7 @@ public RoslynWorkspace(IProjectContext projectInformation, var id = AddProject(projectInformation, configuration); // Since we have resolved all references, we can directly use them as MetadataReferences. // Trying to get ProjectReferences manually might lead to problems when the projects have circular dependency. - if (!MSBuildLocator.IsRegistered) - { - MSBuildLocator.RegisterDefaults(); - } - RoslynWorkspaceHelper roslynHelper = new RoslynWorkspaceHelper(projectInformation.ProjectFullPath); - var projReferenceInformation = roslynHelper.GetProjectReferenceInformation(projectInformation.ProjectReferences); + var projReferenceInformation = RoslynWorkspaceHelper.GetProjectReferenceInformation(projectInformation.ProjectReferences); if (projReferenceInformation != null && projReferenceInformation.Any()) { diff --git a/src/Scaffolding/VS.Web.CG.Utils/Workspaces/RoslynWorkspaceHelper.cs b/src/Scaffolding/VS.Web.CG.Utils/Workspaces/RoslynWorkspaceHelper.cs index fcf19168d..fc42dc6ef 100644 --- a/src/Scaffolding/VS.Web.CG.Utils/Workspaces/RoslynWorkspaceHelper.cs +++ b/src/Scaffolding/VS.Web.CG.Utils/Workspaces/RoslynWorkspaceHelper.cs @@ -7,59 +7,9 @@ namespace Microsoft.VisualStudio.Web.CodeGeneration.Utils { - public class RoslynWorkspaceHelper + public static class RoslynWorkspaceHelper { - private string _projectPath; - private Project _project; - public Project MsBuildProject - { - get - { - if (_project == null) - { - _project = new Project(_projectPath); - } - return _project; - } - } - - public RoslynWorkspaceHelper(string projectPath) - { - _projectPath = projectPath; - } - - public Dictionary GetMsBuildProperties(IEnumerable properties) - { - Dictionary msbuildProperties = new Dictionary(); - if (properties != null && properties.Any()) - { - foreach (string property in properties) - { - string value = GetMsBuildProperty(property); - if (!string.IsNullOrEmpty(value)) - { - msbuildProperties.Add(property, value); - } - } - } - return msbuildProperties; - } - - public string GetMsBuildProperty(string property) - { - if (!string.IsNullOrEmpty(property)) - { - string value = MsBuildProject?.GetProperty(property)?.EvaluatedValue; - if (!string.IsNullOrEmpty(value)) - { - return value; - } - } - - return string.Empty; - } - - internal IEnumerable GetProjectReferenceInformation(IEnumerable projectReferenceStrings) + internal static IEnumerable GetProjectReferenceInformation(IEnumerable projectReferenceStrings) { List projectReferenceInformation = new List(); if (projectReferenceStrings != null && projectReferenceStrings.Any()) @@ -81,7 +31,7 @@ internal IEnumerable GetProjectReferenceInformation return projectReferenceInformation; } - private ProjectReferenceInformation GetProjectInformation(Project project) + internal static ProjectReferenceInformation GetProjectInformation(Project project) { var compileItems = project.GetItems("Compile").Select(i => i.EvaluatedInclude); var fullPath = project.FullPath; diff --git a/src/Shared/Microsoft.DotNet.Scaffolding.Shared/ProjectModel/CommonProjectContext.cs b/src/Shared/Microsoft.DotNet.Scaffolding.Shared/ProjectModel/CommonProjectContext.cs index 162cc6333..430c19b4d 100644 --- a/src/Shared/Microsoft.DotNet.Scaffolding.Shared/ProjectModel/CommonProjectContext.cs +++ b/src/Shared/Microsoft.DotNet.Scaffolding.Shared/ProjectModel/CommonProjectContext.cs @@ -74,5 +74,7 @@ public class CommonProjectContext : IProjectContext /// public string GeneratedImplicitNamespaceImportFile { get; set; } + + public string Nullable { get; set; } } } diff --git a/src/Shared/Microsoft.DotNet.Scaffolding.Shared/ProjectModel/IProjectContext.cs b/src/Shared/Microsoft.DotNet.Scaffolding.Shared/ProjectModel/IProjectContext.cs index 8cd85fc58..e1dfd3606 100644 --- a/src/Shared/Microsoft.DotNet.Scaffolding.Shared/ProjectModel/IProjectContext.cs +++ b/src/Shared/Microsoft.DotNet.Scaffolding.Shared/ProjectModel/IProjectContext.cs @@ -129,5 +129,7 @@ public interface IProjectContext /// .cs file in obj folder generated at compile time with all default namespace imports in .NET 6+. /// string GeneratedImplicitNamespaceImportFile { get; } + + string Nullable { get; } } } diff --git a/src/Shared/Microsoft.DotNet.Scaffolding.Shared/ProjectModel/ProjectContextExtensions.cs b/src/Shared/Microsoft.DotNet.Scaffolding.Shared/ProjectModel/ProjectContextExtensions.cs index d91078d06..aa6e95d27 100644 --- a/src/Shared/Microsoft.DotNet.Scaffolding.Shared/ProjectModel/ProjectContextExtensions.cs +++ b/src/Shared/Microsoft.DotNet.Scaffolding.Shared/ProjectModel/ProjectContextExtensions.cs @@ -57,7 +57,8 @@ public static IProjectContext AddPackageDependencies(this IProjectContext projec TargetDirectory = projectInformation.TargetDirectory, TargetFramework = projectInformation.TargetFramework, TargetFrameworkMoniker = projectInformation.TargetFrameworkMoniker, - GeneratedImplicitNamespaceImportFile = projectInformation.GeneratedImplicitNamespaceImportFile + GeneratedImplicitNamespaceImportFile = projectInformation.GeneratedImplicitNamespaceImportFile, + Nullable = projectInformation.Nullable }; return newProjectContext; } diff --git a/test/Scaffolding/Shared/MSBuildProjectStrings.cs b/test/Scaffolding/Shared/MSBuildProjectStrings.cs index e48ed76bb..21226709e 100644 --- a/test/Scaffolding/Shared/MSBuildProjectStrings.cs +++ b/test/Scaffolding/Shared/MSBuildProjectStrings.cs @@ -11,6 +11,9 @@ namespace Microsoft.VisualStudio.Web.CodeGeneration internal class MsBuildProjectStrings { public const string RootProjectName = "Test.csproj"; + public const string RootProjectName2 = "Test2.csproj"; + public const string RootProjectName3 = "Test3.csproj"; + public const string RootProjectTxt = @" @@ -909,6 +912,37 @@ public static IWebHost BuildWebHost(string[] args) => "; + public const string Net6NullableEnabled = @" + + + + net6.0 + enable + enable + + +"; + + public const string Net6NullableDisabled = @" + + + + net6.0 + disable + enable + + +"; + + public const string Net6NullableMissing = @" + + + + net6.0 + enable + +"; + public const string BaseDbContextText = @" using Microsoft.EntityFrameworkCore; using Test.Models; @@ -1134,7 +1168,8 @@ public class Blog ProjectDepsFileName=""$(ProjectDepsFileName)"" ProjectRuntimeConfigFileName=""$(ProjectRuntimeConfigFileName)"" ProjectAssetsFile=""$(ProjectAssetsFile)"" - GeneratedImplicitNamespaceImportFile = ""$(GeneratedImplicitNamespaceImportFile)""/> + GeneratedImplicitNamespaceImportFile = ""$(GeneratedImplicitNamespaceImportFile)"" + Nullable=""$(Nullable)""/> "; diff --git a/test/Scaffolding/Shared/MsBuildProjectSetupHelper.cs b/test/Scaffolding/Shared/MsBuildProjectSetupHelper.cs index 584e534a1..0bce5756d 100644 --- a/test/Scaffolding/Shared/MsBuildProjectSetupHelper.cs +++ b/test/Scaffolding/Shared/MsBuildProjectSetupHelper.cs @@ -73,6 +73,63 @@ internal void SetupEmptyCodeGenerationProject(TemporaryFileProvider fileProvider RestoreAndBuild(fileProvider.Root, outputHelper); } + internal void SetupCodeGenerationProjectNullableDisabled(TemporaryFileProvider fileProvider, ITestOutputHelper outputHelper) + { + fileProvider.Add("global.json", GlobalJsonText); + Directory.CreateDirectory(Path.Combine(fileProvider.Root, "toolAssets", "net6.0")); + + fileProvider.Add($"TestCodeGeneration.targets", MsBuildProjectStrings.ProjectContextWriterMsbuildHelperText); + + var msbuildTaskDllPath = Path.Combine(Path.GetDirectoryName(typeof(MsBuildProjectSetupHelper).Assembly.Location), "Microsoft.VisualStudio.Web.CodeGeneration.Msbuild.dll"); + fileProvider.Copy(msbuildTaskDllPath, "toolAssets/net6.0/Microsoft.VisualStudio.Web.CodeGeneration.Msbuild.dll"); + + var rootProjectTxt = MsBuildProjectStrings.Net6NullableDisabled; + fileProvider.Add(MsBuildProjectStrings.RootProjectName2, rootProjectTxt); + fileProvider.Add("Startup.cs", MsBuildProjectStrings.EmptyTestStartupText); + fileProvider.Add(MsBuildProjectStrings.DbContextInheritanceProgramName, MsBuildProjectStrings.DbContextInheritanceProjectProgramText); + fileProvider.Add(MsBuildProjectStrings.AppSettingsFileName, MsBuildProjectStrings.AppSettingsFileTxt); + + RestoreAndBuild(fileProvider.Root, outputHelper); + } + + internal void SetupCodeGenerationProjectNullableMissing(TemporaryFileProvider fileProvider, ITestOutputHelper outputHelper) + { + fileProvider.Add("global.json", GlobalJsonText); + Directory.CreateDirectory(Path.Combine(fileProvider.Root, "toolAssets", "net6.0")); + + fileProvider.Add($"TestCodeGeneration.targets", MsBuildProjectStrings.ProjectContextWriterMsbuildHelperText); + + var msbuildTaskDllPath = Path.Combine(Path.GetDirectoryName(typeof(MsBuildProjectSetupHelper).Assembly.Location), "Microsoft.VisualStudio.Web.CodeGeneration.Msbuild.dll"); + fileProvider.Copy(msbuildTaskDllPath, "toolAssets/net6.0/Microsoft.VisualStudio.Web.CodeGeneration.Msbuild.dll"); + + var rootProjectTxt = MsBuildProjectStrings.Net6NullableMissing; + fileProvider.Add(MsBuildProjectStrings.RootProjectName3, rootProjectTxt); + fileProvider.Add("Startup.cs", MsBuildProjectStrings.EmptyTestStartupText); + fileProvider.Add(MsBuildProjectStrings.DbContextInheritanceProgramName, MsBuildProjectStrings.DbContextInheritanceProjectProgramText); + fileProvider.Add(MsBuildProjectStrings.AppSettingsFileName, MsBuildProjectStrings.AppSettingsFileTxt); + + RestoreAndBuild(fileProvider.Root, outputHelper); + } + + internal void SetupCodeGenerationProjectNullableEnabled(TemporaryFileProvider fileProvider, ITestOutputHelper outputHelper) + { + fileProvider.Add("global.json", GlobalJsonText); + Directory.CreateDirectory(Path.Combine(fileProvider.Root, "toolAssets", "net6.0")); + + fileProvider.Add($"TestCodeGeneration.targets", MsBuildProjectStrings.ProjectContextWriterMsbuildHelperText); + + var msbuildTaskDllPath = Path.Combine(Path.GetDirectoryName(typeof(MsBuildProjectSetupHelper).Assembly.Location), "Microsoft.VisualStudio.Web.CodeGeneration.Msbuild.dll"); + fileProvider.Copy(msbuildTaskDllPath, "toolAssets/net6.0/Microsoft.VisualStudio.Web.CodeGeneration.Msbuild.dll"); + + var rootProjectTxt = MsBuildProjectStrings.Net6NullableEnabled; + fileProvider.Add(MsBuildProjectStrings.RootProjectName3, rootProjectTxt); + fileProvider.Add("Startup.cs", MsBuildProjectStrings.EmptyTestStartupText); + fileProvider.Add(MsBuildProjectStrings.DbContextInheritanceProgramName, MsBuildProjectStrings.DbContextInheritanceProjectProgramText); + fileProvider.Add(MsBuildProjectStrings.AppSettingsFileName, MsBuildProjectStrings.AppSettingsFileTxt); + + RestoreAndBuild(fileProvider.Root, outputHelper); + } + public void SetupProjects(TemporaryFileProvider fileProvider, ITestOutputHelper output, bool fullFramework = false) { Directory.CreateDirectory(Path.Combine(fileProvider.Root, "Root")); diff --git a/test/Scaffolding/VS.Web.CG.MSBuild.Test/ProjectContextWriterTests.cs b/test/Scaffolding/VS.Web.CG.MSBuild.Test/ProjectContextWriterTests.cs index d33c12976..d617cd41a 100644 --- a/test/Scaffolding/VS.Web.CG.MSBuild.Test/ProjectContextWriterTests.cs +++ b/test/Scaffolding/VS.Web.CG.MSBuild.Test/ProjectContextWriterTests.cs @@ -42,6 +42,47 @@ public void TestProjectContextFromMsBuild() } } + [Fact] + public void TestProjectContextNullableDisabled() + { + using (var fileProvider = new TemporaryFileProvider()) + { + new MsBuildProjectSetupHelper().SetupCodeGenerationProjectNullableDisabled(fileProvider, _outputHelper); + var path = Path.Combine(fileProvider.Root, MsBuildProjectStrings.RootProjectName2); + + var projectContext = GetProjectContext(path, true); + Assert.NotNull(projectContext.Nullable); + Assert.Equal("disable", projectContext.Nullable); + } + } + + [Fact] + public void TestProjectContextNullableMissing() + { + using (var fileProvider = new TemporaryFileProvider()) + { + new MsBuildProjectSetupHelper().SetupCodeGenerationProjectNullableMissing(fileProvider, _outputHelper); + var path = Path.Combine(fileProvider.Root, MsBuildProjectStrings.RootProjectName3); + + var projectContext = GetProjectContext(path, true); + Assert.Null(projectContext.Nullable); + } + } + + [Fact] + public void TestProjectContextNullableEnabled() + { + using (var fileProvider = new TemporaryFileProvider()) + { + new MsBuildProjectSetupHelper().SetupCodeGenerationProjectNullableEnabled(fileProvider, _outputHelper); + var path = Path.Combine(fileProvider.Root, MsBuildProjectStrings.RootProjectName3); + + var projectContext = GetProjectContext(path, true); + Assert.NotNull(projectContext.Nullable); + Assert.Equal("enable", projectContext.Nullable); + } + } + [SkippableTheory] [InlineData("C:\\Users\\User\\.nuget\\packages\\", "X.Y.Z", "1.2.3", "C:\\Users\\User\\.nuget\\packages\\x.y.z\\1.2.3")] [InlineData("C:\\Users\\User\\.nuget\\", "X.Y.Z", "1.2.3", "C:\\Users\\User\\.nuget\\x.y.z\\1.2.3")] diff --git a/test/Scaffolding/VS.Web.CG.Mvc.Test/ControllerGeneratorBaseTests.cs b/test/Scaffolding/VS.Web.CG.Mvc.Test/ControllerGeneratorBaseTests.cs index b23ecc74a..359d58228 100644 --- a/test/Scaffolding/VS.Web.CG.Mvc.Test/ControllerGeneratorBaseTests.cs +++ b/test/Scaffolding/VS.Web.CG.Mvc.Test/ControllerGeneratorBaseTests.cs @@ -27,7 +27,7 @@ public ControllerGeneratorBaseTests() _codeGenActionService = new Mock(); _serviceProvider = new Mock(); _logger = new ConsoleLogger(); - _applicationInfo = new ApplicationInfo("TestApp", "..", null); + _applicationInfo = new ApplicationInfo("TestApp", ".."); } [Fact] @@ -38,8 +38,7 @@ public void Test_ValidateNameSpace_ThrowsException() _applicationInfo, _codeGenActionService.Object, _serviceProvider.Object, - _logger - ); + _logger); var model = GetModel(); model.ControllerNamespace = "Invalid Namespace"; diff --git a/test/Scaffolding/VS.Web.CG.Mvc.Test/IdentityGeneratorTemplateModelBuilderTests.cs b/test/Scaffolding/VS.Web.CG.Mvc.Test/IdentityGeneratorTemplateModelBuilderTests.cs index ad9fe3eef..bdc48e5c3 100644 --- a/test/Scaffolding/VS.Web.CG.Mvc.Test/IdentityGeneratorTemplateModelBuilderTests.cs +++ b/test/Scaffolding/VS.Web.CG.Mvc.Test/IdentityGeneratorTemplateModelBuilderTests.cs @@ -50,7 +50,7 @@ public async Task TestValidateAndBuild() UseSqlite = false }; - var applicationInfo = new ApplicationInfo("TestApp", "Sample", null); + var applicationInfo = new ApplicationInfo("TestApp", "Sample"); var builder = new IdentityGeneratorTemplateModelBuilder( commandLineModel, @@ -139,7 +139,7 @@ public void SupportFileLocationForExistingLayoutFileTest(bool leadTilde, bool le IdentityGeneratorCommandLineModel commandLineModel = new IdentityGeneratorCommandLineModel(); commandLineModel.Layout = existingLayoutFile; - IApplicationInfo applicationInfo = new ApplicationInfo("test", LayoutFileLocationTestProjectBasePath, null); + IApplicationInfo applicationInfo = new ApplicationInfo("test", LayoutFileLocationTestProjectBasePath); CommonProjectContext context = new CommonProjectContext(); context.ProjectFullPath = LayoutFileLocationTestProjectBasePath; context.ProjectName = "TestProject"; diff --git a/test/Scaffolding/VS.Web.CG.Sources.Test/ApplicationInfoTests.cs b/test/Scaffolding/VS.Web.CG.Sources.Test/ApplicationInfoTests.cs index f02927b42..9851b7a6e 100644 --- a/test/Scaffolding/VS.Web.CG.Sources.Test/ApplicationInfoTests.cs +++ b/test/Scaffolding/VS.Web.CG.Sources.Test/ApplicationInfoTests.cs @@ -14,7 +14,7 @@ public class ApplicationInfoTests [Fact] public void ApplicationEnvironment_Test() { - _applicationInfo = new ApplicationInfo("TestApplication", Directory.GetCurrentDirectory(), null); + _applicationInfo = new ApplicationInfo("TestApplication", Directory.GetCurrentDirectory()); Assert.Equal(Directory.GetCurrentDirectory(), _applicationInfo.ApplicationBasePath); Assert.Equal("TestApplication", _applicationInfo.ApplicationName); }