Merge pull request #4265 from livarcocc/remove_projetjson_from_msbuild_template

Remove projetjson from msbuild template
This commit is contained in:
Livar 2016-09-30 18:55:42 -07:00 committed by GitHub
commit a3a58423d1
21 changed files with 206 additions and 118 deletions

View file

@ -4,14 +4,21 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworkIdentifier>.NETCoreApp</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
<OutputPath>bin\$(Configuration)\netcoreapp1.0</OutputPath>
<TargetFrameworks>netcoreapp1.0</TargetFrameworks>
<OutputPath>bin\$(Configuration)</OutputPath>
</PropertyGroup>
<ItemGroup>
<Compile Include="**\*.cs" />
<None Include="project.json" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.App">
<Version>1.0.1</Version>
</PackageReference>
<PackageReference Include="Microsoft.NETCore.Sdk">
<Version>1.0.0-alpha-20160930-1</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft.CSharp.targets" />

View file

@ -1,13 +0,0 @@
{
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.Sdk": "1.0.0-alpha-20160923-4",
"Microsoft.NETCore.App": {
"version": "1.0.1",
"type": "platform"
}
}
}
}
}

View file

@ -19,6 +19,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration
new MigrateConfigurationsRule(),
new MigrateScriptsRule(),
new TemporaryMutateProjectJsonRule(),
new WorkaroundOptionsRule(),
new SaveOutputProjectRule()
};

View file

@ -97,7 +97,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
// TODO: https://github.com/dotnet/sdk/issues/67
private AddPropertyTransform<CommonCompilerOptions> XmlDocTransformFilePath =>
new AddPropertyTransform<CommonCompilerOptions>("DocumentationFile",
@"$(OutputPath)\$(AssemblyName).xml",
@"$(OutputPath)\$(TargetFramework)\$(AssemblyName).xml",
compilerOptions => compilerOptions.GenerateXmlDocumentation != null && compilerOptions.GenerateXmlDocumentation.Value);
private AddPropertyTransform<CommonCompilerOptions> OutputNameTransform =>

View file

@ -16,18 +16,10 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
public class MigrateTFMRule : IMigrationRule
{
private readonly ITransformApplicator _transformApplicator;
private readonly AddPropertyTransform<NuGetFramework>[] _transforms;
public MigrateTFMRule(ITransformApplicator transformApplicator = null)
{
_transformApplicator = transformApplicator ?? new TransformApplicator();
_transforms = new AddPropertyTransform<NuGetFramework>[]
{
OutputPathTransform,
FrameworkIdentifierTransform,
FrameworkVersionTransform
};
}
public void Apply(MigrationSettings migrationSettings, MigrationRuleInputs migrationRuleInputs)
@ -36,25 +28,16 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
var propertyGroup = migrationRuleInputs.CommonPropertyGroup;
CleanExistingProperties(csproj);
CleanExistingPackageReferences(csproj);
if (migrationRuleInputs.ProjectContexts.Count() > 1)
{
_transformApplicator.Execute(
FrameworksTransform.Transform(migrationRuleInputs.ProjectContexts.Select(p => p.TargetFramework)),
propertyGroup);
}
foreach (var transform in _transforms)
{
_transformApplicator.Execute(
transform.Transform(migrationRuleInputs.DefaultProjectContext.TargetFramework),
propertyGroup);
}
_transformApplicator.Execute(
FrameworksTransform.Transform(migrationRuleInputs.ProjectContexts.Select(p => p.TargetFramework)),
propertyGroup);
}
private void CleanExistingProperties(ProjectRootElement csproj)
{
var existingPropertiesToRemove = new string[] { "TargetFrameworkIdentifier", "TargetFrameworkVersion" };
var existingPropertiesToRemove = new string[] { "TargetFrameworkIdentifier", "TargetFrameworkVersion", "TargetFrameworks" };
var properties = csproj.Properties.Where(p => existingPropertiesToRemove.Contains(p.Name));
foreach (var property in properties)
@ -63,6 +46,23 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
}
}
//TODO: this is a hack right now to clean packagerefs. This is not the final resting place for this piece of code
// @brthor will move it to its final location as part of this PackageRef PR: https://github.com/dotnet/cli/pull/4261
private void CleanExistingPackageReferences(ProjectRootElement outputMSBuildProject)
{
var packageRefs = outputMSBuildProject
.Items
.Where(i => i.ItemType == "PackageReference" && i.Include != ConstantPackageNames.CSdkPackageName)
.ToList();
foreach (var packageRef in packageRefs)
{
var parent = packageRef.Parent;
packageRef.Parent.RemoveChild(packageRef);
parent.RemoveIfEmpty();
}
}
// Taken from private NuGet.Frameworks method
// https://github.com/NuGet/NuGet.Client/blob/33b8f85a94b01f805f1e955f9b68992b297fad6e/src/NuGet.Core/NuGet.Frameworks/NuGetFramework.cs#L234
private static string GetDisplayVersion(Version version)
@ -83,21 +83,6 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Rules
return sb.ToString();
}
// TODO: When we have this inferred in the sdk targets, we won't need this
private AddPropertyTransform<NuGetFramework> OutputPathTransform =>
new AddPropertyTransform<NuGetFramework>("OutputPath",
f => $"bin/$(Configuration)/{f.GetShortFolderName()}",
f => true);
private AddPropertyTransform<NuGetFramework> FrameworkIdentifierTransform =>
new AddPropertyTransform<NuGetFramework>("TargetFrameworkIdentifier",
f => f.Framework,
f => true);
private AddPropertyTransform<NuGetFramework> FrameworkVersionTransform =>
new AddPropertyTransform<NuGetFramework>("TargetFrameworkVersion",
f => "v" + GetDisplayVersion(f.Version),
f => true);
private AddPropertyTransform<IEnumerable<NuGetFramework>> FrameworksTransform =>
new AddPropertyTransform<IEnumerable<NuGetFramework>>("TargetFrameworks",
frameworks => string.Join(";", frameworks.Select(f => f.GetShortFolderName())),

View file

@ -0,0 +1,29 @@
// 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 Microsoft.DotNet.ProjectJsonMigration.Transforms;
namespace Microsoft.DotNet.ProjectJsonMigration.Rules
{
public class WorkaroundOptionsRule : IMigrationRule
{
private readonly ITransformApplicator _transformApplicator;
private AddPropertyTransform<object> ProjectLockFileTransform =>
new AddPropertyTransform<object>("ProjectLockFile",
frameworks => "$(MSBuildProjectDirectory)/project.lock.json",
frameworks => true);
public WorkaroundOptionsRule(ITransformApplicator transformApplicator = null)
{
_transformApplicator = transformApplicator ?? new TransformApplicator();
}
public void Apply(MigrationSettings migrationSettings, MigrationRuleInputs migrationRuleInputs)
{
var propertyGroup = migrationRuleInputs.CommonPropertyGroup;
_transformApplicator.Execute(ProjectLockFileTransform.Transform(string.Empty), propertyGroup);
}
}
}

View file

@ -66,6 +66,8 @@ namespace Microsoft.DotNet.TestFramework
foreach (string lockFile in Directory.GetFiles(_testAssetRoot, "project.lock.json", SearchOption.AllDirectories))
{
string destinationLockFile = lockFile.Replace(_testAssetRoot, Path);
Directory.CreateDirectory(System.IO.Path.GetDirectoryName(destinationLockFile));
File.Copy(lockFile, destinationLockFile, true);
}

View file

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Build.Construction;
using Microsoft.DotNet.Cli;
using Microsoft.DotNet.ProjectJsonMigration;
using Microsoft.DotNet.ProjectModel;
@ -37,8 +36,9 @@ namespace Microsoft.DotNet.Tools.Migrate
var msBuildTemplate = _templateFile != null ?
ProjectRootElement.TryOpen(_templateFile) : _temporaryDotnetNewProject.MSBuildProject;
var sdkVersion = _sdkVersion ?? _temporaryDotnetNewProject.MSBuildProject.GetSdkVersion();
var sdkVersion = _sdkVersion ?? new ProjectJsonParser(_temporaryDotnetNewProject.ProjectJson).SdkPackageVersion;
EnsureNotNull(sdkVersion, "Null Sdk Version");
foreach (var project in projectsToMigrate)

View file

@ -0,0 +1,21 @@
using System.Linq;
using Microsoft.Build.Construction;
using Microsoft.DotNet.ProjectJsonMigration;
namespace Microsoft.DotNet.Tools.Migrate
{
public static class ProjectRootElementExtensions
{
public static string GetSdkVersion(this ProjectRootElement projectRootElement)
{
//TODO: Temporarily pinning the SDK version for Migration. Once we have packageref migration we can remove this.
return "1.0.0-alpha-20160929-1";
// return projectRootElement
// .Items
// .Where(i => i.ItemType == "PackageReference")
// .First(i => i.Include == ConstantPackageNames.CSdkPackageName)
// .GetMetadataWithName("version").Value;
}
}
}

View file

@ -1,11 +1,8 @@
using Microsoft.Build.Construction;
using Microsoft.DotNet.Cli;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.DotNet.ProjectJsonMigration;
namespace Microsoft.DotNet.Tools.Migrate
@ -17,13 +14,11 @@ namespace Microsoft.DotNet.Tools.Migrate
private readonly string _projectDirectory;
public ProjectRootElement MSBuildProject { get; }
public JObject ProjectJson { get; }
public TemporaryDotnetNewTemplateProject()
{
_projectDirectory = CreateDotnetNewMSBuild(c_temporaryDotnetNewMSBuildProjectName);
MSBuildProject = GetMSBuildProject(_projectDirectory);
ProjectJson = GetProjectJson(_projectDirectory);
Clean();
}
@ -60,12 +55,6 @@ namespace Microsoft.DotNet.Tools.Migrate
return ProjectRootElement.Open(templateProjPath);
}
private JObject GetProjectJson(string temporaryDotnetNewMSBuildDirectory)
{
var projectJsonFile = Path.Combine(temporaryDotnetNewMSBuildDirectory, "project.json");
return JObject.Parse(File.ReadAllText(projectJsonFile));
}
private void RunCommand(string commandToExecute, IEnumerable<string> args, string workingDirectory)
{
var command = new DotNetCommandFactory()

View file

@ -3,14 +3,22 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworkIdentifier>.NETCoreApp</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
<TargetFrameworks>netcoreapp1.0</TargetFrameworks>
<OutputPath>bin\$(Configuration)</OutputPath>
</PropertyGroup>
<ItemGroup>
<Compile Include="**\*.cs" Exclude="$(GlobalExclude)" />
<EmbeddedResource Include="**\*.resx" Exclude="$(GlobalExclude)" />
<None Include="project.json" />
<Compile Include="**\*.cs" />
<EmbeddedResource Include="**\*.resx" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.App">
<Version>1.0.1</Version>
</PackageReference>
<PackageReference Include="Microsoft.NETCore.Sdk">
<Version>1.0.0-alpha-20160930-1</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

View file

@ -1,14 +0,0 @@
{
"dependencies": {},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.Sdk": "1.0.0-alpha-20160923-4",
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.1"
}
}
}
}
}

View file

@ -94,10 +94,12 @@ namespace Microsoft.DotNet.Tools.New
private static void ReplaceProjectJsonTemplateValues(string projectDirectory)
{
string projectJsonFile = Path.Combine(projectDirectory, "project.json");
string projectJsonTemplateFile = Path.Combine(projectDirectory, "project.json.template");
File.Move(
Path.Combine(projectDirectory, "project.json.template"),
projectJsonFile);
if(File.Exists(projectJsonTemplateFile))
{
File.Move(projectJsonTemplateFile, projectJsonFile);
}
}
private static void ReplaceFileTemplateNames(string projectDirectory)

View file

@ -13,8 +13,8 @@
"Microsoft.Net.Compilers.netcore": "2.0.0-beta6-60922-08",
"Microsoft.CodeAnalysis.Build.Tasks": "2.0.0-beta6-60922-08",
"Microsoft.Cci": "4.0.0-rc3-24128-00",
"Microsoft.Composition": "1.0.30",
"Microsoft.NuGet.Build.Tasks": "1.0.0-alpha-000004"
"Microsoft.NuGet.Build.Tasks": "1.0.0-alpha-000004",
"Microsoft.Composition": "1.0.30"
},
"frameworks": {
"netcoreapp1.0": {

View file

@ -21,7 +21,7 @@ namespace Microsoft.DotNet.Tests.EndToEnd
.Should()
.Pass();
new RestoreCommand()
new Restore3Command()
.WithWorkingDirectory(projectDirectory)
.Execute()
.Should()
@ -33,9 +33,10 @@ namespace Microsoft.DotNet.Tests.EndToEnd
.Should()
.Pass();
//TODO: https://github.com/dotnet/sdk/issues/187 - remove framework from below.
new Run3Command()
.WithWorkingDirectory(projectDirectory)
.ExecuteWithCapturedOutput()
.ExecuteWithCapturedOutput("--framework netcoreapp1.0")
.Should()
.Pass()
.And

View file

@ -69,7 +69,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
}
[Fact]
public void Migrating_Single_TFM_project_does_not_Populate_TargetFrameworks()
public void Migrating_Single_TFM_project_Populates_TargetFrameworks()
{
var testDirectory = Temp.CreateDirectory().Path;
var testPJ = new ProjectJsonBuilder(TestAssetsManager)
@ -94,7 +94,7 @@ namespace Microsoft.DotNet.ProjectJsonMigration.Tests
new MigrateTFMRule().Apply(migrationSettings, migrationInputs);
Console.WriteLine(mockProj.RawXml);
mockProj.Properties.Count(p => p.Name == "TargetFrameworks").Should().Be(0);
mockProj.Properties.Count(p => p.Name == "TargetFrameworks").Should().Be(1);
}
}
}

View file

@ -0,0 +1,27 @@
// 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 Microsoft.DotNet.Cli.Utils;
namespace Microsoft.DotNet.Tools.Test.Utilities
{
public sealed class Restore3Command : TestCommand
{
public Restore3Command()
: base("dotnet")
{
}
public override CommandResult Execute(string args = "")
{
args = $"restore3 {args}";
return base.Execute(args);
}
public override CommandResult ExecuteWithCapturedOutput(string args = "")
{
args = $"restore3 {args}";
return base.ExecuteWithCapturedOutput(args);
}
}
}

View file

@ -16,11 +16,16 @@ namespace Microsoft.DotNet.Cli.Build3.Tests
{
var testAppName = "MSBuildTestApp";
var testInstance = TestAssetsManager
.CreateTestInstance(testAppName)
.WithLockFiles();
.CreateTestInstance(testAppName);
var testProjectDirectory = testInstance.TestRoot;
new Restore3Command()
.WithWorkingDirectory(testProjectDirectory)
.Execute()
.Should()
.Pass();
new Build3Command()
.WithWorkingDirectory(testProjectDirectory)
.Execute()

View file

@ -29,7 +29,10 @@ namespace Microsoft.DotNet.Migration.Tests
public void It_migrates_apps(string projectName)
{
var projectDirectory = TestAssetsManager.CreateTestInstance(projectName, callingMethod: "i").WithLockFiles().Path;
var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory);
CleanBinObj(projectDirectory);
var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory, projectName);
var outputsIdentical =
outputComparisonData.ProjectJsonBuildOutputs.SetEquals(outputComparisonData.MSBuildBuildOutputs);
@ -79,7 +82,7 @@ namespace Microsoft.DotNet.Migration.Tests
{
var projectDirectory =
TestAssetsManager.CreateTestInstance(projectName, callingMethod: "i").WithLockFiles().Path;
var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory);
var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory, projectName);
var outputsIdentical =
outputComparisonData.ProjectJsonBuildOutputs.SetEquals(outputComparisonData.MSBuildBuildOutputs);
@ -100,7 +103,7 @@ namespace Microsoft.DotNet.Migration.Tests
{
var projectDirectory =
TestAssetsManager.CreateTestInstance(projectName, callingMethod: "i").WithLockFiles().Path;
var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory);
var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory, Path.GetFileNameWithoutExtension(projectName));
var outputsIdentical =
outputComparisonData.ProjectJsonBuildOutputs.SetEquals(outputComparisonData.MSBuildBuildOutputs);
@ -210,7 +213,8 @@ namespace Microsoft.DotNet.Migration.Tests
File.Copy("NuGet.tempaspnetpatch.config", Path.Combine(projectDirectory, "NuGet.Config"));
Restore(projectDirectory);
var outputComparisonData = BuildProjectJsonMigrateBuildMSBuild(projectDirectory);
var outputComparisonData =
BuildProjectJsonMigrateBuildMSBuild(projectDirectory, Path.GetFileNameWithoutExtension(projectDirectory));
return outputComparisonData;
}
@ -227,7 +231,7 @@ namespace Microsoft.DotNet.Migration.Tests
}
}
private MigratedBuildComparisonData BuildProjectJsonMigrateBuildMSBuild(string projectDirectory)
private MigratedBuildComparisonData BuildProjectJsonMigrateBuildMSBuild(string projectDirectory, string projectName)
{
BuildProjectJson(projectDirectory);
var projectJsonBuildOutputs = new HashSet<string>(CollectBuildOutputs(projectDirectory));
@ -238,7 +242,7 @@ namespace Microsoft.DotNet.Migration.Tests
MigrateProject(projectDirectory);
Restore(projectDirectory);
BuildMSBuild(projectDirectory);
BuildMSBuild(projectDirectory, projectName);
var msbuildBuildOutputs = new HashSet<string>(CollectBuildOutputs(projectDirectory));
@ -259,7 +263,10 @@ namespace Microsoft.DotNet.Migration.Tests
foreach (var dir in dirs)
{
Directory.Delete(dir, true);
if(Directory.Exists(dir))
{
Directory.Delete(dir, true);
}
}
}
@ -297,13 +304,22 @@ namespace Microsoft.DotNet.Migration.Tests
.Pass();
}
private string BuildMSBuild(string projectDirectory, string configuration="Debug")
private void Restore3(string projectDirectory, string projectName)
{
new Restore3Command()
.WithWorkingDirectory(projectDirectory)
.Execute($"{projectName}.csproj")
.Should()
.Pass();
}
private string BuildMSBuild(string projectDirectory, string projectName, string configuration="Debug")
{
DeleteXproj(projectDirectory);
var result = new Build3Command()
.WithWorkingDirectory(projectDirectory)
.ExecuteWithCapturedOutput($"/p:Configuration={configuration}");
.ExecuteWithCapturedOutput($"{projectName}.csproj /p:Configuration={configuration}");
result
.Should()

View file

@ -16,17 +16,22 @@ namespace Microsoft.DotNet.Cli.Publish3.Tests
{
var testAppName = "MSBuildTestApp";
var testInstance = TestAssetsManager
.CreateTestInstance(testAppName)
.WithLockFiles();
.CreateTestInstance(testAppName);
var testProjectDirectory = testInstance.TestRoot;
new Publish3Command()
new Restore3Command()
.WithWorkingDirectory(testProjectDirectory)
.Execute()
.Should()
.Pass();
new Publish3Command()
.WithWorkingDirectory(testProjectDirectory)
.Execute("--framework netcoreapp1.0")
.Should()
.Pass();
var configuration = Environment.GetEnvironmentVariable("CONFIGURATION") ?? "Debug";
var outputDll = Path.Combine(testProjectDirectory, "bin", configuration, "netcoreapp1.0", "publish", $"{testAppName}.dll");

View file

@ -13,20 +13,26 @@ namespace Microsoft.DotNet.Cli.Run3.Tests
{
var testAppName = "MSBuildTestApp";
var testInstance = TestAssetsManager
.CreateTestInstance(testAppName)
.WithLockFiles();
.CreateTestInstance(testAppName);
var testProjectDirectory = testInstance.TestRoot;
new Restore3Command()
.WithWorkingDirectory(testProjectDirectory)
.Execute()
.Should()
.Pass();
new Build3Command()
.WithWorkingDirectory(testProjectDirectory)
.Execute()
.Should()
.Pass();
//TODO: https://github.com/dotnet/sdk/issues/187 - remove framework from below.
new Run3Command()
.WithWorkingDirectory(testProjectDirectory)
.ExecuteWithCapturedOutput()
.ExecuteWithCapturedOutput("--framework netcoreapp1.0")
.Should()
.Pass()
.And
@ -38,14 +44,20 @@ namespace Microsoft.DotNet.Cli.Run3.Tests
{
var testAppName = "MSBuildTestApp";
var testInstance = TestAssetsManager
.CreateTestInstance(testAppName)
.WithLockFiles();
.CreateTestInstance(testAppName);
var testProjectDirectory = testInstance.TestRoot;
new Restore3Command()
.WithWorkingDirectory(testProjectDirectory)
.Execute()
.Should()
.Pass();
//TODO: https://github.com/dotnet/sdk/issues/187 - remove framework from below.
new Run3Command()
.WithWorkingDirectory(testProjectDirectory)
.ExecuteWithCapturedOutput()
.ExecuteWithCapturedOutput("--framework netcoreapp1.0")
.Should()
.Pass()
.And
@ -57,11 +69,16 @@ namespace Microsoft.DotNet.Cli.Run3.Tests
{
var testAppName = "MSBuildTestApp";
var testInstance = TestAssetsManager
.CreateTestInstance(testAppName)
.WithLockFiles();
.CreateTestInstance(testAppName);
var testProjectDirectory = testInstance.TestRoot;
new Restore3Command()
.WithWorkingDirectory(testProjectDirectory)
.Execute()
.Should()
.Pass();
new Run3Command()
.WithWorkingDirectory(testProjectDirectory)
.ExecuteWithCapturedOutput("--framework netcoreapp1.0")