Skip to content

Commit 6d73b81

Browse files
committed
Update for AutoUpdate
1 parent 97d9463 commit 6d73b81

File tree

5 files changed

+88
-38
lines changed

5 files changed

+88
-38
lines changed

Source/VisualPairCoding/VisualPairCoding.AvaloniaUI/EnterNamesWindow.axaml.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Threading.Tasks;
66
using VisualPairCoding.BL;
7+
using VisualPairCoding.BL.AutoUpdates;
78
using VisualPairCoding.Infrastructure;
89

910
namespace VisualPairCoding.AvaloniaUI
@@ -16,6 +17,7 @@ public EnterNamesForm()
1617
{
1718
InitializeComponent();
1819
WindowStartupLocation = WindowStartupLocation.CenterScreen;
20+
Opened += OnActivated;
1921
}
2022

2123
public EnterNamesForm(bool autostart)
@@ -26,8 +28,37 @@ public EnterNamesForm(bool autostart)
2628
Opened += OnActivated;
2729
}
2830

29-
private void OnActivated(object? sender, EventArgs e)
31+
private async Task<bool> AutoUpdate()
32+
{
33+
var updater = new AutoUpdater(
34+
"VisualPairCoding",
35+
VersionInformation.Version,
36+
"https://api.github.com/repos/CleverCodeCravers/VisualPairCoding/releases");
37+
38+
if (updater.IsUpdateAvailable())
39+
{
40+
var messageBox = MessageBox.Avalonia.MessageBoxManager.GetMessageBoxStandardWindow("Info", "There is a new update, do you want to install it now ?", MessageBox.Avalonia.Enums.ButtonEnum.YesNo, MessageBox.Avalonia.Enums.Icon.Info, WindowStartupLocation.CenterScreen);
41+
var result = await messageBox.Show();
42+
43+
if (result.ToString() == "Yes")
44+
{
45+
updater.Update();
46+
Close();
47+
return true;
48+
}
49+
50+
}
51+
52+
return false;
53+
}
54+
55+
private async void OnActivated(object? sender, EventArgs e)
3056
{
57+
//Only perform auto - updates if not in dev environment
58+
if (AutoUpdateDetector.isUpdateAvailable())
59+
{
60+
await AutoUpdate();
61+
}
3162

3263
if (_autostart)
3364
{

Source/VisualPairCoding/VisualPairCoding.AvaloniaUI/Program.cs

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,32 @@ static void AppMain(Application app, string[] args)
2525
{
2626
app.Initialize();
2727

28-
// Only perform auto-updates if not in dev environment
29-
//if (VersionInformation.Version != "$$VERSION$$")
30-
//{
31-
// AutoUpdate();
32-
//}
28+
var enterNamesForm = new EnterNamesForm();
3329

34-
if (args.Length >= 1)
30+
if (IsAutoUpdateShouldBeExecutedExplicitlySet(args))
31+
{
32+
AutoUpdateDetector.setUpdateIsAvailable();
33+
}
34+
35+
if (IsStartupWithSessionFile(args))
3536
{
3637
var configPath = args[0];
37-
var form = new EnterNamesForm(true);
38-
form.LoadSessionIntoGui(configPath);
39-
app.Run(form);
40-
return;
38+
39+
enterNamesForm = new EnterNamesForm(true);
40+
enterNamesForm.LoadSessionIntoGui(configPath);
4141
}
4242

43-
app.Run(new EnterNamesForm());
43+
app.Run(enterNamesForm);
44+
}
45+
46+
private static bool IsStartupWithSessionFile(string[] args)
47+
{
48+
return (args.Length >= 1 && args[0] != "AutoUpdate");
49+
}
50+
51+
private static bool IsAutoUpdateShouldBeExecutedExplicitlySet(string[] args)
52+
{
53+
return (args.Length >= 1 && args[0] == "AutoUpdate");
4454
}
4555

4656
public static AppBuilder BuildAvaloniaApp()
@@ -50,28 +60,5 @@ public static AppBuilder BuildAvaloniaApp()
5060
.LogToTrace();
5161
}
5262

53-
54-
private static bool AutoUpdate()
55-
{
56-
var updater = new AutoUpdater(
57-
"VisualPairCoding",
58-
VersionInformation.Version,
59-
"https://api.github.com/repos/CleverCodeCravers/VisualPairCoding/releases");
60-
61-
if (updater.IsUpdateAvailable())
62-
{
63-
var messageBox = MessageBox.Avalonia.MessageBoxManager.GetMessageBoxStandardWindow("Info", "There is a new update, do you want to install it now ?", MessageBox.Avalonia.Enums.ButtonEnum.YesNo, MessageBox.Avalonia.Enums.Icon.Info, WindowStartupLocation.CenterScreen);
64-
var result = messageBox.Show();
65-
66-
if (result.ToString() == "Yes")
67-
{
68-
updater.Update();
69-
return true;
70-
}
71-
72-
}
73-
74-
return false;
75-
}
7663
}
7764
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"profiles": {
3+
"VisualPairCoding.AvaloniaUI": {
4+
"commandName": "Project",
5+
"commandLineArgs": "AutoUpdate"
6+
}
7+
}
8+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace VisualPairCoding.BL.AutoUpdates
2+
{
3+
public static class AutoUpdateDetector
4+
{
5+
private static bool explicitUpdate = false;
6+
7+
public static void setUpdateIsAvailable()
8+
{
9+
explicitUpdate = true;
10+
}
11+
public static bool isUpdateAvailable()
12+
{
13+
if (explicitUpdate) return true;
14+
return VersionInformation.Version != "$$VERSION$$";
15+
}
16+
}
17+
}

Source/VisualPairCoding/VisualPairCoding.Infrastructure/AutoUpdater.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,24 @@ public void Update()
4343

4444
using (WebClient downloadClient = new())
4545
{
46-
downloadClient.DownloadFile(releases[0].Assets[0].Browser_download_url, releases[0].Assets[0].Name);
46+
var asset = releases[0].Assets.FirstOrDefault(x => x.Name.Contains("win-x64"));
47+
48+
if (asset == null) {
49+
throw new Exception("Update not found");
50+
}
51+
52+
downloadClient.DownloadFile(asset.Browser_download_url, asset.Name);
4753
}
4854

4955
var cwd = Environment.CurrentDirectory;
5056
string path = cwd + "\\" + "updater.ps1";
5157

5258
var script =
5359
@"
60+
Write-Host ""Update is executed, please wait a second...""
61+
Start-Sleep -Seconds 1
5462
Set-Location $PSScriptRoot
5563
$ErrorActionPreference = ""Stop""
56-
5764
Start-Sleep -Seconds 5
5865
5966
$Successful = $false
@@ -67,9 +74,9 @@ public void Update()
6774
}
6875
}
6976
70-
Start-Process ""VisualPairCoding.WinForms.exe""
7177
Remove-Item -Path ""$pwd\VisualPairCoding-win-x64.zip"" -Force
7278
Remove-Item -Path ""$pwd\updater.ps1"" -Force
79+
Start-Process ""$pwd\VisualPairCoding.AvaloniaUI.exe""
7380
";
7481

7582
if (!File.Exists(path))

0 commit comments

Comments
 (0)