Update nuget packages and rework UpdateService

pull/37/merge
Alexey Golub 7 years ago
parent 9988300942
commit 5a84fb25e6

@ -1,7 +1,7 @@
using DiscordChatExporter.Cli.ViewModels; using CommonServiceLocator;
using DiscordChatExporter.Cli.ViewModels;
using DiscordChatExporter.Core.Services; using DiscordChatExporter.Core.Services;
using GalaSoft.MvvmLight.Ioc; using GalaSoft.MvvmLight.Ioc;
using Microsoft.Practices.ServiceLocation;
namespace DiscordChatExporter.Cli namespace DiscordChatExporter.Cli
{ {

@ -10,9 +10,9 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="CommonServiceLocator" Version="1.3" /> <PackageReference Include="CommonServiceLocator" Version="2.0.3" />
<PackageReference Include="FluentCommandLineParser" Version="1.4.3" /> <PackageReference Include="FluentCommandLineParser" Version="1.4.3" />
<PackageReference Include="MvvmLightLibs" Version="5.3.0.0" /> <PackageReference Include="MvvmLightLibs" Version="5.4.1" />
<PackageReference Include="Tyrrrz.Extensions" Version="1.5.0" /> <PackageReference Include="Tyrrrz.Extensions" Version="1.5.0" />
</ItemGroup> </ItemGroup>

@ -15,7 +15,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="11.0.1" /> <PackageReference Include="Newtonsoft.Json" Version="11.0.1" />
<PackageReference Include="Onova" Version="1.0.0" /> <PackageReference Include="Onova" Version="2.0.0" />
<PackageReference Include="Tyrrrz.Extensions" Version="1.5.0" /> <PackageReference Include="Tyrrrz.Extensions" Version="1.5.0" />
<PackageReference Include="Tyrrrz.Settings" Version="1.3.2" /> <PackageReference Include="Tyrrrz.Settings" Version="1.3.2" />
</ItemGroup> </ItemGroup>

@ -5,10 +5,10 @@ namespace DiscordChatExporter.Core.Services
{ {
public interface IUpdateService public interface IUpdateService
{ {
Task<Version> CheckForUpdatesAsync(); bool NeedRestart { get; set; }
Task PrepareUpdateAsync(); Task<Version> CheckPrepareUpdateAsync();
Task ApplyUpdateAsync(bool restart = true); Task FinalizeUpdateAsync();
} }
} }

@ -8,82 +8,55 @@ namespace DiscordChatExporter.Core.Services
public class UpdateService : IUpdateService public class UpdateService : IUpdateService
{ {
private readonly ISettingsService _settingsService; private readonly ISettingsService _settingsService;
private readonly UpdateManager _updateManager; private readonly IUpdateManager _manager;
private Version _lastVersion; private Version _updateVersion;
private bool _applied; private bool _updateFinalized;
public bool NeedRestart { get; set; }
public UpdateService(ISettingsService settingsService) public UpdateService(ISettingsService settingsService)
{ {
_settingsService = settingsService; _settingsService = settingsService;
_updateManager = new UpdateManager( _manager = new UpdateManager(
new GithubPackageResolver("Tyrrrz", "DiscordChatExporter", "DiscordChatExporter.zip"), new GithubPackageResolver("Tyrrrz", "DiscordChatExporter", "DiscordChatExporter.zip"),
new ZipPackageExtractor()); new ZipPackageExtractor());
} }
public async Task<Version> CheckForUpdatesAsync() public async Task<Version> CheckPrepareUpdateAsync()
{ {
#if DEBUG // If auto-update is disabled - don't check for updates
// Never update in DEBUG mode
return null;
#endif
// Don't update if user disabled it
if (!_settingsService.IsAutoUpdateEnabled) if (!_settingsService.IsAutoUpdateEnabled)
return null; return null;
try // Cleanup leftover files
{ _manager.Cleanup();
// Remove some junk left over from last update
_updateManager.Cleanup();
// Check for updates // Check for updates
var check = await _updateManager.CheckForUpdatesAsync(); var check = await _manager.CheckForUpdatesAsync();
if (!check.CanUpdate)
// Return latest version or null if running latest version already
return check.CanUpdate ? _lastVersion = check.LastVersion : null;
}
catch
{
// It's okay for update to fail
return null; return null;
}
}
public async Task PrepareUpdateAsync() // Prepare the update
{ await _manager.PrepareUpdateAsync(check.LastVersion);
if (_lastVersion == null)
return;
try return _updateVersion = check.LastVersion;
{
// Download and prepare update
await _updateManager.PreparePackageAsync(_lastVersion);
}
catch
{
// It's okay for update to fail
}
} }
public async Task ApplyUpdateAsync(bool restart = true) public async Task FinalizeUpdateAsync()
{ {
if (_lastVersion == null) // Check if an update is pending
if (_updateVersion == null)
return; return;
if (_applied)
// Check if the update has already been finalized
if (_updateFinalized)
return; return;
try // Launch the updater
{ await _manager.LaunchUpdaterAsync(_updateVersion, NeedRestart);
// Enqueue an update _updateFinalized = true;
await _updateManager.EnqueueApplyPackageAsync(_lastVersion, restart);
_applied = true;
}
catch
{
// It's okay for update to fail
}
} }
} }
} }

@ -1,7 +1,7 @@
using DiscordChatExporter.Core.Services; using CommonServiceLocator;
using DiscordChatExporter.Core.Services;
using DiscordChatExporter.Gui.ViewModels; using DiscordChatExporter.Gui.ViewModels;
using GalaSoft.MvvmLight.Ioc; using GalaSoft.MvvmLight.Ioc;
using Microsoft.Practices.ServiceLocation;
namespace DiscordChatExporter.Gui namespace DiscordChatExporter.Gui
{ {

@ -45,28 +45,28 @@
<Reference Include="AmmySidekick, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7c1296d24569a67d, processorArchitecture=MSIL"> <Reference Include="AmmySidekick, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7c1296d24569a67d, processorArchitecture=MSIL">
<HintPath>..\packages\Ammy.WPF.1.2.94\lib\net40\AmmySidekick.dll</HintPath> <HintPath>..\packages\Ammy.WPF.1.2.94\lib\net40\AmmySidekick.dll</HintPath>
</Reference> </Reference>
<Reference Include="GalaSoft.MvvmLight, Version=5.3.0.19026, Culture=neutral, PublicKeyToken=e7570ab207bcb616, processorArchitecture=MSIL"> <Reference Include="CommonServiceLocator, Version=2.0.3.0, Culture=neutral, PublicKeyToken=489b6accfaf20ef0, processorArchitecture=MSIL">
<HintPath>..\packages\MvvmLightLibs.5.3.0.0\lib\net45\GalaSoft.MvvmLight.dll</HintPath> <HintPath>..\packages\CommonServiceLocator.2.0.3\lib\net45\CommonServiceLocator.dll</HintPath>
</Reference> </Reference>
<Reference Include="GalaSoft.MvvmLight.Extras, Version=5.3.0.19032, Culture=neutral, PublicKeyToken=669f0b5e8f868abf, processorArchitecture=MSIL"> <Reference Include="GalaSoft.MvvmLight, Version=5.4.1.0, Culture=neutral, PublicKeyToken=e7570ab207bcb616, processorArchitecture=MSIL">
<HintPath>..\packages\MvvmLightLibs.5.3.0.0\lib\net45\GalaSoft.MvvmLight.Extras.dll</HintPath> <HintPath>..\packages\MvvmLightLibs.5.4.1\lib\net45\GalaSoft.MvvmLight.dll</HintPath>
</Reference> </Reference>
<Reference Include="GalaSoft.MvvmLight.Platform, Version=5.3.0.19032, Culture=neutral, PublicKeyToken=5f873c45e98af8a1, processorArchitecture=MSIL"> <Reference Include="GalaSoft.MvvmLight.Extras, Version=5.4.1.0, Culture=neutral, PublicKeyToken=669f0b5e8f868abf, processorArchitecture=MSIL">
<HintPath>..\packages\MvvmLightLibs.5.3.0.0\lib\net45\GalaSoft.MvvmLight.Platform.dll</HintPath> <HintPath>..\packages\MvvmLightLibs.5.4.1\lib\net45\GalaSoft.MvvmLight.Extras.dll</HintPath>
</Reference>
<Reference Include="GalaSoft.MvvmLight.Platform, Version=5.4.1.0, Culture=neutral, PublicKeyToken=5f873c45e98af8a1, processorArchitecture=MSIL">
<HintPath>..\packages\MvvmLightLibs.5.4.1\lib\net45\GalaSoft.MvvmLight.Platform.dll</HintPath>
</Reference> </Reference>
<Reference Include="MaterialDesignColors, Version=1.1.3.0, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="MaterialDesignColors, Version=1.1.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MaterialDesignColors.1.1.3\lib\net45\MaterialDesignColors.dll</HintPath> <HintPath>..\packages\MaterialDesignColors.1.1.3\lib\net45\MaterialDesignColors.dll</HintPath>
</Reference> </Reference>
<Reference Include="MaterialDesignThemes.Wpf, Version=2.3.1.953, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="MaterialDesignThemes.Wpf, Version=2.4.0.1044, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MaterialDesignThemes.2.3.1.953\lib\net45\MaterialDesignThemes.Wpf.dll</HintPath> <HintPath>..\packages\MaterialDesignThemes.2.4.0.1044\lib\net45\MaterialDesignThemes.Wpf.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Practices.ServiceLocation, Version=1.3.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\CommonServiceLocator.1.3\lib\portable-net4+sl5+netcore45+wpa81+wp8\Microsoft.Practices.ServiceLocation.dll</HintPath>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="System.Windows.Interactivity, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <Reference Include="System.Windows.Interactivity, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\MvvmLightLibs.5.3.0.0\lib\net45\System.Windows.Interactivity.dll</HintPath> <HintPath>..\packages\MvvmLightLibs.5.4.1\lib\net45\System.Windows.Interactivity.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Xaml"> <Reference Include="System.Xaml">
<RequiredTargetFramework>4.0</RequiredTargetFramework> <RequiredTargetFramework>4.0</RequiredTargetFramework>
@ -74,6 +74,7 @@
<Reference Include="Tyrrrz.Extensions, Version=1.5.0.0, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="Tyrrrz.Extensions, Version=1.5.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Tyrrrz.Extensions.1.5.0\lib\net45\Tyrrrz.Extensions.dll</HintPath> <HintPath>..\packages\Tyrrrz.Extensions.1.5.0\lib\net45\Tyrrrz.Extensions.dll</HintPath>
</Reference> </Reference>
<Reference Include="Tyrrrz.Settings, Version=1.3.2.0, Culture=neutral, PublicKeyToken=null" />
<Reference Include="WindowsBase" /> <Reference Include="WindowsBase" />
<Reference Include="PresentationCore" /> <Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" /> <Reference Include="PresentationFramework" />
@ -146,6 +147,7 @@
<LastGenOutput>Resources.Designer.cs</LastGenOutput> <LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource> </EmbeddedResource>
<None Include="App.ammy" /> <None Include="App.ammy" />
<None Include="app.config" />
<None Include="lib.ammy" /> <None Include="lib.ammy" />
<None Include="packages.config"> <None Include="packages.config">
<SubType>Designer</SubType> <SubType>Designer</SubType>

@ -111,7 +111,7 @@ namespace DiscordChatExporter.Gui.ViewModels
// Messages // Messages
MessengerInstance.Register<StartExportMessage>(this, MessengerInstance.Register<StartExportMessage>(this,
m => { Export(m.Channel, m.FilePath, m.Format, m.From, m.To); }); m => Export(m.Channel, m.FilePath, m.Format, m.From, m.To));
} }
private async void ViewLoaded() private async void ViewLoaded()
@ -122,33 +122,34 @@ namespace DiscordChatExporter.Gui.ViewModels
// Set last token // Set last token
Token = _settingsService.LastToken; Token = _settingsService.LastToken;
// Check for updates // Check and prepare update
var lastVersion = await _updateService.CheckForUpdatesAsync(); try
if (lastVersion != null)
{ {
// Download updates var updateVersion = await _updateService.CheckPrepareUpdateAsync();
await _updateService.PrepareUpdateAsync(); if (updateVersion != null)
{
// Notify user MessengerInstance.Send(new ShowNotificationMessage(
MessengerInstance.Send( $"Update to DiscordChatExporter v{updateVersion} will be installed when you exit",
new ShowNotificationMessage( "INSTALL NOW", () =>
$"DiscordChatExporter v{lastVersion} has been downloaded it will be installed when you exit",
"INSTALL NOW",
async () =>
{ {
await _updateService.ApplyUpdateAsync(); _updateService.NeedRestart = true;
Application.Current.Shutdown(); Application.Current.Shutdown();
})); }));
}
}
catch
{
MessengerInstance.Send(new ShowNotificationMessage("Failed to perform application auto-update"));
} }
} }
private void ViewClosed() private async void ViewClosed()
{ {
// Save settings // Save settings
_settingsService.Save(); _settingsService.Save();
// Apply updates if available // Finalize updates if available
_updateService.ApplyUpdateAsync(false); await _updateService.FinalizeUpdateAsync();
} }
private async void PullData() private async void PullData()
@ -185,13 +186,11 @@ namespace DiscordChatExporter.Gui.ViewModels
} }
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Unauthorized) catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Unauthorized)
{ {
const string message = "Unauthorized make sure the token is valid"; MessengerInstance.Send(new ShowNotificationMessage("Unauthorized make sure the token is valid"));
MessengerInstance.Send(new ShowNotificationMessage(message));
} }
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden) catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden)
{ {
const string message = "Forbidden account may be locked by 2FA"; MessengerInstance.Send(new ShowNotificationMessage("Forbidden account may be locked by 2FA"));
MessengerInstance.Send(new ShowNotificationMessage(message));
} }
AvailableGuilds = _guildChannelsMap.Keys.ToArray(); AvailableGuilds = _guildChannelsMap.Keys.ToArray();
@ -237,13 +236,11 @@ namespace DiscordChatExporter.Gui.ViewModels
// Notify completion // Notify completion
MessengerInstance.Send(new ShowNotificationMessage($"Export completed for channel [{channel.Name}]", MessengerInstance.Send(new ShowNotificationMessage($"Export completed for channel [{channel.Name}]",
"OPEN", "OPEN", () => Process.Start(filePath)));
() => Process.Start(filePath)));
} }
catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden) catch (HttpErrorStatusCodeException ex) when (ex.StatusCode == HttpStatusCode.Forbidden)
{ {
const string message = "You don't have access to that channel"; MessengerInstance.Send(new ShowNotificationMessage("You don't have access to that channel"));
MessengerInstance.Send(new ShowNotificationMessage(message));
} }
IsBusy = false; IsBusy = false;

@ -17,8 +17,13 @@ namespace DiscordChatExporter.Gui.Views
Snackbar.MessageQueue = new SnackbarMessageQueue(TimeSpan.FromSeconds(5)); Snackbar.MessageQueue = new SnackbarMessageQueue(TimeSpan.FromSeconds(5));
// Notification messages // Notification messages
Messenger.Default.Register<ShowNotificationMessage>(this, Messenger.Default.Register<ShowNotificationMessage>(this, m =>
m => Snackbar.MessageQueue.Enqueue(m.Message, m.CallbackCaption, m.Callback)); {
if (m.CallbackCaption != null && m.Callback != null)
Snackbar.MessageQueue.Enqueue(m.Message, m.CallbackCaption, m.Callback);
else
Snackbar.MessageQueue.Enqueue(m.Message);
});
// Dialog messages // Dialog messages
Messenger.Default.Register<ShowExportSetupMessage>(this, Messenger.Default.Register<ShowExportSetupMessage>(this,

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="CommonServiceLocator" publicKeyToken="489b6accfaf20ef0" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.3.0" newVersion="2.0.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

@ -2,9 +2,9 @@
<packages> <packages>
<package id="Ammy" version="1.2.94" targetFramework="net461" /> <package id="Ammy" version="1.2.94" targetFramework="net461" />
<package id="Ammy.WPF" version="1.2.94" targetFramework="net461" /> <package id="Ammy.WPF" version="1.2.94" targetFramework="net461" />
<package id="CommonServiceLocator" version="1.3" targetFramework="net461" /> <package id="CommonServiceLocator" version="2.0.3" targetFramework="net461" />
<package id="MaterialDesignColors" version="1.1.3" targetFramework="net461" /> <package id="MaterialDesignColors" version="1.1.3" targetFramework="net461" />
<package id="MaterialDesignThemes" version="2.3.1.953" targetFramework="net461" /> <package id="MaterialDesignThemes" version="2.4.0.1044" targetFramework="net461" />
<package id="MvvmLightLibs" version="5.3.0.0" targetFramework="net461" /> <package id="MvvmLightLibs" version="5.4.1" targetFramework="net461" />
<package id="Tyrrrz.Extensions" version="1.5.0" targetFramework="net461" /> <package id="Tyrrrz.Extensions" version="1.5.0" targetFramework="net461" />
</packages> </packages>
Loading…
Cancel
Save