Skip to content

Commit 067ae31

Browse files
authored
Add check to current nanoclr version before attempting update (#214)
***NO_CI***
1 parent a291f3d commit 067ae31

File tree

3 files changed

+131
-35
lines changed

3 files changed

+131
-35
lines changed

source/TestAdapter/NanoCLRHelper.cs

Lines changed: 118 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
using CliWrap;
77
using CliWrap.Buffered;
88
using nanoFramework.TestPlatform.TestAdapter;
9+
using Newtonsoft.Json;
910
using System;
1011
using System.Text.RegularExpressions;
1112
using System.Threading;
13+
using System.ComponentModel;
14+
using System.Net;
1215

1316
namespace 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
}

source/TestAdapter/nanoFramework.TestAdapter.csproj

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
<SignAssembly>true</SignAssembly>
66
<AssemblyOriginatorKeyFile>key.snk</AssemblyOriginatorKeyFile>
77
<EnableUnmanagedDebugging>true</EnableUnmanagedDebugging>
8-
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
98
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
9+
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
10+
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
11+
<LangVersion>8.0</LangVersion>
1012
<RestoreLockedMode Condition="'$(TF_BUILD)' == 'True' or '$(ContinuousIntegrationBuild)' == 'True'">true</RestoreLockedMode>
1113
</PropertyGroup>
1214

@@ -19,12 +21,9 @@
1921
<Version>3.6.128</Version>
2022
<PrivateAssets>all</PrivateAssets>
2123
</PackageReference>
22-
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
24+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
2325
</ItemGroup>
2426

25-
<ItemGroup>
26-
<Reference Include="System.Diagnostics.Tracing" />
27-
</ItemGroup>
2827
<Import Project="..\TestFrameworkShared\TestFrameworkShared.projitems" Label="Shared" />
2928

3029
</Project>

source/TestAdapter/packages.lock.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@
5454
"resolved": "3.6.128",
5555
"contentHash": "zeA+Ho3XlPgt6P9MRALlkEvfOOzDQdNseV0xia/nSfC1lSrl0+gizlWyixaHvSYUw2ru7pBIKnK451bYOjPRjA=="
5656
},
57-
"System.Runtime.CompilerServices.Unsafe": {
57+
"Newtonsoft.Json": {
5858
"type": "Direct",
59-
"requested": "[6.0.0, )",
60-
"resolved": "6.0.0",
61-
"contentHash": "/iUeP3tq1S0XdNNoMz5C9twLSrM/TH+qElHkXWaPvuNOt+99G75NrV0OS2EqHx5wMN7popYjpc8oTjC1y16DLg=="
59+
"requested": "[13.0.3, )",
60+
"resolved": "13.0.3",
61+
"contentHash": "HrC5BXdl00IP9zeV+0Z848QWPAoCr9P3bDEZguI+gkLcBKAOxix/tLEAAHC+UvDNPv4a2d18lOReHMOagPa+zQ=="
6262
},
6363
"Fody": {
6464
"type": "Transitive",
@@ -177,6 +177,11 @@
177177
"System.Collections.Immutable": "5.0.0"
178178
}
179179
},
180+
"System.Runtime.CompilerServices.Unsafe": {
181+
"type": "Transitive",
182+
"resolved": "6.0.0",
183+
"contentHash": "/iUeP3tq1S0XdNNoMz5C9twLSrM/TH+qElHkXWaPvuNOt+99G75NrV0OS2EqHx5wMN7popYjpc8oTjC1y16DLg=="
184+
},
180185
"System.Security.AccessControl": {
181186
"type": "Transitive",
182187
"resolved": "5.0.0",

0 commit comments

Comments
 (0)