-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a version check and warn if not using latest (#632)
* Add a version check and warn if not using latest fixes #629 * Review updates * Make check lightweight and once daily * Assert that cache is effective * CR updates * fix test * always warn * sanitize * CR updates
- Loading branch information
Showing
8 changed files
with
210 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using NuGet.Versioning; | ||
|
||
namespace MSBuild.Sdk.SqlProj.DacpacTool | ||
{ | ||
public interface IVersionProvider | ||
{ | ||
NuGetVersion CurrentPackageVersion(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using System.Threading; | ||
using System.Net.Http.Json; | ||
using NuGet.Versioning; | ||
using System.IO; | ||
using System.Net.Http; | ||
|
||
namespace MSBuild.Sdk.SqlProj.DacpacTool | ||
{ | ||
|
||
public class VersionChecker | ||
{ | ||
private readonly IConsole _console; | ||
private readonly IVersionProvider _versionProvider; | ||
#pragma warning disable CA1812 // Avoid uninstantiated internal classes | ||
private sealed record Release(string tag_name); | ||
#pragma warning restore CA1812 | ||
|
||
public VersionChecker(IConsole console, IVersionProvider versionProvider) | ||
{ | ||
_versionProvider = versionProvider; | ||
_console = console; | ||
} | ||
|
||
public async Task CheckForPackageUpdateAsync() | ||
{ | ||
|
||
|
||
try | ||
{ | ||
var timeout = TimeSpan.FromSeconds(2); | ||
|
||
using var cts = new CancellationTokenSource(timeout); | ||
|
||
var cacheFile = Path.Join(Path.GetTempPath(), "MSBuild.Sdk.SqlProj.tag.txt"); | ||
|
||
NuGetVersion latestVersion = null; | ||
|
||
if (File.Exists(cacheFile) && File.GetLastWriteTimeUtc(cacheFile) > DateTime.UtcNow.AddDays(-1)) | ||
{ | ||
var cache = await File.ReadAllTextAsync(cacheFile, cts.Token).ConfigureAwait(false); | ||
latestVersion = NuGetVersion.Parse(cache); | ||
} | ||
else | ||
{ | ||
using var httpClient = new HttpClient | ||
{ | ||
DefaultRequestHeaders = { { "User-Agent", "MSBuild.Sdk.SqlProj" } }, | ||
Timeout = timeout, | ||
}; | ||
|
||
var response = await httpClient.GetFromJsonAsync<Release>("https://api.github.com/repos/rr-wfm/MSBuild.Sdk.SqlProj/releases/latest").ConfigureAwait(false); | ||
if (response is null || response.tag_name is null) | ||
{ | ||
return; | ||
} | ||
|
||
latestVersion = NuGetVersion.Parse(response.tag_name.TrimStart('v')); | ||
|
||
await File.WriteAllTextAsync(cacheFile, latestVersion.ToNormalizedString(), cts.Token).ConfigureAwait(false); | ||
} | ||
|
||
if (latestVersion is null) | ||
{ | ||
return; | ||
} | ||
|
||
if (latestVersion > _versionProvider.CurrentPackageVersion()) | ||
{ | ||
_console.WriteLine($"DacpacTool warning SQLPROJ0002: You are not using the latest version of this SDK, please update to get the latest bug fixes, features and support. Modify your project file: '<Project Sdk=\"MSBuild.Sdk.SqlProj/{latestVersion}\">')"); | ||
} | ||
} | ||
#pragma warning disable CA1031 // Do not catch general exception types | ||
catch (Exception) | ||
{ | ||
// Ignore | ||
} | ||
#pragma warning restore CA1031 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Reflection; | ||
using NuGet.Versioning; | ||
|
||
namespace MSBuild.Sdk.SqlProj.DacpacTool | ||
{ | ||
public class VersionProvider : IVersionProvider | ||
{ | ||
public NuGetVersion CurrentPackageVersion() | ||
{ | ||
return new NuGetVersion( | ||
typeof(VersionProvider) | ||
.Assembly | ||
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()! | ||
.InformationalVersion); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using NuGet.Versioning; | ||
|
||
namespace MSBuild.Sdk.SqlProj.DacpacTool.Tests | ||
{ | ||
public class VersionProvider : IVersionProvider | ||
{ | ||
private readonly string _version; | ||
|
||
public VersionProvider(string version) | ||
{ | ||
_version = version; | ||
} | ||
|
||
public NuGetVersion CurrentPackageVersion() | ||
{ | ||
return new NuGetVersion(_version); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Shouldly; | ||
using System.Diagnostics; | ||
|
||
namespace MSBuild.Sdk.SqlProj.DacpacTool.Tests | ||
{ | ||
[TestClass] | ||
public class VersionCheckerTests | ||
{ | ||
private readonly IConsole _console = new TestConsole(); | ||
|
||
[TestMethod] | ||
public async Task RunsVersionCheck() | ||
{ | ||
// Arrange | ||
var testConsole = (TestConsole)_console; | ||
testConsole.Lines.Clear(); | ||
var versionChecker = new VersionChecker(_console, new VersionProvider("1.17.0+4c0175a82e")); | ||
|
||
var cacheFile = Path.Join(Path.GetTempPath(), "MSBuild.Sdk.SqlProj.tag.txt"); | ||
|
||
if (File.Exists(cacheFile)) | ||
{ | ||
File.Delete(cacheFile); | ||
} | ||
|
||
// Act | ||
await versionChecker.CheckForPackageUpdateAsync(); | ||
|
||
// Assert | ||
testConsole.Lines.Count.ShouldBe(1); | ||
testConsole.Lines[0].ShouldStartWith($"DacpacTool warning SQLPROJ0002: You are not using the latest version of this SDK, please update to get the latest bug fixes, features and support. Modify your project file: "); | ||
|
||
// Arrange | ||
testConsole.Lines.Clear(); | ||
|
||
var stopWatch = Stopwatch.StartNew(); | ||
|
||
// Act | ||
await versionChecker.CheckForPackageUpdateAsync(); | ||
|
||
stopWatch.Stop(); | ||
|
||
testConsole.Lines.Count.ShouldBe(1); | ||
testConsole.Lines[0].ShouldStartWith($"DacpacTool warning SQLPROJ0002: You are not using the latest version of this SDK, please update to get the latest bug fixes, features and support. Modify your project file: "); | ||
File.Exists(cacheFile).ShouldBeTrue(); | ||
stopWatch.ElapsedMilliseconds.ShouldBeLessThan(20); | ||
} | ||
|
||
[TestMethod] | ||
public async Task RunsVersionCheckAndNoLog() | ||
{ | ||
// Arrange | ||
var testConsole = (TestConsole)_console; | ||
testConsole.Lines.Clear(); | ||
var versionChecker = new VersionChecker(_console, new VersionProvider("9999999.9999999.0+4c0175a82e")); | ||
|
||
// Act | ||
await versionChecker.CheckForPackageUpdateAsync(); | ||
|
||
// Assert | ||
testConsole.Lines.Count.ShouldBe(0); | ||
} | ||
} | ||
} |