Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@

<ItemGroup>
<ProjectReference Include="$(RepoRoot)\src\Shared\Microsoft.DotNet.Scaffolding.Shared\Microsoft.DotNet.Scaffolding.Shared.csproj" />
<PackageReference Include="Microsoft.Build.Locator" Version="$(MicrosoftBuildLocatorPackageVersion)" />
<PackageReference Include="System.Security.Cryptography.ProtectedData" Version="6.0.0"/>
<Folder Include="Resources\" />
</ItemGroup>
Expand Down
3 changes: 1 addition & 2 deletions src/Scaffolding/VS.Web.CG.Design/CodeGenCommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IProjectContext>(projectInformation);
serviceProvider.Add<IApplicationInfo>(applicationInfo);
serviceProvider.Add<ICodeGenAssemblyLoadContext>(new DefaultAssemblyLoadContext());
Expand Down
1 change: 0 additions & 1 deletion src/Scaffolding/VS.Web.CG.Design/VS.Web.CG.Design.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

<ItemGroup>
<ProjectReference Include="$(RepoRoot)\src\Shared\Microsoft.DotNet.Scaffolding.Shared\Microsoft.DotNet.Scaffolding.Shared.csproj" />
<PackageReference Include="Microsoft.Build.Locator" Version="$(MicrosoftBuildLocatorPackageVersion)" />
</ItemGroup>
<ItemGroup>
<Content Include="$(OutputPath)$(AssemblyName).dll;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Build.Locator;
using Microsoft.CodeAnalysis;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
Expand Down Expand Up @@ -91,10 +90,6 @@ public async Task Process()
var startupType = _modelTypesLocator.GetType(startupClassName).FirstOrDefault() ?? _modelTypesLocator.GetType("Startup").FirstOrDefault();

ModelType dbContextSymbolInWebProject = null;
if (!MSBuildLocator.IsRegistered)
{
MSBuildLocator.RegisterDefaults();
}
//if there is no Startup.cs (minimal hosting app), this scaffolding scanerio is not supported.
if (startupType == null)
{
Expand Down Expand Up @@ -307,7 +302,7 @@ private async Task EnsureDbContextInLibraryIsValid(ModelType dbContextSymbol)

private async Task AddModelTypeToExistingDbContextIfNeeded(ModelType dbContextSymbol, IApplicationInfo appInfo)
{
bool nullabledEnabled = "enable".Equals(appInfo?.WorkspaceHelper?.GetMsBuildProperty("Nullable"), StringComparison.OrdinalIgnoreCase);
bool nullabledEnabled = "enable".Equals(_projectContext.Nullable, StringComparison.OrdinalIgnoreCase);
var addResult = _dbContextEditorServices.AddModelToContext(dbContextSymbol, _modelTypeSymbol, nullabledEnabled);
var projectCompilation = await _workspace.CurrentSolution.Projects
.First(project => project.AssemblyName == _projectContext.AssemblyName)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion src/Scaffolding/VS.Web.CG.Msbuild/ProjectContextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Outputs the Project Information needed for CodeGeneration to a file.
ProjectRuntimeConfigFileName="$(ProjectRuntimeConfigFileName)"
ProjectAssetsFile="$(ProjectAssetsFile)"
GeneratedImplicitNamespaceImportFile = "$(GeneratedImplicitNamespaceImportFile)"
Nullable = "$(Nullable)"
/>
</Target>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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; }

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
7 changes: 1 addition & 6 deletions src/Scaffolding/VS.Web.CG.Mvc/View/ViewScaffolderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down
12 changes: 3 additions & 9 deletions src/Scaffolding/VS.Web.CG.Utils/DotNet/ApplicationInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -31,7 +31,6 @@ public ApplicationInfo(string appName, string appBasePath, string appConfigurati
ApplicationName = appName;
ApplicationBasePath = appBasePath;
ApplicationConfiguration = appConfiguration;
WorkspaceHelper = workspaceHelper;
}

public string ApplicationBasePath
Expand All @@ -48,10 +47,5 @@ public string ApplicationConfiguration
{
get; private set;
}

public RoslynWorkspaceHelper WorkspaceHelper
{
get; private set;
}
}
}
1 change: 0 additions & 1 deletion src/Scaffolding/VS.Web.CG.Utils/DotNet/IApplicationInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ public interface IApplicationInfo
string ApplicationBasePath { get; }
string ApplicationName { get; }
string ApplicationConfiguration { get; }
RoslynWorkspaceHelper WorkspaceHelper { get; }
}
}
7 changes: 3 additions & 4 deletions src/Scaffolding/VS.Web.CG.Utils/VS.Web.CG.Utils.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>Contains utilities used by ASP.NET Core Code Generation packages.</Description>
Expand All @@ -20,8 +20,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion)" />
<PackageReference Include="Newtonsoft.Json" Version="$(NewtonsoftJsonPackageVersion)" />
<PackageReference Include="Microsoft.Build.Locator" Version="$(MicrosoftBuildLocatorPackageVersion)" />
<PackageReference Include="Microsoft.Build" ExcludeAssets="Runtime" Version="$(MicrosoftBuildPackageVersion)" />
<PackageReference Include="Microsoft.Build" Version="$(MicrosoftBuildPackageVersion)" />
</ItemGroup>

</Project>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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())
{
Expand Down
Loading