-
Notifications
You must be signed in to change notification settings - Fork 368
/
Utils.cs
103 lines (92 loc) · 3.55 KB
/
Utils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.DotNet.Interactive.Utility;
namespace Microsoft.DotNet.Interactive.SqlServer;
internal class DotnetToolInfo {
public string PackageId { get; set; }
public string PackageVersion { get; set; }
public string CommandName { get; set; }
}
internal static class Utils
{
/// <summary>
/// Returns a version of the string quoted with single quotes. Any single quotes in the string are escaped as ''
/// </summary>
/// <param name="str">The string to quote</param>
/// <returns>The quoted string</returns>
public static string AsSingleQuotedString(this string str)
{
return $"'{str.Replace("'", "''")}'";
}
/// <summary>
/// Returns a version of the string quoted with double quotes. Any double quotes in the string are escaped as \"
/// </summary>
/// <param name="str">The string to quote</param>
/// <returns>The quoted string</returns>
public static string AsDoubleQuotedString(this string str)
{
return $"\"{str.Replace("\"", "\\\"")}\"";
}
public static async Task<IEnumerable<DotnetToolInfo>> GetGlobalToolListAsync()
{
var dotnet = new Dotnet();
var result = await dotnet.Execute("tool list --global");
if (result.ExitCode != 0)
{
return new DotnetToolInfo[0];
}
// Output of dotnet tool list is:
// Package Id Version Commands
// -------------------------------------------
// dotnettry.p1 1.0.0 dotnettry.p1
string[] separator = new[] { " " };
return result.Output
.Skip(2)
.Where(s => !string.IsNullOrWhiteSpace(s))
.Select(s =>
{
var parts = s.Split(separator, StringSplitOptions.RemoveEmptyEntries);
return new DotnetToolInfo()
{
PackageId = parts[0],
PackageVersion = parts[1],
CommandName = parts[2]
};
});
}
public static async Task CheckAndInstallGlobalToolAsync(string toolName, string minimumVersion, string nugetPackage)
{
var installedGlobalTools = await Utils.GetGlobalToolListAsync();
var expectedVersion = Version.Parse(minimumVersion);
var installNeeded = true;
var updateNeeded = false;
foreach (var tool in installedGlobalTools)
{
if (string.Equals(tool.CommandName, toolName, StringComparison.InvariantCultureIgnoreCase))
{
installNeeded = false;
var installedVersion = Version.Parse(tool.PackageVersion);
if (installedVersion < expectedVersion)
{
updateNeeded = true;
}
break;
}
}
var dotnet = new Dotnet();
if (updateNeeded)
{
var commandLineResult = await dotnet.Execute($"tool update --global \"{nugetPackage}\" --version \"{minimumVersion}\"");
commandLineResult.ThrowOnFailure();
}
else if (installNeeded)
{
var commandLineResult = await dotnet.Execute($"tool install --global \"{nugetPackage}\" --version \"{minimumVersion}\"");
commandLineResult.ThrowOnFailure();
}
}
}