Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace Microsoft.NET.Build.Containers.Tasks;
namespace Microsoft.NET.Sdk.Common;

/// <summary>
/// Extracts task items and credentials from a Visual Studio host object.
/// Supports both the JSON-based <c>QueryAllTaskItems</c> protocol and the legacy
/// <see cref="IEnumerable{ITaskItem}"/> interface.
/// </summary>
internal sealed class VSHostObject(ITaskHost? hostObject, TaskLoggingHelper log)
{
private const string CredentialItemSpecName = "MsDeployCredential";
Expand Down Expand Up @@ -51,7 +56,11 @@ internal sealed class VSHostObject(ITaskHost? hostObject, TaskLoggingHelper log)
return (username, password);
}

private IEnumerable<ITaskItem>? GetTaskItems()
/// <summary>
/// Gets all task items from the host object.
/// </summary>
/// <returns>The task items if available, null otherwise.</returns>
public IEnumerable<ITaskItem>? GetTaskItems()
{
try
{
Expand All @@ -62,7 +71,6 @@ internal sealed class VSHostObject(ITaskHost? hostObject, TaskLoggingHelper log)
// - Returns a JSON array of objects with the shape:
// [{ "ItemSpec": "<string>", "Metadata": { "<key>": "<value>", ... } }, ...]
// The JSON is deserialized into TaskItemDto records and converted to ITaskItem instances.
// Only UserName and Password metadata are extracted to avoid conflicts with reserved MSBuild metadata.
string? rawTaskItems = (string?)_hostObject!.GetType().InvokeMember(
"QueryAllTaskItems",
BindingFlags.InvokeMethod,
Expand Down Expand Up @@ -101,21 +109,33 @@ static TaskItem ConvertToTaskItem(TaskItemDto dto)
TaskItem taskItem = new(dto.ItemSpec ?? string.Empty);
if (dto.Metadata is not null)
{
if (dto.Metadata.TryGetValue(UserMetaDataName, out string? userName))
{
taskItem.SetMetadata(UserMetaDataName, userName);
}

if (dto.Metadata.TryGetValue(PasswordMetaDataName, out string? password))
foreach (KeyValuePair<string, string> kvp in dto.Metadata)
{
taskItem.SetMetadata(PasswordMetaDataName, password);
try
{
taskItem.SetMetadata(kvp.Key, kvp.Value);
}
catch (ArgumentException)
{
// Skip reserved/built-in MSBuild metadata names (e.g. FullPath, Identity).
}
}
}

return taskItem;
}
}

private readonly record struct TaskItemDto(string? ItemSpec, Dictionary<string, string>? Metadata);
}
private readonly struct TaskItemDto
{
public string? ItemSpec { get; }
public Dictionary<string, string>? Metadata { get; }

[System.Text.Json.Serialization.JsonConstructor]
public TaskItemDto(string? itemSpec, Dictionary<string, string>? metadata)
{
ItemSpec = itemSpec;
Metadata = metadata;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<Compile Include="$(RepoRoot)src\Common\VSHostObject.cs" LinkBase="Common" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' != 'net472'">
<PackageReference Include="Valleysoft.DockerCredsProvider" />

Expand All @@ -74,7 +78,6 @@
<Compile Include="Tasks/ComputeDotnetBaseImageAndTag.cs" />
<Compile Include="ContainerHelpers.cs" />
<Compile Include="net472Definitions.cs" />
<Compile Include="VSHostObject.cs" />
<Compile Include="Port.cs" />
<Compile Include="Resources\Resource.cs" />
<Compile Include="Resources\Strings.Designer.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.MSBuild;
using Microsoft.NET.Build.Containers.Resources;
using Microsoft.NET.Sdk.Common;
using ILogger = Microsoft.Extensions.Logging.ILogger;

namespace Microsoft.NET.Build.Containers.Tasks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.NET.Build.Containers.Resources;
using Microsoft.NET.Sdk.Common;

namespace Microsoft.NET.Build.Containers.Tasks;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

<Compile Include="$(RepoRoot)src\Common\EnvironmentVariableNames.cs" LinkBase="Common"/>
<Compile Include="$(RepoRoot)src\Common\CompileOptions.cs" LinkBase="Common"/>
<Compile Include="$(RepoRoot)src\Common\VSHostObject.cs" LinkBase="Common"/>

<AdditionalContent Include="$(PublishRoot)\Targets\**\*.*">
<Pack>true</Pack>
Expand Down
62 changes: 0 additions & 62 deletions src/WebSdk/Publish/Tasks/MsDeploy/VSHostObject.cs

This file was deleted.

25 changes: 19 additions & 6 deletions src/WebSdk/Publish/Tasks/Tasks/MsDeploy/VsMsdeploy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Build.Framework;
using Microsoft.NET.Sdk.Common;
using Microsoft.NET.Sdk.Publish.Tasks.Properties;
using Collections = System.Collections;
using Diagnostics = System.Diagnostics;
Expand Down Expand Up @@ -823,9 +824,8 @@ public override bool Execute()
else
{
dest = VSMSDeployObjectFactory.CreateVSMSDeployObject(Destination[0]);
VSHostObject hostObj = new(HostObject as IEnumerable<ITaskItem>);
string username, password;
if (hostObj.ExtractCredentials(out username, out password))
VSHostObject hostObj = new(HostObject, Log);
if (hostObj.TryGetCredentials() is (string username, string password))
{
dest.UserName = username;
dest.Password = password;
Expand Down Expand Up @@ -937,11 +937,24 @@ void IVSMSDeployHost.UpdateDeploymentBaseOptions(VSMSDeployObject srcVsMsDeployo
List<string> enableSkipDirectiveList = MSDeployUtility.ConvertStringIntoList(EnableSkipDirective);
List<string> disableSkipDirectiveList = MSDeployUtility.ConvertStringIntoList(DisableSkipDirective);

VSHostObject hostObject = new(HostObject as IEnumerable<ITaskItem>);
ITaskItem[]? srcSkipItems, destSkipsItems;
VSHostObject hostObject = new(HostObject, Log);
IEnumerable<ITaskItem>? allItems = hostObject.GetTaskItems();
ITaskItem[]? srcSkipItems = null;
ITaskItem[]? destSkipsItems = null;

// Add FileSkip rules from Host Object
hostObject.GetFileSkips(out srcSkipItems, out destSkipsItems);
if (allItems is not null)
{
srcSkipItems = allItems.Where(item =>
item.ItemSpec == VSMsDeployTaskHostObject.SkipFileItemSpecName
&& (item.GetMetadata(VSMsDeployTaskHostObject.SkipApplyMetadataName) == VSMsDeployTaskHostObject.SourceDeployObject
|| string.IsNullOrEmpty(item.GetMetadata(VSMsDeployTaskHostObject.SkipApplyMetadataName)))).ToArray();

destSkipsItems = allItems.Where(item =>
item.ItemSpec == VSMsDeployTaskHostObject.SkipFileItemSpecName
&& (item.GetMetadata(VSMsDeployTaskHostObject.SkipApplyMetadataName) == VSMsDeployTaskHostObject.DestinationDeployObject
|| string.IsNullOrEmpty(item.GetMetadata(VSMsDeployTaskHostObject.SkipApplyMetadataName)))).ToArray();
}
Utility.AddSkipDirectiveToBaseOptions(srcVsMsDeployobject.BaseOptions, srcSkipItems, enableSkipDirectiveList, disableSkipDirectiveList, Log);
Utility.AddSkipDirectiveToBaseOptions(destVsMsDeployobject.BaseOptions, destSkipsItems, enableSkipDirectiveList, disableSkipDirectiveList, Log);

Expand Down
16 changes: 13 additions & 3 deletions src/WebSdk/Publish/Tasks/Tasks/OneDeploy/OneDeploy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Build.Framework;
using Microsoft.NET.Sdk.Publish.Tasks.MsDeploy;
using Microsoft.NET.Sdk.Common;
using Microsoft.NET.Sdk.Publish.Tasks.Properties;

namespace Microsoft.NET.Sdk.Publish.Tasks.OneDeploy;
Expand Down Expand Up @@ -161,9 +161,19 @@ internal async Task<bool> OneDeployAsync(

private bool GetCredentialsFromTask(out string user, out string password)
{
VSHostObject hostObj = new(HostObject as IEnumerable<ITaskItem>);
VSHostObject hostObj = new(HostObject, Log);
if (hostObj.TryGetCredentials() is (string u, string p))
{
user = u;
password = p;

return true;
}

user = string.Empty;
password = string.Empty;

return hostObj.ExtractCredentials(out user, out password);
return false;
}

private Task<IHttpResponse?> DeployAsync(
Expand Down
17 changes: 14 additions & 3 deletions src/WebSdk/Publish/Tasks/Tasks/ZipDeploy/ZipDeploy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System.Net;
using Microsoft.Build.Framework;
using Microsoft.NET.Sdk.Publish.Tasks.MsDeploy;
using Microsoft.NET.Sdk.Common;
using Microsoft.NET.Sdk.Publish.Tasks.Properties;

namespace Microsoft.NET.Sdk.Publish.Tasks.ZipDeploy
Expand Down Expand Up @@ -136,8 +136,19 @@ public async Task<bool> ZipDeployAsync(string? zipToPublishPath, string? userNam

private bool GetDestinationCredentials(out string user, out string password)
{
VSHostObject hostObj = new(HostObject as IEnumerable<ITaskItem>);
return hostObj.ExtractCredentials(out user, out password);
VSHostObject hostObj = new(HostObject, Log);
if (hostObj.TryGetCredentials() is (string u, string p))
{
user = u;
password = p;

return true;
}

user = string.Empty;
password = string.Empty;

return false;
}
}
}
Loading