// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Text;
#if ASPIRE_HOSTING_CORE
namespace Aspire.Hosting.Internal;
#else
namespace Aspire.Hosting.Dotnet;
#endif
internal static class MsBuildResponseFileFactory
{
public static async Task<MsBuildResponseFile?> CreateAsync(
IReadOnlyDictionary<string, string> properties,
bool isFileBasedApp,
ITempFileSystemService temporaryFileSystem,
CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(properties);
ArgumentNullException.ThrowIfNull(temporaryFileSystem);
if (properties.Count == 0)
{
return null;
}
var directory = temporaryFileSystem.CreateTempSubdirectory("aspire-msbuild-");
var responseFilePath = Path.Combine(directory.Path, "build-properties.rsp");
try
{
var options = new FileStreamOptions
{
Mode = FileMode.CreateNew,
Access = FileAccess.Write,
Share = FileShare.Read,
Options = FileOptions.Asynchronous,
};
if (!OperatingSystem.IsWindows())
{
// Build properties are not a secret transport, but the response file still belongs only to this
// process. Set the final mode atomically so another local user cannot read project-specific values.
options.UnixCreateMode = UnixFileMode.UserRead | UnixFileMode.UserWrite;
}
var stream = new FileStream(responseFilePath, options);
await using var streamScope = stream.ConfigureAwait(false);
var writer = new StreamWriter(stream, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
await using var writerScope = writer.ConfigureAwait(false);
foreach (var (name, value) in properties)
{
// MSBuild's response-file tokenizer splits on all Unicode whitespace. Quoting the complete switch
// keeps those characters in the property value; percent escaping handles embedded quotes,
// backslashes, and line-breaking whitespace without relying on platform quoting rules.
// The .NET CLI parses switches for file-based apps itself and does not recognize a quoted switch,
// so it would fall back to MSBuild. Its parser consumes a trailing ';', which also keeps it from
// trimming trailing Unicode whitespace from the value.
var argument = CreatePropertyArgument(name, value);
await writer.WriteLineAsync(
(isFileBasedApp ? $"{argument};" : $"\"{argument}\"").AsMemory(),
cancellationToken).ConfigureAwait(false);
}
return new MsBuildResponseFile(directory, responseFilePath);
}
catch
{
directory.Dispose();
throw;
}
}
public static string CreatePropertyArgument(string name, string value) =>
$"--property:{EscapePropertyValue(name)}={EscapePropertyValue(value)}";
// Escaped ';' (%3B) stays literal when MSBuild expands a property into items, so list-valued properties
// such as RuntimeIdentifiers would collapse into a single entry. Escape each entry and keep the separators
// as real ';' inside quotes so MSBuild's property-switch parser doesn't split the value into new properties.
public static string CreateListPropertyArgument(string name, IEnumerable<string> values) =>
$"--property:{EscapePropertyValue(name)}=\"{string.Join(';', values.Select(EscapePropertyValue))}\"";
// The .NET CLI parses property switches for file-based apps instead of MSBuild. It keeps quotes literally
// and appends ';' segments without '=' to the previous value, so list values must be passed unquoted.
public static string CreateFileBasedAppListPropertyArgument(string name, IEnumerable<string> values) =>
$"--property:{EscapePropertyValue(name)}={string.Join(';', values.Select(EscapePropertyValue))}";
private static string EscapePropertyValue(string value)
{
// MSBuild decodes %-escaped special characters in property values. Response files are line-oriented
// command input, so quotes, backslashes, and ASCII whitespace are escaped before the complete switch is
// quoted by the caller.
// https://learn.microsoft.com/visualstudio/msbuild/msbuild-response-files
// Escape '%' first so an existing sequence such as "%3B" remains literal.
// https://learn.microsoft.com/visualstudio/msbuild/how-to-escape-special-characters-in-msbuild
return value
.Replace("%", "%25", StringComparison.Ordinal)
.Replace("$", "%24", StringComparison.Ordinal)
.Replace("@", "%40", StringComparison.Ordinal)
.Replace("(", "%28", StringComparison.Ordinal)
.Replace(")", "%29", StringComparison.Ordinal)
.Replace("*", "%2A", StringComparison.Ordinal)
.Replace("\\", "%5C", StringComparison.Ordinal)
.Replace("\"", "%22", StringComparison.Ordinal)
.Replace("'", "%27", StringComparison.Ordinal)
.Replace(",", "%2C", StringComparison.Ordinal)
.Replace(";", "%3B", StringComparison.Ordinal)
.Replace("?", "%3F", StringComparison.Ordinal)
.Replace("=", "%3D", StringComparison.Ordinal)
.Replace(" ", "%20", StringComparison.Ordinal)
.Replace("\t", "%09", StringComparison.Ordinal)
.Replace("\r", "%0D", StringComparison.Ordinal)
.Replace("\n", "%0A", StringComparison.Ordinal);
}
}
internal sealed class MsBuildResponseFile(
TempDirectory directory,
string responseFilePath) : IDisposable
{
private TempDirectory? _directory = directory;
public string Argument { get; } = $"@{responseFilePath}";
internal string FilePath { get; } = responseFilePath;
public void Dispose()
{
Interlocked.Exchange(ref _directory, null)?.Dispose();
}
}