1
- // Licensed to the .NET Foundation under one or more agreements.
1
+ // Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
4
using System ;
@@ -16,6 +16,9 @@ namespace nanoFramework.nanoCLR.CLI
16
16
public static class ClrInstanceOperationsProcessor
17
17
{
18
18
private const string _cloudSmithApiUrl = "https://api.cloudsmith.io/v1/packages/net-nanoframework/" ;
19
+ private const string _refTargetsDevRepo = "nanoframework-images-dev" ;
20
+ private const string _refTargetsStableRepo = "nanoframework-images" ;
21
+
19
22
private static HttpClient _httpClient = new HttpClient ( ) ;
20
23
21
24
public static int ProcessVerb ( ClrInstanceOperationsOptions options )
@@ -52,6 +55,7 @@ public static int ProcessVerb(ClrInstanceOperationsOptions options)
52
55
return ( int ) UpdateNanoCLRAsync (
53
56
options . TargetVersion ,
54
57
hostBuilder . GetCLRVersion ( ) ,
58
+ options . CheckPreviewVersions ,
55
59
hostBuilder ) . Result ;
56
60
}
57
61
else if ( options . GetCLRVersion || options . GetNativeAssemblies )
@@ -110,12 +114,13 @@ orderby na.Name
110
114
private static async Task < ExitCode > UpdateNanoCLRAsync (
111
115
string targetVersion ,
112
116
string currentVersion ,
117
+ bool usePreview ,
113
118
nanoCLRHostBuilder hostBuilder )
114
119
{
115
120
try
116
121
{
117
122
// compose current version
118
- // need to get rid of git hub has , in case it has one
123
+ // need to get rid of GitHub hash , in case it has one
119
124
if ( string . IsNullOrEmpty ( currentVersion ) )
120
125
{
121
126
currentVersion = "0.0.0.0" ;
@@ -127,7 +132,7 @@ private static async Task<ExitCode> UpdateNanoCLRAsync(
127
132
currentVersion . IndexOf ( "+" ) < 0 ? currentVersion . Length : currentVersion . IndexOf ( "+" ) ) ;
128
133
}
129
134
130
- Version version = Version . Parse ( currentVersion ) ;
135
+ Version installedVersion = Version . Parse ( currentVersion ) ;
131
136
132
137
string nanoClrDllLocation = Path . Combine (
133
138
Path . GetDirectoryName ( Assembly . GetExecutingAssembly ( ) . Location ) ,
@@ -137,43 +142,62 @@ private static async Task<ExitCode> UpdateNanoCLRAsync(
137
142
_httpClient . BaseAddress = new Uri ( _cloudSmithApiUrl ) ;
138
143
_httpClient . DefaultRequestHeaders . Add ( "Accept" , "*/*" ) ;
139
144
140
- // get latest version available for download
141
- HttpResponseMessage response = await _httpClient . GetAsync ( $ "nanoframework-images/?query=name:^WIN_DLL_nanoCLR version:^latest$" ) ;
142
- string responseBody = await response . Content . ReadAsStringAsync ( ) ;
145
+ string repoName = usePreview ? _refTargetsDevRepo : _refTargetsStableRepo ;
146
+ List < CloudsmithPackageInfo > packageInfo ;
147
+ string responseBody ;
143
148
144
- if ( responseBody == "[]" )
149
+ if ( string . IsNullOrEmpty ( targetVersion ) )
145
150
{
146
- Console . WriteLine ( $ "Error getting latest nanoCLR version.") ;
147
- return ExitCode . E9005 ;
151
+ // no specific version requested, get latest version available for download
152
+ HttpResponseMessage response = await _httpClient . GetAsync ( $ "{ repoName } /?query=name:^WIN_DLL_nanoCLR version:^latest$") ;
153
+
154
+ responseBody = await response . Content . ReadAsStringAsync ( ) ;
155
+
156
+ if ( responseBody == "[]" )
157
+ {
158
+ Console . WriteLine ( $ "Error getting latest available nanoCLR package.") ;
159
+ return ExitCode . E9005 ;
160
+ }
148
161
}
162
+ else
163
+ {
164
+ // specific version requested, get details for that version
165
+ HttpResponseMessage response = await _httpClient . GetAsync ( $ "{ repoName } /?query=name:^WIN_DLL_nanoCLR version:{ targetVersion } ") ;
149
166
150
- var packageInfo = JsonSerializer . Deserialize < List < CloudsmithPackageInfo > > ( responseBody ) ;
167
+ responseBody = await response . Content . ReadAsStringAsync ( ) ;
168
+
169
+ if ( responseBody == "[]" )
170
+ {
171
+ Console . WriteLine ( $ "Error getting package details for v{ targetVersion } .") ;
172
+ return ExitCode . E9005 ;
173
+ }
174
+ }
175
+
176
+ // parse the response
177
+ packageInfo = JsonSerializer . Deserialize < List < CloudsmithPackageInfo > > ( responseBody ) ;
151
178
152
179
if ( packageInfo . Count != 1 )
153
180
{
154
- Console . WriteLine ( $ "Error parsing latest nanoCLR version.") ;
181
+ Console . WriteLine ( $ "Error parsing nanoCLR version from package details .") ;
155
182
return ExitCode . E9005 ;
156
183
}
157
184
else
158
185
{
159
- Version latestFwVersion = Version . Parse ( packageInfo [ 0 ] . Version ) ;
186
+ Version availableFwVersion = Version . Parse ( packageInfo [ 0 ] . Version ) ;
160
187
161
- if ( latestFwVersion < version )
162
- {
163
- Console . WriteLine ( $ "Current version { version } lower than available version { packageInfo [ 0 ] . Version } ") ;
164
- }
165
- else if ( latestFwVersion > version
166
- || ( ! string . IsNullOrEmpty ( targetVersion )
167
- && ( Version . Parse ( targetVersion ) > Version . Parse ( currentVersion ) ) ) )
188
+ // update if the version is different from the installed one (either the requested target version or the latest available in the repo)
189
+ if ( ( ! string . IsNullOrEmpty ( targetVersion )
190
+ && ( Version . Parse ( targetVersion ) != installedVersion ) )
191
+ || ( availableFwVersion > installedVersion ) )
168
192
{
169
- response = await _httpClient . GetAsync ( packageInfo [ 0 ] . DownloadUrl ) ;
193
+ HttpResponseMessage response = await _httpClient . GetAsync ( packageInfo [ 0 ] . DownloadUrl ) ;
170
194
response . EnsureSuccessStatusCode ( ) ;
171
195
172
196
// need to unload the DLL before updating it
173
197
hostBuilder . UnloadNanoClrDll ( ) ;
174
198
175
- await using var ms = await response . Content . ReadAsStreamAsync ( ) ;
176
- await using var fs = File . OpenWrite ( nanoClrDllLocation ) ;
199
+ await using Stream ms = await response . Content . ReadAsStreamAsync ( ) ;
200
+ await using FileStream fs = File . OpenWrite ( nanoClrDllLocation ) ;
177
201
178
202
ms . Seek ( 0 , SeekOrigin . Begin ) ;
179
203
await ms . CopyToAsync ( fs ) ;
@@ -184,7 +208,7 @@ private static async Task<ExitCode> UpdateNanoCLRAsync(
184
208
}
185
209
else
186
210
{
187
- Console . WriteLine ( $ "Already at v{ packageInfo [ 0 ] . Version } ") ;
211
+ Console . WriteLine ( $ "At v{ packageInfo [ 0 ] . Version } , skipping update ") ;
188
212
}
189
213
190
214
return ExitCode . OK ;
0 commit comments