Skip to content

Commit 089ad0c

Browse files
authored
Merge pull request #6037 from retailcoder/versioncheckservice
Version check service
2 parents b3f027d + 78353d9 commit 089ad0c

File tree

12 files changed

+95
-69
lines changed

12 files changed

+95
-69
lines changed

Rubberduck.Core/App.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public sealed class App : IDisposable
2626
private readonly IConfigurationService<Configuration> _configService;
2727
private readonly IAppMenu _appMenus;
2828
private readonly IRubberduckHooks _hooks;
29-
private readonly IVersionCheck _version;
29+
private readonly IVersionCheckService _version;
3030
private readonly CommandBase _checkVersionCommand;
3131

3232
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
@@ -38,7 +38,7 @@ public App(IMessageBox messageBox,
3838
IConfigurationService<Configuration> configService,
3939
IAppMenu appMenus,
4040
IRubberduckHooks hooks,
41-
IVersionCheck version,
41+
IVersionCheckService version,
4242
CommandBase checkVersionCommand,
4343
IFileSystem filesystem)
4444
{

Rubberduck.Core/Rubberduck.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<PackageReference Include="HtmlAgilityPack">
7878
<Version>1.8.4</Version>
7979
</PackageReference>
80+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
8081
<PackageReference Include="NLog">
8182
<Version>4.5.10</Version>
8283
</PackageReference>

Rubberduck.Core/UI/About/AboutControlViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ namespace Rubberduck.UI.About
1212
{
1313
public class AboutControlViewModel
1414
{
15-
private readonly IVersionCheck _version;
15+
private readonly IVersionCheckService _version;
1616
private readonly IWebNavigator _web;
1717

18-
public AboutControlViewModel(IVersionCheck version, IWebNavigator web)
18+
public AboutControlViewModel(IVersionCheckService version, IWebNavigator web)
1919
{
2020
_version = version;
2121
_web = web;

Rubberduck.Core/UI/About/AboutDialog.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Rubberduck.UI.About
55
{
66
public partial class AboutDialog : Form
77
{
8-
public AboutDialog(IVersionCheck versionCheck, IWebNavigator web) : this()
8+
public AboutDialog(IVersionCheckService versionCheck, IWebNavigator web) : this()
99
{
1010
ViewModel = new AboutControlViewModel(versionCheck, web);
1111
}

Rubberduck.Core/UI/Command/AboutCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ namespace Rubberduck.UI.Command
1010
[ComVisible(false)]
1111
public class AboutCommand : CommandBase
1212
{
13-
public AboutCommand(IVersionCheck versionService, IWebNavigator web)
13+
public AboutCommand(IVersionCheckService versionService, IWebNavigator web)
1414
{
1515
_versionService = versionService;
1616
_web = web;
1717
}
1818

19-
private readonly IVersionCheck _versionService;
19+
private readonly IVersionCheckService _versionService;
2020
private readonly IWebNavigator _web;
2121

2222
protected override void OnExecute(object parameter)

Rubberduck.Core/UI/Command/VersionCheckCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ public void Start(string fileName)
2828

2929
public class VersionCheckCommand : CommandBase
3030
{
31-
private readonly IVersionCheck _versionCheck;
31+
private readonly IVersionCheckService _versionCheck;
3232
private readonly IMessageBox _prompt;
3333
private readonly IExternalProcess _process;
3434
IConfigurationService<Configuration> _config;
3535

36-
public VersionCheckCommand(IVersionCheck versionCheck, IMessageBox prompt, IExternalProcess process, IConfigurationService<Configuration> config)
36+
public VersionCheckCommand(IVersionCheckService versionCheck, IMessageBox prompt, IExternalProcess process, IConfigurationService<Configuration> config)
3737
{
3838
_versionCheck = versionCheck;
3939
_prompt = prompt;

Rubberduck.Core/UI/Splash2021.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public Splash2021()
1111
InitializeComponent();
1212
}
1313

14-
public Splash2021(IVersionCheck versionCheck) : this()
14+
public Splash2021(IVersionCheckService versionCheck) : this()
1515
{
1616
VersionLabel.Text = string.Format(Resources.RubberduckUI.Rubberduck_AboutBuild, versionCheck.VersionString);
1717
VersionLabel.Parent = pictureBox1;

Rubberduck.Core/VersionCheck/IVersionCheck.cs renamed to Rubberduck.Core/VersionCheck/IVersionCheckService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Rubberduck.VersionCheck
77
{
8-
public interface IVersionCheck
8+
public interface IVersionCheckService
99
{
1010
Task<Version> GetLatestVersionAsync(GeneralSettings settings, CancellationToken token = default);
1111
Version CurrentVersion { get; }

Rubberduck.Core/VersionCheck/VersionCheck.cs

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using Newtonsoft.Json;
2+
using Rubberduck.Settings;
3+
using System;
4+
using System.Linq;
5+
using System.Net.Http;
6+
using System.Threading;
7+
using System.Threading.Tasks;
8+
9+
namespace Rubberduck.VersionCheck
10+
{
11+
public class VersionCheckService : IVersionCheckService
12+
{
13+
/// <param name="version">That would be the version of the assembly for the <c>_Extension</c> class.</param>
14+
public VersionCheckService(Version version)
15+
{
16+
CurrentVersion = version;
17+
#if DEBUG
18+
IsDebugBuild = true;
19+
#endif
20+
VersionString = IsDebugBuild
21+
? $"{version.Major}.{version.Minor}.{version.Build}.x (debug)"
22+
: version.ToString();
23+
}
24+
25+
private Version _latestVersion;
26+
public async Task<Version> GetLatestVersionAsync(GeneralSettings settings, CancellationToken token = default)
27+
{
28+
if (_latestVersion != default) { return _latestVersion; }
29+
30+
try
31+
{
32+
return settings.IncludePreRelease
33+
? await GetGitHubNext()
34+
: await GetGitHubMain();
35+
}
36+
catch
37+
{
38+
return _latestVersion;
39+
}
40+
}
41+
42+
public Version CurrentVersion { get; }
43+
public bool IsDebugBuild { get; }
44+
public string VersionString { get; }
45+
46+
private async Task<Version> GetGitHubMain()
47+
{
48+
var url = new Uri("https://github.com/repos/rubberduck-vba/Rubberduck/releases/latest");
49+
using (var client = new HttpClient())
50+
{
51+
client.DefaultRequestHeaders.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue("rubberduck.version-check"));
52+
using (var response = await client.GetAsync(url))
53+
{
54+
var content = await response.Content.ReadAsStringAsync();
55+
var tagName = (string)JsonConvert.DeserializeObject<dynamic>(content).tag_name;
56+
57+
// assumes a tag name like "v2.5.3.0"
58+
return new Version(tagName.Substring("v".Length));
59+
}
60+
}
61+
}
62+
63+
private async Task<Version> GetGitHubNext()
64+
{
65+
var url = new Uri("https://github.com/repos/rubberduck-vba/Rubberduck/releases");
66+
using (var client = new HttpClient())
67+
{
68+
client.DefaultRequestHeaders.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue("rubberduck.version-check"));
69+
using (var response = await client.GetAsync(url))
70+
{
71+
var content = await response.Content.ReadAsStringAsync();
72+
var tagName = (string)JsonConvert.DeserializeObject<dynamic>(content)[0].tag_name;
73+
74+
// assumes a tag name like "Prerelease-v2.5.2.1234"
75+
return new Version(tagName.Substring("Prerelease-v".Length));
76+
}
77+
}
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)