Skip to content
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

Add models from other projects [Regression] #1718

Merged
merged 4 commits into from
Dec 4, 2021
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
9 changes: 5 additions & 4 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<!-- Ref packages -->
</PropertyGroup>
<PropertyGroup>
<VersionPrefix>6.0.0</VersionPrefix>
<VersionPrefix>6.0.1</VersionPrefix>
<PreReleaseVersionLabel>rtm</PreReleaseVersionLabel>
<IsServicingBuild Condition="'$(PreReleaseVersionLabel)' == 'servicing'">true</IsServicingBuild>
<!--
Expand All @@ -26,9 +26,10 @@
<!-- Microsoft.AspNetCore.Razor.Language -->
<MicrosoftAspNetCoreRazorLanguagePackageVersion>6.0.0</MicrosoftAspNetCoreRazorLanguagePackageVersion>
<!-- Microsoft.Build-->
<MicrosoftBuildPackageVersion>16.9.0 </MicrosoftBuildPackageVersion>
<MicrosoftBuildPackageVersion>17.0.0 </MicrosoftBuildPackageVersion>
<!-- Microsoft.Build.Utilities-->
<MicrosoftBuildUtilitiesCorePackageVersion>16.9.0 </MicrosoftBuildUtilitiesCorePackageVersion>
<MicrosoftBuildUtilitiesCorePackageVersion>17.0.0 </MicrosoftBuildUtilitiesCorePackageVersion>
<MicrosoftBuildLocatorPackageVersion>1.4.1</MicrosoftBuildLocatorPackageVersion>
<!-- Microsoft.CodeAnalysis.CSharp -->
<MicrosoftCodeAnalysisCSharpPackageVersion>4.0.0</MicrosoftCodeAnalysisCSharpPackageVersion>
<!-- Microsoft.CodeAnalysis.Razor -->
Expand Down Expand Up @@ -63,7 +64,7 @@
<!-- Microsoft.AspNetCore -->
<!-- this is from aspnetcore-dev (test project only) -->
<MicrosoftAspNetCorePackageVersion>6.0.0</MicrosoftAspNetCorePackageVersion>
<MicrosoftBuildRuntimePackageVersion>16.9.0</MicrosoftBuildRuntimePackageVersion>
<MicrosoftBuildRuntimePackageVersion>17.0.0</MicrosoftBuildRuntimePackageVersion>
<!-- Microsoft.AspNetCore.Mvc -->
<!-- this is from aspnetcore-dev (test only) -->
<MicrosoftAspNetCoreMvcPackageVersion>6.0.0</MicrosoftAspNetCoreMvcPackageVersion>
Expand Down
4 changes: 2 additions & 2 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"tools": {
"dotnet": "6.0.100-rc.1.21430.12"
"dotnet": "6.0.100"
},
"msbuild-sdks": {
"Microsoft.DotNet.Arcade.Sdk": "7.0.0-beta.21569.2"
},
"sdk": {
"version": "6.0.100-rc.1.21430.12",
"version": "6.0.100",
"allowPrerelease": true
}
}
2 changes: 1 addition & 1 deletion scripts/install-aspnet-codegenerator.cmd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set VERSION=6.0.0-dev
set VERSION=6.0.1
set DEFAULT_NUPKG_PATH=%userprofile%\.nuget\packages
set SRC_DIR=%cd%
set NUPKG=artifacts/packages/Debug/Shipping/
Expand Down
2 changes: 1 addition & 1 deletion src/Scaffolding/VS.Web.CG.Core/VS.Web.CG.Core.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 the core infrastructure used by ASP.NET Core Code Generators.</Description>
Expand Down
3 changes: 2 additions & 1 deletion src/Scaffolding/VS.Web.CG.Design/VS.Web.CG.Design.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">
<!--
NOTE: Do not add package/ project references to this file.
Edit the Shared.proj file to add additional package/ project references.
Expand All @@ -23,6 +23,7 @@

<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
148 changes: 2 additions & 146 deletions src/Scaffolding/VS.Web.CG.Msbuild/ProjectContextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Text.Json;
using Microsoft.Build.Framework;
using Microsoft.DotNet.Scaffolding.Shared;
using Microsoft.DotNet.Scaffolding.Shared.ProjectModel;
using Microsoft.VisualStudio.Web.CodeGeneration.Utils;

Expand Down Expand Up @@ -92,7 +93,7 @@ public override bool Execute()
AssemblyName = string.IsNullOrEmpty(this.AssemblyName) ? Path.GetFileName(this.AssemblyFullPath) : this.AssemblyName,
CompilationAssemblies = GetCompilationAssemblies(this.ResolvedReferences),
CompilationItems = this.CompilationItems.Select(i => i.ItemSpec),
PackageDependencies = GetPackageDependencies(this.ProjectAssetsFile, this.TargetFramework, this.TargetFrameworkMoniker),
PackageDependencies = ProjectContextHelper.GetPackageDependencies(this.ProjectAssetsFile, this.TargetFramework, this.TargetFrameworkMoniker),
Config = this.AssemblyFullPath + ".config",
Configuration = this.Configuration,
DepsFile = this.ProjectDepsFileName,
Expand Down Expand Up @@ -122,151 +123,6 @@ public override bool Execute()
return true;
}

internal static IEnumerable<DependencyDescription> GetPackageDependencies(string projectAssetsFile, string tfm, string tfmMoniker)
{
IList<DependencyDescription> packageDependencies = new List<DependencyDescription>();
if (!string.IsNullOrEmpty(projectAssetsFile) && File.Exists(projectAssetsFile) && !string.IsNullOrEmpty(tfm))
{
//target framework moniker for the current project. We use this to get all targets for said moniker.
var targetFramework = tfm;
var targetFrameworkMoniker = tfmMoniker;
if (string.IsNullOrEmpty(targetFrameworkMoniker))
{
//if targetFrameworkMoniker is null, targetFramework is the TargetFrameworkMoniker (issue w/ IProjectContext sent from VS)
targetFrameworkMoniker = tfm;
targetFramework = ProjectModelHelper.GetShortTfm(tfm);
}
string json = File.ReadAllText(projectAssetsFile);
if (!string.IsNullOrEmpty(json) && !string.IsNullOrEmpty(targetFrameworkMoniker))
{
try
{
JsonDocument baseDocument = JsonDocument.Parse(json);
if (baseDocument != null)
{
JsonElement root = baseDocument.RootElement;
//"targets" gives us all top-level and transitive dependencies. "packageFolders" gives us the path where the dependencies are on disk.
if (root.TryGetProperty(TargetsProperty, out var targets) && root.TryGetProperty(PackageFoldersProperty, out var packageFolderPath))
{
if (targets.TryGetProperty(targetFramework, out var packages))
{
packageDependencies = DeserializePackages(packages, packageFolderPath, targetFrameworkMoniker);
}
else if(targets.TryGetProperty(targetFrameworkMoniker, out var legacyPackages))
{
packageDependencies = DeserializePackages(legacyPackages, packageFolderPath, targetFrameworkMoniker);
}
}
}
}
catch (JsonException)
{
Debug.Assert(false, "Completely empty json.");
}
}
}
return packageDependencies;
}

internal static IEnumerable<ResolvedReference> GetScaffoldingAssemblies(IEnumerable<DependencyDescription> dependencies)
{
var compilationAssemblies = new List<ResolvedReference>();
foreach (var item in dependencies)
{
//only add Microsoft.EntityFrameworkCore.* or Microsoft.AspNetCore.Identity.* assemblies. Any others might be duplicates which cause in-memory compilation errors and those are the assemblies we care about.
if (item.Name.Contains(EntityFrameworkCore, StringComparison.OrdinalIgnoreCase) || item.Name.Contains(AspNetCoreIdentity, StringComparison.OrdinalIgnoreCase))
{
var name = $"{item.Name}.dll";
//costly search but we're only doing it a handful of times.
var file = Directory.GetFiles(item.Path, name, SearchOption.AllDirectories).FirstOrDefault();
if (file != null)
{
var resolvedPath = file.ToString();
var reference = new ResolvedReference(name, resolvedPath);
compilationAssemblies.Add(reference);
}
}
}
return compilationAssemblies;
}

private static IList<DependencyDescription> DeserializePackages(JsonElement packages, JsonElement packageFolderPath, string targetFrameworkMoniker)
{
IList<DependencyDescription> packageDependencies = new List<DependencyDescription>();
var packagesEnumerator = packages.EnumerateObject();
//populate are our own List<DependencyDescription> of all the dependencies we find.
foreach (var package in packagesEnumerator)
{
var fullName = package.Name;
//get default nuget package path.
var path = packageFolderPath.EnumerateObject().Any() ? packageFolderPath.EnumerateObject().First().Name : string.Empty;
if (!string.IsNullOrEmpty(fullName) && !string.IsNullOrEmpty(path) && package.Value.TryGetProperty(TypeProperty, out var type))
{
//fullName is in the format {Package Name}/{Version} for example "System.Text.MoreText/2.1.1" Split into tuple.
Tuple<string, string> nameAndVersion = ProjectContextWriter.GetName(fullName);
if (nameAndVersion != null)
{
var dependencyTypeValue = type.ToString();
var DependencyTypeEnum = DependencyType.Unknown;
if (Enum.TryParse(typeof(DependencyType), dependencyTypeValue, true, out var dependencyType))
{
DependencyTypeEnum = (DependencyType)dependencyType;
}

string packagePath = ProjectContextWriter.GetPath(path, nameAndVersion);
DependencyDescription dependency = new DependencyDescription(nameAndVersion.Item1,
nameAndVersion.Item2,
Directory.Exists(packagePath) ? packagePath : string.Empty,
targetFrameworkMoniker,
DependencyTypeEnum,
true);
if (package.Value.TryGetProperty(DependencyProperty, out var dependencies))
{
var dependenciesList = dependencies.EnumerateObject();
//Add all transitive dependencies
foreach (var dep in dependenciesList)
{
if (!string.IsNullOrEmpty(dep.Name))
{
Dependency transitiveDependency = new Dependency(dep.Name, dep.Value.ToString());
dependency.AddDependency(transitiveDependency);
}
}
}
packageDependencies.Add(dependency);
}
}
}

return packageDependencies;
}

internal static string GetPath(string nugetPath, Tuple<string, string> nameAndVersion)
{
string path = string.Empty;
if (!string.IsNullOrEmpty(nugetPath) && !string.IsNullOrEmpty(nameAndVersion.Item1) && !string.IsNullOrEmpty(nameAndVersion.Item2))
{
path = Path.Combine(nugetPath, nameAndVersion.Item1, nameAndVersion.Item2);
path = Directory.Exists(path) ? path : Path.Combine(nugetPath, nameAndVersion.Item1.ToLower(), nameAndVersion.Item2);
}

return path;
}

private static Tuple<string, string> GetName(string fullName)
{
Tuple<string, string> nameAndVersion = null;
if (!string.IsNullOrEmpty(fullName))
{
string[] splitName = fullName.Split("/");
if (splitName.Length > 1)
{
nameAndVersion = new Tuple<string, string>(splitName[0], splitName[1]);
}
}
return nameAndVersion;
}

private IEnumerable<ProjectReferenceInformation> GetProjectDependency(
IEnumerable<string> projectReferences,
string rootProjectFullpath)
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
6 changes: 3 additions & 3 deletions src/Scaffolding/VS.Web.CG.Msbuild/VS.Web.CG.Msbuild.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>MSBuild task (EvaluateProjectInfoForCodeGeneration) used by Microsoft.VisualStudio.Web.CodeGeneration.Tools</Description>
Expand All @@ -18,8 +18,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Build" PrivateAssets="All" Version="$(MicrosoftBuildPackageVersion)" />
<PackageReference Include="Microsoft.Build.Utilities.Core" PrivateAssets="All" Version="$(MicrosoftBuildUtilitiesCorePackageVersion)" />
<PackageReference Include="Microsoft.Build" PrivateAssets="All" ExcludeAssets="Runtime" Version="$(MicrosoftBuildPackageVersion)" />
<PackageReference Include="Microsoft.Build.Utilities.Core" PrivateAssets="All" ExcludeAssets="Runtime" Version="$(MicrosoftBuildUtilitiesCorePackageVersion)" />
<PackageReference Include="Newtonsoft.Json" Version="$(NewtonsoftJsonPackageVersion)" PrivateAssets="All">
<!-- This version needs to line up with what is bundled in MSBuild and Visual Studio -->
<NoWarn>KRB4002</NoWarn>
Expand Down
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 Razor based templating host used by ASP.NET Core Code Generators.</Description>
Expand Down
13 changes: 11 additions & 2 deletions src/Scaffolding/VS.Web.CG.Utils/TemplateFoldersUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.DotNet.Scaffolding.Shared.ProjectModel;
using Microsoft.VisualStudio.Web.CodeGeneration.Utils;

Expand Down Expand Up @@ -43,7 +44,7 @@ public static List<string> GetTemplateFolders(

rootFolders.Add(applicationBasePath);

var dependency = projectContext.GetPackage(containingProject);
var dependency = GetPackage(projectContext, containingProject);

if (dependency != null)
{
Expand Down Expand Up @@ -82,5 +83,13 @@ public static List<string> GetTemplateFolders(
}
return templateFolders;
}

public static DependencyDescription GetPackage(IProjectContext context, string name)
{
Requires.NotNullOrEmpty(name, nameof(name));
Requires.NotNull(context, nameof(context));

return context.PackageDependencies.FirstOrDefault(package => package.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
}
}
}
}
5 changes: 2 additions & 3 deletions src/Scaffolding/VS.Web.CG.Utils/VS.Web.CG.Utils.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
<EmbeddedResource Include="..\..\Shared\Microsoft.DotNet.Scaffolding.Shared\General\**\*.resx">
<Link>Shared\%(RecursiveDir)%(FileName).resx</Link>
</EmbeddedResource>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(RepoRoot)\src\Shared\Microsoft.DotNet.Scaffolding.Shared\Microsoft.DotNet.Scaffolding.Shared.csproj" />
</ItemGroup>

<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)" />
</ItemGroup>

</Project>
15 changes: 10 additions & 5 deletions src/Scaffolding/VS.Web.CG.Utils/Workspaces/RoslynWorkspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
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;
using Microsoft.DotNet.Scaffolding.Shared.ProjectModel;
using Microsoft.VisualStudio.Web.CodeGeneration.Utils.Workspaces;
using PInfo = Microsoft.CodeAnalysis.ProjectInfo;

namespace Microsoft.VisualStudio.Web.CodeGeneration.Utils
Expand All @@ -31,14 +32,18 @@ public RoslynWorkspace(IProjectContext projectInformation,
: base(MefHostServices.DefaultHost, "Custom")
{
Requires.NotNull(projectInformation, nameof(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 (projectInformation.ProjectReferenceInformation != null)
if (!MSBuildLocator.IsRegistered)
{
MSBuildLocator.RegisterDefaults();
}
var projReferenceInformation = RoslynWorkspaceHelper.GetProjectReferenceInformation(projectInformation.ProjectReferences);

if (projReferenceInformation != null && projReferenceInformation.Any())
{
foreach (var projectReference in projectInformation.ProjectReferenceInformation)
foreach (var projectReference in projReferenceInformation)
{
AddProject(projectReference, configuration);
}
Expand Down
Loading