Skip to content
This repository has been archived by the owner on Nov 20, 2023. It is now read-only.

Commit

Permalink
#1091 Quote path to DotNetWatch.targets (#1210)
Browse files Browse the repository at this point in the history
* #1091 Quote path to DotNetWatch.targets

- Since the path is passed as a command-line parameter to dotnet, it needs to be enclosed in quotation marks in case it contains a space.

* #1091 Minor refactoring

- Apply SRP to the methods: now each method has its own responsibility, it's clear what the arguments passed to `dotnet` are, and the quotation marks are where they should be, making their intent clear.

Co-authored-by: Ignacio Errico <pedro.errico@ey.com>
  • Loading branch information
ignacioerrico and Ignacio Errico authored Jan 20, 2022
1 parent b8faf17 commit 873818f
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions src/Microsoft.Tye.Hosting/Watch/Internal/MsBuildFileSetFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public class MsBuildFileSetFactory : IFileSetFactory
{
private const string TargetName = "GenerateWatchList";
private const string WatchTargetsFileName = "DotNetWatch.targets";
private readonly bool _waitOnError;
private readonly ILogger _logger;
private readonly string _projectFile;
private readonly bool _waitOnError;
private readonly IReadOnlyList<string> _buildFlags;
private readonly bool _trace;

public MsBuildFileSetFactory(ILogger reporter,
string projectFile,
Expand All @@ -39,7 +39,7 @@ internal MsBuildFileSetFactory(ILogger logger,
{
_logger = logger;
_projectFile = projectFile;
_buildFlags = InitializeArgs(FindTargetsFile(), trace);
_trace = trace;
}

public async Task<IFileSet> CreateAsync(CancellationToken cancellationToken)
Expand All @@ -53,19 +53,11 @@ public async Task<IFileSet> CreateAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

var args = new StringBuilder();
args.Append($"msbuild \"{_projectFile}\" /p:_DotNetWatchListFile=\"{watchList}\"");
foreach (var flag in _buildFlags)
{
args.Append(" ");
args.Append(flag);
}

var processSpec = new ProcessSpec
{
Executable = "dotnet",
WorkingDirectory = projectDir!,
Arguments = args.ToString()
Arguments = GetArgs(watchList!),
WorkingDirectory = projectDir!
};

_logger.LogDebug($"Running MSBuild target '{TargetName}' on '{_projectFile}'");
Expand Down Expand Up @@ -128,26 +120,30 @@ public async Task<IFileSet> CreateAsync(CancellationToken cancellationToken)
}
}

private IReadOnlyList<string> InitializeArgs(string watchTargetsFile, bool trace)
private string GetArgs(string watchList)
{
var watchTargetsFile = FindTargetsFile();

var args = new List<string>
{
$"msbuild \"{_projectFile}\"",
$"/p:_DotNetWatchListFile=\"{watchList}\"",
"/nologo",
"/v:n",
"/t:" + TargetName,
$"/t:{TargetName}",
"/p:DotNetWatchBuild=true", // extensibility point for users
"/p:DesignTimeBuild=true", // don't do expensive things
"/p:CustomAfterMicrosoftCommonTargets=" + watchTargetsFile,
"/p:CustomAfterMicrosoftCommonCrossTargetingTargets=" + watchTargetsFile,
$"/p:CustomAfterMicrosoftCommonTargets=\"{watchTargetsFile}\"",
$"/p:CustomAfterMicrosoftCommonCrossTargetingTargets=\"{watchTargetsFile}\"",
};

if (trace)
if (_trace)
{
// enables capturing markers to know which projects have been visited
args.Add("/p:_DotNetWatchTraceOutput=true");
}

return args;
return string.Join(" ", args);
}

private string FindTargetsFile()
Expand All @@ -164,9 +160,10 @@ private string FindTargetsFile()
var targetPath = searchPaths.Select(p => Path.Combine(p, WatchTargetsFileName)).FirstOrDefault(File.Exists);
if (targetPath == null)
{
_logger.LogError("Fatal error: could not find DotNetWatch.targets");
_logger.LogError($"Fatal error: could not find {WatchTargetsFileName}");
return null!;
}

return targetPath;
}
}
Expand Down

0 comments on commit 873818f

Please sign in to comment.