Add support for DOTNET_WATCH_ITERATION (#443)

This environment variable can be used to determine how many types the inner command has been re-launched

Resolves #387
This commit is contained in:
Nate McMaster 2018-05-24 08:24:56 -07:00 committed by GitHub
parent 4fb42482d9
commit 83134227ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 3 deletions

View File

@ -2,9 +2,6 @@
<Import Project="dependencies.props" /> <Import Project="dependencies.props" />
<ItemGroup> <ItemGroup>
<DotNetCoreRuntime Include="2.0.0" />
<DotNetCoreRuntime Include="$(MicrosoftNETCoreApp20PackageVersion)" />
<DotNetCoreRuntime Include="$(MicrosoftNETCoreApp21PackageVersion)" />
<DotNetCoreRuntime Include="$(MicrosoftNETCoreApp22PackageVersion)" /> <DotNetCoreRuntime Include="$(MicrosoftNETCoreApp22PackageVersion)" />
</ItemGroup> </ItemGroup>

View File

@ -53,6 +53,10 @@ Environment variables:
DOTNET_WATCH DOTNET_WATCH
dotnet-watch sets this variable to '1' on all child processes launched. dotnet-watch sets this variable to '1' on all child processes launched.
DOTNET_WATCH_ITERATION
dotnet-watch sets this variable to '1' and increments by one each time
a file is changed and the command is restarted.
Remarks: Remarks:
The special option '--' is used to delimit the end of the options and The special option '--' is used to delimit the end of the options and
the beginning of arguments that will be passed to the child dotnet process. the beginning of arguments that will be passed to the child dotnet process.

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Globalization;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.DotNet.Watcher.Internal; using Microsoft.DotNet.Watcher.Internal;
@ -32,8 +33,13 @@ namespace Microsoft.DotNet.Watcher
cancellationToken.Register(state => ((TaskCompletionSource<object>) state).TrySetResult(null), cancellationToken.Register(state => ((TaskCompletionSource<object>) state).TrySetResult(null),
cancelledTaskSource); cancelledTaskSource);
var iteration = 1;
while (true) while (true)
{ {
processSpec.EnvironmentVariables["DOTNET_WATCH_ITERATION"] = iteration.ToString(CultureInfo.InvariantCulture);
iteration++;
var fileSet = await fileSetFactory.CreateAsync(cancellationToken); var fileSet = await fileSetFactory.CreateAsync(cancellationToken);
if (fileSet == null) if (fileSet == null)

View File

@ -3,6 +3,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Globalization;
using System.Threading.Tasks; using System.Threading.Tasks;
using Xunit; using Xunit;
using Xunit.Abstractions; using Xunit.Abstractions;
@ -30,6 +32,23 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests
Assert.Equal("1", envValue); Assert.Equal("1", envValue);
} }
[Fact]
public async Task RunsWithIterationEnvVariable()
{
await _app.StartWatcherAsync();
var source = Path.Combine(_app.SourceDirectory, "Program.cs");
const string messagePrefix = "DOTNET_WATCH_ITERATION = ";
for (var i = 1; i <= 4; i++)
{
var message = await _app.Process.GetOutputLineStartsWithAsync(messagePrefix, TimeSpan.FromMinutes(2));
var count = int.Parse(message.Substring(messagePrefix.Length), CultureInfo.InvariantCulture);
Assert.Equal(i, count);
File.SetLastWriteTime(source, DateTime.Now);
await _app.HasRestarted();
}
}
public void Dispose() public void Dispose()
{ {
_app.Dispose(); _app.Dispose();

View File

@ -13,6 +13,7 @@ namespace KitchenSink
Console.WriteLine("Started"); Console.WriteLine("Started");
Console.WriteLine("PID = " + Process.GetCurrentProcess().Id); Console.WriteLine("PID = " + Process.GetCurrentProcess().Id);
Console.WriteLine("DOTNET_WATCH = " + Environment.GetEnvironmentVariable("DOTNET_WATCH")); Console.WriteLine("DOTNET_WATCH = " + Environment.GetEnvironmentVariable("DOTNET_WATCH"));
Console.WriteLine("DOTNET_WATCH_ITERATION = " + Environment.GetEnvironmentVariable("DOTNET_WATCH_ITERATION"));
} }
} }
} }