public void InstallJsonTest() { NugetHelper.UninstallAll(); var json608 = new NugetPackageIdentifier("Newtonsoft.Json", "6.0.8"); Assert.IsTrue(NugetHelper.IsFullyUninstalled(json608), "The package is at least partially installed before we installed it: {0} {1}", json608.Id, json608.Version); var json701 = new NugetPackageIdentifier("Newtonsoft.Json", "7.0.1"); Assert.IsTrue(NugetHelper.IsFullyUninstalled(json701), "The package is at least partially installed before we installed it: {0} {1}", json701.Id, json701.Version); // install a specific version NugetHelper.InstallIdentifier(json608); Assert.IsTrue(NugetHelper.IsInstalled(json608), "The package was NOT installed: {0} {1}", json608.Id, json608.Version); // install a newer version NugetHelper.InstallIdentifier(json701); Assert.IsTrue(NugetHelper.IsInstalled(json701), "The package was NOT installed: {0} {1}", json701.Id, json701.Version); // The previous version should have been removed. Assert.IsTrue(NugetHelper.IsFullyUninstalled(json608), "The package is STILL at least partially installed: {0} {1}", json608.Id, json608.Version); // try to install an old version while a newer is already installed NugetHelper.InstallIdentifier(json608); Assert.IsTrue(NugetHelper.IsInstalled(json701), "The package was NOT installed: {0} {1}", json701.Id, json701.Version); Assert.IsTrue(NugetHelper.IsFullyUninstalled(json608), "The package is installed and should not have been: {0} {1}", json608.Id, json608.Version); NugetHelper.UninstallAll(); Assert.IsTrue(NugetHelper.IsFullyUninstalled(json701), "The package is STILL at least partially installed: {0} {1}", json701.Id, json701.Version); }
private void InstallIdentifier(NugetPackageIdentifier package) { Type type = typeof(NugetHelper); MethodInfo info = type.GetMethod( "InstallIdentifier", BindingFlags.NonPublic | BindingFlags.Static); info.Invoke(null, new object[] { package, false }); }
private bool IsInstalled(NugetPackageIdentifier package) { Type type = typeof(NugetHelper); MethodInfo info = type.GetMethod( "IsInstalled", BindingFlags.NonPublic | BindingFlags.Static); object isInstalled = info.Invoke(null, new object[] { package }); return((bool)isInstalled); }
public void InstallProtobufTest() { var id = new NugetPackageIdentifier("protobuf-net", "2.0.0.668"); NugetHelper.InstallIdentifier(id); Assert.IsTrue(NugetHelper.IsInstalled(id), "The package was NOT installed: {0} {1}", id.Id, id.Version); NugetHelper.Uninstall(id, false); Assert.IsFalse(NugetHelper.IsInstalled(id), "The package is STILL installed: {0} {1}", id.Id, id.Version); }
public void InstallProtobufTest() { var protobuf = new NugetPackageIdentifier("protobuf-net", "2.0.0.668"); // install the package InstallIdentifier(protobuf); Assert.IsTrue(IsInstalled(protobuf), "The package was NOT installed: {0} {1}", protobuf.Id, protobuf.Version); // uninstall the package UninstallAll(); Assert.IsFalse(IsInstalled(protobuf), "The package is STILL installed: {0} {1}", protobuf.Id, protobuf.Version); }
public void InstallSharpConfigTest() { var packageIdentifier = new NugetPackageIdentifier("sharpconfig", "3.2.8"); // install the package NugetHelper.InstallIdentifier(packageIdentifier); Assert.IsTrue(NugetHelper.IsInstalled(packageIdentifier), "The package was NOT installed: {0} {1}", packageIdentifier.Id, packageIdentifier.Version); // uninstall the package NugetHelper.UninstallAll(); Assert.IsTrue(NugetHelper.IsFullyUninstalled(packageIdentifier), "The package is STILL at least partially installed: {0} {1}", packageIdentifier.Id, packageIdentifier.Version); }
public void SimplerInstallJsonTest() { var json = new NugetPackageIdentifier("Newtonsoft.Json", "11.0.2"); // install the package NugetHelper.InstallIdentifier(json); Assert.IsTrue(NugetHelper.IsInstalled(json), "The package was NOT installed: {0} {1}", json.Id, json.Version); // uninstall the package NugetHelper.UninstallAll(); Assert.IsTrue(NugetHelper.IsFullyUninstalled(json), "The package is STILL at least partially installed: {0} {1}", json.Id, json.Version); }
public void InstallProtobufTest() { var protobuf = new NugetPackageIdentifier("protobuf-net", "2.0.0.668"); // install the package NugetHelper.InstallIdentifier(protobuf); Assert.IsTrue(NugetHelper.IsInstalled(protobuf), "The package was NOT installed: {0} {1}", protobuf.Id, protobuf.Version); // uninstall the package NugetHelper.UninstallAll(); Assert.IsTrue(NugetHelper.IsFullyUninstalled(protobuf), "The package is STILL at least partially installed: {0} {1}", protobuf.Id, protobuf.Version); }
public void InstallProtobufViaConfigFileModificationsTest() { var protobuf = new NugetPackageIdentifier("protobuf-net", "2.0.0.668"); NugetHelper.UninstallAll(); Assert.IsTrue(NugetHelper.IsFullyUninstalled(protobuf), "The package is installed before we installed it: {0} {1}", protobuf.Id, protobuf.Version); NugetHelper.PackagesConfigFile.AddPackage(protobuf); NugetHelper.Restore(); Assert.IsTrue(NugetHelper.IsInstalled(protobuf), "The package was NOT installed: {0} {1}", protobuf.Id, protobuf.Version); // uninstall the package NugetHelper.UninstallAll(); Assert.IsTrue(NugetHelper.IsFullyUninstalled(protobuf), "The package is STILL at least partially installed: {0} {1}", protobuf.Id, protobuf.Version); }
public void InstallStyleCopTest() { var styleCopPlusId = new NugetPackageIdentifier("StyleCopPlus.MSBuild", "4.7.49.5"); var styleCopId = new NugetPackageIdentifier("StyleCop.MSBuild", "4.7.49.0"); InstallIdentifier(styleCopPlusId); // StyleCopPlus depends on StyleCop, so they should both be installed // it depends on version 4.7.49.0, so ensure it is also installed Assert.IsTrue(IsInstalled(styleCopPlusId), "The package was NOT installed: {0} {1}", styleCopPlusId.Id, styleCopPlusId.Version); Assert.IsTrue(IsInstalled(styleCopId), "The package was NOT installed: {0} {1}", styleCopId.Id, styleCopId.Version); // cleanup and uninstall everything UninstallAll(); Assert.IsFalse(IsInstalled(styleCopPlusId), "The package is STILL installed: {0} {1}", styleCopPlusId.Id, styleCopPlusId.Version); Assert.IsFalse(IsInstalled(styleCopId), "The package is STILL installed: {0} {1}", styleCopId.Id, styleCopId.Version); }
public void InstallJsonTest() { var id = new NugetPackageIdentifier("Newtonsoft.Json", "6.0.8"); NugetHelper.InstallIdentifier(id); Assert.IsTrue(NugetHelper.IsInstalled(id), "The package was NOT installed: {0} {1}", id.Id, id.Version); // install a newer version id.Version = "7.0.1"; NugetHelper.InstallIdentifier(id); Assert.IsTrue(NugetHelper.IsInstalled(id), "The package was NOT installed: {0} {1}", id.Id, id.Version); // try to install an old version while a newer is already installed id.Version = "6.0.8"; NugetHelper.InstallIdentifier(id); id.Version = "7.0.1"; Assert.IsTrue(NugetHelper.IsInstalled(id), "The package was NOT installed: {0} {1}", id.Id, id.Version); NugetHelper.Uninstall(id, false); Assert.IsFalse(NugetHelper.IsInstalled(id), "The package is STILL installed: {0} {1}", id.Id, id.Version); }
/// <summary> /// Adds a package to the packages.config file. /// </summary> /// <param name="package">The NugetPackage to add to the packages.config file.</param> public void AddPackage(NugetPackageIdentifier package) { NugetPackageIdentifier existingPackage = Packages.Find(p => p.Id == package.Id); if (existingPackage != null) { if (existingPackage < package) { Debug.LogWarningFormat("{0} {1} is already listed in the packages.config file. Updating to {2}", existingPackage.Id, existingPackage.Version, package.Version); Packages.Remove(existingPackage); Packages.Add(package); } else if (existingPackage > package) { Debug.LogWarningFormat("Trying to add {0} {1} to the packages.config file. {2} is already listed, so using that.", package.Id, package.Version, existingPackage.Version); } } else { Packages.Add(package); } }
public void InstallBootstrapCSSTest() { // disable the cache for now to force getting the lowest version of the dependency NugetHelper.NugetConfigFile.InstallFromCache = false; var bootstrap337 = new NugetPackageIdentifier("bootstrap", "3.3.7"); InstallIdentifier(bootstrap337); Assert.IsTrue(IsInstalled(bootstrap337), "The package was NOT installed: {0} {1}", bootstrap337.Id, bootstrap337.Version); // Bootstrap CSS 3.3.7 has a dependency on jQuery [1.9.1, 4.0.0) ... 1.9.1 <= x < 4.0.0 // Therefore it should install 1.9.1 since that is the lowest compatible version available var jQuery191 = new NugetPackageIdentifier("jQuery", "1.9.1"); Assert.IsTrue(IsInstalled(jQuery191), "The package was NOT installed: {0} {1}", jQuery191.Id, jQuery191.Version); // now upgrade jQuery to 3.1.1 var jQuery311 = new NugetPackageIdentifier("jQuery", "3.1.1"); InstallIdentifier(jQuery311); Assert.IsTrue(IsInstalled(jQuery311), "The package was NOT installed: {0} {1}", jQuery311.Id, jQuery311.Version); // reinstall bootstrap, which should use the currently installed jQuery 3.1.1 NugetHelper.Uninstall(bootstrap337, false); InstallIdentifier(bootstrap337); Assert.IsFalse(IsInstalled(jQuery191), "The package IS installed: {0} {1}", jQuery191.Id, jQuery191.Version); Assert.IsTrue(IsInstalled(jQuery311), "The package was NOT installed: {0} {1}", jQuery311.Id, jQuery311.Version); // cleanup and uninstall everything UninstallAll(); // confirm they are uninstalled Assert.IsFalse(IsInstalled(bootstrap337), "The package is STILL installed: {0} {1}", bootstrap337.Id, bootstrap337.Version); Assert.IsFalse(IsInstalled(jQuery191), "The package is STILL installed: {0} {1}", jQuery191.Id, jQuery191.Version); Assert.IsFalse(IsInstalled(jQuery311), "The package is STILL installed: {0} {1}", jQuery311.Id, jQuery311.Version); // turn cache back on NugetHelper.NugetConfigFile.InstallFromCache = true; }
public void InstallJsonTest() { // install a specific version var json608 = new NugetPackageIdentifier("Newtonsoft.Json", "6.0.8"); InstallIdentifier(json608); Assert.IsTrue(IsInstalled(json608), "The package was NOT installed: {0} {1}", json608.Id, json608.Version); // install a newer version var json701 = new NugetPackageIdentifier("Newtonsoft.Json", "7.0.1"); InstallIdentifier(json701); Assert.IsTrue(IsInstalled(json701), "The package was NOT installed: {0} {1}", json701.Id, json701.Version); // try to install an old version while a newer is already installed InstallIdentifier(json608); Assert.IsTrue(IsInstalled(json701), "The package was NOT installed: {0} {1}", json701.Id, json701.Version); UninstallAll(); Assert.IsFalse(IsInstalled(json608), "The package is STILL installed: {0} {1}", json608.Id, json608.Version); Assert.IsFalse(IsInstalled(json701), "The package is STILL installed: {0} {1}", json701.Id, json701.Version); }
public void InstallBootstrapCSSTest() { // disable the cache for now to force getting the lowest version of the dependency NugetHelper.NugetConfigFile.InstallFromCache = false; var bootstrap337 = new NugetPackageIdentifier("bootstrap", "3.3.7"); NugetHelper.InstallIdentifier(bootstrap337); Assert.IsTrue(NugetHelper.IsInstalled(bootstrap337), "The package was NOT installed: {0} {1}", bootstrap337.Id, bootstrap337.Version); // Bootstrap CSS 3.3.7 has a dependency on jQuery [1.9.1, 4.0.0) ... 1.9.1 <= x < 4.0.0 // Therefore it should install 1.9.1 since that is the lowest compatible version available var jQuery191 = new NugetPackageIdentifier("jQuery", "1.9.1"); Assert.IsTrue(NugetHelper.IsInstalled(jQuery191), "The package was NOT installed: {0} {1}", jQuery191.Id, jQuery191.Version); // now upgrade jQuery to 3.1.1 var jQuery311 = new NugetPackageIdentifier("jQuery", "3.1.1"); NugetHelper.InstallIdentifier(jQuery311); Assert.IsTrue(NugetHelper.IsInstalled(jQuery311), "The package was NOT installed: {0} {1}", jQuery311.Id, jQuery311.Version); // reinstall bootstrap, which should use the currently installed jQuery 3.1.1 NugetHelper.Uninstall(bootstrap337, false); NugetHelper.InstallIdentifier(bootstrap337); Assert.IsFalse(NugetHelper.IsInstalled(jQuery191), "The package IS installed: {0} {1}", jQuery191.Id, jQuery191.Version); Assert.IsTrue(NugetHelper.IsInstalled(jQuery311), "The package was NOT installed: {0} {1}", jQuery311.Id, jQuery311.Version); // cleanup and uninstall everything NugetHelper.Uninstall(bootstrap337, false); NugetHelper.Uninstall(jQuery311, false); Assert.IsFalse(NugetHelper.IsInstalled(bootstrap337), "The package IS installed: {0} {1}", bootstrap337.Id, bootstrap337.Version); Assert.IsFalse(NugetHelper.IsInstalled(jQuery191), "The package IS installed: {0} {1}", jQuery191.Id, jQuery191.Version); Assert.IsFalse(NugetHelper.IsInstalled(jQuery311), "The package IS installed: {0} {1}", jQuery311.Id, jQuery311.Version); // turn cache back on NugetHelper.NugetConfigFile.InstallFromCache = true; }
public void InstallSignalRClientTest() { var signalRClient = new NugetPackageIdentifier("Microsoft.AspNet.SignalR.Client", "2.2.2"); NugetHelper.InstallIdentifier(signalRClient); Assert.IsTrue(NugetHelper.IsPackageInstalled(signalRClient), "The package was NOT installed: {0} {1}", signalRClient.Id, signalRClient.Version); var directory45 = Path.Combine(NugetHelper.NugetConfigFile.RepositoryPath, $"{signalRClient.Id}.{signalRClient.Version}\\lib\\net45"); // SignalR 2.2.2 only contains .NET 4.0 and .NET 4.5 libraries, so it should install .NET 4.5 when using .NET 4.6 in Unity, and be empty in other cases if ((int)NugetHelper.DotNetVersion == 3) // 3 = NET_4_6 { Assert.IsTrue(Directory.Exists(directory45), "The directory does NOT exist: {0}", directory45); } else // it must be using .NET 2.0 (actually 3.5 in Unity) { Assert.IsTrue(!Directory.Exists(directory45), "The directory DOES exist: {0}", directory45); } // cleanup and uninstall everything NugetHelper.UninstallAll(); Assert.IsFalse(NugetHelper.IsPackageInstalled(signalRClient), "The package is STILL installed: {0} {1}", signalRClient.Id, signalRClient.Version); }
public void InstallSignalRClientTest() { var signalRClient = new NugetPackageIdentifier("Microsoft.AspNet.SignalR.Client", "2.2.2"); InstallIdentifier(signalRClient); Assert.IsTrue(IsInstalled(signalRClient), "The package was NOT installed: {0} {1}", signalRClient.Id, signalRClient.Version); var directory45 = Path.Combine(NugetHelper.NugetConfigFile.RepositoryPath, string.Format("{0}.{1}\\lib\\net45", signalRClient.Id, signalRClient.Version)); // SignalR 2.2.2 only contains .NET 4.0 and .NET 4.5 libraries, so it should install .NET 4.5 when using .NET 4.6 in Unity, and be empty in other cases if (PlayerSettings.GetApiCompatibilityLevel(EditorUserBuildSettings.selectedBuildTargetGroup) == ApiCompatibilityLevel.NET_4_6) // 3 = NET_4_6 { Assert.IsTrue(Directory.Exists(directory45), "The directory does NOT exist: {0}", directory45); } else // it must be using .NET 2.0 (actually 3.5 in Unity) { Assert.IsTrue(!Directory.Exists(directory45), "The directory DOES exist: {0}", directory45); } // cleanup and uninstall everything UninstallAll(); Assert.IsFalse(IsInstalled(signalRClient), "The package is STILL installed: {0} {1}", signalRClient.Id, signalRClient.Version); }
public void VersionOutOfRangeTest(string versionRange, string version) { var id = new NugetPackageIdentifier("TestPackage", versionRange); Assert.IsFalse(id.InRange(version), "{0} WAS in range of {1}!", version, versionRange); }
public void VersionInRangeTest(string versionRange, string version) { var id = new NugetPackageIdentifier("TestPackage", versionRange); Assert.IsTrue(id.InRange(version), "{0} was NOT in range of {1}!", version, versionRange); }
/// <summary> /// "Uninstalls" the given package by simply deleting its folder. /// </summary> /// <param name="package">The NugetPackage to uninstall.</param> /// <param name="refreshAssets">True to force Unity to refesh its Assets folder. False to temporarily ignore the change. Defaults to true.</param> public static void Uninstall(NugetPackageIdentifier package, bool refreshAssets = true) { LogVerbose("Uninstalling: {0} {1}", package.Id, package.Version); // update the package.config file PackagesConfigFile.RemovePackage(package); PackagesConfigFile.Save(PackagesConfigFilePath); string packageInstallDirectory = Path.Combine(NugetConfigFile.RepositoryPath, string.Format("{0}.{1}", package.Id, package.Version)); DeleteDirectory(packageInstallDirectory); string metaFile = Path.Combine(NugetConfigFile.RepositoryPath, string.Format("{0}.{1}.meta", package.Id, package.Version)); DeleteFile(metaFile); installedPackages.RemoveAll(x => x.Id == package.Id && x.Version == package.Version); if (refreshAssets) AssetDatabase.Refresh(); }
/// <summary> /// Loads a .nuspec file inside the given <see cref="XDocument"/>. /// </summary> /// <param name="nuspecDocument">The .nuspec file as an <see cref="XDocument"/>.</param> /// <returns>The newly loaded <see cref="NuspecFile"/>.</returns> public static NuspecFile Load(XDocument nuspecDocument) { NuspecFile nuspec = new NuspecFile(); string nuspecNamespace = nuspecDocument.Root.GetDefaultNamespace().ToString(); XElement package = nuspecDocument.Element(XName.Get("package", nuspecNamespace)); XElement metadata = package.Element(XName.Get("metadata", nuspecNamespace)); nuspec.Id = (string)metadata.Element(XName.Get("id", nuspecNamespace)) ?? string.Empty; nuspec.Version = (string)metadata.Element(XName.Get("version", nuspecNamespace)) ?? string.Empty; nuspec.Title = (string)metadata.Element(XName.Get("title", nuspecNamespace)) ?? string.Empty; nuspec.Authors = (string)metadata.Element(XName.Get("authors", nuspecNamespace)) ?? string.Empty; nuspec.Owners = (string)metadata.Element(XName.Get("owners", nuspecNamespace)) ?? string.Empty; nuspec.LicenseUrl = (string)metadata.Element(XName.Get("licenseUrl", nuspecNamespace)) ?? string.Empty; nuspec.ProjectUrl = (string)metadata.Element(XName.Get("projectUrl", nuspecNamespace)) ?? string.Empty; nuspec.IconUrl = (string)metadata.Element(XName.Get("iconUrl", nuspecNamespace)) ?? string.Empty; nuspec.RequireLicenseAcceptance = bool.Parse((string)metadata.Element(XName.Get("requireLicenseAcceptance", nuspecNamespace)) ?? "False"); nuspec.Description = (string)metadata.Element(XName.Get("description", nuspecNamespace)) ?? string.Empty; nuspec.ReleaseNotes = (string)metadata.Element(XName.Get("releaseNotes", nuspecNamespace)) ?? string.Empty; nuspec.Copyright = (string)metadata.Element(XName.Get("copyright", nuspecNamespace)); nuspec.Tags = (string)metadata.Element(XName.Get("tags", nuspecNamespace)) ?? string.Empty; nuspec.Dependencies = new List<NugetPackageIdentifier>(); var dependenciesElement = metadata.Element(XName.Get("dependencies", nuspecNamespace)); if (dependenciesElement != null) { foreach (var dependencyElement in dependenciesElement.Elements(XName.Get("dependency", nuspecNamespace))) { NugetPackageIdentifier dependency = new NugetPackageIdentifier(); dependency.Id = (string)dependencyElement.Attribute("id") ?? string.Empty; dependency.Version = (string)dependencyElement.Attribute("version") ?? string.Empty; nuspec.Dependencies.Add(dependency); } } return nuspec; }
/// <summary> /// Removes a package from the packages.config file. /// </summary> /// <param name="package">The NugetPackage to remove from the packages.config file.</param> public void RemovePackage(NugetPackageIdentifier package) { Packages.Remove(package); }
/// <summary> /// Tries to find an already installed package that matches (or is in the range of) the given package ID. /// </summary> /// <param name="packageId">The <see cref="NugetPackageIdentifier"/> of the <see cref="NugetPackage"/> to find.</param> /// <returns>The best <see cref="NugetPackage"/> match, if there is one, otherwise null.</returns> private static NugetPackage GetInstalledPackage(NugetPackageIdentifier packageId) { NugetPackage package = null; foreach (var installedPackage in installedPackages) { if (packageId.Id == installedPackage.Id) { if (packageId.InRange(installedPackage.Version)) { if (packageId.Version != installedPackage.Version) { LogVerbose("Requested {0} {1}, but {2} is already installed, so using that.", packageId.Id, packageId.Version, installedPackage.Version); } package = installedPackage; break; } } } return package; }
/// <summary> /// Cleans up a package after it has been installed. /// Since we are in Unity, we can make certain assumptions on which files will NOT be used, so we can delete them. /// </summary> /// <param name="package">The NugetPackage to clean.</param> private static void Clean(NugetPackageIdentifier package) { string packageInstallDirectory = Path.Combine(NugetConfigFile.RepositoryPath, string.Format("{0}.{1}", package.Id, package.Version)); LogVerbose("Cleaning {0}", packageInstallDirectory); FixSpaces(packageInstallDirectory); // delete a remnant .meta file that may exist from packages created by Unity DeleteFile(packageInstallDirectory + "/" + package.Id + ".nuspec.meta"); // delete directories & files that NuGet normally deletes, but since we are installing "manually" they exist DeleteDirectory(packageInstallDirectory + "/_rels"); DeleteDirectory(packageInstallDirectory + "/package"); DeleteFile(packageInstallDirectory + "/" + package.Id + ".nuspec"); DeleteFile(packageInstallDirectory + "/[Content_Types].xml"); // Unity has no use for the build directory DeleteDirectory(packageInstallDirectory + "/build"); // For now, delete Content. We may use it later... DeleteDirectory(packageInstallDirectory + "/Content"); // For now, delete src. We may use it later... DeleteDirectory(packageInstallDirectory + "/src"); // Delete documentation folders since they sometimes have HTML docs with JavaScript, which Unity tried to parse as "UnityScript" DeleteDirectory(packageInstallDirectory + "/docs"); // Unity can only use .NET 3.5 or lower, so delete everything else if (Directory.Exists(packageInstallDirectory + "/lib")) { //bool has20 = Directory.Exists(packageInstallDirectory + "/lib/net20"); bool has30 = Directory.Exists(packageInstallDirectory + "/lib/net30"); bool has35 = Directory.Exists(packageInstallDirectory + "/lib/net35"); // See here: https://docs.nuget.org/ndocs/schema/target-frameworks List<string> directoriesToDelete = new List<string> { // .NET Framework "net4", "net40", "net403", "net45", "net451", "net452", "net46", "net461", "net462", // .NET Core "netcore", "netcore45", "netcore451", "netcore50", // .NET MicroFramework "netmf", // Windows "win", // windows store "win8", "win81", "win10", // Silverlight "sl", "sl4", "sl5", // Windows Phone "windowsphone", "wp", "wp7", "wp75", "wp8", "wp81", "wpa81", // Universal Windows Platform "uap", "uap10", // .NET Standard // See here: https://blogs.msdn.microsoft.com/dotnet/2016/09/26/introducing-net-standard/ "netstandard", "netstandard1.0", "netstandard1.1", "netstandard1.2", "netstandard1.3", "netstandard1.4", "netstandard1.5", "netstandard1.6", // .NET Core App "netcoreapp", "netcoreapp1.0", "cf", // compact framework "monoandroid", "monotouch", "xamarin.ios10", "xamarin.mac20" }; string[] libDirectories = Directory.GetDirectories(packageInstallDirectory + "/lib"); foreach (var directory in libDirectories) { string directoryName = new DirectoryInfo(directory).Name.ToLower(); if (directoriesToDelete.Any(directoryName.Contains)) { LogVerbose("Deleting unused directory: {0}", directory); DeleteDirectory(directory); } else if (directoryName.Contains("net20") && (has30 || has35)) { // if .NET 2.0 exists, keep it, unless there is also a .NET 3.0 or 3.5 version as well LogVerbose("Deleting net20: {0}", directory); DeleteDirectory(directory); } else if (directoryName.Contains("net30") && has35) { // if .NET 3.0 exists, keep it, unless there is also a .NET 3.5 version as well LogVerbose("Deleting net30: {0}", directory); DeleteDirectory(directory); } } } if (Directory.Exists(packageInstallDirectory + "/tools")) { // Delete all JavaScript in the tools directory since Unity will think it's "UnityScript" DeleteAllFiles(packageInstallDirectory + "/tools", "*.js"); // Delete all DLLs in the tools directory since we can't tell which .NET version they target DeleteAllFiles(packageInstallDirectory + "/tools", "*.dll"); } // delete all PDB files since Unity uses Mono and requires MDB files, which causes it to output "missing MDB" errors DeleteAllFiles(packageInstallDirectory, "*.pdb"); // if there are native DLLs, copy them to the Unity project root (1 up from Assets) if (Directory.Exists(packageInstallDirectory + "/output")) { string[] files = Directory.GetFiles(packageInstallDirectory + "/output"); foreach (string file in files) { string newFilePath = Directory.GetCurrentDirectory() + "/" + Path.GetFileName(file); LogVerbose("Moving {0} to {1}", file, newFilePath); DeleteFile(newFilePath); File.Move(file, newFilePath); } LogVerbose("Deleting {0}", packageInstallDirectory + "/output"); DeleteDirectory(packageInstallDirectory + "/output"); } // if there are Unity plugin DLLs, copy them to the Unity Plugins folder (Assets/Plugins) if (Directory.Exists(packageInstallDirectory + "/unityplugin")) { string pluginsDirectory = Application.dataPath + "/Plugins/"; if (!Directory.Exists(pluginsDirectory)) { Directory.CreateDirectory(pluginsDirectory); } string[] files = Directory.GetFiles(packageInstallDirectory + "/unityplugin"); foreach (string file in files) { string newFilePath = pluginsDirectory + Path.GetFileName(file); try { LogVerbose("Moving {0} to {1}", file, newFilePath); DeleteFile(newFilePath); File.Move(file, newFilePath); } catch (UnauthorizedAccessException) { Debug.LogWarningFormat("{0} couldn't be overwritten. It may be a native plugin already locked by Unity. Please close Unity and manually delete it.", newFilePath); } } LogVerbose("Deleting {0}", packageInstallDirectory + "/unityplugin"); DeleteDirectory(packageInstallDirectory + "/unityplugin"); } // if there are Unity StreamingAssets, copy them to the Unity StreamingAssets folder (Assets/StreamingAssets) if (Directory.Exists(packageInstallDirectory + "/StreamingAssets")) { string streamingAssetsDirectory = Application.dataPath + "/StreamingAssets/"; if (!Directory.Exists(streamingAssetsDirectory)) { Directory.CreateDirectory(streamingAssetsDirectory); } // move the files string[] files = Directory.GetFiles(packageInstallDirectory + "/StreamingAssets"); foreach (string file in files) { string newFilePath = streamingAssetsDirectory + Path.GetFileName(file); try { LogVerbose("Moving {0} to {1}", file, newFilePath); DeleteFile(newFilePath); File.Move(file, newFilePath); } catch (Exception e) { Debug.LogWarningFormat("{0} couldn't be moved. \n{1}", newFilePath, e.ToString()); } } // move the directories string[] directories = Directory.GetDirectories(packageInstallDirectory + "/StreamingAssets"); foreach (string directory in directories) { string newDirectoryPath = streamingAssetsDirectory + new DirectoryInfo(directory).Name; try { LogVerbose("Moving {0} to {1}", directory, newDirectoryPath); if (Directory.Exists(newDirectoryPath)) { DeleteDirectory(newDirectoryPath); } Directory.Move(directory, newDirectoryPath); } catch (Exception e) { Debug.LogWarningFormat("{0} couldn't be moved. \n{1}", newDirectoryPath, e.ToString()); } } // delete the package's StreamingAssets folder and .meta file LogVerbose("Deleting {0}", packageInstallDirectory + "/StreamingAssets"); DeleteDirectory(packageInstallDirectory + "/StreamingAssets"); DeleteFile(packageInstallDirectory + "/StreamingAssets.meta"); } }
private void DrawDepencency(NugetPackageIdentifier dependency) { NugetPackage fullDependency = installedPackages.Find(p => p.Id == dependency.Id); if (fullDependency != null) { DrawPackage(fullDependency); } else { Debug.LogErrorFormat("{0} {1} is not installed!", dependency.Id, dependency.Version); } }
/// <summary> /// Checks if a given package is installed. /// </summary> /// <param name="package">The package to check if is installed.</param> /// <returns>True if the given package is installed. False if it is not.</returns> internal static bool IsInstalled(NugetPackageIdentifier package) { return installedPackages.Any(x => x.Id == package.Id && x.Version == package.Version); }
/// <summary> /// Installs the package given by the identifer. It fetches the appropriate full package from the installed packages, package cache, or package sources and installs it. /// </summary> /// <param name="package">The identifer of the package to install.</param> internal static void InstallIdentifier(NugetPackageIdentifier package) { NugetPackage foundPackage = GetSpecificPackage(package); if (foundPackage != null) { Install(foundPackage, false); } else { Debug.LogErrorFormat("Could not find {0} {1} or greater.", package.Id, package.Version); } }
public void InstallStyleCopTest() { var styleCopPlusId = new NugetPackageIdentifier("StyleCopPlus.MSBuild", "4.7.49.5"); var styleCopId = new NugetPackageIdentifier("StyleCop.MSBuild", "4.7.49.1"); NugetHelper.InstallIdentifier(styleCopPlusId); // StyleCopPlus depends on StyleCop, so they should both be installed // it depends on version 4.7.49.0, but that version doesn't exist. the next closest is 4.7.49.1 Assert.IsTrue(NugetHelper.IsInstalled(styleCopPlusId), "The package was NOT installed: {0} {1}", styleCopPlusId.Id, styleCopPlusId.Version); Assert.IsTrue(NugetHelper.IsInstalled(styleCopId), "The package was NOT installed: {0} {1}", styleCopId.Id, styleCopId.Version); NugetHelper.Uninstall(styleCopPlusId, false); NugetHelper.Uninstall(styleCopId, false); Assert.IsFalse(NugetHelper.IsInstalled(styleCopPlusId), "The package is STILL installed: {0} {1}", styleCopPlusId.Id, styleCopPlusId.Version); Assert.IsFalse(NugetHelper.IsInstalled(styleCopId), "The package is STILL installed: {0} {1}", styleCopId.Id, styleCopId.Version); }
/// <summary> /// Tries to find an "online" (in the package sources - which could be local) package that matches (or is in the range of) the given package ID. /// </summary> /// <param name="packageId">The <see cref="NugetPackageIdentifier"/> of the <see cref="NugetPackage"/> to find.</param> /// <returns>The best <see cref="NugetPackage"/> match, if there is one, otherwise null.</returns> private static NugetPackage GetOnlinePackage(NugetPackageIdentifier packageId) { NugetPackage package = null; // Loop through all active sources and stop once the package is found foreach (var source in packageSources.Where(s => s.IsEnabled)) { var foundPackage = source.GetSpecificPackage(packageId); if (foundPackage != null) { if (package == null) { // if another package hasn't been found yet, use the current found one LogVerbose("{0} {1} was found in {2}, and wanted {3}", foundPackage.Id, foundPackage.Version, source.Name, packageId.Version); package = foundPackage; } else { // another package has been found previously, but neither match identically if (foundPackage < package) { // use the new package if it's closer to the desired version LogVerbose("{0} {1} was found in {2}, but wanted {3}", foundPackage.Id, foundPackage.Version, source.Name, packageId.Version); package = foundPackage; } } } } return package; }
/// <summary> /// Gets a NugetPackage from the NuGet server that matches (or is in range of) the <see cref="NugetPackageIdentifier"/> given. /// If an exact match isn't found, it selects the next closest version available. /// </summary> /// <param name="package">The <see cref="NugetPackageIdentifier"/> containing the ID and Version of the package to get.</param> /// <returns>The retrieved package, if there is one. Null if no matching package was found.</returns> public NugetPackage GetSpecificPackage(NugetPackageIdentifier package) { NugetPackage foundPackage = null; if (IsLocalPath) { string localPackagePath = System.IO.Path.Combine(Path, string.Format("./{0}.{1}.nupkg", package.Id, package.Version)); if (File.Exists(localPackagePath)) { foundPackage = NugetPackage.FromNupkgFile(localPackagePath); } else { // TODO: Sort the local packages? Currently assuming they are in alphabetical order due to the filesystem. // TODO: Optimize to no longer use GetLocalPackages, since that loads the .nupkg itself // Try to find later versions of the same package var packages = GetLocalPackages(package.Id, true, true); foundPackage = packages.SkipWhile(x => !package.InRange(x.Version)).FirstOrDefault(); } } else { // See here: http://www.odata.org/documentation/odata-version-2-0/uri-conventions/ string url = string.Empty; if (!package.HasVersionRange) { url = string.Format("{0}FindPackagesById()?$orderby=Version asc&id='{1}'&$filter=Version ge '{2}'", Path, package.Id, package.Version); } else { url = string.Format("{0}FindPackagesById()?$orderby=Version asc&id='{1}'&$filter=", Path, package.Id); bool hasMin = false; if (!string.IsNullOrEmpty(package.MinimumVersion)) { hasMin = true; if (package.IsMinInclusive) { url += string.Format("Version ge '{0}'", package.MinimumVersion); } else { url += string.Format("Version gt '{0}'", package.MinimumVersion); } } if (!string.IsNullOrEmpty(package.MaximumVersion)) { if (hasMin) { url += " and "; } if (package.IsMaxInclusive) { url += string.Format("Version le '{0}'", package.MaximumVersion); } else { url += string.Format("Version lt '{0}'", package.MaximumVersion); } } else { if (package.IsMaxInclusive) { // if there is no MaxVersion specified, but the Max is Inclusive, then it is an EXACT version match with the stored MINIMUM url += string.Format(" and Version le '{0}'", package.MinimumVersion); } } } foundPackage = GetPackagesFromUrl(url).FirstOrDefault(); } if (foundPackage != null) { foundPackage.PackageSource = this; } return foundPackage; }
/// <summary> /// Builds a list of NugetPackages from the XML returned from the HTTP GET request issued at the given URL. /// Note that NuGet uses an Atom-feed (XML Syndicaton) superset called OData. /// See here http://www.odata.org/documentation/odata-version-2-0/uri-conventions/ /// </summary> /// <param name="url"></param> /// <returns></returns> private List<NugetPackage> GetPackagesFromUrl(string url) { NugetHelper.LogVerbose("Getting packages from: {0}", url); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(url); getRequest.Timeout = 5000; getRequest.ReadWriteTimeout = 5000; Stream responseStream = getRequest.GetResponse().GetResponseStream(); StreamReader objReader = new StreamReader(responseStream); SyndicationFeed atomFeed = SyndicationFeed.Load(XmlReader.Create(objReader)); List<NugetPackage> packages = new List<NugetPackage>(); foreach (var item in atomFeed.Items) { var propertiesExtension = item.ElementExtensions.First(); var reader = propertiesExtension.GetReader(); var properties = (XElement)XDocument.ReadFrom(reader); NugetPackage package = new NugetPackage(); package.PackageSource = this; package.DownloadUrl = ((UrlSyndicationContent)item.Content).Url.ToString(); package.Id = item.Title.Text; package.Title = (string)properties.Element(XName.Get("Title", "http://schemas.microsoft.com/ado/2007/08/dataservices")) ?? string.Empty; package.Version = (string)properties.Element(XName.Get("Version", "http://schemas.microsoft.com/ado/2007/08/dataservices")) ?? string.Empty; package.Description = (string)properties.Element(XName.Get("Description", "http://schemas.microsoft.com/ado/2007/08/dataservices")) ?? string.Empty; package.ReleaseNotes = (string)properties.Element(XName.Get("ReleaseNotes", "http://schemas.microsoft.com/ado/2007/08/dataservices")) ?? string.Empty; package.LicenseUrl = (string)properties.Element(XName.Get("LicenseUrl", "http://schemas.microsoft.com/ado/2007/08/dataservices")) ?? string.Empty; string iconUrl = (string)properties.Element(XName.Get("IconUrl", "http://schemas.microsoft.com/ado/2007/08/dataservices")) ?? string.Empty; if (!string.IsNullOrEmpty(iconUrl)) { package.Icon = NugetHelper.DownloadImage(iconUrl); } // if there is no title, just use the ID as the title if (string.IsNullOrEmpty(package.Title)) { package.Title = package.Id; } // Get dependencies package.Dependencies = new List<NugetPackageIdentifier>(); string rawDependencies = (string)properties.Element(XName.Get("Dependencies", "http://schemas.microsoft.com/ado/2007/08/dataservices")) ?? string.Empty; if (!string.IsNullOrEmpty(rawDependencies)) { string[] dependencies = rawDependencies.Split('|'); foreach (var dependencyString in dependencies) { string[] details = dependencyString.Split(':'); string id = details[0]; string version = details[1]; string framework = string.Empty; if (details.Length > 2) { framework = details[2]; } // some packages (ex: FSharp.Data - 2.1.0) have inproper "semi-empty" dependencies such as: // "Zlib.Portable:1.10.0:portable-net40+sl50+wp80+win80|::net40" // so we need to only add valid dependencies and skip invalid ones if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(version)) { // only use the dependency if there is no framework specified, or it is explicitly .NET 3.0 if (string.IsNullOrEmpty(framework) || framework == "net30") { NugetPackageIdentifier dependency = new NugetPackageIdentifier(id, version); package.Dependencies.Add(dependency); } } } } packages.Add(package); } stopwatch.Stop(); NugetHelper.LogVerbose("Retreived {0} packages in {1} ms", packages.Count, stopwatch.ElapsedMilliseconds); return packages; }
/// <summary> /// Gets a NugetPackage from the NuGet server with the exact ID and Version given. /// If an exact match isn't found, it selects the next closest version available. /// </summary> /// <param name="packageId">The <see cref="NugetPackageIdentifier"/> containing the ID and Version of the package to get.</param> /// <returns>The retrieved package, if there is one. Null if no matching package was found.</returns> private static NugetPackage GetSpecificPackage(NugetPackageIdentifier packageId) { // First look to see if the package is already installed NugetPackage package = GetInstalledPackage(packageId); if (package == null) { // That package isn't installed yet, so look in the cache next package = GetCachedPackage(packageId); } if (package == null) { // It's not in the cache, so we need to look in the active sources package = GetOnlinePackage(packageId); } return package; }
/// <summary> /// Updates a package by uninstalling the currently installed version and installing the "new" version. /// </summary> /// <param name="currentVersion">The current package to uninstall.</param> /// <param name="newVersion">The package to install.</param> /// <param name="refreshAssets">True to refresh the assets inside Unity. False to ignore them (for now). Defaults to true.</param> public static void Update(NugetPackageIdentifier currentVersion, NugetPackage newVersion, bool refreshAssets = true) { LogVerbose("Updating {0} {1} to {2}", currentVersion.Id, currentVersion.Version, newVersion.Version); Uninstall(currentVersion, false); Install(newVersion, refreshAssets); }
/// <summary> /// Tries to find an already cached package that matches (or is in the range of) the given package ID. /// </summary> /// <param name="packageId">The <see cref="NugetPackageIdentifier"/> of the <see cref="NugetPackage"/> to find.</param> /// <returns>The best <see cref="NugetPackage"/> match, if there is one, otherwise null.</returns> private static NugetPackage GetCachedPackage(NugetPackageIdentifier packageId) { NugetPackage package = null; if (NugetHelper.NugetConfigFile.InstallFromCache) { // find all .nupkg files in the cache containing the package ID in the filename List<string> cachedPackages = Directory.GetFiles(NugetHelper.PackOutputDirectory, string.Format("{0}.*.nupkg", packageId.Id)).OrderBy(x => x).ToList(); foreach (var cachedPackage in cachedPackages) { //LogVerbose(cachedPackage); // read the version string from the filename int startIndex = cachedPackage.IndexOf(packageId.Id) + packageId.Id.Length + 1; int endIndex = cachedPackage.IndexOf(".nupkg"); string version = cachedPackage.Substring(startIndex, endIndex - startIndex); //LogVerbose(version); if (packageId.InRange(version)) { // since the packages were sorted in alphabetical order, we take the lowest (first) package that is in range LogVerbose("Found in local cache: {0}", cachedPackage); package = NugetPackage.FromNupkgFile(cachedPackage); break; } } } return package; }