Skip to content

Commit

Permalink
Fixed moving packages out of Assets
Browse files Browse the repository at this point in the history
  • Loading branch information
igor84 committed Mar 5, 2024
1 parent 9bd10c7 commit edd6104
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 70 deletions.
39 changes: 35 additions & 4 deletions src/NuGetForUnity/Editor/Configuration/ConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,16 @@ public static class ConfigurationManager

static ConfigurationManager()
{
NugetConfigFileDirectoryPath = UnityPathHelper.AbsoluteAssetsPath;
NugetConfigFilePath = Path.Combine(NugetConfigFileDirectoryPath, NugetConfigFile.FileName);
NugetConfigFilePath = Path.Combine(UnityPathHelper.AbsoluteUnityPackagesNugetPath, NugetConfigFile.FileName);
if (File.Exists(NugetConfigFilePath))
{
NugetConfigFileDirectoryPath = UnityPathHelper.AbsoluteUnityPackagesNugetPath;
}
else
{
NugetConfigFilePath = Path.Combine(UnityPathHelper.AbsoluteAssetsPath, NugetConfigFile.FileName);
NugetConfigFileDirectoryPath = UnityPathHelper.AbsoluteAssetsPath;
}
}

/// <summary>
Expand All @@ -45,7 +53,7 @@ static ConfigurationManager()
/// <see cref="NugetConfigFile" />.
/// </remarks>
[NotNull]
public static string NugetConfigFilePath { get; }
public static string NugetConfigFilePath { get; private set; }

/// <summary>
/// Gets the loaded NuGet.config file that holds the settings for NuGet.
Expand All @@ -69,7 +77,7 @@ public static NugetConfigFile NugetConfigFile
/// Gets the path to the directory containing the NuGet.config file.
/// </summary>
[NotNull]
internal static string NugetConfigFileDirectoryPath { get; }
internal static string NugetConfigFileDirectoryPath { get; private set; }

/// <summary>
/// Gets a value indicating whether verbose logging is enabled.
Expand Down Expand Up @@ -207,5 +215,28 @@ public static INugetPackage GetSpecificPackage([NotNull] INugetPackageIdentifier
{
return ActivePackageSource.GetSpecificPackage(nugetPackageIdentifier);
}

/// <summary>
/// Moves the Nuget.config under newPlacement and updated local properties to point to it.
/// </summary>
/// <param name="newPlacement">New placement for configs.</param>
public static void MoveConfig(NugetPlacement newPlacement)
{
NugetConfigFile.ChangePlacement(newPlacement);
var newConfigsPath = newPlacement == NugetPlacement.InPackagesFolder ?
UnityPathHelper.AbsoluteUnityPackagesNugetPath :
UnityPathHelper.AbsoluteAssetsPath;
var newConfigFilePath = Path.Combine(newConfigsPath, NugetConfigFile.FileName);

File.Move(NugetConfigFilePath, newConfigFilePath);
var configMeta = NugetConfigFilePath + ".meta";
if (File.Exists(configMeta))
{
File.Move(configMeta, newConfigFilePath + ".meta");
}

NugetConfigFilePath = newConfigFilePath;
NugetConfigFileDirectoryPath = newConfigsPath;
}
}
}
73 changes: 65 additions & 8 deletions src/NuGetForUnity/Editor/Configuration/NugetConfigFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,18 @@ public class NugetConfigFile

private const string SupportsPackageIdSearchFilterAttributeName = "supportsPackageIdSearchFilter";

[NotNull]
private readonly string unityPackagesNugetInstallPath = Path.Combine(UnityPathHelper.AbsoluteUnityPackagesNugetPath, "InstalledPackages");

[NotNull]
private string configuredRepositoryPath = "Packages";

[NotNull]
private string packagesConfigDirectoryPath = Application.dataPath;

[NotNull]
private string repositoryPath = Path.GetFullPath(Path.Combine(Application.dataPath, "Packages"));

/// <summary>
/// Gets the list of package sources that are defined in the NuGet.config file.
/// </summary>
Expand All @@ -80,11 +89,21 @@ public class NugetConfigFile
[CanBeNull]
public INugetPackageSource ActivePackageSource { get; private 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>
public NugetPlacement Placement { get; private set; }

/// <summary>
/// Gets the absolute path where packages are to be installed.
/// </summary>
[NotNull]
public string RepositoryPath { get; private set; } = Path.GetFullPath(Path.Combine(Application.dataPath, "Packages"));
public string RepositoryPath
{
get => Placement == NugetPlacement.InPackagesFolder ? unityPackagesNugetInstallPath : repositoryPath;

private set => repositoryPath = value;
}

/// <summary>
/// Gets or sets the incomplete path that is saved. The path is expanded and made public via the property above.
Expand All @@ -98,14 +117,14 @@ public string ConfiguredRepositoryPath
{
configuredRepositoryPath = value;

var repositoryPath = Environment.ExpandEnvironmentVariables(value);
var expandedPath = Environment.ExpandEnvironmentVariables(value);

if (!Path.IsPathRooted(repositoryPath))
if (!Path.IsPathRooted(expandedPath))
{
repositoryPath = Path.Combine(Application.dataPath, repositoryPath);
expandedPath = Path.Combine(Application.dataPath, expandedPath);
}

RepositoryPath = Path.GetFullPath(repositoryPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar));
RepositoryPath = Path.GetFullPath(expandedPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar));
}
}

Expand Down Expand Up @@ -141,7 +160,12 @@ public string ConfiguredRepositoryPath
/// Gets or sets absolute path to directory containing packages.config file.
/// </summary>
[NotNull]
public string PackagesConfigDirectoryPath { get; set; } = Application.dataPath;
public string PackagesConfigDirectoryPath
{
get => Placement == NugetPlacement.InPackagesFolder ? UnityPathHelper.AbsoluteUnityPackagesNugetPath : packagesConfigDirectoryPath;

set => packagesConfigDirectoryPath = value;
}

/// <summary>
/// Gets the relative path to directory containing packages.config file. The path is relative to the folder containing the 'NuGet.config' file.
Expand Down Expand Up @@ -169,6 +193,29 @@ public string RelativePackagesConfigDirectoryPath
/// </summary>
internal List<NugetForUnityPluginId> EnabledPlugins { get; } = new List<NugetForUnityPluginId>();

/// <summary>
/// Changes the placement config and also moves the packages.config to the new location.
/// </summary>
/// <param name="newPlacement">New placement to set.</param>
public void ChangePlacement(NugetPlacement newPlacement)
{
if (newPlacement == Placement)
{
return;
}

var oldPackagesConfigPath = PackagesConfigFilePath;
Placement = newPlacement;
UnityPathHelper.EnsurePackageInstallDirIsSetup();
var newConfigPath = PackagesConfigFilePath;
File.Move(oldPackagesConfigPath, newConfigPath);
var configMeta = oldPackagesConfigPath + ".meta";
if (File.Exists(configMeta))
{
File.Move(configMeta, newConfigPath + ".meta");
}
}

/// <summary>
/// Loads a NuGet.config file at the given file-path.
/// </summary>
Expand Down Expand Up @@ -317,7 +364,11 @@ public static NugetConfigFile Load([NotNull] string filePath)
var key = add.Attribute("key")?.Value;
var value = add.Attribute("value")?.Value ?? throw new InvalidOperationException($"config misses 'value' attribute. Element:\n{add}");

if (string.Equals(key, "repositoryPath", StringComparison.OrdinalIgnoreCase))
if (string.Equals(key, "Placement", StringComparison.OrdinalIgnoreCase))
{
configFile.Placement = (NugetPlacement)Enum.Parse(typeof(NugetPlacement), value);
}
else if (string.Equals(key, "repositoryPath", StringComparison.OrdinalIgnoreCase))
{
configFile.ConfiguredRepositoryPath = value;
}
Expand Down Expand Up @@ -373,6 +424,7 @@ public static NugetConfigFile CreateDefaultFile([NotNull] string filePath)
<add key=""All"" value=""(Aggregate source)"" />
</activePackageSource>
<config>
<add key=""Placement"" value=""CustomWithinAssets"" />
<add key=""repositoryPath"" value=""./Packages"" />
<add key=""PackagesConfigDirectoryPath"" value=""."" />
<add key=""slimRestore"" value=""true"" />
Expand Down Expand Up @@ -465,6 +517,11 @@ public void Save([NotNull] string filePath)

var config = new XElement("config");

addElement = new XElement("add");
addElement.Add(new XAttribute("key", "Placement"));
addElement.Add(new XAttribute("value", Placement.ToString()));
config.Add(addElement);

if (!string.IsNullOrEmpty(ConfiguredRepositoryPath))
{
// save the un-expanded repository path
Expand All @@ -476,7 +533,7 @@ public void Save([NotNull] string filePath)

addElement = new XElement("add");
addElement.Add(new XAttribute("key", PackagesConfigDirectoryPathConfigKey));
addElement.Add(new XAttribute("value", RelativePackagesConfigDirectoryPath));
addElement.Add(new XAttribute("value", PathHelper.GetRelativePath(Application.dataPath, packagesConfigDirectoryPath)));
config.Add(addElement);

// save the default push source
Expand Down
2 changes: 1 addition & 1 deletion src/NuGetForUnity/Editor/Configuration/NugetPlacement.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace NugetForUnity.Configuration
{
/// <summary>
/// Tells the system where the packages are to be installed and configurations are to be stored.
/// Tells the system how to determine where the packages are to be installed and configurations are to be stored.
/// </summary>
public enum NugetPlacement
{
Expand Down
15 changes: 10 additions & 5 deletions src/NuGetForUnity/Editor/Helper/UnityPathHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,15 @@ static UnityPathHelper()
{
AbsoluteAssetsPath = Path.GetFullPath(Application.dataPath);
AbsoluteProjectPath = Path.GetDirectoryName(AbsoluteAssetsPath) ?? throw new InvalidOperationException("Can't detect project root.");
AbsoluteUnityPackagesNugetPath = Path.GetFullPath(Path.Combine(Application.dataPath, "../Packages/nuget-packages"));
}

/// <summary>
/// Gets the absolute path to 'project root'/Packages/nuget-packages.
/// </summary>
[NotNull]
internal static string AbsoluteUnityPackagesNugetPath { get; }

/// <summary>
/// Gets the absolute path to the Unity-Project 'Assets' directory.
/// </summary>
Expand Down Expand Up @@ -70,21 +77,19 @@ internal static bool IsValidInstallPath([NotNull] string path)
/// </summary>
internal static void EnsurePackageInstallDirIsSetup()
{
var relativePath = ConfigurationManager.NugetConfigFile.ConfiguredRepositoryPath;

Directory.CreateDirectory(ConfigurationManager.NugetConfigFile.RepositoryPath);

if (relativePath.Length <= "../Packages/".Length || !relativePath.StartsWith("../Packages/", StringComparison.Ordinal))
if (ConfigurationManager.NugetConfigFile.Placement == NugetPlacement.CustomWithinAssets)
{
return;
}

var jsonPath = Path.Combine(ConfigurationManager.NugetConfigFile.RepositoryPath, "package.json");
var jsonPath = Path.Combine(AbsoluteUnityPackagesNugetPath, "package.json");
if (!File.Exists(jsonPath))
{
File.WriteAllText(
jsonPath,
@"{ ""name"": ""com.dummy.nugetpackages"",""version"": ""1.0.0"",""displayName"": ""NuGetPackages"", ""description"": ""NuGetPackages"", ""dependencies"": {}}");
@"{ ""name"": ""nuget-packages"",""version"": ""1.0.0"",""displayName"": ""NuGetPackages"", ""description"": ""NuGetPackages"", ""dependencies"": {}}");
}
}

Expand Down
Loading

0 comments on commit edd6104

Please sign in to comment.