-
Notifications
You must be signed in to change notification settings - Fork 748
Expand file tree
/
Copy pathMonoAndroidDeprecation.cs
More file actions
84 lines (76 loc) · 3.61 KB
/
MonoAndroidDeprecation.cs
File metadata and controls
84 lines (76 loc) · 3.61 KB
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using NuGet.Frameworks;
using NuGet.ProjectModel;
namespace NuGet.Commands
{
/// <summary>
/// Detects when a package uses the deprecated MonoAndroid framework instead of net6.0-android or later.
/// This warning is gated on .NET 11 SDK (SdkAnalysisLevel >= 11.0.100) and targeting net11.0-android or later.
/// </summary>
internal static class MonoAndroidDeprecation
{
/// <summary>
/// Determines whether the MonoAndroid deprecation check should be performed for the given project and target framework.
/// </summary>
/// <param name="project">The package spec containing restore metadata.</param>
/// <param name="framework">The target framework of the current graph.</param>
/// <returns>True if the deprecation check should be performed.</returns>
internal static bool ShouldCheck(PackageSpec project, NuGetFramework framework)
{
if (project.RestoreMetadata == null)
{
return false;
}
// Gate on SDK analysis level >= 11.0.100
if (!SdkAnalysisLevelMinimums.IsEnabled(
project.RestoreMetadata.SdkAnalysisLevel,
project.RestoreMetadata.UsingMicrosoftNETSdk,
SdkAnalysisLevelMinimums.V11_0_100))
{
return false;
}
// Only check for .NETCoreApp frameworks targeting android with version >= 11.0
return StringComparer.OrdinalIgnoreCase.Equals(framework.Framework, FrameworkConstants.FrameworkIdentifiers.NetCoreApp)
&& framework.Version.Major >= 11
&& framework.HasPlatform
&& framework.Platform.Equals("android", StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Checks whether the given lock file target library uses the deprecated MonoAndroid framework
/// by inspecting the paths of compile-time and runtime assemblies.
/// </summary>
/// <param name="library">The lock file target library to check.</param>
/// <returns>True if the library uses MonoAndroid framework assets.</returns>
internal static bool UsesMonoAndroidFramework(LockFileTargetLibrary library)
{
return ContainsMonoAndroidItem(library.CompileTimeAssemblies)
|| ContainsMonoAndroidItem(library.RuntimeAssemblies);
}
private static bool ContainsMonoAndroidItem(IList<LockFileItem> items)
{
for (int i = 0; i < items.Count; i++)
{
string path = items[i].Path;
// Paths are like "lib/monoandroid10.0/Assembly.dll" or "ref/monoandroid10.0/Assembly.dll"
// Extract the framework folder segment (between first and second '/').
int firstSlash = path.IndexOf('/');
if (firstSlash >= 0)
{
int secondSlash = path.IndexOf('/', firstSlash + 1);
if (secondSlash > firstSlash + 1)
{
var folderName = path.AsSpan(firstSlash + 1, secondSlash - firstSlash - 1);
if (folderName.StartsWith("monoandroid".AsSpan(), StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
}
}
return false;
}
}
}