22using System . IO . Compression ;
33using System . Net ;
44
5+ using log4net ;
6+
57using Microsoft . VisualBasic . FileIO ;
68
79using Newtonsoft . Json ;
@@ -12,6 +14,11 @@ namespace Nullinside.Api.Common.Desktop;
1214/// Handles checking for updates to the application via GitHub releases.
1315/// </summary>
1416public static class GitHubUpdateManager {
17+ /// <summary>
18+ /// The logger.
19+ /// </summary>
20+ private static readonly ILog Log = LogManager . GetLogger ( typeof ( GitHubUpdateManager ) ) ;
21+
1522 /// <summary>
1623 /// Gets the latest version number of the release.
1724 /// </summary>
@@ -45,60 +52,75 @@ public static class GitHubUpdateManager {
4552 /// Prepares to update this application before this application is closed.
4653 /// </summary>
4754 public static async Task PrepareUpdate ( ) {
48- // To prepare the update, we just need to back up our files
49- string ourFolder = Path . GetDirectoryName ( typeof ( GitHubUpdateManager ) . Assembly . Location ) ?? "" ;
50- string backupFolder = Path . Combine ( ourFolder , ".." , "backup" ) ;
51- await DeleteFolderRetry ( backupFolder ) ;
52-
53- Directory . CreateDirectory ( backupFolder ) ;
54- FileSystem . CopyDirectory ( ourFolder , backupFolder ) ;
55+ try {
56+ // To prepare the update, we just need to back up our files
57+ string ourFolder = Path . GetDirectoryName ( typeof ( GitHubUpdateManager ) . Assembly . Location ) ?? "" ;
58+ string backupFolder = Path . Combine ( ourFolder , ".." , "backup" ) ;
59+ await DeleteFolderRetry ( backupFolder ) ;
60+
61+ Directory . CreateDirectory ( backupFolder ) ;
62+ FileSystem . CopyDirectory ( ourFolder , backupFolder ) ;
63+ }
64+ catch ( Exception ex ) {
65+ Log . Error ( ex ) ;
66+ }
5567 }
5668
5769 /// <summary>
5870 /// Runs this application from the backup folder to initiate the update on the installed folder.
5971 /// </summary>
6072 public static void ExitApplicationToUpdate ( ) {
61- // Since we have a backup folder from PrepareUpdate() we can just run the backup executable
62- string ourFolder = Path . GetDirectoryName ( typeof ( GitHubUpdateManager ) . Assembly . Location ) ?? "" ;
63- string backupFolder = Path . Combine ( ourFolder , ".." , "backup" ) ;
64- if ( ! Directory . Exists ( backupFolder ) ) {
65- return ;
66- }
73+ try {
74+ // Since we have a backup folder from PrepareUpdate() we can just run the backup executable
75+ string ourFolder = Path . GetDirectoryName ( typeof ( GitHubUpdateManager ) . Assembly . Location ) ?? "" ;
76+ string backupFolder = Path . Combine ( ourFolder , ".." , "backup" ) ;
77+ if ( ! Directory . Exists ( backupFolder ) ) {
78+ return ;
79+ }
6780
68- string ourExecutable = $ "{ AppDomain . CurrentDomain . FriendlyName } .exe";
81+ string ourExecutable = $ "{ AppDomain . CurrentDomain . FriendlyName } .exe";
6982
70- // we must pass the installation folder to the executable so it knows where to install
71- Process . Start ( Path . Combine ( backupFolder , ourExecutable ) , $ "--update \" { ourFolder } \" ") ;
72- Environment . Exit ( 0 ) ;
83+ // we must pass the installation folder to the executable so it knows where to install
84+ Process . Start ( Path . Combine ( backupFolder , ourExecutable ) , $ "--update \" { ourFolder } \" ") ;
85+ Environment . Exit ( 0 ) ;
86+ }
87+ catch ( Exception ex ) {
88+ Log . Error ( ex ) ;
89+ }
7390 }
7491
7592 /// <summary>
7693 /// Performs the application update. This involves downloading the latest release from GitHub, extracting its contents
7794 /// to the installation folder, and closing the currently running application while running the new one.
7895 /// </summary>
7996 public static async Task PerformUpdateAndRestart ( string owner , string repo , string installFolder , string assetName ) {
80- // Delete the old install folder.
81- await DeleteFolderContentsRetry ( installFolder ) ;
82-
83- // Get the latest version of the application from GitHub.
84- string ourFolder = Path . GetDirectoryName ( typeof ( GitHubUpdateManager ) . Assembly . Location ) ?? "" ;
85- string zipLocation = Path . Combine ( ourFolder , assetName ) ;
86- GithubLatestReleaseJson ? latestVersion = await GetLatestVersion ( owner , repo ) ;
87- using ( var client = new HttpClient ( ) ) {
88- using HttpResponseMessage response = await client . GetAsync ( $ "https://github.com/{ owner } /{ repo } /releases/download/{ latestVersion ? . name } /{ assetName } ") ;
89- await using Stream streamToReadFrom = await response . Content . ReadAsStreamAsync ( ) ;
90- await using var fileStream = new FileStream ( zipLocation , FileMode . Create ) ;
91- await streamToReadFrom . CopyToAsync ( fileStream ) ;
92- }
97+ try {
98+ // Delete the old install folder.
99+ await DeleteFolderContentsRetry ( installFolder ) ;
100+
101+ // Get the latest version of the application from GitHub.
102+ string ourFolder = Path . GetDirectoryName ( typeof ( GitHubUpdateManager ) . Assembly . Location ) ?? "" ;
103+ string zipLocation = Path . Combine ( ourFolder , assetName ) ;
104+ GithubLatestReleaseJson ? latestVersion = await GetLatestVersion ( owner , repo ) ;
105+ using ( var client = new HttpClient ( ) ) {
106+ using HttpResponseMessage response = await client . GetAsync ( $ "https://github.com/{ owner } /{ repo } /releases/download/{ latestVersion ? . name } /{ assetName } ") ;
107+ await using Stream streamToReadFrom = await response . Content . ReadAsStreamAsync ( ) ;
108+ await using var fileStream = new FileStream ( zipLocation , FileMode . Create ) ;
109+ await streamToReadFrom . CopyToAsync ( fileStream ) ;
110+ }
93111
94- // Extract the zip file to the installation folder.
95- ZipFile . ExtractToDirectory ( zipLocation , installFolder ) ;
112+ // Extract the zip file to the installation folder.
113+ ZipFile . ExtractToDirectory ( zipLocation , installFolder ) ;
96114
97- // Run the new version of the application.
98- Process . Start ( Path . Combine ( installFolder , $ "{ AppDomain . CurrentDomain . FriendlyName } .exe") , "--justUpdated" ) ;
115+ // Run the new version of the application.
116+ Process . Start ( Path . Combine ( installFolder , $ "{ AppDomain . CurrentDomain . FriendlyName } .exe") , "--justUpdated" ) ;
99117
100- // Close this version of the application.
101- Environment . Exit ( 0 ) ;
118+ // Close this version of the application.
119+ Environment . Exit ( 0 ) ;
120+ }
121+ catch ( Exception ex ) {
122+ Log . Error ( ex ) ;
123+ }
102124 }
103125
104126 /// <summary>
@@ -116,30 +138,40 @@ public static async Task CleanupUpdate() {
116138 /// </summary>
117139 /// <param name="folder">The folder to delete.</param>
118140 private static async Task DeleteFolderRetry ( string folder ) {
119- await Retry . Execute ( ( ) => {
120- if ( Directory . Exists ( folder ) ) {
121- Directory . Delete ( folder , true ) ;
122- }
141+ try {
142+ await Retry . Execute ( ( ) => {
143+ if ( Directory . Exists ( folder ) ) {
144+ Directory . Delete ( folder , true ) ;
145+ }
123146
124- return Task . FromResult ( true ) ;
125- } , 30 , waitTime : TimeSpan . FromSeconds ( 1 ) ) ;
147+ return Task . FromResult ( true ) ;
148+ } , 30 , waitTime : TimeSpan . FromSeconds ( 1 ) ) ;
149+ }
150+ catch ( Exception ex ) {
151+ Log . Error ( ex ) ;
152+ }
126153 }
127154
128155 /// <summary>
129156 /// Retries deleting the contents of a folder multiple times.
130157 /// </summary>
131158 /// <param name="folder">The folder to delete the contents of.</param>
132159 private static async Task DeleteFolderContentsRetry ( string folder ) {
133- await Retry . Execute ( ( ) => {
134- if ( ! Directory . Exists ( folder ) ) {
135- return Task . FromResult ( true ) ;
136- }
160+ try {
161+ await Retry . Execute ( ( ) => {
162+ if ( ! Directory . Exists ( folder ) ) {
163+ return Task . FromResult ( true ) ;
164+ }
137165
138- foreach ( string file in Directory . GetFiles ( folder ) ) {
139- File . Delete ( file ) ;
140- }
166+ foreach ( string file in Directory . GetFiles ( folder ) ) {
167+ File . Delete ( file ) ;
168+ }
141169
142- return Task . FromResult ( true ) ;
143- } , 30 , waitTime : TimeSpan . FromSeconds ( 1 ) ) ;
170+ return Task . FromResult ( true ) ;
171+ } , 30 , waitTime : TimeSpan . FromSeconds ( 1 ) ) ;
172+ }
173+ catch ( Exception ex ) {
174+ Log . Error ( ex ) ;
175+ }
144176 }
145177}
0 commit comments