/// <summary> /// Creates a NuGet.config file with the default settings at the given full filepath. /// </summary> /// <param name="filePath">The full filepath where to create the NuGet.config file.</param> /// <returns>The loaded <see cref="NugetConfigFile"/> loaded off of the newly created default file.</returns> public static NugetConfigFile CreateDefaultFile(string filePath) { const string contents = @"<?xml version=""1.0"" encoding=""utf-8""?> <configuration> <packageSources> <clear/> <add key=""NuGet"" value=""http://www.nuget.org/api/v2/"" /> </packageSources> <disabledPackageSources /> <activePackageSource> <add key=""All"" value=""(Aggregate source)"" /> </activePackageSource> <config> <add key=""repositoryPath"" value=""./Packages"" /> <add key=""DefaultPushSource"" value=""http://www.nuget.org/api/v2/"" /> </config> </configuration>"; File.WriteAllText(filePath, contents, new UTF8Encoding()); AssetDatabase.Refresh(); NugetHelper.DisableWSAPExportSetting(filePath, false); return(Load(filePath)); }
/// <summary> /// Loads a list of all currently installed packages by reading the packages.config file. /// </summary> /// <returns>A newly created <see cref="PackagesConfigFile"/>.</returns> public static PackagesConfigFile Load(string filepath) { PackagesConfigFile configFile = new PackagesConfigFile { Packages = new List <NugetPackageIdentifier>() }; // Create a package.config file, if there isn't already one in the project if (!File.Exists(filepath)) { Debug.LogFormat("No packages.config file found. Creating default at {0}", filepath); configFile.Save(filepath); AssetDatabase.Refresh(); } XDocument packagesFile = XDocument.Load(filepath); // Force disable NugetHelper.DisableWSAPExportSetting(filepath, false); foreach (XElement packageElement in packagesFile.Root.Elements()) { NugetPackage package = new NugetPackage { Id = packageElement.Attribute("id").Value, Version = packageElement.Attribute("version").Value }; configFile.Packages.Add(package); } return(configFile); }
/// <summary> /// Saves the packages.config file and populates it with given installed NugetPackages. /// </summary> /// <param name="filepath">The filepath to where this packages.config will be saved.</param> public void Save(string filepath) { Packages.Sort(delegate(NugetPackageIdentifier x, NugetPackageIdentifier y) { if (x.Id == null && y.Id == null) { return(0); } if (x.Id == null) { return(-1); } if (y.Id == null) { return(1); } return(x.Id == y.Id ? string.Compare(x.Version, y.Version, StringComparison.Ordinal) : string.Compare(x.Id, y.Id, StringComparison.Ordinal)); }); XDocument packagesFile = new XDocument(); packagesFile.Add(new XElement("packages")); foreach (NugetPackageIdentifier package in Packages) { XElement packageElement = new XElement("package"); packageElement.Add(new XAttribute("id", package.Id)); packageElement.Add(new XAttribute("version", package.Version)); packagesFile.Root?.Add(packageElement); } // remove the read only flag on the file, if there is one. bool packageExists = File.Exists(filepath); if (packageExists) { FileAttributes attributes = File.GetAttributes(filepath); if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { attributes &= ~FileAttributes.ReadOnly; File.SetAttributes(filepath, attributes); } } var directoryInfo = new FileInfo(filepath).Directory; if (directoryInfo != null) { Directory.CreateDirectory(directoryInfo.FullName); } packagesFile.Save(filepath); NugetHelper.DisableWSAPExportSetting(filepath, packageExists); }
/// <summary> /// Saves the packages.config file and populates it with given installed NugetPackages. /// </summary> /// <param name="filepath">The filepath to where this packages.config will be saved.</param> public void Save(string filepath) { Packages.Sort(delegate(NugetPackageIdentifier x, NugetPackageIdentifier y) { if (x.Id == null && y.Id == null) { return(0); } else if (x.Id == null) { return(-1); } else if (y.Id == null) { return(1); } else if (x.Id == y.Id) { return(x.Version.CompareTo(y.Version)); } else { return(x.Id.CompareTo(y.Id)); } }); XDocument packagesFile = new XDocument(); packagesFile.Add(new XElement("packages")); foreach (NugetPackageIdentifier package in Packages) { XElement packageElement = new XElement("package"); packageElement.Add(new XAttribute("id", package.Id)); packageElement.Add(new XAttribute("version", package.Version)); packagesFile.Root.Add(packageElement); } // remove the read only flag on the file, if there is one. bool packageExists = File.Exists(filepath); if (packageExists) { FileAttributes attributes = File.GetAttributes(filepath); if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { attributes &= ~FileAttributes.ReadOnly; File.SetAttributes(filepath, attributes); } } packagesFile.Save(filepath); NugetHelper.DisableWSAPExportSetting(filepath, packageExists); }
/// <summary> /// Creates a NuGet.config file with the default settings at the given full filepath. /// </summary> /// <param name="filePath">The full filepath where to create the NuGet.config file.</param> /// <returns>The loaded <see cref="NugetConfigFile"/> loaded off of the newly created default file.</returns> public static NugetConfigFile CreateDefaultFile(string filePath) { NugetConfigFile configFile = new NugetConfigFile(); configFile.InstallFromCache = true; configFile.ReadOnlyPackageFiles = true; // Add the default values PopulateDefaultValues(ref configFile); configFile.Save(filePath); AssetDatabase.Refresh(); NugetHelper.DisableWSAPExportSetting(filePath, false); return(configFile); }
/// <summary> /// Saves this NuGet.config file to disk. /// </summary> /// <param name="filepath">The filepath to where this NuGet.config will be saved.</param> public void Save(string filepath) { XDocument configFile = new XDocument(); XElement packageSources = new XElement("packageSources"); XElement disabledPackageSources = new XElement("disabledPackageSources"); XElement packageSourceCredentials = new XElement("packageSourceCredentials"); XElement addElement; // save all enabled and disabled package sources foreach (var source in PackageSources) { addElement = new XElement("add"); addElement.Add(new XAttribute("key", source.Name)); addElement.Add(new XAttribute("value", source.SavedPath)); packageSources.Add(addElement); if (!source.IsEnabled) { addElement = new XElement("add"); addElement.Add(new XAttribute("key", source.Name)); addElement.Add(new XAttribute("value", "true")); disabledPackageSources.Add(addElement); } if (source.HasPassword) { XElement sourceElement = new XElement(source.Name); packageSourceCredentials.Add(sourceElement); addElement = new XElement("add"); addElement.Add(new XAttribute("key", "userName")); addElement.Add(new XAttribute("value", source.UserName ?? string.Empty)); sourceElement.Add(addElement); addElement = new XElement("add"); addElement.Add(new XAttribute("key", "clearTextPassword")); addElement.Add(new XAttribute("value", source.SavedPassword)); sourceElement.Add(addElement); } } // save the active package source (may be an aggregate) XElement activePackageSource = new XElement("activePackageSource"); addElement = new XElement("add"); addElement.Add(new XAttribute("key", "All")); addElement.Add(new XAttribute("value", "(Aggregate source)")); activePackageSource.Add(addElement); XElement config = new XElement("config"); // save the un-expanded respository path addElement = new XElement("add"); addElement.Add(new XAttribute("key", "repositoryPath")); addElement.Add(new XAttribute("value", savedRepositoryPath)); config.Add(addElement); // save the default push source if (DefaultPushSource != null) { addElement = new XElement("add"); addElement.Add(new XAttribute("key", "DefaultPushSource")); addElement.Add(new XAttribute("value", DefaultPushSource)); config.Add(addElement); } if (Verbose) { addElement = new XElement("add"); addElement.Add(new XAttribute("key", "verbose")); addElement.Add(new XAttribute("value", Verbose.ToString().ToLower())); config.Add(addElement); } if (!InstallFromCache) { addElement = new XElement("add"); addElement.Add(new XAttribute("key", "InstallFromCache")); addElement.Add(new XAttribute("value", InstallFromCache.ToString().ToLower())); config.Add(addElement); } if (!ReadOnlyPackageFiles) { addElement = new XElement("add"); addElement.Add(new XAttribute("key", "ReadOnlyPackageFiles")); addElement.Add(new XAttribute("value", ReadOnlyPackageFiles.ToString().ToLower())); config.Add(addElement); } XElement supportedPlatforms = new XElement("supportedPlatforms"); if (SupportedPlatforms != null) { // Add default supported platforms XElement platform; foreach (var supportedPlatform in SupportedPlatforms) { platform = new XElement("platform"); platform.Add(new XAttribute("name", supportedPlatform.Platform.ToString())); foreach (var library in supportedPlatform.LibraryNames) { addElement = new XElement("nugetLib"); addElement.Add(new XAttribute("name", library)); platform.Add(addElement); } supportedPlatforms.Add(platform); } } XElement configuration = new XElement("configuration"); configuration.Add(packageSources); configuration.Add(disabledPackageSources); configuration.Add(packageSourceCredentials); configuration.Add(activePackageSource); configuration.Add(config); configuration.Add(supportedPlatforms); configFile.Add(configuration); bool fileExists = File.Exists(filepath); // remove the read only flag on the file, if there is one. if (fileExists) { FileAttributes attributes = File.GetAttributes(filepath); if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { attributes &= ~FileAttributes.ReadOnly; File.SetAttributes(filepath, attributes); } } configFile.Save(filepath); NugetHelper.DisableWSAPExportSetting(filepath, fileExists); }
/// <summary> /// Loads a NuGet.config file at the given filepath. /// </summary> /// <param name="filePath">The full filepath to the NuGet.config file to load.</param> /// <returns>The newly loaded <see cref="NugetConfigFile"/>.</returns> public static NugetConfigFile Load(string filePath) { NugetConfigFile configFile = new NugetConfigFile(); configFile.PackageSources = new List <NugetPackageSource>(); configFile.InstallFromCache = true; configFile.ReadOnlyPackageFiles = false; XDocument file = XDocument.Load(filePath); // Force disable NugetHelper.DisableWSAPExportSetting(filePath, false); // read the full list of package sources (some may be disabled below) XElement packageSources = file.Root.Element("packageSources"); if (packageSources != null) { var adds = packageSources.Elements("add"); foreach (var add in adds) { configFile.PackageSources.Add(new NugetPackageSource(add.Attribute("key").Value, add.Attribute("value").Value)); } } // read the active package source (may be an aggregate of all enabled sources!) XElement activePackageSource = file.Root.Element("activePackageSource"); if (activePackageSource != null) { var add = activePackageSource.Element("add"); configFile.ActivePackageSource = new NugetPackageSource(add.Attribute("key").Value, add.Attribute("value").Value); } // disable all listed disabled package sources XElement disabledPackageSources = file.Root.Element("disabledPackageSources"); if (disabledPackageSources != null) { var adds = disabledPackageSources.Elements("add"); foreach (var add in adds) { string name = add.Attribute("key").Value; string disabled = add.Attribute("value").Value; if (String.Equals(disabled, "true", StringComparison.OrdinalIgnoreCase)) { var source = configFile.PackageSources.FirstOrDefault(p => p.Name == name); if (source != null) { source.IsEnabled = false; } } } } // set all listed passwords for package source credentials XElement packageSourceCredentials = file.Root.Element("packageSourceCredentials"); if (packageSourceCredentials != null) { foreach (var sourceElement in packageSourceCredentials.Elements()) { string name = sourceElement.Name.LocalName; var source = configFile.PackageSources.FirstOrDefault(p => p.Name == name); if (source != null) { var adds = sourceElement.Elements("add"); foreach (var add in adds) { if (string.Equals(add.Attribute("key").Value, "userName", StringComparison.OrdinalIgnoreCase)) { string userName = add.Attribute("value").Value; source.UserName = userName; } if (string.Equals(add.Attribute("key").Value, "clearTextPassword", StringComparison.OrdinalIgnoreCase)) { string password = add.Attribute("value").Value; source.SavedPassword = password; } } } } } // read the configuration data XElement config = file.Root.Element("config"); if (config != null) { var adds = config.Elements("add"); foreach (var add in adds) { string key = add.Attribute("key").Value; string value = add.Attribute("value").Value; if (String.Equals(key, "repositoryPath", StringComparison.OrdinalIgnoreCase)) { configFile.savedRepositoryPath = value; configFile.RepositoryPath = Environment.ExpandEnvironmentVariables(value); if (!Path.IsPathRooted(configFile.RepositoryPath)) { string repositoryPath = Path.Combine(UnityEngine.Application.dataPath, configFile.RepositoryPath); repositoryPath = Path.GetFullPath(repositoryPath); configFile.RepositoryPath = repositoryPath; } } else if (String.Equals(key, "DefaultPushSource", StringComparison.OrdinalIgnoreCase)) { configFile.DefaultPushSource = value; } else if (String.Equals(key, "verbose", StringComparison.OrdinalIgnoreCase)) { configFile.Verbose = bool.Parse(value); } else if (String.Equals(key, "InstallFromCache", StringComparison.OrdinalIgnoreCase)) { configFile.InstallFromCache = bool.Parse(value); } else if (String.Equals(key, "ReadOnlyPackageFiles", StringComparison.OrdinalIgnoreCase)) { configFile.ReadOnlyPackageFiles = bool.Parse(value); } } } // read the supported platforms XElement supportedPlatforms = file.Root.Element("supportedPlatforms"); if (supportedPlatforms != null) { configFile.SupportedPlatforms = new List <NugetPackageSupportedPlatform>(); var platforms = supportedPlatforms.Elements("platform"); foreach (var platform in platforms) { string platformName = platform.Attribute("name").Value; List <string> nugetLibs = new List <string>(); var libs = platform.Elements("nugetLib"); foreach (var lib in libs) { string libName = lib.Attribute("name").Value; nugetLibs.Add(libName); } BuildTargetGroup buildTarget = (BuildTargetGroup)Enum.Parse(typeof(BuildTargetGroup), platformName); configFile.SupportedPlatforms.Add(new NugetPackageSupportedPlatform(buildTarget, nugetLibs)); } } // Add any default values : this will help when adding new data to the config file that should be supported PopulateDefaultValues(ref configFile); return(configFile); }
/// <summary> /// Loads a NuGet.config file at the given filepath. /// </summary> /// <param name="filePath">The full filepath to the NuGet.config file to load.</param> /// <returns>The newly loaded <see cref="NugetConfigFile"/>.</returns> public static NugetConfigFile Load(string filePath) { NugetConfigFile configFile = new NugetConfigFile(); configFile.PackageSources = new List <NugetPackageSource>(); configFile.InstallFromCache = true; configFile.ReadOnlyPackageFiles = false; configFile.FilePath = filePath; XDocument file = XDocument.Load(filePath); // Force disable NugetHelper.DisableWSAPExportSetting(filePath, false); // read the full list of package sources (some may be disabled below) XElement packageSources = file.Root.Element("packageSources"); if (packageSources != null) { var adds = packageSources.Elements("add"); foreach (var add in adds) { int protocolVersion = 2; if (add.Attribute("protocolVersion") != null) { protocolVersion = int.Parse(add.Attribute("protocolVersion").Value); } configFile.PackageSources.Add(new NugetPackageSource(add.Attribute("key").Value, add.Attribute("value").Value, protocolVersion)); } } // read the active package source (may be an aggregate of all enabled sources!) XElement activePackageSource = file.Root.Element("activePackageSource"); if (activePackageSource != null) { var add = activePackageSource.Element("add"); int protocolVersion = 2; if (add.Attribute("protocolVersion") != null) { protocolVersion = int.Parse(add.Attribute("protocolVersion").Value); } configFile.ActivePackageSource = new NugetPackageSource(add.Attribute("key").Value, add.Attribute("value").Value, protocolVersion); } // disable all listed disabled package sources XElement disabledPackageSources = file.Root.Element("disabledPackageSources"); if (disabledPackageSources != null) { var adds = disabledPackageSources.Elements("add"); foreach (var add in adds) { string name = add.Attribute("key").Value; string disabled = add.Attribute("value").Value; if (String.Equals(disabled, "true", StringComparison.OrdinalIgnoreCase)) { var source = configFile.PackageSources.FirstOrDefault(p => p.Name == name); if (source != null) { source.IsEnabled = false; } } } } // set all listed passwords for package source credentials XElement packageSourceCredentials = file.Root.Element("packageSourceCredentials"); if (packageSourceCredentials != null) { foreach (var sourceElement in packageSourceCredentials.Elements()) { string name = sourceElement.Name.LocalName.Replace("_", " "); var source = configFile.PackageSources.FirstOrDefault(p => p.Name == name); if (source != null) { var adds = sourceElement.Elements("add"); foreach (var add in adds) { if (string.Equals(add.Attribute("key").Value, "userName", StringComparison.OrdinalIgnoreCase)) { string userName = add.Attribute("value").Value; source.UserName = userName; } if (string.Equals(add.Attribute("key").Value, "clearTextPassword", StringComparison.OrdinalIgnoreCase)) { string password = add.Attribute("value").Value; source.SavedPassword = password; } } } } } // read the configuration data XElement config = file.Root.Element("config"); if (config != null) { var adds = config.Elements("add"); foreach (var add in adds) { string key = add.Attribute("key").Value; string value = add.Attribute("value").Value; if (String.Equals(key, "repositoryPath", StringComparison.OrdinalIgnoreCase)) { configFile.savedRepositoryPath = value; configFile.RepositoryPath = Environment.ExpandEnvironmentVariables(value); if (!Path.IsPathRooted(configFile.RepositoryPath)) { string repositoryPath = Path.Combine(UnityEngine.Application.dataPath, configFile.RepositoryPath); repositoryPath = Path.GetFullPath(repositoryPath); configFile.RepositoryPath = repositoryPath; } } else if (String.Equals(key, "DefaultPushSource", StringComparison.OrdinalIgnoreCase)) { configFile.DefaultPushSource = value; } else if (String.Equals(key, "verbose", StringComparison.OrdinalIgnoreCase)) { configFile.Verbose = bool.Parse(value); } else if (String.Equals(key, "InstallFromCache", StringComparison.OrdinalIgnoreCase)) { configFile.InstallFromCache = bool.Parse(value); } else if (String.Equals(key, "ReadOnlyPackageFiles", StringComparison.OrdinalIgnoreCase)) { configFile.ReadOnlyPackageFiles = bool.Parse(value); } } } return(configFile); }