Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Commit

Permalink
use Sdks attribute in dotnet new templates (#4916)
Browse files Browse the repository at this point in the history
* Move dotnet-new templates to Sdk attribute

* Update to MSBuild 15.1.0-preview-000454-01

To pick up a fix for dotnet/msbuild#1431.

* Fix template newlines

* Fix casing on Microsoft.Net.Sdk

* Move migration test csproj's to Sdk attribute

* Disable parallel sdk restore

Each SDK restore operation will try to manipulate the same assets.json file since the dependency name&version are injected into a common csproj file. This can cause runtime failures when two NuGets try to restore the project at once.

* Make casing of SDK 'NET' and not 'Net'

* Remove redundatn imports

* Fix test string

* Additional race

* Replacing the SDK with the Web.Sdk when it is a Web project.

* Fixing the test by writting the csproj before running the migration rule.
  • Loading branch information
Piotr Puszkiewicz authored Dec 5, 2016
1 parent dc3d88c commit d6cab4a
Show file tree
Hide file tree
Showing 30 changed files with 179 additions and 177 deletions.
2 changes: 1 addition & 1 deletion TestAssets/TestProjects/MSBuildIntegration/build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />

<ItemGroup>
<Sdk Include="Microsoft.Net.Sdk" Version="$(CLI_NETSDK_Version)" />
<Sdk Include="Microsoft.NET.Sdk" Version="$(CLI_NETSDK_Version)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />

<PropertyGroup>
Expand All @@ -14,14 +13,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk">
<Version>$(CLI_NETSDK_Version)</Version>
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="NETStandard.Library">
<Version>1.6.0</Version>
</PackageReference>
</ItemGroup>

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />

<PropertyGroup>
Expand All @@ -14,14 +13,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk">
<Version>$(CLI_NETSDK_Version)</Version>
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="NETStandard.Library">
<Version>1.6.0</Version>
</PackageReference>
</ItemGroup>

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />

<PropertyGroup>
Expand All @@ -14,14 +13,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk">
<Version>$(CLI_NETSDK_Version)</Version>
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="NETStandard.Library">
<Version>1.6.0</Version>
</PackageReference>
</ItemGroup>

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
3 changes: 3 additions & 0 deletions build/Microsoft.DotNet.Cli.BundledSdks.proj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
Outputs="@(SdkContent->'$(SdkLayoutDirectory)/%(RecursiveDir)%(FileName)%(Extension)')">
<Copy SourceFiles="@(SdkContent)"
DestinationFiles="@(SdkContent->'$(SdkLayoutDirectory)/%(RecursiveDir)%(FileName)%(Extension)')" />

<Message Text="Copied Sdk $(SdkPackageName) from $(SdkNuPkgPath) to $(SdkLayoutDirectory)."
Importance="High" />
</Target>

<Target Name="GetSdkItemsToCopy">
Expand Down
2 changes: 1 addition & 1 deletion build/Microsoft.DotNet.Cli.BundledSdks.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<ItemGroup>
<!-- CLI cannot use the latest SDK until we move away from SDK PackageRef -->
<BundledSdk Include="NuGet.Build.Tasks.Pack" Version="4.0.0-rc2" />
<BundledSdk Include="Microsoft.Net.Sdk" Version="1.0.0-alpha-20161203-1" />
<BundledSdk Include="Microsoft.NET.Sdk" Version="1.0.0-alpha-20161203-1" />
<BundledSdk Include="Microsoft.NET.Sdk.Web" Version="$(CLI_WEBSDK_Version)" />
<BundledSdk Include="Microsoft.NET.Sdk.Publish" Version="$(CLI_WEBSDK_Version)" />
<BundledSdk Include="Microsoft.NET.Sdk.Web.ProjectSystem" Version="$(CLI_WEBSDK_Version)" />
Expand Down
4 changes: 2 additions & 2 deletions build/Microsoft.DotNet.Cli.Compile.targets
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@
<Properties>
CLIBuildDll=$(CLIBuildDll);
NuGetPackagesDir=$(NuGetPackagesDir);
SdkLayoutDirectory=$(SdkOutputDirectory)/Extensions/%(BundledSdk.Identity);
SdkLayoutDirectory=$(SdkOutputDirectory)/Sdks/%(BundledSdk.Identity);
SdkPackageName=%(BundledSdk.Identity);
SdkPackageVersion=%(BundledSdk.Version);
Stage0Directory=$(Stage0Directory)
Expand All @@ -260,7 +260,7 @@
</ItemGroup>

<MSBuild
BuildInParallel="True"
BuildInParallel="False"
Projects="@(SdksToBundle)">
</MSBuild>
</Target>
Expand Down
2 changes: 1 addition & 1 deletion build/Microsoft.DotNet.Cli.DependencyVersions.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<CLI_MSBuild_Version>15.1.0-preview-000451-02</CLI_MSBuild_Version>
<CLI_MSBuild_Version>15.1.0-preview-000454-01</CLI_MSBuild_Version>
<CLI_NETSDK_Version>1.0.0-alpha-20161104-2</CLI_NETSDK_Version>
<CLI_WEBSDK_Version>1.0.0-alpha-20161104-2-112</CLI_WEBSDK_Version>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ internal class ConstantPackageVersions
public const string MstestTestAdapterVersion = "1.1.3-preview";
public const string MstestTestFrameworkVersion = "1.0.4-preview";
public const string BundleMinifierToolVersion = "2.2.301";
public const string WebSdkPackageVersion = "1.0.0-alpha-20161117-1-119" ;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,27 @@ internal class MigrationSettings
public string ProjectXProjFilePath { get; }
public string ProjectDirectory { get; }
public string OutputDirectory { get; }
public string SdkPackageVersion { get; }
public ProjectRootElement MSBuildProjectTemplate { get; }
public string SdkDefaultsFilePath { get; }

public MigrationSettings(
string projectDirectory,
string outputDirectory,
string sdkPackageVersion,
ProjectRootElement msBuildProjectTemplate,
string projectXprojFilePath=null,
string sdkDefaultsFilePath=null) : this(
projectDirectory, outputDirectory, sdkPackageVersion, projectXprojFilePath, sdkDefaultsFilePath)
projectDirectory, outputDirectory, projectXprojFilePath, sdkDefaultsFilePath)
{
MSBuildProjectTemplate = msBuildProjectTemplate != null ? msBuildProjectTemplate.DeepClone() : null;
}

public MigrationSettings(
string projectDirectory,
string outputDirectory,
string sdkPackageVersion,
string msBuildProjectTemplatePath,
string projectXprojFilePath=null,
string sdkDefaultsFilePath=null) : this(
projectDirectory, outputDirectory, sdkPackageVersion, projectXprojFilePath, sdkDefaultsFilePath)
projectDirectory, outputDirectory, projectXprojFilePath, sdkDefaultsFilePath)
{
_msBuildProjectTemplatePath = msBuildProjectTemplatePath;
MSBuildProjectTemplate = ProjectRootElement.Open(
Expand All @@ -48,13 +45,11 @@ public MigrationSettings(
private MigrationSettings(
string projectDirectory,
string outputDirectory,
string sdkPackageVersion,
string projectXprojFilePath=null,
string sdkDefaultsFilePath=null)
{
ProjectDirectory = projectDirectory;
OutputDirectory = outputDirectory;
SdkPackageVersion = sdkPackageVersion;
ProjectXProjFilePath = projectXprojFilePath;
SdkDefaultsFilePath = sdkDefaultsFilePath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ public MigrationReport Migrate(MigrationSettings rootSettings, bool skipProjectR
var projectDir = Path.GetDirectoryName(project.ProjectFilePath);
var settings = new MigrationSettings(projectDir,
projectDir,
rootSettings.SdkPackageVersion,
tempMSBuildProjectTemplate);
MigrateProject(settings);
projectMigrationReports.Add(MigrateProject(settings));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,6 @@ public void Apply(MigrationSettings migrationSettings, MigrationRuleInputs migra

var noFrameworkPackageReferenceItemGroup = migrationRuleInputs.OutputMSBuildProject.AddItemGroup();

// Inject Sdk dependency
_transformApplicator.Execute(
SdkPackageDependencyTransform.Transform(
new PackageDependencyInfo
{
Name = PackageConstants.SdkPackageName,
Version = migrationSettings.SdkPackageVersion,
PrivateAssets = "All"
}), noFrameworkPackageReferenceItemGroup, mergeExisting: false);

AddProjectTypeSpecificDependencies(
migrationRuleInputs,
migrationSettings,
Expand Down Expand Up @@ -90,18 +80,6 @@ private void AddProjectTypeSpecificDependencies(
var type = project.GetProjectType();
switch (type)
{
case ProjectType.Web:
_transformApplicator.Execute(
SdkPackageDependencyTransform.Transform(
new PackageDependencyInfo
{
Name = PackageConstants.WebSdkPackageName,
Version = migrationSettings.SdkPackageVersion,
PrivateAssets = "All"
}),
noFrameworkPackageReferenceItemGroup,
mergeExisting: false);
break;
case ProjectType.Test:
_transformApplicator.Execute(
PackageDependencyInfoTransform().Transform(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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.IO;
using Microsoft.Build.Construction;

namespace Microsoft.DotNet.ProjectJsonMigration.Rules
{
//HACK to workaround https://github.com/Microsoft/msbuild/issues/1429
internal class MigrateWebSdkRule : IMigrationRule
{
private static string GetContainingFolderName(string projectDirectory)
{
projectDirectory = projectDirectory.TrimEnd(new char[] { '/', '\\' });
return Path.GetFileName(projectDirectory);
}

public void Apply(MigrationSettings migrationSettings, MigrationRuleInputs migrationRuleInputs)
{
var project = migrationRuleInputs.DefaultProjectContext.ProjectFile;
var type = project.GetProjectType();

if(type == ProjectType.Web)
{
ReplaceSdkWithWebSdk(migrationSettings);
}
}

private void ReplaceSdkWithWebSdk(MigrationSettings migrationSettings)
{
string csprojName = $"{GetContainingFolderName(migrationSettings.ProjectDirectory)}.csproj";
var outputProject = Path.Combine(migrationSettings.OutputDirectory, csprojName);

var csprojContent = File.ReadAllText(outputProject);
csprojContent = csprojContent.Replace("Sdk=\"Microsoft.NET.Sdk\"", "Sdk=\"Microsoft.NET.Sdk.Web\"");

File.WriteAllText(outputProject, csprojContent);
}
}
}
5 changes: 0 additions & 5 deletions src/dotnet/commands/dotnet-migrate/MigrateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ public int Execute()
var projectsToMigrate = GetProjectsToMigrate(_projectArg);

var msBuildTemplatePath = _templateFile ?? temporaryDotnetNewProject.MSBuildProjectPath;

var sdkVersion = _sdkVersion ?? temporaryDotnetNewProject.MSBuildProject.GetSdkVersion();

EnsureNotNull(sdkVersion, "Null Sdk Version");

MigrationReport migrationReport = null;

Expand All @@ -73,7 +69,6 @@ public int Execute()
var migrationSettings = new MigrationSettings(
projectDirectory,
outputDirectory,
sdkVersion,
msBuildTemplatePath,
_xprojFilePath);
var projectMigrationReport = new ProjectMigrator().Migrate(migrationSettings, _skipProjectReferences);
Expand Down
4 changes: 2 additions & 2 deletions src/dotnet/commands/dotnet-msbuild/MSBuildForwardingApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class MSBuildForwardingApp

private const string MSBuildExeName = "MSBuild.dll";

private const string ExtensionsDirectoryName = "Extensions";
private const string SdksDirectoryName = "Sdks";

private readonly ForwardingApp _forwardingApp;

Expand Down Expand Up @@ -95,7 +95,7 @@ private static string GetMSBuildSDKsPath()

return Path.Combine(
AppContext.BaseDirectory,
ExtensionsDirectoryName);
SdksDirectoryName);
}

private static string GetRunCscPath()
Expand Down
16 changes: 4 additions & 12 deletions src/dotnet/commands/dotnet-new/CSharp_Console/$projectName$.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />

<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.0</TargetFramework>
Expand All @@ -12,14 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NETCore.App">
<Version>1.0.1</Version>
</PackageReference>
<PackageReference Include="Microsoft.NET.Sdk">
<Version>1.0.0-alpha-20161104-2</Version>
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NETCore.App" Version="1.0.1" />
</ItemGroup>

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

</Project>
12 changes: 2 additions & 10 deletions src/dotnet/commands/dotnet-new/CSharp_Lib/$projectName$.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">

<PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework>
Expand All @@ -11,14 +10,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="NETStandard.Library">
<Version>1.6</Version>
</PackageReference>
<PackageReference Include="Microsoft.NET.Sdk">
<Version>1.0.0-alpha-20161104-2</Version>
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="NETStandard.Library" Version="1.6" />
</ItemGroup>

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
26 changes: 6 additions & 20 deletions src/dotnet/commands/dotnet-new/CSharp_Mstest/$projectName$.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand All @@ -12,23 +11,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NETCore.App">
<Version>1.0.1</Version>
</PackageReference>
<PackageReference Include="Microsoft.NET.Sdk">
<Version>1.0.0-alpha-20161104-2</Version>
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk">
<Version>15.0.0-preview-20161123-03</Version>
</PackageReference>
<PackageReference Include="MSTest.TestAdapter">
<Version>1.1.5-preview</Version>
</PackageReference>
<PackageReference Include="MSTest.TestFramework">
<Version>1.0.6-preview</Version>
</PackageReference>
<PackageReference Include="Microsoft.NETCore.App" Version="1.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0-preview-20161123-03" />
<PackageReference Include="MSTest.TestAdapter" Version="1.1.5-preview" />
<PackageReference Include="MSTest.TestFramework" Version="1.0.6-preview" />
</ItemGroup>

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
</Project>
Loading

0 comments on commit d6cab4a

Please sign in to comment.