Пример #1
0
        /// <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.ToLower() == package.Id.ToLower());

            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);
            }
        }
Пример #2
0
        /// <summary>
        /// Use the Unity GUI to draw the controls.
        /// </summary>
        protected void OnGUI()
        {
            if (_nuspec == null)
            {
                Reload();
            }

            if (_nuspec == null)
            {
                titleContent = new GUIContent("[NO NUSPEC]");
                EditorGUILayout.LabelField("There is no .nuspec file selected.");
            }
            else
            {
                EditorGUIUtility.labelWidth = 100;
                _nuspec.Id         = EditorGUILayout.TextField(new GUIContent("ID", "The name of the package."), _nuspec.Id);
                _nuspec.Version    = EditorGUILayout.TextField(new GUIContent("Version", "The semantic version of the package."), _nuspec.Version);
                _nuspec.Authors    = EditorGUILayout.TextField(new GUIContent("Authors", "The authors of the package."), _nuspec.Authors);
                _nuspec.Owners     = EditorGUILayout.TextField(new GUIContent("Owners", "The owners of the package."), _nuspec.Owners);
                _nuspec.LicenseUrl = EditorGUILayout.TextField(new GUIContent("License URL", "The URL for the license of the package."), _nuspec.LicenseUrl);
                _nuspec.ProjectUrl = EditorGUILayout.TextField(new GUIContent("Project URL", "The URL of the package project."), _nuspec.ProjectUrl);
                _nuspec.IconUrl    = EditorGUILayout.TextField(new GUIContent("Icon URL", "The URL for the icon of the package."), _nuspec.IconUrl);
                _nuspec.RequireLicenseAcceptance = EditorGUILayout.Toggle(new GUIContent("Require License Acceptance", "Does the package license need to be accepted before use?"), _nuspec.RequireLicenseAcceptance);
                _nuspec.Description  = EditorGUILayout.TextField(new GUIContent("Description", "The description of the package."), _nuspec.Description);
                _nuspec.Summary      = EditorGUILayout.TextField(new GUIContent("Summary", "The brief description of the package."), _nuspec.Summary);
                _nuspec.ReleaseNotes = EditorGUILayout.TextField(new GUIContent("Release Notes", "The release notes for this specific version of the package."), _nuspec.ReleaseNotes);
                _nuspec.Copyright    = EditorGUILayout.TextField(new GUIContent("Copyright", "The copyright details for the package."), _nuspec.Copyright);
                _nuspec.Tags         = EditorGUILayout.TextField(new GUIContent("Tags", "The space-delimited list of tags and keywords that describe the package and aid discoverability of packages through search and filtering."), _nuspec.Tags);

                _dependenciesExpanded = EditorGUILayout.Foldout(_dependenciesExpanded, new GUIContent("Dependencies", "The list of NuGet packages that this packages depends on."));

                if (_dependenciesExpanded)
                {
                    EditorGUILayout.BeginHorizontal();
                    {
                        GUILayout.Space(50);

                        // automatically fill in the dependencies based upon the "root" packages currently installed in the project
                        if (GUILayout.Button(new GUIContent("Automatically Fill Dependencies", "Populates the list of dependencies with the \"root\" NuGet packages currently installed in the project.")))
                        {
                            NugetHelper.UpdateInstalledPackages();
                            List <NugetPackage> installedPackages = NugetHelper.InstalledPackages.ToList();

                            // default all packages to being roots
                            List <NugetPackage> roots = new List <NugetPackage>(installedPackages);

                            // remove a package as a root if another package is dependent on it
                            foreach (NugetPackage package in installedPackages)
                            {
                                NugetFrameworkGroup packageFrameworkGroup = NugetHelper.GetBestDependencyFrameworkGroupForCurrentSettings(package);
                                foreach (NugetPackageIdentifier dependency in packageFrameworkGroup.Dependencies)
                                {
                                    roots.RemoveAll(p => p.Id == dependency.Id);
                                }
                            }

                            // remove all existing dependencies from the .nuspec
                            _nuspec.Dependencies.Clear();

                            _nuspec.Dependencies.Add(new NugetFrameworkGroup());
                            _nuspec.Dependencies[0].Dependencies = roots.Cast <NugetPackageIdentifier>().ToList();
                        }
                    }
                    EditorGUILayout.EndHorizontal();

                    // display the dependencies
                    NugetPackageIdentifier toDelete             = null;
                    NugetFrameworkGroup    nuspecFrameworkGroup = NugetHelper.GetBestDependencyFrameworkGroupForCurrentSettings(_nuspec);
                    foreach (var dependency in nuspecFrameworkGroup.Dependencies)
                    {
                        EditorGUILayout.BeginHorizontal();
                        GUILayout.Space(75);
                        float prevLabelWidth = EditorGUIUtility.labelWidth;
                        EditorGUIUtility.labelWidth = 50;
                        dependency.Id = EditorGUILayout.TextField(new GUIContent("ID", "The ID of the dependency package."), dependency.Id);
                        EditorGUILayout.EndHorizontal();

                        //int oldSeletedIndex = IndexOf(ref existingComponents, dependency.Id);
                        //int newSelectIndex = EditorGUILayout.Popup("Name", oldSeletedIndex, existingComponents);
                        //if (oldSeletedIndex != newSelectIndex)
                        //{
                        //    dependency.Name = existingComponents[newSelectIndex];
                        //}

                        EditorGUILayout.BeginHorizontal();
                        GUILayout.Space(75);
                        dependency.Version = EditorGUILayout.TextField(new GUIContent("Version", "The version number of the dependency package. (specify ranges with =><)"), dependency.Version);
                        EditorGUILayout.EndHorizontal();

                        EditorGUILayout.BeginHorizontal();
                        {
                            GUILayout.Space(75);

                            if (GUILayout.Button("Remove " + dependency.Id))
                            {
                                toDelete = dependency;
                            }
                        }
                        EditorGUILayout.EndHorizontal();

                        EditorGUILayout.Separator();

                        EditorGUIUtility.labelWidth = prevLabelWidth;
                    }

                    if (toDelete != null)
                    {
                        nuspecFrameworkGroup.Dependencies.Remove(toDelete);
                    }

                    EditorGUILayout.BeginHorizontal();
                    {
                        GUILayout.Space(50);

                        if (GUILayout.Button("Add Dependency"))
                        {
                            nuspecFrameworkGroup.Dependencies.Add(new NugetPackageIdentifier());
                        }
                    }
                    EditorGUILayout.EndHorizontal();
                }

                EditorGUILayout.Separator();

                if (GUILayout.Button($"Save {Path.GetFileName(_filepath)}"))
                {
                    _nuspec.Save(_filepath);
                }

                EditorGUILayout.Separator();

                if (GUILayout.Button(string.Format("Pack {0}.nupkg", Path.GetFileNameWithoutExtension(_filepath))))
                {
                    NugetHelper.Pack(_filepath);
                }

                EditorGUILayout.Separator();

                _apiKey = EditorGUILayout.TextField(new GUIContent("API Key", "The API key to use when pushing the package to the server"), _apiKey);

                if (GUILayout.Button("Push to Server"))
                {
                    NugetHelper.Push(_nuspec, _filepath, _apiKey);
                }
            }
        }
Пример #3
0
        /// <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();

            Debug.Assert(nuspecDocument.Root != null, "nuspecDocument.Root != null");
            string nuspecNamespace = nuspecDocument.Root.GetDefaultNamespace().ToString();

            XElement package = nuspecDocument.Element(XName.Get("package", nuspecNamespace));

            Debug.Assert(package != null, nameof(package) + " != null");
            XElement metadata = package.Element(XName.Get("metadata", nuspecNamespace));

            Debug.Assert(metadata != null, nameof(metadata) + " != null");
            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.Summary      = (string)metadata.Element(XName.Get("summary", 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;

            var repositoryElement = metadata.Element(XName.Get("repository", nuspecNamespace));

            if (repositoryElement != null)
            {
                nuspec.RepositoryType   = (string)repositoryElement.Attribute(XName.Get("type")) ?? string.Empty;
                nuspec.RepositoryUrl    = (string)repositoryElement.Attribute(XName.Get("url")) ?? string.Empty;
                nuspec.RepositoryBranch = (string)repositoryElement.Attribute(XName.Get("branch")) ?? string.Empty;
                nuspec.RepositoryCommit = (string)repositoryElement.Attribute(XName.Get("commit")) ?? string.Empty;
            }

            var dependenciesElement = metadata.Element(XName.Get("dependencies", nuspecNamespace));

            if (dependenciesElement != null)
            {
                // Dependencies specified for specific target frameworks
                foreach (var frameworkGroup in dependenciesElement.Elements(XName.Get("group", nuspecNamespace)))
                {
                    NugetFrameworkGroup group = new NugetFrameworkGroup();
                    group.TargetFramework = ConvertFromNupkgTargetFrameworkName((string)frameworkGroup.Attribute("targetFramework") ?? string.Empty);

                    foreach (var dependencyElement in frameworkGroup.Elements(XName.Get("dependency", nuspecNamespace)))
                    {
                        NugetPackageIdentifier dependency =
                            new NugetPackageIdentifier
                        {
                            Id = (string)dependencyElement
                                 .Attribute("id") ?? string.Empty,
                            Version =
                                (string)dependencyElement.Attribute(
                                    "version") ?? string.Empty
                        };

                        group.Dependencies.Add(dependency);
                    }

                    nuspec.Dependencies.Add(group);
                }

                // Flat dependency list
                if (nuspec.Dependencies.Count == 0)
                {
                    NugetFrameworkGroup group = new NugetFrameworkGroup();
                    foreach (var dependencyElement in dependenciesElement.Elements(XName.Get("dependency", nuspecNamespace)))
                    {
                        NugetPackageIdentifier dependency =
                            new NugetPackageIdentifier
                        {
                            Id = (string)dependencyElement
                                 .Attribute("id") ?? string.Empty,
                            Version =
                                (string)dependencyElement.Attribute(
                                    "version") ?? string.Empty
                        };
                        group.Dependencies.Add(dependency);
                    }

                    nuspec.Dependencies.Add(group);
                }
            }

            var filesElement = package.Element(XName.Get("files", nuspecNamespace));

            if (filesElement != null)
            {
                //UnityEngine.Debug.Log("Loading files!");
                foreach (var fileElement in filesElement.Elements(XName.Get("file", nuspecNamespace)))
                {
                    NuspecContentFile file = new NuspecContentFile
                    {
                        Source = (string)fileElement.Attribute("src") ??
                                 string.Empty,
                        Target = (string)fileElement.Attribute("target") ??
                                 string.Empty
                    };
                    nuspec.Files.Add(file);
                }
            }

            return(nuspec);
        }
Пример #4
0
        /// <summary>
        /// Parses the given <see cref="XDocument"/> and returns the list of <see cref="NugetPackage"/>s contained within.
        /// </summary>
        /// <param name="document">The <see cref="XDocument"/> that is the OData XML response from the NuGet server.</param>
        /// <returns>The list of <see cref="NugetPackage"/>s read from the given XML.</returns>
        public static List <NugetPackage> Parse(XDocument document)
        {
            List <NugetPackage> packages = new List <NugetPackage>();

            IEnumerable <XElement> packageEntries;

            Debug.Assert(document.Root != null, "document.Root != null");
            if (document.Root.Name.Equals(XName.Get("entry", AtomNamespace)))
            {
                packageEntries = Enumerable.Repeat(document.Root, 1);
            }
            else
            {
                packageEntries = document.Root.Elements(XName.Get("entry", AtomNamespace));
            }

            foreach (var entry in packageEntries)
            {
                NugetPackage package = new NugetPackage();
                package.Id          = entry.GetAtomElement("title").Value;
                package.DownloadUrl = entry.GetAtomElement("content").Attribute("src")?.Value;

                var entryProperties = entry.Element(XName.Get("properties", MetaDataNamespace));
                package.Title         = entryProperties.GetProperty("Title");
                package.Version       = entryProperties.GetProperty("Version");
                package.Description   = entryProperties.GetProperty("Description");
                package.Summary       = entryProperties.GetProperty("Summary");
                package.ReleaseNotes  = entryProperties.GetProperty("ReleaseNotes");
                package.LicenseUrl    = entryProperties.GetProperty("LicenseUrl");
                package.ProjectUrl    = entryProperties.GetProperty("ProjectUrl");
                package.Authors       = entryProperties.GetProperty("Authors");
                package.DownloadCount = int.Parse(entryProperties.GetProperty("DownloadCount"));

                string iconUrl = entryProperties.GetProperty("IconUrl");
                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
                string rawDependencies = entryProperties.GetProperty("Dependencies");
                if (!string.IsNullOrEmpty(rawDependencies))
                {
                    var dependencyGroups = new Dictionary <string, NugetFrameworkGroup>();

                    string[] dependencies = rawDependencies.Split('|');
                    foreach (var dependencyString in dependencies)
                    {
                        string[] details = dependencyString.Split(':');

                        string framework = string.Empty;
                        if (details.Length > 2)
                        {
                            framework = details[2];
                        }

                        NugetFrameworkGroup group;
                        if (!dependencyGroups.TryGetValue(framework, out group))
                        {
                            group = new NugetFrameworkGroup();
                            group.TargetFramework = framework;
                            dependencyGroups.Add(framework, group);
                        }

                        var dependency = new NugetPackageIdentifier(details[0], details[1]);
                        // 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(dependency.Id) && !string.IsNullOrEmpty(dependency.Version))
                        {
                            group.Dependencies.Add(dependency);
                        }
                    }

                    foreach (var group in dependencyGroups.Values)
                    {
                        package.Dependencies.Add(group);
                    }
                }

                packages.Add(package);
            }

            return(packages);
        }
Пример #5
0
 /// <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.RemoveAll(p => p.CompareTo(package) == 0);
 }