1- // Licensed to the .NET Foundation under one or more agreements.
1+ // Licensed to the .NET Foundation under one or more agreements.
22// The .NET Foundation licenses this file to you under the MIT license.
33
44using System ;
@@ -16,6 +16,9 @@ namespace nanoFramework.nanoCLR.CLI
1616 public static class ClrInstanceOperationsProcessor
1717 {
1818 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+
1922 private static HttpClient _httpClient = new HttpClient ( ) ;
2023
2124 public static int ProcessVerb ( ClrInstanceOperationsOptions options )
@@ -52,6 +55,7 @@ public static int ProcessVerb(ClrInstanceOperationsOptions options)
5255 return ( int ) UpdateNanoCLRAsync (
5356 options . TargetVersion ,
5457 hostBuilder . GetCLRVersion ( ) ,
58+ options . CheckPreviewVersions ,
5559 hostBuilder ) . Result ;
5660 }
5761 else if ( options . GetCLRVersion || options . GetNativeAssemblies )
@@ -110,12 +114,13 @@ orderby na.Name
110114 private static async Task < ExitCode > UpdateNanoCLRAsync (
111115 string targetVersion ,
112116 string currentVersion ,
117+ bool usePreview ,
113118 nanoCLRHostBuilder hostBuilder )
114119 {
115120 try
116121 {
117122 // 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
119124 if ( string . IsNullOrEmpty ( currentVersion ) )
120125 {
121126 currentVersion = "0.0.0.0" ;
@@ -127,7 +132,7 @@ private static async Task<ExitCode> UpdateNanoCLRAsync(
127132 currentVersion . IndexOf ( "+" ) < 0 ? currentVersion . Length : currentVersion . IndexOf ( "+" ) ) ;
128133 }
129134
130- Version version = Version . Parse ( currentVersion ) ;
135+ Version installedVersion = Version . Parse ( currentVersion ) ;
131136
132137 string nanoClrDllLocation = Path . Combine (
133138 Path . GetDirectoryName ( Assembly . GetExecutingAssembly ( ) . Location ) ,
@@ -137,32 +142,37 @@ private static async Task<ExitCode> UpdateNanoCLRAsync(
137142 _httpClient . BaseAddress = new Uri ( _cloudSmithApiUrl ) ;
138143 _httpClient . DefaultRequestHeaders . Add ( "Accept" , "*/*" ) ;
139144
145+ string repoName = usePreview ? _refTargetsDevRepo : _refTargetsStableRepo ;
146+
140147 // get latest version available for download
141- HttpResponseMessage response = await _httpClient . GetAsync ( $ "nanoframework-images /?query=name:^WIN_DLL_nanoCLR version:^latest$") ;
148+ HttpResponseMessage response = await _httpClient . GetAsync ( $ "{ repoName } /?query=name:^WIN_DLL_nanoCLR version:^latest$") ;
142149 string responseBody = await response . Content . ReadAsStringAsync ( ) ;
143150
144151 if ( responseBody == "[]" )
145152 {
146- Console . WriteLine ( $ "Error getting latest nanoCLR version .") ;
153+ Console . WriteLine ( $ "Error getting latest available nanoCLR package .") ;
147154 return ExitCode . E9005 ;
148155 }
149156
150157 var packageInfo = JsonSerializer . Deserialize < List < CloudsmithPackageInfo > > ( responseBody ) ;
151158
152159 if ( packageInfo . Count != 1 )
153160 {
154- Console . WriteLine ( $ "Error parsing latest nanoCLR version.") ;
161+ Console . WriteLine ( $ "Error parsing nanoCLR version from package details .") ;
155162 return ExitCode . E9005 ;
156163 }
157164 else
158165 {
159- Version latestFwVersion = Version . Parse ( packageInfo [ 0 ] . Version ) ;
166+ Version availableFwVersion = Version . Parse ( packageInfo [ 0 ] . Version ) ;
160167
161- if ( latestFwVersion < version )
168+ // only perform version check if preview wasn't requested
169+ if ( ! usePreview && ( availableFwVersion < installedVersion ) )
162170 {
163- Console . WriteLine ( $ "Current version { version } lower than available version { packageInfo [ 0 ] . Version } ") ;
171+ Console . WriteLine ( $ "Current version { installedVersion } higher than available version { packageInfo [ 0 ] . Version } ") ;
164172 }
165- else if ( latestFwVersion > version
173+
174+ if ( usePreview
175+ || ( availableFwVersion > installedVersion )
166176 || ( ! string . IsNullOrEmpty ( targetVersion )
167177 && ( Version . Parse ( targetVersion ) > Version . Parse ( currentVersion ) ) ) )
168178 {
@@ -172,8 +182,8 @@ private static async Task<ExitCode> UpdateNanoCLRAsync(
172182 // need to unload the DLL before updating it
173183 hostBuilder . UnloadNanoClrDll ( ) ;
174184
175- await using var ms = await response . Content . ReadAsStreamAsync ( ) ;
176- await using var fs = File . OpenWrite ( nanoClrDllLocation ) ;
185+ await using Stream ms = await response . Content . ReadAsStreamAsync ( ) ;
186+ await using FileStream fs = File . OpenWrite ( nanoClrDllLocation ) ;
177187
178188 ms . Seek ( 0 , SeekOrigin . Begin ) ;
179189 await ms . CopyToAsync ( fs ) ;
0 commit comments