Skip to content

Commit

Permalink
Add option to prefer .NET Standard over .NET Framework as TargetFrame…
Browse files Browse the repository at this point in the history
…work
  • Loading branch information
mayuki committed Apr 26, 2024
1 parent 12574c3 commit 39f95a3
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public static NugetConfigFile NugetConfigFile
/// </summary>
internal static bool IsVerboseLoggingEnabled => nugetConfigFile?.Verbose ?? false;

/// <summary>
/// Gets the value indicating whether .NET Standard is preferred over .NET Framework as the TargetFramework.
/// </summary>
internal static bool PreferNetStandardOverNetFramework => nugetConfigFile?.PreferNetStandardOverNetFramework ?? false;

/// <summary>
/// Gets the <see cref="INugetPackageSource" /> to use.
/// </summary>
Expand Down
19 changes: 19 additions & 0 deletions src/NuGetForUnity/Editor/Configuration/NugetConfigFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class NugetConfigFile

private const string PackagesConfigDirectoryPathConfigKey = "PackagesConfigDirectoryPath";

private const string PreferNetStandardOverNetFrameworkConfigKey = "PreferNetStandardOverNetFramework";

private const string ProtocolVersionAttributeName = "protocolVersion";

private const string PasswordAttributeName = "password";
Expand Down Expand Up @@ -186,6 +188,11 @@ public string RelativePackagesConfigDirectoryPath
/// </summary>
public int RequestTimeoutSeconds { get; set; } = DefaultRequestTimeout;

/// <summary>
/// Gets or sets the value indicating whether .NET Standard is preferred over .NET Framework as the TargetFramework.
/// </summary>
public bool PreferNetStandardOverNetFramework { get; set; }

/// <summary>
/// Gets the value that tells the system how to determine where the packages are to be installed and configurations are to be stored.
/// </summary>
Expand Down Expand Up @@ -380,6 +387,10 @@ public static NugetConfigFile Load([NotNull] string filePath)
{
configFile.RelativePackagesConfigDirectoryPath = value;
}
else if (string.Equals(key, PreferNetStandardOverNetFrameworkConfigKey, StringComparison.OrdinalIgnoreCase))
{
configFile.PreferNetStandardOverNetFramework = bool.Parse(value);
}
}

return configFile;
Expand Down Expand Up @@ -562,6 +573,14 @@ public void Save([NotNull] string filePath)
config.Add(addElement);
}

if (PreferNetStandardOverNetFramework)
{
addElement = new XElement("add");
addElement.Add(new XAttribute("key", PreferNetStandardOverNetFrameworkConfigKey));
addElement.Add(new XAttribute("value", PreferNetStandardOverNetFramework.ToString().ToLowerInvariant()));
config.Add(addElement);
}

var configuration = new XElement("configuration");
configuration.Add(packageSources);
configuration.Add(disabledPackageSources);
Expand Down
85 changes: 84 additions & 1 deletion src/NuGetForUnity/Editor/TargetFrameworkResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using NugetForUnity.Configuration;
using NugetForUnity.Models;
using UnityEditor;

Expand Down Expand Up @@ -106,6 +107,85 @@ internal static class TargetFrameworkResolver
new TargetFrameworkSupport(string.Empty),
};

// Almost same as PrioritizedTargetFrameworks, but it prefers .NET Standard 2.x over .NET Framework.
[NotNull]
private static readonly TargetFrameworkSupport[] PrioritizedTargetFrameworksPreferNetStandard20Or21 =
{
new TargetFrameworkSupport("unity"),

// Prefer .NET Standard 2.x over .NET Framework
new TargetFrameworkSupport(
"netstandard21",
new UnityVersion(2021, 2, 0, 'f', 0),
DotnetVersionCompatibilityLevel.NetStandard20Or21,
DotnetVersionCompatibilityLevel.NetFramework46Or48),
new TargetFrameworkSupport(
"netstandard20",
null,
DotnetVersionCompatibilityLevel.NetStandard20Or21,
DotnetVersionCompatibilityLevel.NetFramework46Or48),

// .net framework (as we don't support unity < 2018 we can expect that at least .net framework 4.4 is supported)
new TargetFrameworkSupport("net48", new UnityVersion(2021, 2, 0, 'f', 0), DotnetVersionCompatibilityLevel.NetFramework46Or48),
new TargetFrameworkSupport("net472", null, DotnetVersionCompatibilityLevel.NetFramework46Or48),
new TargetFrameworkSupport("net471", null, DotnetVersionCompatibilityLevel.NetFramework46Or48),
new TargetFrameworkSupport("net47", null, DotnetVersionCompatibilityLevel.NetFramework46Or48),
new TargetFrameworkSupport("net462", null, DotnetVersionCompatibilityLevel.NetFramework46Or48),
new TargetFrameworkSupport("net461", null, DotnetVersionCompatibilityLevel.NetFramework46Or48),
new TargetFrameworkSupport("net46", null, DotnetVersionCompatibilityLevel.NetFramework46Or48),
new TargetFrameworkSupport("net452", null, DotnetVersionCompatibilityLevel.NetFramework46Or48),
new TargetFrameworkSupport("net451", null, DotnetVersionCompatibilityLevel.NetFramework46Or48),
new TargetFrameworkSupport("net45", null, DotnetVersionCompatibilityLevel.NetFramework46Or48),
new TargetFrameworkSupport("net403", null, DotnetVersionCompatibilityLevel.NetFramework46Or48),
new TargetFrameworkSupport("net40", null, DotnetVersionCompatibilityLevel.NetFramework46Or48),
new TargetFrameworkSupport("net4", null, DotnetVersionCompatibilityLevel.NetFramework46Or48),
new TargetFrameworkSupport("net35-unity full v35", null, DotnetVersionCompatibilityLevel.NetFramework46Or48),
new TargetFrameworkSupport("net35-unity subset v35", null, DotnetVersionCompatibilityLevel.NetFramework46Or48),
new TargetFrameworkSupport("net35", null, DotnetVersionCompatibilityLevel.NetFramework46Or48),
new TargetFrameworkSupport("net20", null, DotnetVersionCompatibilityLevel.NetFramework46Or48),
new TargetFrameworkSupport("net11", null, DotnetVersionCompatibilityLevel.NetFramework46Or48),

// .net standard
new TargetFrameworkSupport(
"netstandard16",
null,
DotnetVersionCompatibilityLevel.NetStandard20Or21,
DotnetVersionCompatibilityLevel.NetFramework46Or48),
new TargetFrameworkSupport(
"netstandard15",
null,
DotnetVersionCompatibilityLevel.NetStandard20Or21,
DotnetVersionCompatibilityLevel.NetFramework46Or48),
new TargetFrameworkSupport(
"netstandard14",
null,
DotnetVersionCompatibilityLevel.NetStandard20Or21,
DotnetVersionCompatibilityLevel.NetFramework46Or48),
new TargetFrameworkSupport(
"netstandard13",
null,
DotnetVersionCompatibilityLevel.NetStandard20Or21,
DotnetVersionCompatibilityLevel.NetFramework46Or48),
new TargetFrameworkSupport(
"netstandard12",
null,
DotnetVersionCompatibilityLevel.NetStandard20Or21,
DotnetVersionCompatibilityLevel.NetFramework46Or48),
new TargetFrameworkSupport(
"netstandard11",
null,
DotnetVersionCompatibilityLevel.NetStandard20Or21,
DotnetVersionCompatibilityLevel.NetFramework46Or48),
new TargetFrameworkSupport(
"netstandard10",
null,
DotnetVersionCompatibilityLevel.NetStandard20Or21,
DotnetVersionCompatibilityLevel.NetFramework46Or48),

// fallback if there is one with empty string
new TargetFrameworkSupport(string.Empty),
};

/// <summary>
/// Gets the <see cref="ApiCompatibilityLevel" /> of the current selected build target.
/// </summary>
Expand Down Expand Up @@ -191,7 +271,10 @@ public static T TryGetBestTargetFramework<T>(

var currentDotnetVersion = CurrentBuildTargetDotnetVersionCompatibilityLevel;
var currentUnityVersion = UnityVersion.Current;
foreach (var targetFrameworkSupport in PrioritizedTargetFrameworks)
var prioritizedTargetFrameworks = ConfigurationManager.PreferNetStandardOverNetFramework ?
PrioritizedTargetFrameworksPreferNetStandard20Or21 :
PrioritizedTargetFrameworks;
foreach (var targetFrameworkSupport in prioritizedTargetFrameworks)
{
if (targetFrameworkSupport.SupportedDotnetVersions.Length != 0 &&
!targetFrameworkSupport.SupportedDotnetVersions.Contains(currentDotnetVersion))
Expand Down
9 changes: 8 additions & 1 deletion src/NuGetForUnity/Editor/Ui/NugetPreferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public override void OnGUI([CanBeNull] string searchContext)
var sourcePathChangedThisFrame = false;
var needsAssetRefresh = false;

var biggestLabelSize = EditorStyles.label.CalcSize(new GUIContent("Request Timeout in seconds")).x;
var biggestLabelSize = EditorStyles.label.CalcSize(new GUIContent("Prefer .NET Standard as TargetFramework")).x;
EditorGUIUtility.labelWidth = biggestLabelSize;
EditorGUILayout.LabelField($"Version: {NuGetForUnityVersion}");

Expand Down Expand Up @@ -244,6 +244,13 @@ public override void OnGUI([CanBeNull] string searchContext)
}
}

var preferNetStandardOverNetFramework = EditorGUILayout.Toggle("Prefer .NET Standard as TargetFramework", ConfigurationManager.NugetConfigFile.PreferNetStandardOverNetFramework);
if (preferNetStandardOverNetFramework != ConfigurationManager.NugetConfigFile.PreferNetStandardOverNetFramework)
{
preferencesChangedThisFrame = true;
ConfigurationManager.NugetConfigFile.PreferNetStandardOverNetFramework = preferNetStandardOverNetFramework;
}

var requestTimeout = EditorGUILayout.IntField(
new GUIContent(
"Request Timeout in seconds",
Expand Down

0 comments on commit 39f95a3

Please sign in to comment.