forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NuGetUtils.NuGet.cs
101 lines (85 loc) · 3.4 KB
/
NuGetUtils.NuGet.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
// 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.Diagnostics;
using System.IO;
using System.Linq;
using NuGet.Frameworks;
using NuGet.Packaging.Core;
using NuGet.ProjectModel;
using NuGet.RuntimeModel;
namespace Microsoft.NET.Build.Tasks
{
internal static partial class NuGetUtils
{
public static bool IsPlaceholderFile(string path)
{
// PERF: avoid allocations here as we check this for every file in project.assets.json
if (!path.EndsWith("_._", StringComparison.Ordinal))
{
return false;
}
if (path.Length == 3)
{
return true;
}
char separator = path[path.Length - 4];
return separator == '\\' || separator == '/';
}
public static string GetLockFileLanguageName(string projectLanguage)
{
switch (projectLanguage)
{
case "C#": return "cs";
case "F#": return "fs";
default: return projectLanguage?.ToLowerInvariant();
}
}
public static NuGetFramework ParseFrameworkName(string frameworkName)
{
return frameworkName == null ? null : NuGetFramework.Parse(frameworkName);
}
public static bool IsApplicableAnalyzer(string file, string projectLanguage)
{
// This logic is preserved from previous implementations.
// See https://github.com/NuGet/Home/issues/6279#issuecomment-353696160 for possible issues with it.
bool IsAnalyzer()
{
return file.StartsWith("analyzers", StringComparison.Ordinal)
&& file.EndsWith(".dll", StringComparison.OrdinalIgnoreCase)
&& !file.EndsWith(".resources.dll", StringComparison.OrdinalIgnoreCase);
}
bool CS() => file.IndexOf("/cs/", StringComparison.OrdinalIgnoreCase) >= 0;
bool VB() => file.IndexOf("/vb/", StringComparison.OrdinalIgnoreCase) >= 0;
bool FileMatchesProjectLanguage()
{
switch (projectLanguage)
{
case "C#":
return CS() || !VB();
case "VB":
return VB() || !CS();
default:
return false;
}
}
return IsAnalyzer() && FileMatchesProjectLanguage();
}
public static string GetBestMatchingRid(RuntimeGraph runtimeGraph, string runtimeIdentifier,
IEnumerable<string> availableRuntimeIdentifiers, out bool wasInGraph)
{
wasInGraph = runtimeGraph.Runtimes.ContainsKey(runtimeIdentifier);
HashSet<string> availableRids = new HashSet<string>(availableRuntimeIdentifiers);
foreach (var candidateRuntimeIdentifier in runtimeGraph.ExpandRuntime(runtimeIdentifier))
{
if (availableRids.Contains(candidateRuntimeIdentifier))
{
return candidateRuntimeIdentifier;
}
}
// No compatible RID found in availableRuntimeIdentifiers
return null;
}
}
}