Skip to content

Commit

Permalink
Fix updating for dependencies that were explicitly installed (#667)
Browse files Browse the repository at this point in the history
  • Loading branch information
igor84 authored Oct 26, 2024
1 parent a011c51 commit 8f865a2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/NuGetForUnity.Tests/Assets/Tests/Resources/NuGet.config
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
<add key="All" value="(Aggregate source)" />
</activePackageSource>
<config>
<add key="packageInstallLocation" value="CustomWithinAssets" />
<add key="repositoryPath" value="./Packages" />
<add key="PackagesConfigDirectoryPath" value="." />
<add key="slimRestore" value="true" />
<add key="ReadOnlyPackageFiles" value="false" />
<add key="PreferNetStandardOverNetFramework" value="true" />
</config>
</configuration>
30 changes: 26 additions & 4 deletions src/NuGetForUnity/Editor/NugetPackageInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ public static class NugetPackageInstaller
/// <param name="package">The identifier of the package to install.</param>
/// <param name="refreshAssets">True to refresh the Unity asset database. False to ignore the changes (temporarily).</param>
/// <param name="isSlimRestoreInstall">True to skip checking if lib is imported in Unity and skip installing dependencies.</param>
/// <param name="allowUpdateForExplicitlyInstalled">False to prevent updating of packages that are explicitly installed.</param>
/// <returns>True if the package was installed successfully, otherwise false.</returns>
public static bool InstallIdentifier([NotNull] INugetPackageIdentifier package, bool refreshAssets = true, bool isSlimRestoreInstall = false)
public static bool InstallIdentifier(
[NotNull] INugetPackageIdentifier package,
bool refreshAssets = true,
bool isSlimRestoreInstall = false,
bool allowUpdateForExplicitlyInstalled = true)
{
if (!isSlimRestoreInstall && UnityPreImportedLibraryResolver.IsAlreadyImportedInEngine(package.Id, false))
{
Expand All @@ -42,7 +47,7 @@ public static bool InstallIdentifier([NotNull] INugetPackageIdentifier package,
}

foundPackage.IsManuallyInstalled = package.IsManuallyInstalled;
return Install(foundPackage, refreshAssets, isSlimRestoreInstall);
return Install(foundPackage, refreshAssets, isSlimRestoreInstall, allowUpdateForExplicitlyInstalled);
}

/// <summary>
Expand All @@ -51,8 +56,13 @@ public static bool InstallIdentifier([NotNull] INugetPackageIdentifier package,
/// <param name="package">The package to install.</param>
/// <param name="refreshAssets">True to refresh the Unity asset database. False to ignore the changes (temporarily).</param>
/// <param name="isSlimRestoreInstall">True to skip checking if lib is imported in Unity and skip installing dependencies.</param>
/// <param name="allowUpdateForExplicitlyInstalled">False to prevent updating of packages that are explicitly installed.</param>
/// <returns>True if the package was installed successfully, otherwise false.</returns>
private static bool Install([NotNull] INugetPackage package, bool refreshAssets, bool isSlimRestoreInstall)
private static bool Install(
[NotNull] INugetPackage package,
bool refreshAssets,
bool isSlimRestoreInstall,
bool allowUpdateForExplicitlyInstalled)
{
if (!isSlimRestoreInstall && UnityPreImportedLibraryResolver.IsAlreadyImportedInEngine(package.Id, false))
{
Expand All @@ -64,6 +74,18 @@ private static bool Install([NotNull] INugetPackage package, bool refreshAssets,
if (InstalledPackagesManager.TryGetById(package.Id, out var installedPackage))
{
var comparisonResult = installedPackage.CompareTo(package);

if (!allowUpdateForExplicitlyInstalled && comparisonResult != 0 && installedPackage.IsManuallyInstalled)
{
NugetLogger.LogVerbose(
"{0} {1} is installed explicitly so it will not be replaced with {3}",
installedPackage.Id,
installedPackage.Version,
package.Version,
package.Version);
return true;
}

if (comparisonResult < 0)
{
NugetLogger.LogVerbose(
Expand Down Expand Up @@ -131,7 +153,7 @@ private static bool Install([NotNull] INugetPackage package, bool refreshAssets,
foreach (var dependency in frameworkGroup.Dependencies)
{
NugetLogger.LogVerbose("Installing Dependency: {0} {1}", dependency.Id, dependency.Version);
var installed = InstallIdentifier(dependency, false);
var installed = InstallIdentifier(dependency, false, false, false);
if (!installed)
{
throw new InvalidOperationException($"Failed to install dependency: {dependency.Id} {dependency.Version}.");
Expand Down

0 comments on commit 8f865a2

Please sign in to comment.