Skip to content

Commit

Permalink
Add support for dotnet publish
Browse files Browse the repository at this point in the history
  • Loading branch information
zijchen committed Sep 6, 2024
1 parent bd6a69c commit 57903ad
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 29 deletions.
8 changes: 8 additions & 0 deletions src/Microsoft.Build.Sql/sdk/Sdk.targets
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
<DebugSymbols Condition=" '$(DebugSymbols)' == '' ">true</DebugSymbols>
</PropertyGroup>

<Import Project="$(MSBuildSdksPath)/Microsoft.NET.Sdk/Sdk/Sdk.BeforeCommon.targets" />

<!-- Pack target properties -->
<PropertyGroup>
<PackageType>$(PackageType);DACPAC</PackageType>
Expand Down Expand Up @@ -63,6 +65,12 @@
</ItemGroup>
</Target>

<Target Name="AddDacpacToPublishList" BeforeTargets="ComputeResolvedFilesToPublishList">
<ItemGroup>
<ResolvedFileToPublish Include="$(SqlTargetPath)" RelativePath="$(SqlTargetFile)" CopyToPublishDirectory="PreserveNewest" />
</ItemGroup>
</Target>

<ItemGroup>
<!-- This is necessary for building on non-Windows platforms. -->
<PackageReference Condition="'$(NetCoreBuild)' == 'true'" Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="All" IsImplicitlyDefined="true" />
Expand Down
31 changes: 31 additions & 0 deletions test/Microsoft.Build.Sql.Tests/DotnetTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.IO;
using System.Text;
using Microsoft.Build.Construction;
using Microsoft.SqlServer.Dac;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
Expand Down Expand Up @@ -294,6 +295,36 @@ protected void AddProjectReference(params string[] projects)
ProjectUtils.AddItemGroup(this.GetProjectFilePath(), "ProjectReference", projects);
}

/// <summary>
/// Add a package reference to a Nuget package.
/// </summary>
protected void AddPackageReference(string packageName, string version, string serverSqlcmdVariable = "", string databaseSqlcmdVariable = "", string databaseVariableLiteralValue = "", bool? suppressMissingDependenciesErrors = null)
{
ProjectUtils.AddItemGroup(this.GetProjectFilePath(), "PackageReference", new string[] { packageName }, (ProjectItemElement item) => {
item.AddMetadata("Version", version);
if (!string.IsNullOrEmpty(serverSqlcmdVariable))
{
item.AddMetadata("ServerSqlCmdVariable", serverSqlcmdVariable);
}
if (!string.IsNullOrEmpty(databaseSqlcmdVariable))
{
item.AddMetadata("DatabaseSqlCmdVariable", databaseSqlcmdVariable);
}
if (!string.IsNullOrEmpty(databaseVariableLiteralValue))
{
item.AddMetadata("DatabaseVariableLiteralValue", databaseVariableLiteralValue);
}
if (suppressMissingDependenciesErrors.HasValue)
{
item.AddMetadata("SuppressMissingDependenciesErrors", suppressMissingDependenciesErrors.ToString());
}
});
}

/// <summary>
/// Returns the full path to the sqlproj file used for this test.
/// </summary>
Expand Down
29 changes: 0 additions & 29 deletions test/Microsoft.Build.Sql.Tests/PackageReferenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Collections.Generic;
using System.IO;
using Microsoft.Build.Construction;
using NUnit.Framework;

namespace Microsoft.Build.Sql.Tests
Expand Down Expand Up @@ -110,33 +109,5 @@ public void VerifyPackageReferenceToMasterAndGenerateCreateScript()
this.VerifyDacPackage();
FileAssert.Exists(Path.Combine(this.GetOutputDirectory(), $"{DatabaseProjectName}_Create.sql"));
}

private void AddPackageReference(string packageName, string version, string serverSqlcmdVariable = "", string databaseSqlcmdVariable = "", string databaseVariableLiteralValue = "", bool? suppressMissingDependenciesErrors = null)
{
// Add a package reference to ReferenceProj version 5.5.5
ProjectUtils.AddItemGroup(this.GetProjectFilePath(), "PackageReference", new string[] { packageName }, (ProjectItemElement item) => {
item.AddMetadata("Version", version);
if (!string.IsNullOrEmpty(serverSqlcmdVariable))
{
item.AddMetadata("ServerSqlCmdVariable", serverSqlcmdVariable);
}
if (!string.IsNullOrEmpty(databaseSqlcmdVariable))
{
item.AddMetadata("DatabaseSqlCmdVariable", databaseSqlcmdVariable);
}
if (!string.IsNullOrEmpty(databaseVariableLiteralValue))
{
item.AddMetadata("DatabaseVariableLiteralValue", databaseVariableLiteralValue);
}
if (suppressMissingDependenciesErrors.HasValue)
{
item.AddMetadata("SuppressMissingDependenciesErrors", suppressMissingDependenciesErrors.ToString());
}
});
}
}
}
104 changes: 104 additions & 0 deletions test/Microsoft.Build.Sql.Tests/PublishTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.IO;
using Microsoft.Build.Construction;
using NUnit.Framework;

namespace Microsoft.Build.Sql.Tests
{
[TestFixture]
public class PublishTests : DotnetTestBase
{
[Test]
public void VerifySimplePublish()
{
int exitCode = this.RunDotnetCommandOnProject("publish", out _, out string stdError);

// Verify success
Assert.AreEqual(0, exitCode, "Publish failed with error " + stdError);
Assert.AreEqual(string.Empty, stdError);
this.VerifyDacPackage();
this.VerifyPublishFolder();
}

[Test]
public void VerifyPublishWithNoBuild()
{
// Run build first
int exitCode = this.RunDotnetCommandOnProject("publish", out _, out string stdError);
Assert.AreEqual(0, exitCode, "Build failed with error " + stdError);
Assert.AreEqual(string.Empty, stdError);
this.VerifyDacPackage();

// Run publish with --no-build
exitCode = this.RunDotnetCommandOnProject("publish --no-build", out _, out stdError);
Assert.AreEqual(0, exitCode, "publish failed with error " + stdError);
Assert.AreEqual(string.Empty, stdError);
this.VerifyPublishFolder();
}

[Test]
public void VerifyPublishkWithIncludedFiles()
{
// Add a content file that is copied to output
string includedContent = Path.Combine(this.WorkingDirectory, "include_content.txt");
File.WriteAllText(includedContent, "test");
ProjectUtils.AddItemGroup(this.GetProjectFilePath(), "Content", new[] { includedContent }, (ProjectItemElement item) =>
{
item.AddMetadata("CopyToOutputDirectory", "PreserveNewest");
});

// Run dotnet publish
int exitCode = this.RunDotnetCommandOnProject("publish", out _, out string stdError);

// Verify
Assert.AreEqual(0, exitCode, "Publish failed with error " + stdError);
Assert.AreEqual(string.Empty, stdError);
this.VerifyPublishFolder("include_content.txt");
}

[Test]
public void VerifyPublishWithProjectReference()
{
// Add a project reference to ReferenceProj, which should be copied to the publish directory
string tempFolder = TestUtils.CreateTempDirectory();
TestUtils.CopyDirectoryRecursive(Path.Combine(this.CommonTestDataDirectory, "ReferenceProj"), tempFolder);

this.AddProjectReference(Path.Combine(tempFolder, "ReferenceProj.sqlproj"));

int exitCode = this.RunDotnetCommandOnProject("publish", out _, out string stdError);

Assert.AreEqual(0, exitCode, "Publish failed with error " + stdError);
Assert.AreEqual(string.Empty, stdError);
this.VerifyDacPackage();
this.VerifyPublishFolder("ReferenceProj.dacpac");
}

[Test]
public void VerifyPublishWithPackageReference()
{
// Add a package reference to master.dacpac, which should be copied to the publish directory
this.AddPackageReference(packageName: "Microsoft.SqlServer.Dacpacs.Azure.Master", version: "160.*");

int exitCode = this.RunDotnetCommandOnProject("publish", out _, out string stdError);

Assert.AreEqual(0, exitCode, "Publish failed with error " + stdError);
Assert.AreEqual(string.Empty, stdError);
this.VerifyDacPackage();
this.VerifyPublishFolder("master.dacpac");
}

/// <summary>
/// Verify dacpac is in the publish directory, along with any additional expected files.
/// </summary>
private void VerifyPublishFolder(params string[] additionalFiles)
{
string publishFolder = Path.Combine(this.GetOutputDirectory(), "publish");
FileAssert.Exists(Path.Combine(publishFolder, $"{DatabaseProjectName}.dacpac"));
foreach (string file in additionalFiles) {
FileAssert.Exists(Path.Combine(publishFolder, file));
}
}
}
}

0 comments on commit 57903ad

Please sign in to comment.