-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathVersionCheckTask.cs
More file actions
62 lines (53 loc) · 2.13 KB
/
VersionCheckTask.cs
File metadata and controls
62 lines (53 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Threading;
using Microsoft.Build.Framework;
using NuGet.Versioning;
namespace Microsoft.Build.Sql;
/// <summary>
/// This task displays a message in the build log if there is a newer version of the package available on NuGet.org.
/// </summary>
public class VersionCheckTask : Microsoft.Build.Utilities.Task, ICancelableTask
{
/// <summary>
/// Current version of the package.
/// </summary>
[Required]
public string Version { get; set; } = string.Empty;
private const string PackageName = "Microsoft.Build.Sql";
private CancellationTokenSource _cancellationTokenSource = new();
public override bool Execute()
{
if (NuGetVersion.TryParse(Version, out NuGetVersion currentVersion) == false)
{
Log.LogWarning($"Invalid NuGetVersion format: {Version}");
return true; // This task should not fail build even if the version check fails.
}
try
{
// Check for prerelease versions if current version is a prerelease version, otherwise check for stable versions only.
NuGetVersion latestVersion = NuGetClient.GetLatestPackageVersion(
PackageName,
currentVersion.IsPrerelease,
_cancellationTokenSource.Token).GetAwaiter().GetResult();
if (latestVersion > currentVersion)
{
Log.LogWarning($"A newer version of {PackageName} is available: {latestVersion}. You are using {currentVersion}.");
}
else
{
Log.LogMessage(MessageImportance.Low, $"Already using the latest version of {PackageName}: {currentVersion}.");
}
}
catch (Exception ex)
{
Log.LogMessage(MessageImportance.Low, $"Failed to check for the latest version of {PackageName} on NuGet: {ex.Message}");
}
return true; // This task should not fail build even if the version check fails.
}
public void Cancel()
{
_cancellationTokenSource.Cancel();
}
}