private static void PopulateDefaultValues(ref NugetConfigFile configFile) { // Add default package sources if (configFile.PackageSources == null) { configFile.PackageSources = new List <NugetPackageSource>(); configFile.PackageSources.Add(new NugetPackageSource("NuGet", "http://www.nuget.org/api/v2/")); } if (configFile.ActivePackageSource == null) { configFile.ActivePackageSource = new NugetPackageSource("All", "(Aggregate source)"); } if (string.IsNullOrEmpty(configFile.savedRepositoryPath)) { const string DefaultPath = "./Packages"; configFile.savedRepositoryPath = DefaultPath; configFile.RepositoryPath = Environment.ExpandEnvironmentVariables(DefaultPath); if (!Path.IsPathRooted(configFile.RepositoryPath)) { string repositoryPath = Path.Combine(UnityEngine.Application.dataPath, configFile.RepositoryPath); repositoryPath = Path.GetFullPath(repositoryPath); configFile.RepositoryPath = repositoryPath; } } if (string.IsNullOrEmpty(configFile.DefaultPushSource)) { configFile.DefaultPushSource = "http://www.nuget.org/api/v2/"; } // Default supported platforms if (configFile.SupportedPlatforms == null) { configFile.SupportedPlatforms = new List <NugetPackageSupportedPlatform>(); // Standalone List <string> validStandaloneLibraries = GetBestStandaloneFrameworks(); configFile.SupportedPlatforms.Add(new NugetPackageSupportedPlatform(BuildTargetGroup.Standalone, validStandaloneLibraries)); // WSA configFile.SupportedPlatforms.Add(new NugetPackageSupportedPlatform(BuildTargetGroup.WSA, new List <string>() { "uap10.0" } )); } }
/// <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> /// 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; XDocument file = XDocument.Load(filePath); // 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 (disabled == "true") { var source = configFile.PackageSources.FirstOrDefault(p => p.Name == name); if (source != null) { source.IsEnabled = false; } } } } // 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 (key == "repositoryPath") { configFile.savedRepositoryPath = value; configFile.RepositoryPath = value; if (!Path.IsPathRooted(configFile.RepositoryPath)) { if (configFile.RepositoryPath.StartsWith("./")) { // since ./ is just a relative path to the same directory, replace it configFile.RepositoryPath = configFile.RepositoryPath.Replace("./", "\\"); } configFile.RepositoryPath = Path.GetFullPath((UnityEngine.Application.dataPath + configFile.RepositoryPath).Replace('/', '\\')); } } else if (key == "DefaultPushSource") { configFile.DefaultPushSource = value; } else if (key == "verbose") { configFile.Verbose = bool.Parse(value); } else if (key == "InstallFromCache") { configFile.InstallFromCache = bool.Parse(value); } } } return(configFile); }
public static void PreferencesGUI() { bool preferencesChangedThisFrame = false; EditorGUILayout.LabelField(string.Format("Version: {0}", NuGetForUnityVersion)); if (NugetHelper.NugetConfigFile == null) { NugetHelper.LoadNugetConfigFile(); } bool installFromCache = EditorGUILayout.Toggle("Install From the Cache", NugetHelper.NugetConfigFile.InstallFromCache); if (installFromCache != NugetHelper.NugetConfigFile.InstallFromCache) { preferencesChangedThisFrame = true; NugetHelper.NugetConfigFile.InstallFromCache = installFromCache; } bool readOnlyPackageFiles = EditorGUILayout.Toggle("Read-Only Package Files", NugetHelper.NugetConfigFile.ReadOnlyPackageFiles); if (readOnlyPackageFiles != NugetHelper.NugetConfigFile.ReadOnlyPackageFiles) { preferencesChangedThisFrame = true; NugetHelper.NugetConfigFile.ReadOnlyPackageFiles = readOnlyPackageFiles; } bool verbose = EditorGUILayout.Toggle("Use Verbose Logging", NugetHelper.NugetConfigFile.Verbose); if (verbose != NugetHelper.NugetConfigFile.Verbose) { preferencesChangedThisFrame = true; NugetHelper.NugetConfigFile.Verbose = verbose; } EditorGUILayout.LabelField("Package Sources:"); scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition); NugetPackageSource sourceToMoveUp = null; NugetPackageSource sourceToMoveDown = null; NugetPackageSource sourceToRemove = null; foreach (var source in NugetHelper.NugetConfigFile.PackageSources) { EditorGUILayout.BeginVertical(); { EditorGUILayout.BeginHorizontal(); { EditorGUILayout.BeginVertical(GUILayout.Width(20)); { GUILayout.Space(10); bool isEnabled = EditorGUILayout.Toggle(source.IsEnabled, GUILayout.Width(20)); if (isEnabled != source.IsEnabled) { preferencesChangedThisFrame = true; source.IsEnabled = isEnabled; } } EditorGUILayout.EndVertical(); EditorGUILayout.BeginVertical(); { string name = EditorGUILayout.TextField(source.Name); if (name != source.Name) { preferencesChangedThisFrame = true; source.Name = name; } string savedPath = EditorGUILayout.TextField(source.SavedPath).Trim(); if (savedPath != source.SavedPath) { preferencesChangedThisFrame = true; source.SavedPath = savedPath; } } EditorGUILayout.EndVertical(); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); { GUILayout.Space(29); EditorGUIUtility.labelWidth = 75; EditorGUILayout.BeginVertical(); bool hasPassword = EditorGUILayout.Toggle("Credentials", source.HasPassword); if (hasPassword != source.HasPassword) { preferencesChangedThisFrame = true; source.HasPassword = hasPassword; } if (source.HasPassword) { string userName = EditorGUILayout.TextField("User Name", source.UserName); if (userName != source.UserName) { preferencesChangedThisFrame = true; source.UserName = userName; } string savedPassword = EditorGUILayout.PasswordField("Password", source.SavedPassword); if (savedPassword != source.SavedPassword) { preferencesChangedThisFrame = true; source.SavedPassword = savedPassword; } } else { source.UserName = null; } EditorGUIUtility.labelWidth = 0; EditorGUILayout.EndVertical(); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); { if (GUILayout.Button(string.Format("Move Up"))) { sourceToMoveUp = source; } if (GUILayout.Button(string.Format("Move Down"))) { sourceToMoveDown = source; } if (GUILayout.Button(string.Format("Remove"))) { sourceToRemove = source; } } EditorGUILayout.EndHorizontal(); } EditorGUILayout.EndVertical(); } if (sourceToMoveUp != null) { int index = NugetHelper.NugetConfigFile.PackageSources.IndexOf(sourceToMoveUp); if (index > 0) { NugetHelper.NugetConfigFile.PackageSources[index] = NugetHelper.NugetConfigFile.PackageSources[index - 1]; NugetHelper.NugetConfigFile.PackageSources[index - 1] = sourceToMoveUp; } preferencesChangedThisFrame = true; } if (sourceToMoveDown != null) { int index = NugetHelper.NugetConfigFile.PackageSources.IndexOf(sourceToMoveDown); if (index < NugetHelper.NugetConfigFile.PackageSources.Count - 1) { NugetHelper.NugetConfigFile.PackageSources[index] = NugetHelper.NugetConfigFile.PackageSources[index + 1]; NugetHelper.NugetConfigFile.PackageSources[index + 1] = sourceToMoveDown; } preferencesChangedThisFrame = true; } if (sourceToRemove != null) { NugetHelper.NugetConfigFile.PackageSources.Remove(sourceToRemove); preferencesChangedThisFrame = true; } if (GUILayout.Button(string.Format("Add New Source"))) { NugetHelper.NugetConfigFile.PackageSources.Add(new NugetPackageSource("New Source", "source_path")); preferencesChangedThisFrame = true; } EditorGUILayout.EndScrollView(); if (GUILayout.Button(string.Format("Reset To Default"))) { NugetConfigFile.CreateDefaultFile(NugetHelper.NugetConfigFilePath); NugetHelper.LoadNugetConfigFile(); preferencesChangedThisFrame = true; } if (preferencesChangedThisFrame) { NugetHelper.NugetConfigFile.Save(NugetHelper.NugetConfigFilePath); } }
/// <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; XDocument file = XDocument.Load(filePath); // 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 (disabled == "true") { 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 (add.Attribute("key").Value == "clearTextPassword") { 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 (key == "repositoryPath") { 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 (key == "DefaultPushSource") { configFile.DefaultPushSource = value; } else if (key == "verbose") { configFile.Verbose = bool.Parse(value); } else if (key == "InstallFromCache") { configFile.InstallFromCache = bool.Parse(value); } } } 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); }
#pragma warning restore CS0618 public static void PreferencesGUI() { bool preferencesChangedThisFrame = false; EditorGUILayout.LabelField(string.Format("Version: {0}", NuGetForUnityVersion)); if (NugetHelper.NugetConfigFile == null) { NugetHelper.LoadNugetConfigFile(); } bool installFromCache = EditorGUILayout.Toggle("Install From the Cache", NugetHelper.NugetConfigFile.InstallFromCache); if (installFromCache != NugetHelper.NugetConfigFile.InstallFromCache) { preferencesChangedThisFrame = true; NugetHelper.NugetConfigFile.InstallFromCache = installFromCache; } bool readOnlyPackageFiles = EditorGUILayout.Toggle("Read-Only Package Files", NugetHelper.NugetConfigFile.ReadOnlyPackageFiles); if (readOnlyPackageFiles != NugetHelper.NugetConfigFile.ReadOnlyPackageFiles) { preferencesChangedThisFrame = true; NugetHelper.NugetConfigFile.ReadOnlyPackageFiles = readOnlyPackageFiles; } bool verbose = EditorGUILayout.Toggle("Use Verbose Logging", NugetHelper.NugetConfigFile.Verbose); if (verbose != NugetHelper.NugetConfigFile.Verbose) { preferencesChangedThisFrame = true; NugetHelper.NugetConfigFile.Verbose = verbose; } EditorGUILayout.LabelField("Package Sources:"); scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition); NugetPackageSource sourceToMoveUp = null; NugetPackageSource sourceToMoveDown = null; NugetPackageSource sourceToRemove = null; foreach (var source in NugetHelper.NugetConfigFile.PackageSources) { EditorGUILayout.BeginVertical(); { EditorGUILayout.BeginHorizontal(); { EditorGUILayout.BeginVertical(GUILayout.Width(20)); { GUILayout.Space(10); bool isEnabled = EditorGUILayout.Toggle(source.IsEnabled, GUILayout.Width(20)); if (isEnabled != source.IsEnabled) { preferencesChangedThisFrame = true; source.IsEnabled = isEnabled; } } EditorGUILayout.EndVertical(); EditorGUILayout.BeginVertical(); { string name = EditorGUILayout.TextField(source.Name); if (name != source.Name) { preferencesChangedThisFrame = true; source.Name = name; } string savedPath = EditorGUILayout.TextField(source.SavedPath).Trim(); if (savedPath != source.SavedPath) { preferencesChangedThisFrame = true; source.SavedPath = savedPath; } } EditorGUILayout.EndVertical(); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); { GUILayout.Space(29); EditorGUIUtility.labelWidth = 75; EditorGUILayout.BeginVertical(); bool hasPassword = EditorGUILayout.Toggle("Credentials", source.HasPassword); if (hasPassword != source.HasPassword) { preferencesChangedThisFrame = true; source.HasPassword = hasPassword; } if (source.HasPassword) { string userName = EditorGUILayout.TextField("User Name", source.UserName); if (userName != source.UserName) { preferencesChangedThisFrame = true; source.UserName = userName; } string savedPassword = EditorGUILayout.PasswordField("Password", source.SavedPassword); if (savedPassword != source.SavedPassword) { preferencesChangedThisFrame = true; source.SavedPassword = savedPassword; } } else { source.UserName = null; } EditorGUIUtility.labelWidth = 0; EditorGUILayout.EndVertical(); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); { if (GUILayout.Button(string.Format("Move Up"))) { sourceToMoveUp = source; } if (GUILayout.Button(string.Format("Move Down"))) { sourceToMoveDown = source; } if (GUILayout.Button(string.Format("Remove"))) { sourceToRemove = source; } } EditorGUILayout.EndHorizontal(); } EditorGUILayout.EndVertical(); } if (sourceToMoveUp != null) { int index = NugetHelper.NugetConfigFile.PackageSources.IndexOf(sourceToMoveUp); if (index > 0) { NugetHelper.NugetConfigFile.PackageSources[index] = NugetHelper.NugetConfigFile.PackageSources[index - 1]; NugetHelper.NugetConfigFile.PackageSources[index - 1] = sourceToMoveUp; } preferencesChangedThisFrame = true; } if (sourceToMoveDown != null) { int index = NugetHelper.NugetConfigFile.PackageSources.IndexOf(sourceToMoveDown); if (index < NugetHelper.NugetConfigFile.PackageSources.Count - 1) { NugetHelper.NugetConfigFile.PackageSources[index] = NugetHelper.NugetConfigFile.PackageSources[index + 1]; NugetHelper.NugetConfigFile.PackageSources[index + 1] = sourceToMoveDown; } preferencesChangedThisFrame = true; } if (sourceToRemove != null) { NugetHelper.NugetConfigFile.PackageSources.Remove(sourceToRemove); preferencesChangedThisFrame = true; } if (GUILayout.Button(string.Format("Add New Source"))) { NugetHelper.NugetConfigFile.PackageSources.Add(new NugetPackageSource("New Source", "source_path")); preferencesChangedThisFrame = true; } { EditorGUILayout.LabelField("Supported Libs:"); foreach (var platform in NugetHelper.NugetConfigFile.SupportedPlatforms) { EditorGUILayout.BeginVertical(); { EditorGUILayout.BeginHorizontal(); { EditorGUILayout.BeginVertical(); { BuildTargetGroup buildTarget = BuildTargetGroup.Standalone; EditorGUILayout.BeginHorizontal(); { buildTarget = (BuildTargetGroup)EditorGUILayout.EnumPopup(platform.Platform); if (buildTarget != platform.Platform) { preferencesChangedThisFrame = true; platform.Platform = buildTarget; } if (GUILayout.Button("Remove")) { NugetHelper.NugetConfigFile.SupportedPlatforms.Remove(platform); preferencesChangedThisFrame = true; break; } } EditorGUILayout.EndHorizontal(); EditorGUI.indentLevel++; for (int i = 0; i < platform.LibraryNames.Count; i++) { string libName = platform.LibraryNames[i]; EditorGUILayout.BeginHorizontal(); { string name = EditorGUILayout.TextField(libName); if (name != libName) { preferencesChangedThisFrame = true; platform.LibraryNames[i] = name; } if (GUILayout.Button("Up") && i > 0) { // Swap values platform.LibraryNames[i] = platform.LibraryNames[i - 1]; platform.LibraryNames[i - 1] = libName; } if (GUILayout.Button("Down") && i < (platform.LibraryNames.Count - 1)) { platform.LibraryNames[i] = platform.LibraryNames[i + 1]; platform.LibraryNames[i + 1] = libName; } if (GUILayout.Button("Remove")) { platform.LibraryNames.RemoveAt(i); } } EditorGUILayout.EndHorizontal(); } EditorGUI.indentLevel--; } EditorGUILayout.EndVertical(); } EditorGUILayout.EndHorizontal(); } if (GUILayout.Button(string.Format("Add New Library"))) { platform.LibraryNames.Add("New Library"); preferencesChangedThisFrame = true; } EditorGUILayout.EndVertical(); } if (GUILayout.Button(string.Format("Add New Platform"))) { NugetHelper.NugetConfigFile.SupportedPlatforms.Add(new NugetPackageSupportedPlatform(BuildTargetGroup.Standalone)); preferencesChangedThisFrame = true; } } EditorGUILayout.EndScrollView(); if (GUILayout.Button(string.Format("Reset To Default"))) { NugetConfigFile.CreateDefaultFile(NugetHelper.NugetConfigFilePath); NugetHelper.LoadNugetConfigFile(); preferencesChangedThisFrame = true; } if (preferencesChangedThisFrame) { NugetHelper.NugetConfigFile.Save(NugetHelper.NugetConfigFilePath); } }
private static void DisplayPackageSource(NugetConfigFile configFile) { if (configFile == null) { return; } scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition); bool preferencesChangedThisFrame = false; NugetPackageSource sourceToMoveUp = null; NugetPackageSource sourceToMoveDown = null; NugetPackageSource sourceToRemove = null; foreach (var source in configFile.PackageSources) { preferencesChangedThisFrame = DisplaySourceUI(source, out sourceToMoveUp, out sourceToMoveDown, out sourceToRemove); if (sourceToMoveUp != null) { int index = configFile.PackageSources.IndexOf(sourceToMoveUp); if (index > 0) { configFile.PackageSources[index] = configFile.PackageSources[index - 1]; configFile.PackageSources[index - 1] = sourceToMoveUp; } preferencesChangedThisFrame = true; } if (sourceToMoveDown != null) { int index = configFile.PackageSources.IndexOf(sourceToMoveDown); if (index < configFile.PackageSources.Count - 1) { configFile.PackageSources[index] = configFile.PackageSources[index + 1]; configFile.PackageSources[index + 1] = sourceToMoveDown; } preferencesChangedThisFrame = true; } if (sourceToRemove != null) { configFile.PackageSources.Remove(sourceToRemove); preferencesChangedThisFrame = true; } if (preferencesChangedThisFrame) { configFile.Save(configFile.FilePath); } } if (GUILayout.Button(string.Format("Add New Source"))) { configFile.PackageSources.Add(new NugetPackageSource("New Source", "source_path", 2)); preferencesChangedThisFrame = true; } EditorGUILayout.EndScrollView(); if (GUILayout.Button(string.Format("Reset To Default"))) { NugetConfigFile.CreateDefaultFile(NugetHelper.NugetConfigFilePath); NugetHelper.LoadNugetConfigFile(); preferencesChangedThisFrame = true; } }
/// <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) { var configFile = new NugetConfigFile { PackageSources = new List <NugetPackageSource>(), InstallFromCache = true, ReadOnlyPackageFiles = true }; var file = XDocument.Load(filePath); // read the full list of package sources (some may be disabled below) var 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!) var 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 var disabledPackageSources = file.Root.Element("disabledPackageSources"); if (disabledPackageSources != null) { var adds = disabledPackageSources.Elements("add"); foreach (var add in adds) { var name = add.Attribute("key").Value; var 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 var packageSourceCredentials = file.Root.Element("packageSourceCredentials"); if (packageSourceCredentials != null) { foreach (var sourceElement in packageSourceCredentials.Elements()) { var 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)) { var userName = add.Attribute("value").Value; source.UserName = userName; } if (string.Equals(add.Attribute("key").Value, "clearTextPassword", StringComparison.OrdinalIgnoreCase)) { var password = add.Attribute("value").Value; source.SavedPassword = password; } } } } } // read the configuration data var config = file.Root.Element("config"); if (config != null) { var adds = config.Elements("add"); foreach (var add in adds) { var key = add.Attribute("key").Value; var value = add.Attribute("value").Value; if (string.Equals(key, "repositoryPath", StringComparison.OrdinalIgnoreCase)) { configFile.SavedRepositoryPath = value; configFile.RefreshRepositoryPath(); } 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); } else if (string.Equals(key, "AllowUninstallAll", StringComparison.OrdinalIgnoreCase)) { configFile.AllowUninstallAll = bool.Parse(value); } } } 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; XDocument file = XDocument.Load(filePath); // 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 (disabled == "true") { var source = configFile.PackageSources.FirstOrDefault(p => p.Name == name); if (source != null) { source.IsEnabled = false; } } } } // 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 (key == "repositoryPath") { configFile.savedRepositoryPath = value; configFile.RepositoryPath = value; if (!Path.IsPathRooted(configFile.RepositoryPath)) { if (configFile.RepositoryPath.StartsWith("./")) { // since ./ is just a relative path to the same directory, replace it configFile.RepositoryPath = configFile.RepositoryPath.Replace("./", "\\"); } configFile.RepositoryPath = Path.GetFullPath((UnityEngine.Application.dataPath + configFile.RepositoryPath).Replace('/', '\\')); } } else if (key == "DefaultPushSource") { configFile.DefaultPushSource = value; } else if (key == "verbose") { configFile.Verbose = bool.Parse(value); } else if (key == "InstallFromCache") { configFile.InstallFromCache = bool.Parse(value); } } } return configFile; }
/// <summary> /// Draws the preferences GUI inside the Unity preferences window in the Editor. /// </summary> public static void PreferencesGUI() { EditorGUILayout.LabelField($"Version: {NuGetForUnityVersion}"); var conf = NugetHelper.NugetConfigFile; EditorGUI.BeginChangeCheck(); conf.InstallFromCache = EditorGUILayout.Toggle("Install From the Cache", conf.InstallFromCache); conf.ReadOnlyPackageFiles = EditorGUILayout.Toggle("Read-Only Package Files", conf.ReadOnlyPackageFiles); conf.Verbose = EditorGUILayout.Toggle("Use Verbose Logging", conf.Verbose); conf.SavedRepositoryPath = EditorGUILayout.TextField("Packages Directory", conf.SavedRepositoryPath); conf.AllowUninstallAll = EditorGUILayout.Toggle("Allow Uninstall All", conf.AllowUninstallAll); EditorGUILayout.LabelField("Package Sources:"); scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition); NugetPackageSource sourceToMoveUp = null; NugetPackageSource sourceToMoveDown = null; NugetPackageSource sourceToRemove = null; foreach (var source in conf.PackageSources) { EditorGUILayout.BeginVertical(); { EditorGUILayout.BeginHorizontal(); { EditorGUILayout.BeginVertical(GUILayout.Width(20)); { GUILayout.Space(10); source.IsEnabled = EditorGUILayout.Toggle(source.IsEnabled, GUILayout.Width(20)); } EditorGUILayout.EndVertical(); EditorGUILayout.BeginVertical(); { source.Name = EditorGUILayout.TextField(source.Name); EditorGUI.BeginChangeCheck(); source.SavedPath = EditorGUILayout.TextField(source.SavedPath); if (EditorGUI.EndChangeCheck()) { source.SavedPath = source.SavedPath.Trim(); } } EditorGUILayout.EndVertical(); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); { GUILayout.Space(29); EditorGUIUtility.labelWidth = 75; EditorGUILayout.BeginVertical(); source.HasPassword = EditorGUILayout.Toggle("Credentials", source.HasPassword); if (source.HasPassword) { source.UserName = EditorGUILayout.TextField("User Name", source.UserName); source.SavedPassword = EditorGUILayout.PasswordField("Password", source.SavedPassword); } else { source.UserName = null; } EditorGUIUtility.labelWidth = 0; EditorGUILayout.EndVertical(); } EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); { if (GUILayout.Button("Move Up")) { sourceToMoveUp = source; } if (GUILayout.Button("Move Down")) { sourceToMoveDown = source; } if (GUILayout.Button("Remove")) { sourceToRemove = source; } } EditorGUILayout.EndHorizontal(); } EditorGUILayout.EndVertical(); } if (sourceToMoveUp != null) { var index = conf.PackageSources.IndexOf(sourceToMoveUp); if (index > 0) { conf.PackageSources[index] = conf.PackageSources[index - 1]; conf.PackageSources[index - 1] = sourceToMoveUp; GUI.changed = true; } } if (sourceToMoveDown != null) { var index = conf.PackageSources.IndexOf(sourceToMoveDown); if (index < conf.PackageSources.Count - 1) { conf.PackageSources[index] = conf.PackageSources[index + 1]; conf.PackageSources[index + 1] = sourceToMoveDown; GUI.changed = true; } } if (sourceToRemove != null) { conf.PackageSources.Remove(sourceToRemove); GUI.changed = true; } if (GUILayout.Button("Add New Source")) { conf.PackageSources.Add(new NugetPackageSource("New Source", "source_path")); GUI.changed = true; } EditorGUILayout.EndScrollView(); if (GUILayout.Button("Reset To Default")) { NugetConfigFile.CreateDefaultFile(NugetHelper.NugetConfigFilePath); NugetHelper.ForceReloadNugetConfig(); GUI.changed = true; } if (EditorGUI.EndChangeCheck()) { conf.Save(NugetHelper.NugetConfigFilePath); } }