16
16
using Flow . Launcher . Infrastructure . UserSettings ;
17
17
using Flow . Launcher . Plugin ;
18
18
using System . Text . Json . Serialization ;
19
+ using System . Threading ;
19
20
20
21
namespace Flow . Launcher . Core
21
22
{
@@ -28,21 +29,21 @@ public Updater(string gitHubRepository)
28
29
GitHubRepository = gitHubRepository ;
29
30
}
30
31
31
- public async Task UpdateApp ( IPublicAPI api , bool silentUpdate = true )
32
+ private SemaphoreSlim UpdateLock { get ; } = new SemaphoreSlim ( 1 ) ;
33
+
34
+ public async Task UpdateAppAsync ( IPublicAPI api , bool silentUpdate = true )
32
35
{
36
+ await UpdateLock . WaitAsync ( ) ;
33
37
try
34
38
{
35
- UpdateInfo newUpdateInfo ;
36
-
37
39
if ( ! silentUpdate )
38
40
api . ShowMsg ( api . GetTranslation ( "pleaseWait" ) ,
39
- api . GetTranslation ( "update_flowlauncher_update_check" ) ) ;
41
+ api . GetTranslation ( "update_flowlauncher_update_check" ) ) ;
40
42
41
43
using var updateManager = await GitHubUpdateManager ( GitHubRepository ) . ConfigureAwait ( false ) ;
42
44
43
-
44
45
// UpdateApp CheckForUpdate will return value only if the app is squirrel installed
45
- newUpdateInfo = await updateManager . CheckForUpdate ( ) . NonNull ( ) . ConfigureAwait ( false ) ;
46
+ var newUpdateInfo = await updateManager . CheckForUpdate ( ) . NonNull ( ) . ConfigureAwait ( false ) ;
46
47
47
48
var newReleaseVersion = Version . Parse ( newUpdateInfo . FutureReleaseEntry . Version . ToString ( ) ) ;
48
49
var currentVersion = Version . Parse ( Constant . Version ) ;
@@ -58,7 +59,7 @@ public async Task UpdateApp(IPublicAPI api, bool silentUpdate = true)
58
59
59
60
if ( ! silentUpdate )
60
61
api . ShowMsg ( api . GetTranslation ( "update_flowlauncher_update_found" ) ,
61
- api . GetTranslation ( "update_flowlauncher_updating" ) ) ;
62
+ api . GetTranslation ( "update_flowlauncher_updating" ) ) ;
62
63
63
64
await updateManager . DownloadReleases ( newUpdateInfo . ReleasesToApply ) . ConfigureAwait ( false ) ;
64
65
@@ -70,8 +71,8 @@ public async Task UpdateApp(IPublicAPI api, bool silentUpdate = true)
70
71
FilesFolders . CopyAll ( DataLocation . PortableDataPath , targetDestination ) ;
71
72
if ( ! FilesFolders . VerifyBothFolderFilesEqual ( DataLocation . PortableDataPath , targetDestination ) )
72
73
MessageBox . Show ( string . Format ( api . GetTranslation ( "update_flowlauncher_fail_moving_portable_user_profile_data" ) ,
73
- DataLocation . PortableDataPath ,
74
- targetDestination ) ) ;
74
+ DataLocation . PortableDataPath ,
75
+ targetDestination ) ) ;
75
76
}
76
77
else
77
78
{
@@ -87,12 +88,15 @@ public async Task UpdateApp(IPublicAPI api, bool silentUpdate = true)
87
88
UpdateManager . RestartApp ( Constant . ApplicationFileName ) ;
88
89
}
89
90
}
90
- catch ( Exception e ) when ( e is HttpRequestException || e is WebException || e is SocketException || e . InnerException is TimeoutException )
91
+ catch ( Exception e ) when ( e is HttpRequestException or WebException or SocketException || e . InnerException is TimeoutException )
91
92
{
92
93
Log . Exception ( $ "|Updater.UpdateApp|Check your connection and proxy settings to github-cloud.s3.amazonaws.com.", e ) ;
93
94
api . ShowMsg ( api . GetTranslation ( "update_flowlauncher_fail" ) ,
94
- api . GetTranslation ( "update_flowlauncher_check_connection" ) ) ;
95
- return ;
95
+ api . GetTranslation ( "update_flowlauncher_check_connection" ) ) ;
96
+ }
97
+ finally
98
+ {
99
+ UpdateLock . Release ( ) ;
96
100
}
97
101
}
98
102
@@ -115,13 +119,16 @@ private async Task<UpdateManager> GitHubUpdateManager(string repository)
115
119
var uri = new Uri ( repository ) ;
116
120
var api = $ "https://api.github.com/repos{ uri . AbsolutePath } /releases";
117
121
118
- var jsonStream = await Http . GetStreamAsync ( api ) . ConfigureAwait ( false ) ;
122
+ await using var jsonStream = await Http . GetStreamAsync ( api ) . ConfigureAwait ( false ) ;
119
123
120
124
var releases = await System . Text . Json . JsonSerializer . DeserializeAsync < List < GithubRelease > > ( jsonStream ) . ConfigureAwait ( false ) ;
121
125
var latest = releases . Where ( r => ! r . Prerelease ) . OrderByDescending ( r => r . PublishedAt ) . First ( ) ;
122
126
var latestUrl = latest . HtmlUrl . Replace ( "/tag/" , "/download/" ) ;
123
-
124
- var client = new WebClient { Proxy = Http . WebProxy } ;
127
+
128
+ var client = new WebClient
129
+ {
130
+ Proxy = Http . WebProxy
131
+ } ;
125
132
var downloader = new FileDownloader ( client ) ;
126
133
127
134
var manager = new UpdateManager ( latestUrl , urlDownloader : downloader ) ;
0 commit comments