Skip to content

Commit 5a58223

Browse files
committed
Automatic Update
1 parent 97a9ff4 commit 5a58223

File tree

14 files changed

+118
-46
lines changed

14 files changed

+118
-46
lines changed

DataCommander.Providers/AboutForm.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public AboutForm()
3535
<br/>
3636
<br/>
3737
Version: {assembly.GetName().Version}
38+
<br/>
39+
<br/>
3840
Build date: {lastWriteTime.ToString("yyyy-MM-dd")}
3941
<br/>
4042
<br/>

DataCommander.Updater/Program.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Diagnostics;
32
using System.Windows.Forms;
43
using Foundation.Deployment;
54

@@ -12,8 +11,6 @@ static void Main(string[] args)
1211
{
1312
try
1413
{
15-
Debugger.Launch();
16-
1714
var applicationExeFileName = args[0];
1815
var updaterDirectory = Environment.CurrentDirectory;
1916
DeploymentHelper.Update(updaterDirectory, applicationExeFileName);

DataCommander/DataCommander.csproj

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<DelaySign>false</DelaySign>
2020
<OutputType>WinExe</OutputType>
2121
<RootNamespace>DataCommander</RootNamespace>
22-
<RunPostBuildEvent>OnBuildSuccess</RunPostBuildEvent>
22+
<RunPostBuildEvent>OnOutputUpdated</RunPostBuildEvent>
2323
<StartupObject>
2424
</StartupObject>
2525
<FileUpgradeFlags>
@@ -171,12 +171,12 @@
171171
<Reference Include="System.Xml" />
172172
</ItemGroup>
173173
<ItemGroup>
174-
<Compile Include="Update\Events\CheckForUpdateCompleted.cs" />
175174
<Compile Include="Update\Events\CheckForUpdatesStarted.cs" />
176175
<Compile Include="Update\Events\DownloadingNewVersionStarted.cs" />
177176
<Compile Include="Update\Events\DownloadProgressChanged.cs" />
178177
<Compile Include="Update\Events\Event.cs" />
179178
<Compile Include="Update\EventHandler.cs" />
179+
<Compile Include="Update\Events\ExceptionOccured.cs" />
180180
<Compile Include="Update\Events\NewVersionDownloaded.cs" />
181181
<Compile Include="Update\Updater.cs" />
182182
<Compile Include="Update\UpdaterForm.cs">
@@ -237,7 +237,20 @@
237237
<PropertyGroup>
238238
<PreBuildEvent>
239239
</PreBuildEvent>
240-
<PostBuildEvent>
241-
</PostBuildEvent>
240+
<PostBuildEvent>rd /S /Q cs
241+
rd /S /Q de
242+
rd /S /Q es
243+
rd /S /Q fr
244+
rd /S /Q it
245+
rd /S /Q ja
246+
rd /S /Q ko
247+
rd /S /Q pl
248+
rd /S /Q pt-BR
249+
rd /S /Q ru
250+
rd /S /Q tr
251+
rd /S /Q zh-Hans
252+
rd /S /Q zh-Hant
253+
del System.*.dll
254+
del System.*.pdb</PostBuildEvent>
242255
</PropertyGroup>
243256
</Project>

DataCommander/EntryPoint.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ public static void Main()
1818
{
1919
try
2020
{
21-
//var updateStarted = Update();
22-
var updateStarted = false;
21+
var updateStarted = Update();
2322
if (!updateStarted)
2423
{
2524
LogFactory.Read();

DataCommander/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Runtime.InteropServices;
44

55
[assembly: AssemblyTitle("Data Commander")]
6-
[assembly: AssemblyVersion("1.0.0.0")]
6+
[assembly: AssemblyVersion("1.0.1.0")]
77
[assembly: AssemblyCopyright("Copyright © 2002-2018 Csaba Bernáth")]
88
[assembly: AssemblyCompany("")]
99
[assembly: AssemblyProduct("Data Commander")]

DataCommander/Update/EventHandler.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,14 @@ internal sealed class EventHandler
1414
private void Handle(DownloadingNewVersionStarted @event) => _updaterForm.Invoke(() =>
1515
{
1616
_updaterForm.WindowState = FormWindowState.Normal;
17-
_updaterForm.Log("Downloading new version...");
17+
_updaterForm.Log($"Downloading new version {@event.Version}...");
1818
});
1919

2020
private void Handle(DownloadProgressChanged @event) =>
2121
_updaterForm.Invoke(() => _updaterForm.Log($"{@event.DownloadProgressChangedEventArgs.ProgressPercentage}% complete."));
2222

2323
private void Handle(NewVersionDownloaded @event) => _updaterForm.Invoke(() => _updaterForm.Log("New version downloaded."));
2424

25-
private void Handle(CheckForUpdateCompleted @event) => _updaterForm.Invoke(() =>
26-
{
27-
_updaterForm.Log("Checking for updates completed.");
28-
_updaterForm.Close();
29-
});
25+
private void Handle(ExceptionOccured @event) => _updaterForm.Invoke(() => _updaterForm.Log(@event.Exception.ToString()));
3026
}
3127
}

DataCommander/Update/Events/CheckForUpdateCompleted.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
namespace DataCommander.Update.Events
1+
using System;
2+
3+
namespace DataCommander.Update.Events
24
{
35
public sealed class DownloadingNewVersionStarted : Event
46
{
7+
public readonly Version Version;
8+
9+
public DownloadingNewVersionStarted(Version version)
10+
{
11+
Version = version;
12+
}
513
}
614
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace DataCommander.Update.Events
4+
{
5+
public sealed class ExceptionOccured : Event
6+
{
7+
public readonly Exception Exception;
8+
9+
public ExceptionOccured(Exception exception)
10+
{
11+
Exception = exception;
12+
}
13+
}
14+
}

DataCommander/Update/Updater.cs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.IO;
3+
using System.Linq;
34
using System.Reflection;
45
using System.Threading.Tasks;
6+
using System.Windows.Forms;
57
using DataCommander.Update.Events;
68
using Foundation;
79
using Foundation.Deployment;
@@ -10,29 +12,33 @@ namespace DataCommander.Update
1012
{
1113
public sealed class Updater
1214
{
13-
private const string ApplicationName = "DataCommander";
1415
private readonly Action<Event> _eventPublisher;
1516
private bool _updateStarted;
1617
public bool UpdateStarted => _updateStarted;
1718
public Updater(Action<Event> eventHandler) => _eventPublisher = eventHandler;
1819

1920
public Task Update()
2021
{
21-
var command = DeploymentCommandRepository.Get(ApplicationName);
22+
var entryAsembly = Assembly.GetEntryAssembly();
23+
var title = entryAsembly.GetCustomAttributes().OfType<AssemblyTitleAttribute>().First().Title;
24+
var applicationName = title;
25+
26+
var command = DeploymentCommandRepository.Get(applicationName);
2227
return Handle((dynamic) command);
2328
}
2429

2530
private async Task Handle(CheckForUpdates checkForUpdates)
2631
{
2732
if (checkForUpdates.When <= UniversalTime.Default.UtcNow)
2833
{
29-
var localVersion = Assembly.GetEntryAssembly().GetName().Version;
34+
var entryAssembly = Assembly.GetEntryAssembly();
35+
var localVersion = entryAssembly.GetName().Version;
3036
var remoteVersionUri = new Uri("https://raw.githubusercontent.com/csbernath/DataCommander/master/Version.txt");
3137
_eventPublisher(new CheckForUpdatesStarted());
3238
var remoteVersion = await DeploymentHelper.GetRemoteVersion(remoteVersionUri);
3339
if (localVersion < remoteVersion)
3440
{
35-
_eventPublisher(new DownloadingNewVersionStarted());
41+
_eventPublisher(new DownloadingNewVersionStarted(remoteVersion));
3642
var address = new Uri($"https://github.com/csbernath/DataCommander/releases/download/{remoteVersion}/DataCommander.Updater.zip");
3743
var guid = Guid.NewGuid();
3844
var updaterDirectory = Path.Combine(Path.GetTempPath(), guid.ToString());
@@ -43,24 +49,37 @@ await DeploymentHelper.DownloadUpdater(address, updaterDirectory, zipFileName,
4349
DeploymentHelper.ExtractZip(zipFileName, updaterDirectory);
4450

4551
var updaterExeFileName = Path.Combine(updaterDirectory, "DataCommander.Updater.exe");
46-
var applicationExeFileName = Assembly.GetEntryAssembly().Location;
52+
var applicationExeFileName = entryAssembly.Location;
4753
DeploymentHelper.StartUpdater(updaterExeFileName, applicationExeFileName);
4854
_updateStarted = true;
4955
}
5056
else
51-
{
52-
var now = UniversalTime.Default.UtcNow;
53-
var when = now.AddDays(1);
54-
DeploymentCommandRepository.Save(ApplicationName, new CheckForUpdates {When = when});
55-
}
57+
ScheduleCheckForUpdates();
5658
}
59+
}
5760

58-
_eventPublisher(new CheckForUpdateCompleted());
61+
private static void ScheduleCheckForUpdates()
62+
{
63+
var entryAsembly = Assembly.GetEntryAssembly();
64+
var title = entryAsembly.GetCustomAttributes().OfType<AssemblyTitleAttribute>().First().Title;
65+
var applicationName = title;
66+
var now = UniversalTime.Default.UtcNow;
67+
var tomorrow = now.AddDays(1);
68+
DeploymentCommandRepository.Save(applicationName, new CheckForUpdates(tomorrow));
5969
}
6070

6171
private Task Handle(DeleteUpdater deleteUpdater)
6272
{
63-
DeploymentHelper.DeleteUpdater(deleteUpdater.Directory);
73+
try
74+
{
75+
DeploymentHelper.DeleteUpdater(deleteUpdater.Directory);
76+
}
77+
catch (Exception e)
78+
{
79+
MessageBox.Show(e.ToString());
80+
}
81+
82+
ScheduleCheckForUpdates();
6483
return Task.CompletedTask;
6584
}
6685
}

DataCommander/Update/UpdaterForm.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Windows.Forms;
33
using Foundation;
4+
using Foundation.Windows.Forms;
45

56
namespace DataCommander.Update
67
{
@@ -15,19 +16,25 @@ public UpdaterForm()
1516

1617
public Updater Updater => _updater;
1718

18-
protected override void OnShown(EventArgs e)
19+
protected override void OnLoad(EventArgs e)
1920
{
20-
base.OnShown(e);
21+
base.OnLoad(e);
2122

2223
var eventHandler = new EventHandler(this);
2324
_updater = new Updater(eventHandler.Handle);
24-
_updater.Update();
25+
_updater.Update().ContinueWith(task =>
26+
{
27+
if (task.IsFaulted)
28+
MessageBox.Show(task.Exception.ToString());
29+
30+
this.Invoke(Close);
31+
});
2532
}
2633

2734
public void Log(string message)
2835
{
29-
richTextBox.AppendText($"[{LocalTime.Default.Now.ToString("HH:mm:ss.fff")}] {message}\r\n");
30-
Application.DoEvents();
36+
var time = LocalTime.Default.Now.ToString("HH:mm:ss.fff");
37+
richTextBox.AppendText($"[{time}] {message}\r\n");
3138
}
3239
}
3340
}

Foundation.NetStandard-2.0/Deployment/CheckForUpdates.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@ namespace Foundation.Deployment
77
public class CheckForUpdates : DeploymentCommand
88
{
99
[DataMember] public DateTime When;
10+
11+
public CheckForUpdates(DateTime @when)
12+
{
13+
When = when;
14+
}
1015
}
1116
}

Foundation.NetStandard-2.0/Deployment/DeploymentCommandRepository.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ public static DeploymentCommand Get(string applicationName)
1717
deploymentCommand = DataContractJsonSerialization.Deserialize<DeploymentCommand>(json);
1818
}
1919
else
20-
deploymentCommand = new CheckForUpdates {When = UniversalTime.Default.UtcNow};
20+
{
21+
var now = UniversalTime.Default.UtcNow;
22+
deploymentCommand = new CheckForUpdates(now);
23+
}
2124

2225
return deploymentCommand;
2326
}

Foundation.NetStandard-2.0/Deployment/DeploymentHelper.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Foundation.Deployment
99
{
1010
public static class DeploymentHelper
1111
{
12-
private const string ApplicationName = "DataCommander";
12+
private const string ApplicationName = "Data Commander";
1313

1414
public static async Task<Version> GetRemoteVersion(Uri address)
1515
{
@@ -72,11 +72,26 @@ public static void Update(string updaterDirectory, string applicationExeFileName
7272
var applicationDirectory = Path.GetDirectoryName(applicationExeFileName);
7373
var backupDirectory = $"{applicationDirectory}.Backup";
7474

75-
Directory.Move(applicationDirectory, backupDirectory);
76-
Directory.Move(updaterDirectory, applicationDirectory);
77-
Directory.Delete(backupDirectory);
75+
while (true)
76+
{
77+
try
78+
{
79+
Directory.Move(applicationDirectory, backupDirectory);
80+
break;
81+
}
82+
catch (Exception e)
83+
{
84+
System.Threading.Thread.Sleep(1000);
85+
}
86+
}
87+
88+
var applicationName = Path.GetFileName(applicationDirectory);
89+
var sourceDirectory = Path.Combine(updaterDirectory, applicationName);
90+
91+
Directory.Move(sourceDirectory, applicationDirectory);
92+
Directory.Delete(backupDirectory, true);
7893

79-
DeploymentCommandRepository.Save(ApplicationName, new DeleteUpdater { Directory = updaterDirectory });
94+
DeploymentCommandRepository.Save(applicationName, new DeleteUpdater {Directory = updaterDirectory});
8095

8196
var processStartInfo = new ProcessStartInfo();
8297
processStartInfo.WorkingDirectory = applicationDirectory;

0 commit comments

Comments
 (0)