66using CliWrap ;
77using CliWrap . Buffered ;
88using nanoFramework . TestPlatform . TestAdapter ;
9+ using Newtonsoft . Json ;
910using System ;
1011using System . Text . RegularExpressions ;
1112using System . Threading ;
13+ using System . ComponentModel ;
14+ using System . Net ;
1215
1316namespace nanoFramework . TestAdapter
1417{
@@ -25,50 +28,134 @@ public static bool InstallNanoClr(LogMessenger logger)
2528 "Install/upate nanoclr tool" ,
2629 Settings . LoggingLevel . Verbose ) ;
2730
28- var cmd = Cli . Wrap ( "dotnet" )
29- . WithArguments ( "tool update -g nanoclr" )
31+ // get installed tool version (if installed)
32+ var cmd = Cli . Wrap ( "nanoclr" )
33+ . WithArguments ( "--help" )
3034 . WithValidation ( CommandResultValidation . None ) ;
3135
32- // setup cancellation token with a timeout of 1 minute
33- using ( var cts = new CancellationTokenSource ( TimeSpan . FromMinutes ( 1 ) ) )
36+ bool performInstallUpdate = false ;
37+
38+ // setup cancellation token with a timeout of 10 seconds
39+ using var cts = new CancellationTokenSource ( TimeSpan . FromSeconds ( 10 ) ) ;
40+
41+ try
3442 {
3543 var cliResult = cmd . ExecuteBufferedAsync ( cts . Token ) . Task . Result ;
3644
3745 if ( cliResult . ExitCode == 0 )
3846 {
39- // this will be either (on update):
40- // Tool 'nanoclr' was successfully updated from version '1.0.205' to version '1.0.208'.
41- // or (update becoming reinstall with same version, if there is no new version):
42- // Tool 'nanoclr' was reinstalled with the latest stable version (version '1.0.208').
43- var regexResult = Regex . Match ( cliResult . StandardOutput , @"((?>version ')(?'version'\d+\.\d+\.\d+)(?>'))" ) ;
47+ var regexResult = Regex . Match ( cliResult . StandardOutput , @"(?'version'\d+\.\d+\.\d+)" , RegexOptions . RightToLeft ) ;
4448
4549 if ( regexResult . Success )
4650 {
47- logger . LogMessage ( $ "Install/update successful. Running v{ regexResult . Groups [ "version" ] . Value } ",
48- Settings . LoggingLevel . Verbose ) ;
51+ logger . LogMessage ( $ "Running nanoclr v{ regexResult . Groups [ "version" ] . Value } ", Settings . LoggingLevel . Verbose ) ;
52+
53+ // compose version
54+ Version installedVersion = new Version ( regexResult . Groups [ 1 ] . Value ) ;
4955
5056 NanoClrIsInstalled = true ;
57+ string responseContent = null ;
58+
59+ // check latest version
60+ using ( System . Net . WebClient client = new WebClient ( ) )
61+ {
62+ try
63+ {
64+ // Set the user agent string to identify the client.
65+ client . Headers . Add ( "User-Agent" , "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" ) ;
66+
67+ // Set any additional headers, if needed.
68+ client . Headers . Add ( "Content-Type" , "application/json" ) ;
69+
70+ // Set the URL to request.
71+ string url = "https://api.nuget.org/v3-flatcontainer/nanoclr/index.json" ;
72+
73+ // Make the HTTP request and retrieve the response.
74+ responseContent = client . DownloadString ( url ) ;
75+ }
76+ catch ( WebException e )
77+ {
78+ // Handle any exceptions that occurred during the request.
79+ Console . WriteLine ( e . Message ) ;
80+ }
81+ }
82+
83+ var package = JsonConvert . DeserializeObject < NuGetPackage > ( responseContent ) ;
84+ Version latestPackageVersion = new Version ( package . Versions [ package . Versions . Length - 1 ] ) ;
85+
86+ // check if we are running the latest one
87+ if ( latestPackageVersion > installedVersion )
88+ {
89+ // need to update
90+ performInstallUpdate = true ;
91+ }
92+ else
93+ {
94+ logger . LogMessage ( $ "No need to update. Running v{ latestPackageVersion } ",
95+ Settings . LoggingLevel . Verbose ) ;
96+ }
5197 }
5298 else
5399 {
54- logger . LogPanicMessage ( $ "*** Failed to install/update nanoclr. { cliResult . StandardOutput } .") ;
55-
56- NanoClrIsInstalled = false ;
100+ // something wrong with the output, can't proceed
101+ logger . LogPanicMessage ( "Failed to parse current nanoCLR CLI version!" ) ;
57102 }
58103 }
59- else
104+ }
105+ catch ( Win32Exception )
106+ {
107+ // nanoclr doesn't seem to be installed
108+ performInstallUpdate = true ;
109+ NanoClrIsInstalled = false ;
110+ }
111+
112+ if ( performInstallUpdate )
113+ {
114+ cmd = Cli . Wrap ( "dotnet" )
115+ . WithArguments ( "tool update -g nanoclr" )
116+ . WithValidation ( CommandResultValidation . None ) ;
117+
118+ // setup cancellation token with a timeout of 1 minute
119+ using ( var cts1 = new CancellationTokenSource ( TimeSpan . FromMinutes ( 1 ) ) )
60120 {
61- logger . LogPanicMessage (
62- $ "Failed to install/update nanoclr. Exit code { cliResult . ExitCode } ."
63- + Environment . NewLine
64- + Environment . NewLine
65- + "****************************************"
66- + Environment . NewLine
67- + "*** WON'T BE ABLE TO RUN UNITS TESTS ***"
68- + Environment . NewLine
69- + "****************************************" ) ;
70-
71- NanoClrIsInstalled = false ;
121+ var cliResult = cmd . ExecuteBufferedAsync ( cts1 . Token ) . Task . Result ;
122+
123+ if ( cliResult . ExitCode == 0 )
124+ {
125+ // this will be either (on update):
126+ // Tool 'nanoclr' was successfully updated from version '1.0.205' to version '1.0.208'.
127+ // or (update becoming reinstall with same version, if there is no new version):
128+ // Tool 'nanoclr' was reinstalled with the latest stable version (version '1.0.208').
129+ var regexResult = Regex . Match ( cliResult . StandardOutput , @"((?>version ')(?'version'\d+\.\d+\.\d+)(?>'))" ) ;
130+
131+ if ( regexResult . Success )
132+ {
133+ logger . LogMessage ( $ "Install/update successful. Running v{ regexResult . Groups [ "version" ] . Value } ",
134+ Settings . LoggingLevel . Verbose ) ;
135+
136+ NanoClrIsInstalled = true ;
137+ }
138+ else
139+ {
140+ logger . LogPanicMessage ( $ "*** Failed to install/update nanoclr. { cliResult . StandardOutput } .") ;
141+
142+ NanoClrIsInstalled = false ;
143+ }
144+ }
145+ else
146+ {
147+ logger . LogPanicMessage (
148+ $ "Failed to install/update nanoclr. Exit code { cliResult . ExitCode } ."
149+ + Environment . NewLine
150+ + Environment . NewLine
151+ + "****************************************"
152+ + Environment . NewLine
153+ + "*** WON'T BE ABLE TO RUN UNITS TESTS ***"
154+ + Environment . NewLine
155+ + "****************************************" ) ;
156+
157+ NanoClrIsInstalled = false ;
158+ }
72159 }
73160 }
74161
@@ -126,5 +213,10 @@ public static void UpdateNanoCLRInstance(
126213 }
127214 }
128215 }
216+ internal class NuGetPackage
217+ {
218+ public string [ ] Versions { get ; set ; }
219+ }
220+
129221 }
130222}
0 commit comments