Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to install in /Packages/NuGetAssets #618

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/NuGetForUnity/Editor/Configuration/NugetConfigFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,27 @@
[CanBeNull]
public INugetPackageSource ActivePackageSource { get; private set; }

/// <summary>
/// The underlying absolute path where packages are to be installed.
/// </summary>
[NotNull]
private string underlyingRepositoryPath { get; set; } = Path.GetFullPath(Path.Combine(Application.dataPath, "Packages"));

Check warning on line 90 in src/NuGetForUnity/Editor/Configuration/NugetConfigFile.cs

View workflow job for this annotation

GitHub Actions / Pack .NET Core Global Tool (CLI) and PluginAPI

Element 'underlyingRepositoryPath' should begin with an uppercase letter (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1300.md)

Check warning on line 91 in src/NuGetForUnity/Editor/Configuration/NugetConfigFile.cs

View workflow job for this annotation

GitHub Actions / Pack .NET Core Global Tool (CLI) and PluginAPI

/// <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

Check warning on line 96 in src/NuGetForUnity/Editor/Configuration/NugetConfigFile.cs

View workflow job for this annotation

GitHub Actions / Pack .NET Core Global Tool (CLI) and PluginAPI

Check warning on line 96 in src/NuGetForUnity/Editor/Configuration/NugetConfigFile.cs

View workflow job for this annotation

GitHub Actions / Pack .NET Core Global Tool (CLI) and PluginAPI

{

Check warning on line 97 in src/NuGetForUnity/Editor/Configuration/NugetConfigFile.cs

View workflow job for this annotation

GitHub Actions / Pack .NET Core Global Tool (CLI) and PluginAPI

get=>underlyingRepositoryPath;
set => underlyingRepositoryPath = savedRepositoryPath = value;
}

/// <summary>
/// Gets the relative path to directory where packages are to be installed. The path is relative to the folder containing the 'NuGet.config' file.
/// </summary>
[NotNull]
public string RelativeRepositoryPath => PathHelper.GetRelativePath(Application.dataPath, RepositoryPath);

/// <summary>
/// Gets the default package source to push NuGet packages to.
Expand Down
89 changes: 89 additions & 0 deletions src/NuGetForUnity/Editor/InstalledPackagesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using JetBrains.Annotations;
using NugetForUnity.Configuration;
using NugetForUnity.Helper;
using NugetForUnity.Models;
using NugetForUnity.PackageSource;
using NugetForUnity.PluginSupport;
Expand Down Expand Up @@ -358,7 +359,95 @@

return roots;
}

/// <summary>
/// Create /Packages/NuGetAssets Directory
/// </summary>
internal static void CreateNuGetAssetsDirectory()
{
var nugetAssetsPath = Path.Combine(UnityPathHelper.AbsoluteProjectPath,Path.Combine("Packages","NuGetAssets"));

Check warning on line 368 in src/NuGetForUnity/Editor/InstalledPackagesManager.cs

View workflow job for this annotation

GitHub Actions / Pack .NET Core Global Tool (CLI) and PluginAPI

Check warning on line 368 in src/NuGetForUnity/Editor/InstalledPackagesManager.cs

View workflow job for this annotation

GitHub Actions / Pack .NET Core Global Tool (CLI) and PluginAPI


if (!Directory.Exists(nugetAssetsPath))
{
Directory.CreateDirectory(nugetAssetsPath);
}

var packageJsonPath = Path.Combine(nugetAssetsPath, "package.json");
if (!File.Exists(packageJsonPath))
{
File.WriteAllText(packageJsonPath,
@"{ ""name"": ""com.dummy.nugetassets"",""version"": ""1.0.0"",""displayName"": ""NuGetAssets"", ""description"": ""NuGetAssets"", ""dependencies"": {}}",
new System.Text.UTF8Encoding());
}
}

/// <summary>
/// Move installed packages
/// </summary>
/// <param name="newPath">Path to move installed packages to.</param>
internal static void Move([NotNull] string newPath)
{
var nugetConfig = ConfigurationManager.NugetConfigFile;
var oldPath = nugetConfig.RepositoryPath;
var installedPackageIds = InstalledPackagesDictionary.Keys;
var dirs = Directory.GetDirectories(oldPath);
try
{
foreach (var subDir in dirs)
{
var relativePath = subDir.Split(Path.DirectorySeparatorChar).Last();
if (installedPackageIds.All(x =>!relativePath.Contains(x)))
{
Debug.Log(relativePath);
break;
}

var newPath1 = Path.Combine(newPath,relativePath);
Directory.Move(subDir,newPath1);
}
}
catch (Exception e)
{
// usually unauthorized access or IO exception (trying to move to a folder where the same file exists)
Debug.LogException(e);
return;
}

foreach (var subDir in dirs)
{
var relativePath = subDir.Split(Path.DirectorySeparatorChar).Last();
if (installedPackageIds.All(x =>!relativePath.Contains(x)))
{
Debug.Log(relativePath);
break;
}

if(Directory.Exists(subDir))

Check warning on line 425 in src/NuGetForUnity/Editor/InstalledPackagesManager.cs

View workflow job for this annotation

GitHub Actions / Pack .NET Core Global Tool (CLI) and PluginAPI

{
Directory.Delete(subDir);
}

// also delete its meta file if it exists
if (File.Exists(Path.Combine(oldPath,$"{relativePath}.meta")))
{
File.Delete(Path.Combine(oldPath,$"{relativePath}.meta"));
}
}

Check warning on line 435 in src/NuGetForUnity/Editor/InstalledPackagesManager.cs

View workflow job for this annotation

GitHub Actions / Pack .NET Core Global Tool (CLI) and PluginAPI

// if the old path is now an empty directory, delete it

Check warning on line 436 in src/NuGetForUnity/Editor/InstalledPackagesManager.cs

View workflow job for this annotation

GitHub Actions / Pack .NET Core Global Tool (CLI) and PluginAPI

if (!Directory.EnumerateFileSystemEntries(oldPath).Any())
{
Directory.Delete(oldPath);

// also delete its meta file if it exists
if (File.Exists($"{oldPath}.meta"))
{
File.Delete($"{oldPath}.meta");
}
}

nugetConfig.RepositoryPath = newPath;
}

private static void AddPackageToInstalledInternal([NotNull] INugetPackage package, ref int manuallyInstalledPackagesNumber)
{
var packages = InstalledPackagesDictionary;
Expand Down
28 changes: 27 additions & 1 deletion src/NuGetForUnity/Editor/Ui/NugetPreferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,40 @@ public override void OnGUI([CanBeNull] string searchContext)
}
}
}

if (shouldShowPackagesConfigPathWarning)
{
EditorGUILayout.HelpBox(
"The packages.config is placed outside of Assets folder, this disables the functionality of automatically restoring packages if the file is changed on the disk.",
MessageType.Warning);
}

using (new EditorGUILayout.HorizontalScope())
{
var repositoryPath = ConfigurationManager.NugetConfigFile.RepositoryPath;

GUILayout.Label(
new GUIContent("Repository path:", $"Absolute path: {repositoryPath}"),
GUILayout.Width(EditorGUIUtility.labelWidth));
GUILayout.Label(ConfigurationManager.NugetConfigFile.RelativeRepositoryPath);
if (GUILayout.Button("Create NuGetAssets Directory", GUILayout.Width(200)))
{
InstalledPackagesManager.CreateNuGetAssetsDirectory();
}
if (GUILayout.Button("Browse", GUILayout.Width(100)))
{
var newPath = EditorUtility.OpenFolderPanel("Select Folder", repositoryPath, string.Empty);

if (!string.IsNullOrEmpty(newPath) && newPath != repositoryPath)
{
InstalledPackagesManager.Move(newPath);
preferencesChangedThisFrame = true;
}
}


}

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