Skip to content

Commit ad5eac2

Browse files
authored
Adding slim restore option (#576)
* Adding slim restore option for skipping dependency installation and checking for already imported Unity libs on auto restore
1 parent c58092e commit ad5eac2

File tree

9 files changed

+73
-48
lines changed

9 files changed

+73
-48
lines changed

src/NuGetForUnity.Cli/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static int Main(string[] args)
6161
Application.SetUnityProjectPath(projectPath);
6262

6363
// need to disable dependency installation as UnityPreImportedLibraryResolver.GetAlreadyImportedLibs is not working outside Unity.
64-
PackageRestorer.Restore(false);
64+
PackageRestorer.Restore(true);
6565
FixRoslynAnalyzerImportSettings();
6666
return Debug.HasError ? 1 : 0;
6767
}

src/NuGetForUnity.Tests/Assets/Tests/Editor/NuGetTests.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void Cleanup()
4545
[Order(1)]
4646
public void SimpleRestoreTest()
4747
{
48-
PackageRestorer.Restore();
48+
PackageRestorer.Restore(false);
4949
}
5050

5151
[Test]
@@ -205,7 +205,7 @@ public void InstallStyleCopWithoutDependenciesTest()
205205
{
206206
var styleCopPlusId = new NugetPackageIdentifier("StyleCopPlus.MSBuild", "4.7.49.5");
207207

208-
NugetPackageInstaller.InstallIdentifier(styleCopPlusId, installDependencies: false);
208+
NugetPackageInstaller.InstallIdentifier(styleCopPlusId, isSlimRestoreInstall: true);
209209

210210
// StyleCopPlus depends on StyleCop, so without 'installDependencies' they are both installed
211211
Assert.That(InstalledPackagesManager.InstalledPackages, Is.EquivalentTo(new[] { styleCopPlusId }));
@@ -752,6 +752,29 @@ public void GetRelativePathTest(string path, string expected)
752752
Assert.That(relativePath, Is.EqualTo(expected));
753753
}
754754

755+
[Test]
756+
[TestCase("Microsoft.Extensions.Logging", "7.0.0")]
757+
public void TestSlimRestoreInstall(string packageId, string packageVersion)
758+
{
759+
var package = new NugetPackageIdentifier(packageId, packageVersion) { IsManuallyInstalled = true };
760+
761+
Assume.That(InstalledPackagesManager.IsInstalled(package), Is.False, "The package IS installed: {0} {1}", package.Id, package.Version);
762+
763+
var packagesConfigFile = new PackagesConfigFile();
764+
packagesConfigFile.AddPackage(package);
765+
packagesConfigFile.Save();
766+
767+
InstalledPackagesManager.ReloadPackagesConfig();
768+
PackageRestorer.Restore(true);
769+
770+
Assert.IsTrue(InstalledPackagesManager.IsInstalled(package), "The package was NOT installed: {0} {1}", package.Id, package.Version);
771+
Assert.IsTrue(
772+
InstalledPackagesManager.InstalledPackages.Count() == 1,
773+
"The dependencies WERE installed for package {0} {1}",
774+
package.Id,
775+
package.Version);
776+
}
777+
755778
private static void ConfigureNugetConfig(InstallMode installMode)
756779
{
757780
var nugetConfigFile = ConfigurationManager.NugetConfigFile;

src/NuGetForUnity/Editor/Configuration/NugetConfigFile.cs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ public class NugetConfigFile
4444

4545
private const string RequestTimeoutSecondsConfigKey = "RequestTimeoutSeconds";
4646

47-
private const string LockPackagesOnRestoreConfigKey = "LockPackagesOnRestore";
48-
4947
private const string PackagesConfigDirectoryPathConfigKey = "PackagesConfigDirectoryPath";
5048

5149
/// <summary>
@@ -88,6 +86,12 @@ public class NugetConfigFile
8886
/// </summary>
8987
public bool Verbose { get; set; }
9088

89+
/// <summary>
90+
/// Gets or sets a value indicating whether to skip installing dependencies and checking for pre-imported Unity libs
91+
/// while auto-restoring.
92+
/// </summary>
93+
public bool SlimRestore { get; set; } = true;
94+
9195
/// <summary>
9296
/// Gets or sets a value indicating whether a package is installed from the cache (if present), or if it always downloads the package from the
9397
/// server.
@@ -126,12 +130,6 @@ public string RelativePackagesConfigDirectoryPath
126130
/// </summary>
127131
public int RequestTimeoutSeconds { get; set; } = DefaultRequestTimeout;
128132

129-
/// <summary>
130-
/// Gets or sets a value indicating whether the installed packages should be fixed, so only the packages that are configure inside the
131-
/// 'package.config' are installed without installing the dependencies of them.
132-
/// </summary>
133-
public bool LockPackagesOnRestore { get; set; }
134-
135133
/// <summary>
136134
/// Loads a NuGet.config file at the given file-path.
137135
/// </summary>
@@ -260,6 +258,10 @@ public static NugetConfigFile Load([NotNull] string filePath)
260258
{
261259
configFile.Verbose = bool.Parse(value);
262260
}
261+
else if (string.Equals(key, "slimRestore", StringComparison.OrdinalIgnoreCase))
262+
{
263+
configFile.SlimRestore = bool.Parse(value);
264+
}
263265
else if (string.Equals(key, "InstallFromCache", StringComparison.OrdinalIgnoreCase))
264266
{
265267
configFile.InstallFromCache = bool.Parse(value);
@@ -272,10 +274,6 @@ public static NugetConfigFile Load([NotNull] string filePath)
272274
{
273275
configFile.RequestTimeoutSeconds = int.Parse(value, CultureInfo.InvariantCulture);
274276
}
275-
else if (string.Equals(key, LockPackagesOnRestoreConfigKey, StringComparison.OrdinalIgnoreCase))
276-
{
277-
configFile.LockPackagesOnRestore = bool.Parse(value);
278-
}
279277
else if (string.Equals(key, PackagesConfigDirectoryPathConfigKey, StringComparison.OrdinalIgnoreCase))
280278
{
281279
configFile.RelativePackagesConfigDirectoryPath = value;
@@ -306,6 +304,7 @@ public static NugetConfigFile CreateDefaultFile([NotNull] string filePath)
306304
<config>
307305
<add key=""repositoryPath"" value=""./Packages"" />
308306
<add key=""PackagesConfigDirectoryPath"" value=""."" />
307+
<add key=""slimRestore"" value=""true"" />
309308
</config>
310309
</configuration>";
311310

@@ -402,6 +401,11 @@ public void Save([NotNull] string filePath)
402401
config.Add(addElement);
403402
}
404403

404+
addElement = new XElement("add");
405+
addElement.Add(new XAttribute("key", "slimRestore"));
406+
addElement.Add(new XAttribute("value", SlimRestore.ToString().ToLowerInvariant()));
407+
config.Add(addElement);
408+
405409
if (!InstallFromCache)
406410
{
407411
addElement = new XElement("add");
@@ -426,14 +430,6 @@ public void Save([NotNull] string filePath)
426430
config.Add(addElement);
427431
}
428432

429-
if (LockPackagesOnRestore)
430-
{
431-
addElement = new XElement("add");
432-
addElement.Add(new XAttribute("key", LockPackagesOnRestoreConfigKey));
433-
addElement.Add(new XAttribute("value", LockPackagesOnRestore.ToString().ToLowerInvariant()));
434-
config.Add(addElement);
435-
}
436-
437433
var configuration = new XElement("configuration");
438434
configuration.Add(packageSources);
439435
configuration.Add(disabledPackageSources);

src/NuGetForUnity/Editor/NugetAssetPostprocessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ internal static void OnPostprocessAllAssets(
8585
}
8686

8787
InstalledPackagesManager.ReloadPackagesConfig();
88-
PackageRestorer.Restore();
88+
PackageRestorer.Restore(ConfigurationManager.NugetConfigFile.SlimRestore);
8989
}
9090

9191
[NotNull]

src/NuGetForUnity/Editor/NugetPackageInstaller.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ public static class NugetPackageInstaller
2222
/// </summary>
2323
/// <param name="package">The identifier of the package to install.</param>
2424
/// <param name="refreshAssets">True to refresh the Unity asset database. False to ignore the changes (temporarily).</param>
25-
/// <param name="installDependencies">True to also install all dependencies of the <paramref name="package" />.</param>
25+
/// <param name="isSlimRestoreInstall">True to skip checking if lib is imported in Unity and skip installing dependencies.</param>
2626
/// <returns>True if the package was installed successfully, otherwise false.</returns>
27-
public static bool InstallIdentifier([NotNull] INugetPackageIdentifier package, bool refreshAssets = true, bool installDependencies = true)
27+
public static bool InstallIdentifier([NotNull] INugetPackageIdentifier package, bool refreshAssets = true, bool isSlimRestoreInstall = false)
2828
{
29-
if (UnityPreImportedLibraryResolver.IsAlreadyImportedInEngine(package, false))
29+
if (!isSlimRestoreInstall && UnityPreImportedLibraryResolver.IsAlreadyImportedInEngine(package, false))
3030
{
3131
NugetLogger.LogVerbose("Package {0} is already imported in engine, skipping install.", package);
3232
return true;
@@ -41,19 +41,19 @@ public static bool InstallIdentifier([NotNull] INugetPackageIdentifier package,
4141
}
4242

4343
foundPackage.IsManuallyInstalled = package.IsManuallyInstalled;
44-
return Install(foundPackage, refreshAssets, installDependencies);
44+
return Install(foundPackage, refreshAssets, isSlimRestoreInstall);
4545
}
4646

4747
/// <summary>
4848
/// Installs the given package.
4949
/// </summary>
5050
/// <param name="package">The package to install.</param>
5151
/// <param name="refreshAssets">True to refresh the Unity asset database. False to ignore the changes (temporarily).</param>
52-
/// <param name="installDependencies">True to also install all dependencies of the <paramref name="package" />.</param>
52+
/// <param name="isSlimRestoreInstall">True to skip checking if lib is imported in Unity and skip installing dependencies.</param>
5353
/// <returns>True if the package was installed successfully, otherwise false.</returns>
54-
private static bool Install([NotNull] INugetPackage package, bool refreshAssets, bool installDependencies)
54+
private static bool Install([NotNull] INugetPackage package, bool refreshAssets, bool isSlimRestoreInstall)
5555
{
56-
if (UnityPreImportedLibraryResolver.IsAlreadyImportedInEngine(package, false))
56+
if (!isSlimRestoreInstall && UnityPreImportedLibraryResolver.IsAlreadyImportedInEngine(package, false))
5757
{
5858
NugetLogger.LogVerbose("Package {0} is already imported in engine, skipping install.", package);
5959
return true;
@@ -110,7 +110,7 @@ private static bool Install([NotNull] INugetPackage package, bool refreshAssets,
110110
EditorUtility.DisplayProgressBar($"Installing {package.Id} {package.Version}", "Installing Dependencies", 0.1f);
111111
}
112112

113-
if (installDependencies)
113+
if (!isSlimRestoreInstall)
114114
{
115115
var dependencies = package.Dependencies;
116116

src/NuGetForUnity/Editor/OnLoadNugetPackageRestorer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static OnLoadNugetPackageRestorer()
3232
ConfigurationManager.LoadNugetConfigFile();
3333

3434
// restore packages - this will be called EVERY time the project is loaded
35-
PackageRestorer.Restore(!ConfigurationManager.NugetConfigFile.LockPackagesOnRestore);
35+
PackageRestorer.Restore(ConfigurationManager.NugetConfigFile.SlimRestore);
3636
}
3737
}
3838
}

src/NuGetForUnity/Editor/PackageRestorer.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public static class PackageRestorer
1414
/// <summary>
1515
/// Restores all packages defined in packages.config.
1616
/// </summary>
17-
/// <param name="installDependencies">True to also install all dependencies of the packages listed in the <see cref="PackagesConfigFile" />.</param>
18-
public static void Restore(bool installDependencies = true)
17+
/// <param name="slimRestore">True if we want to skip installing dependencies and checking if the lib is imported in Unity</param>
18+
public static void Restore(bool slimRestore)
1919
{
2020
InstalledPackagesManager.UpdateInstalledPackages();
2121

@@ -41,7 +41,9 @@ public static void Restore(bool installDependencies = true)
4141
$"Restoring {package.Id} {package.Version}",
4242
currentProgress);
4343
NugetLogger.LogVerbose("---Restoring {0} {1}", package.Id, package.Version);
44-
NugetPackageInstaller.InstallIdentifier(package, installDependencies: installDependencies);
44+
NugetPackageInstaller.InstallIdentifier(
45+
package,
46+
isSlimRestoreInstall: slimRestore);
4547
somethingChanged = true;
4648
}
4749

src/NuGetForUnity/Editor/Ui/NugetPreferences.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,21 @@ public override void OnGUI([CanBeNull] string searchContext)
9292
ConfigurationManager.NugetConfigFile.Verbose = verbose;
9393
}
9494

95+
var slimRestore = EditorGUILayout.Toggle(
96+
new GUIContent(
97+
"Slim Restore",
98+
"Slim restore is a faster way of installing/restoring packages after opening the Unity project. " +
99+
"To achieve this, the package installation step skips installing dependencies not listed inside the package.config, " +
100+
"also it skips checking against Unity pre-imported libraries. If you have a complex project setup with multiple target " +
101+
"platforms that include different packages, you might need to disable this feature. " +
102+
"Manually restoring via menu option will ignore this setting."),
103+
ConfigurationManager.NugetConfigFile.SlimRestore);
104+
if (slimRestore != ConfigurationManager.NugetConfigFile.SlimRestore)
105+
{
106+
preferencesChangedThisFrame = true;
107+
ConfigurationManager.NugetConfigFile.SlimRestore = slimRestore;
108+
}
109+
95110
EditorGUILayout.BeginHorizontal();
96111
{
97112
var packagesConfigPath = ConfigurationManager.NugetConfigFile.PackagesConfigDirectoryPath;
@@ -133,17 +148,6 @@ public override void OnGUI([CanBeNull] string searchContext)
133148
ConfigurationManager.NugetConfigFile.RequestTimeoutSeconds = requestTimeout;
134149
}
135150

136-
var lockPackagesOnRestore = EditorGUILayout.Toggle(
137-
new GUIContent(
138-
"Lock Packages on Restore",
139-
"When lock packages on restore is enabled only packages explicitly listed inside the 'packages.config' file will be installed. No dependencies are installed. This feature should only be used when having issues that unneeded or broken packages are installed."),
140-
ConfigurationManager.NugetConfigFile.LockPackagesOnRestore);
141-
if (lockPackagesOnRestore != ConfigurationManager.NugetConfigFile.LockPackagesOnRestore)
142-
{
143-
preferencesChangedThisFrame = true;
144-
ConfigurationManager.NugetConfigFile.LockPackagesOnRestore = lockPackagesOnRestore;
145-
}
146-
147151
EditorGUILayout.LabelField("Package Sources:");
148152

149153
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);

src/NuGetForUnity/Editor/Ui/NugetWindow.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ protected static void DisplayNugetWindow()
229229
[MenuItem("NuGet/Restore Packages", false, 1)]
230230
protected static void RestorePackages()
231231
{
232-
PackageRestorer.Restore(!ConfigurationManager.NugetConfigFile.LockPackagesOnRestore);
232+
PackageRestorer.Restore(false);
233233
foreach (var nugetWindow in Resources.FindObjectsOfTypeAll<NugetWindow>())
234234
{
235235
nugetWindow.ClearViewCache();

0 commit comments

Comments
 (0)