Skip to content

Commit

Permalink
Fix Publish with GeneratePackageOnBuild=true
Browse files Browse the repository at this point in the history
What I did is essentially write a different dependency graph in GeneratePackageOnBuild = true to avoid circular dependency . Original bug. I added the https://github.com/dotnet/sdk/pull/3255/files and essentially regressed NuGet/Home#7801. Instead of the logic is in nuget, it is in SDK now. Publish should not skip Build  if GeneratePackageOnBuild is true. Or it will mean no build even use Publish verb.

But revert 3255 means, #2114 will be back. The solution is to break Pack’s dependency on Build in GeneratePackageOnBuild. But it still need Publish. So I need to let Pack depend directly on no build version of publish.

> Why cannot directly use “_PublishNoBuildAlternative”?

We want to exclude Build step regardless NoBuild flag is true or not.
  • Loading branch information
William Li committed Aug 5, 2019
1 parent 527e0e0 commit 15de252
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ Copyright (c) .NET Foundation. All rights reserved.
<PropertyGroup>
<_ToolsSettingsFilePath>$(BaseIntermediateOutputPath)DotnetToolSettings.xml</_ToolsSettingsFilePath>
<SuppressDependenciesWhenPacking Condition=" '$(PackAsTool)' == 'true' ">true</SuppressDependenciesWhenPacking>
<_PackToolPublishDependency Condition=" ('$(GeneratePackageOnBuild)' != 'true' and '$(NoBuild)' != 'true') and $(IsPublishable) == 'true' ">_PublishBuildAlternative</_PackToolPublishDependency>
<_PackToolPublishDependency Condition=" ('$(GeneratePackageOnBuild)' == 'true' or '$(NoBuild)' == 'true') and $(IsPublishable) == 'true' ">$(_PublishNoBuildAlternativeDependsOn)</_PackToolPublishDependency>
</PropertyGroup>

<Target Name="PackTool" DependsOnTargets="GenerateToolsSettingsFileFromBuildProperty;Publish;_PackToolValidation" Condition=" '$(PackAsTool)' == 'true' ">
<Target Name="PackTool" DependsOnTargets="GenerateToolsSettingsFileFromBuildProperty;$(_PackToolPublishDependency);_PackToolValidation" Condition=" '$(PackAsTool)' == 'true' ">
<ItemGroup>
<_GeneratedFiles Include="$(PublishDepsFilePath)"/>
<_GeneratedFiles Include="$(PublishRuntimeConfigFilePath)"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,17 @@ Copyright (c) .NET Foundation. All rights reserved.
GeneratePublishDependencyFile;
BundlePublishDirectory;
</_CorePublishTargets>

<_PublishNoBuildAlternativeDependsOn>$(_BeforePublishNoBuildTargets);$(_CorePublishTargets)</_PublishNoBuildAlternativeDependsOn>
</PropertyGroup>

<Target Name="_PublishBuildAlternative"
Condition="'$(NoBuild)' != 'true' and '$(GeneratePackageOnBuild)' != 'true'"
Condition="'$(NoBuild)' != 'true'"
DependsOnTargets="Build;$(_CorePublishTargets)" />

<Target Name="_PublishNoBuildAlternative"
Condition="'$(NoBuild)' == 'true' or '$(GeneratePackageOnBuild)' == 'true'"
DependsOnTargets="$(_BeforePublishNoBuildTargets);$(_CorePublishTargets)" />
Condition="'$(NoBuild)' == 'true'"
DependsOnTargets="$(_PublishNoBuildAlternativeDependsOn)" />

<Target Name="Publish"
Condition="$(IsPublishable) == 'true'"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Xml.Linq;
using FluentAssertions;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.NET.TestFramework;
using Microsoft.NET.TestFramework.Commands;
using NuGet.Packaging;
using Xunit;
using Xunit.Abstractions;
using Microsoft.NET.TestFramework.Assertions;

namespace Microsoft.NET.ToolPack.Tests
{
public class GivenThatWeWantToBuildWithGeneratePackageOnBuildAndPackAsTool : SdkTest
{
public GivenThatWeWantToBuildWithGeneratePackageOnBuildAndPackAsTool(ITestOutputHelper log) : base(log)
{}

[Theory]
[InlineData(false, false)]
[InlineData(false, true)]
[InlineData(true, false)]
[InlineData(true, true)]
public void It_builds_successfully(bool generatePackageOnBuild, bool packAsTool)
{
TestAsset testAsset = _testAssetsManager
.CopyTestAsset("HelloWorld", identifier: generatePackageOnBuild.ToString() + packAsTool.ToString())
.WithSource()
.WithProjectChanges((projectPath, project) =>
{
XNamespace ns = project.Root.Name.Namespace;
XElement propertyGroup = project.Root.Elements(ns + "PropertyGroup").First();
propertyGroup.Add(new XElement(ns + "GeneratePackageOnBuild", generatePackageOnBuild.ToString()));
propertyGroup.Add(new XElement(ns + "PackAsTool", packAsTool.ToString()));
});

var appProjectDirectory = Path.Combine(testAsset.TestRoot);
var buildCommand = new BuildCommand(Log, appProjectDirectory);

CommandResult result = buildCommand.Execute("/restore");

result.Should()
.Pass();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// 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;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Xml.Linq;
using FluentAssertions;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.NET.TestFramework;
using Microsoft.NET.TestFramework.Commands;
using NuGet.Packaging;
using Xunit;
using Xunit.Abstractions;
using Microsoft.NET.TestFramework.Assertions;

namespace Microsoft.NET.ToolPack.Tests
{
public class GivenThatWeWantToPublishWithGeneratePackageOnBuildAndPackAsTool : SdkTest
{
public GivenThatWeWantToPublishWithGeneratePackageOnBuildAndPackAsTool(ITestOutputHelper log) : base(log)
{}

[Theory]
[InlineData(false, false)]
[InlineData(false, true)]
[InlineData(true, false)]
[InlineData(true, true)]
public void It_publishes_successfully(bool generatePackageOnBuild, bool packAsTool)
{
Console.WriteLine(generatePackageOnBuild.ToString() + packAsTool.ToString());

TestAsset testAsset = _testAssetsManager
.CopyTestAsset("HelloWorld", identifier: generatePackageOnBuild.ToString() + packAsTool.ToString())
.WithSource()
.WithProjectChanges((projectPath, project) =>
{
XNamespace ns = project.Root.Name.Namespace;
XElement propertyGroup = project.Root.Elements(ns + "PropertyGroup").First();
propertyGroup.Add(new XElement(ns + "GeneratePackageOnBuild", generatePackageOnBuild.ToString()));
propertyGroup.Add(new XElement(ns + "PackAsTool", packAsTool.ToString()));
});

var appProjectDirectory = Path.Combine(testAsset.TestRoot);
var publishCommand = new PublishCommand(Log, appProjectDirectory);

CommandResult result = publishCommand.Execute("/restore");

result.Should()
.Pass();
}

[Theory]
[InlineData(false, false)]
[InlineData(false, true)]
[InlineData(true, false)]
[InlineData(true, true)]
public void It_builds_successfully(bool generatePackageOnBuild, bool packAsTool)
{
TestAsset testAsset = _testAssetsManager
.CopyTestAsset("HelloWorld", identifier: generatePackageOnBuild.ToString() + packAsTool.ToString())
.WithSource()
.WithProjectChanges((projectPath, project) =>
{
XNamespace ns = project.Root.Name.Namespace;
XElement propertyGroup = project.Root.Elements(ns + "PropertyGroup").First();
propertyGroup.Add(new XElement(ns + "GeneratePackageOnBuild", generatePackageOnBuild.ToString()));
propertyGroup.Add(new XElement(ns + "PackAsTool", packAsTool.ToString()));
});

var appProjectDirectory = Path.Combine(testAsset.TestRoot);
var buildCommand = new BuildCommand(Log, appProjectDirectory);

CommandResult result = buildCommand.Execute("/restore");

result.Should()
.Pass();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,35 @@ public void It_builds_and_result_contains_dependencies_dll()
}
}

[Theory(Skip = "https://github.com/dotnet/sdk/issues/3471")]
[InlineData(false, false)]
[InlineData(false, true)]
[InlineData(true, false)]
[InlineData(true, true)]
public void It_packs_successfully(bool generatePackageOnBuild, bool packAsTool)
{
Console.WriteLine(generatePackageOnBuild.ToString() + packAsTool.ToString());

TestAsset testAsset = _testAssetsManager
.CopyTestAsset("HelloWorld", identifier: generatePackageOnBuild.ToString() + packAsTool.ToString())
.WithSource()
.WithProjectChanges((projectPath, project) =>
{
XNamespace ns = project.Root.Name.Namespace;
XElement propertyGroup = project.Root.Elements(ns + "PropertyGroup").First();
propertyGroup.Add(new XElement(ns + "GeneratePackageOnBuild", generatePackageOnBuild.ToString()));
propertyGroup.Add(new XElement(ns + "PackAsTool", packAsTool.ToString()));
});

var appProjectDirectory = Path.Combine(testAsset.TestRoot);
var packCommand = new PackCommand(Log, appProjectDirectory);

CommandResult result = packCommand.Execute("/restore");

result.Should()
.Pass();
}

private bool IsAppProject(string projectPath)
{
return Path.GetFileNameWithoutExtension(projectPath).Equals(AppName, StringComparison.OrdinalIgnoreCase);
Expand Down

0 comments on commit 15de252

Please sign in to comment.