1
1
using Azure . Functions . Cli . Common ;
2
+ using Azure . Functions . Cli . Extensions ;
2
3
using Colors . Net ;
3
4
using Newtonsoft . Json ;
4
5
using Newtonsoft . Json . Serialization ;
7
8
using System . Collections . Generic ;
8
9
using System . IO ;
9
10
using System . Linq ;
11
+ using System . Net . Http ;
10
12
using System . Reflection ;
11
13
using System . Runtime . InteropServices ;
12
14
using System . Text ;
@@ -17,6 +19,9 @@ namespace Azure.Functions.Cli.Helpers
17
19
{
18
20
internal class VersionHelper
19
21
{
22
+ // Set the CliVersion for testing
23
+ internal static string CliVersion { get ; set ; } = Constants . CliVersion ;
24
+
20
25
public static async Task < string > RunAsync ( Task < bool > isRunningOlderVersion = null )
21
26
{
22
27
isRunningOlderVersion ??= IsRunningAnOlderVersion ( ) ;
@@ -36,39 +41,25 @@ public static async Task<string> RunAsync(Task<bool> isRunningOlderVersion = nul
36
41
// Check that current core tools is the latest version.
37
42
// To ensure that it doesn't block other tasks. The HTTP Request timeout is only 1 second.
38
43
// We simply ingnore the exception if for any reason the check fails.
39
- public static async Task < bool > IsRunningAnOlderVersion ( )
44
+ public static async Task < bool > IsRunningAnOlderVersion ( HttpClient client = null )
40
45
{
41
46
try
42
47
{
43
- var client = new System . Net . Http . HttpClient
44
- {
45
- Timeout = TimeSpan . FromSeconds ( 1 )
46
- } ;
47
- var response = await client . GetAsync ( Constants . CoreToolsVersionsFeedUrl ) ;
48
- var content = await response . Content . ReadAsStringAsync ( ) ;
49
- var data = JsonConvert . DeserializeObject < CliFeed > ( content ) ;
50
- IEnumerable releases = ( ( IEnumerable ) data . Releases ) ;
51
- var releaseList = new List < ReleaseSummary > ( ) ;
52
- foreach ( var item in releases )
48
+ using ( client ??= new HttpClient ( ) )
53
49
{
54
- var jProperty = ( Newtonsoft . Json . Linq . JProperty ) item ;
55
- var releaseDetail = JsonConvert . DeserializeObject < ReleaseDetail > ( jProperty . Value . ToString ( ) ) ;
56
- releaseList . Add ( new ReleaseSummary ( ) { Release = jProperty . Name , ReleaseDetail = releaseDetail . ReleaseList . FirstOrDefault ( ) } ) ;
57
- }
50
+ client . DefaultRequestHeaders . Add ( "User-Agent" , "AzureFunctionsCoreToolsClient" ) ;
58
51
59
- var latestCoreToolsReleaseVersion = releaseList . FirstOrDefault ( x => x . Release == data . Tags . V4Release . ReleaseVersion ) ? . CoreToolsReleaseNumber ;
52
+ var response = await client . GetAsync ( Constants . GitHubReleaseApiUrl ) ;
53
+ response . EnsureSuccessStatusCode ( ) ;
60
54
61
- if ( ! string . IsNullOrEmpty ( latestCoreToolsReleaseVersion ) &&
62
- Constants . CliVersion != latestCoreToolsReleaseVersion )
63
- {
64
- return true ;
65
- }
55
+ var content = await response . Content . ReadAsStringAsync ( ) ;
56
+ var release = JsonConvert . DeserializeObject < GitHubRelease > ( content ) ;
66
57
67
- return false ;
58
+ return ! release . TagName . EqualsIgnoreCase ( CliVersion ) ;
59
+ }
68
60
}
69
61
catch ( Exception )
70
62
{
71
- // ignore exception and no warning when the check fails.
72
63
return false ;
73
64
}
74
65
}
@@ -123,91 +114,10 @@ private static async Task<string> GetMultipleInstallationMessage(bool isRunningO
123
114
return string . Empty ;
124
115
}
125
116
126
- private class CliFeed
127
- {
128
- [ JsonProperty ( "tags" ) ]
129
- public Tags Tags { get ; set ; }
130
-
131
- [ JsonProperty ( "releases" ) ]
132
- public Object Releases { get ; set ; }
133
- }
134
-
135
- private class Tags
136
- {
137
- [ JsonProperty ( "v4" ) ]
138
- public Release V4Release { get ; set ; }
139
-
140
- [ JsonProperty ( "v4-prerelease" ) ]
141
- public Release V4PreRelease { get ; set ; }
142
-
143
- [ JsonProperty ( "v3" ) ]
144
- public Release V3Release { get ; set ; }
145
-
146
- [ JsonProperty ( "v3-prerelease" ) ]
147
- public Release V3PreRelease { get ; set ; }
148
- }
149
-
150
- private class Release
151
- {
152
- [ JsonProperty ( "release" ) ]
153
- public string ReleaseVersion { get ; set ; }
154
-
155
- [ JsonProperty ( "releaseQuality" ) ]
156
- public string ReleaseQuality { get ; set ; }
157
-
158
- [ JsonProperty ( "hidden" ) ]
159
- public bool Hidden { get ; set ; }
160
- }
161
-
162
- private class ReleaseSummary
117
+ private class GitHubRelease
163
118
{
164
- public string Release { get ; set ; }
165
-
166
- public string CoreToolsReleaseNumber
167
- {
168
- get
169
- {
170
- var downloadLink = ReleaseDetail ? . DownloadLink ;
171
- if ( string . IsNullOrEmpty ( ReleaseDetail ? . DownloadLink ) )
172
- {
173
- return string . Empty ;
174
- }
175
-
176
- Uri uri = new UriBuilder ( ReleaseDetail ? . DownloadLink ) . Uri ;
177
-
178
- if ( uri . Segments . Length < 4 )
179
- {
180
- return string . Empty ;
181
- }
182
-
183
- return uri . Segments [ 2 ] . Replace ( "/" , string . Empty ) ;
184
- }
185
- }
186
- public CoreToolsRelease ReleaseDetail { get ; set ; }
187
- }
188
-
189
- private class ReleaseDetail
190
- {
191
- [ JsonProperty ( "coreTools" ) ]
192
- public IList < CoreToolsRelease > ReleaseList { get ; set ; }
193
- }
194
-
195
- private class CoreToolsRelease
196
- {
197
- [ JsonProperty ( "OS" ) ]
198
- public string Os { get ; set ; }
199
-
200
- [ JsonProperty ( "Architecture" ) ]
201
- public string Architecture { get ; set ; }
202
-
203
- [ JsonProperty ( "downloadLink" ) ]
204
- public string DownloadLink { get ; set ; }
205
-
206
- [ JsonProperty ( "size" ) ]
207
- public string Size { get ; set ; }
208
-
209
- [ JsonProperty ( "default" ) ]
210
- public bool Default { get ; set ; }
119
+ [ JsonProperty ( "tag_name" ) ]
120
+ public string TagName { get ; set ; }
211
121
}
212
122
}
213
123
0 commit comments