Represents a .nuspec file used to store metadata for a NuGet package.
Example #1
0
        /// <summary>
        /// Loads a <see cref="NugetPackage"/> from the .nupkg file at the given filepath.
        /// </summary>
        /// <param name="nupkgFilepath">The filepath to the .nupkg file to load.</param>
        /// <returns>The <see cref="NugetPackage"/> loaded from the .nupkg file.</returns>
        public static NugetPackage FromNupkgFile(string nupkgFilepath)
        {
            NugetPackage package = FromNuspec(NuspecFile.FromNupkgFile(nupkgFilepath));

            package.DownloadUrl = nupkgFilepath;
            return(package);
        }
        /// <summary>
        /// Creates a new <see cref="NugetPackage"/> from the given <see cref="NuspecFile"/>.
        /// </summary>
        /// <param name="nuspec">The <see cref="NuspecFile"/> to use to create the <see cref="NugetPackage"/>.</param>
        /// <returns>The newly created <see cref="NugetPackage"/>.</returns>
        public static NugetPackage FromNuspec(NuspecFile nuspec)
        {
            NugetPackage package = new NugetPackage();

            package.Id            = nuspec.Id;
            package.Version       = nuspec.Version;
            package.Title         = nuspec.Title;
            package.Authors       = nuspec.Authors;
            package.Description   = nuspec.Description;
            package.Summary       = nuspec.Summary;
            package.ReleaseNotes  = nuspec.ReleaseNotes;
            package.LicenseUrl    = nuspec.LicenseUrl;
            package.ProjectUrl    = nuspec.ProjectUrl;
            package.IconUrl       = nuspec.IconUrl;
            package.RepositoryUrl = nuspec.RepositoryUrl;

            try
            {
                package.RepositoryType = (RepositoryType)Enum.Parse(typeof(RepositoryType), nuspec.RepositoryType, true);
            }
            catch (Exception) { }

            package.RepositoryBranch = nuspec.RepositoryBranch;
            package.RepositoryCommit = nuspec.RepositoryCommit;

            // if there is no title, just use the ID as the title
            if (string.IsNullOrEmpty(package.Title))
            {
                package.Title = package.Id;
            }

            package.Dependencies = nuspec.Dependencies;

            return(package);
        }
Example #3
0
        /// <summary>
        /// Loads the .nuspec file inside the .nupkg file at the given filepath.
        /// </summary>
        /// <param name="nupkgFilepath">The filepath to the .nupkg file to load.</param>
        /// <returns>The .nuspec file loaded from inside the .nupkg file.</returns>
        public static NuspecFile FromNupkgFile(string nupkgFilepath)
        {
            NuspecFile nuspec = new NuspecFile();

            if (File.Exists(nupkgFilepath))
            {
                // get the .nuspec file from inside the .nupkg
                using (ZipArchive zip = ZipFile.OpenRead(nupkgFilepath))
                {
                    //var entry = zip[string.Format("{0}.nuspec", packageId)];
                    var entry = zip.Entries.First(x => x.FullName.EndsWith(".nuspec"));

                    using (Stream stream = entry.Open())
                    {
                        nuspec = Load(stream);
                    }
                }
            }
            else
            {
                UnityEngine.Debug.LogErrorFormat("Package could not be read: {0}", nupkgFilepath);

                //nuspec.Id = packageId;
                //nuspec.Version = packageVersion;
                nuspec.Description = string.Format("COULD NOT LOAD {0}", nupkgFilepath);
            }

            return(nuspec);
        }
Example #4
0
        /// <summary>
        /// Loads the .nuspec file inside the .nupkg file at the given filepath.
        /// </summary>
        /// <param name="nupkgFilepath">The filepath to the .nupkg file to load.</param>
        /// <returns>The .nuspec file loaded from inside the .nupkg file.</returns>
        public static NuspecFile FromNupkgFile(string nupkgFilepath)
        {
            NuspecFile nuspec;

            if (File.Exists(nupkgFilepath))
            {
                // get the .nuspec file from inside the .nupkg
                using (var zip = ZipFile.OpenRead(nupkgFilepath))
                {
                    //var entry = zip[string.Format("{0}.nuspec", packageId)];
                    var entry = zip.Entries.First(x => x.FullName.EndsWith(".nuspec"));

                    nuspec = Load(entry.Open());
                }
            }
            else
            {
                SystemProxy.LogError($"Package could not be read: {nupkgFilepath}");

                nuspec = new NuspecFile
                {
                    //Id = packageId,
                    //Version = packageVersion,
                    Description = $"COULD NOT LOAD {nupkgFilepath}"
                };
            }

            return(nuspec);
        }
Example #5
0
        /// <summary>
        /// Reload the currently selected asset as a .nuspec file.
        /// </summary>
        protected void Reload()
        {
            var defaultAsset = Selection.activeObject as DefaultAsset;

            if (defaultAsset != null)
            {
                var assetFilepath = AssetDatabase.GetAssetPath(defaultAsset);
                var dataPath      = Application.dataPath.Substring(0, Application.dataPath.Length - "Assets".Length);
                assetFilepath = Path.Combine(dataPath, assetFilepath);

                var isNuspec = Path.GetExtension(assetFilepath) == ".nuspec";

                if (!isNuspec || nuspec != null && filepath == assetFilepath)
                {
                    return;
                }

                filepath     = assetFilepath;
                nuspec       = NuspecFile.Load(filepath);
                titleContent = new GUIContent(Path.GetFileNameWithoutExtension(filepath));

                // force a repaint
                Repaint();
            }
        }
Example #6
0
        /// <summary>
        /// Creates a new <see cref="NugetPackage"/> from the given <see cref="NuspecFile"/>.
        /// </summary>
        /// <param name="nuspec">The <see cref="NuspecFile"/> to use to create the <see cref="NugetPackage"/>.</param>
        /// <returns>The newly created <see cref="NugetPackage"/>.</returns>
        public static NugetPackage FromNuspec(NuspecFile nuspec)
        {
            NugetPackage package = new NugetPackage();

            package.Id           = nuspec.Id;
            package.Version      = nuspec.Version;
            package.Title        = nuspec.Title;
            package.Description  = nuspec.Description;
            package.ReleaseNotes = nuspec.ReleaseNotes;
            package.LicenseUrl   = nuspec.LicenseUrl;
            //package.DownloadUrl = not in a nuspec

            if (!string.IsNullOrEmpty(nuspec.IconUrl))
            {
                package.Icon = NugetHelper.DownloadImage(nuspec.IconUrl);
            }

            // if there is no title, just use the ID as the title
            if (string.IsNullOrEmpty(package.Title))
            {
                package.Title = package.Id;
            }

            package.Dependencies = nuspec.Dependencies;

            return(package);
        }
        /// <summary>
        /// Loads the .nuspec file inside the .nupkg file at the given filepath.
        /// </summary>
        /// <param name="nupkgFilepath">The filepath to the .nupkg file to load.</param>
        /// <returns>The .nuspec file loaded from inside the .nupkg file.</returns>
        public static NuspecFile FromNupkgFile(string nupkgFilepath)
        {
            NuspecFile nuspec = new NuspecFile();

            if (File.Exists(nupkgFilepath))
            {
                // get the .nuspec file from inside the .nupkg
                using (ZipStorer zip = ZipStorer.Open(nupkgFilepath, FileAccess.Read))
                {
                    var entry = zip.ReadCentralDir().First(x => x.FilenameInZip.EndsWith(".nuspec"));

                    using (MemoryStream stream = new MemoryStream())
                    {
                        zip.ExtractFile(entry, stream);
                        stream.Position = 0;

                        nuspec = Load(stream);
                    }
                }
            }
            else
            {
                UnityEngine.Debug.LogErrorFormat("Package could not be read: {0}", nupkgFilepath);

                //nuspec.Id = packageId;
                //nuspec.Version = packageVersion;
                nuspec.Description = string.Format("COULD NOT LOAD {0}", nupkgFilepath);
            }

            return(nuspec);
        }
Example #8
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();

            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)
            {
                IEnumerable <XElement> groups = dependenciesElement.Elements(XName.Get("group", nuspecNamespace)); // get groups
                if (groups.Count() > 0)                                                                            // if there are groups
                {
                    foreach (var iGroup in groups)                                                                 // add dependancy in game
                    {
                        nuspec.addDependancies(iGroup, nuspecNamespace);
                    }
                }
                else // no groups; add children of "dependancies"
                {
                    nuspec.addDependancies(dependenciesElement, nuspecNamespace);
                }
            }

            nuspec.Files = new List <NuspecContentFile>();
            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();
                    file.Source = (string)fileElement.Attribute("src") ?? string.Empty;
                    file.Target = (string)fileElement.Attribute("target") ?? string.Empty;
                    nuspec.Files.Add(file);
                }
            }

            return(nuspec);
        }
Example #9
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();

            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);
                }
            }

            nuspec.Files = new List <NuspecContentFile>();
            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();
                    file.Source = (string)fileElement.Attribute("src") ?? string.Empty;
                    file.Target = (string)fileElement.Attribute("target") ?? string.Empty;
                    nuspec.Files.Add(file);
                }
            }

            return(nuspec);
        }
Example #10
0
        /// <summary>
        /// Automatically called by Unity when the Inspector is first opened (when a .nuspec file is clicked on in the Project view).
        /// </summary>
        public void OnEnable()
        {
            filepath = AssetDatabase.GetAssetPath(target);
            string dataPath = Application.dataPath.Substring(0, Application.dataPath.Length - "Assets".Length);
            filepath = Path.Combine(dataPath, filepath);

            isNuspec = Path.GetExtension(filepath) == ".nuspec";
            if (isNuspec)
            {
                nuspec = NuspecFile.Load(filepath);
            }
        }
Example #11
0
        /// <summary>
        /// Automatically called by Unity when the Inspector is first opened (when a .nuspec file is clicked on in the Project view).
        /// </summary>
        public void OnEnable()
        {
            filepath = AssetDatabase.GetAssetPath(target);
            string dataPath = Application.dataPath.Substring(0, Application.dataPath.Length - "Assets".Length);

            filepath = Path.Combine(dataPath, filepath);

            isNuspec = Path.GetExtension(filepath) == ".nuspec";
            if (isNuspec)
            {
                nuspec = NuspecFile.Load(filepath);
            }
        }
Example #12
0
        protected static void CreateNuspecFile()
        {
            string filepath = Application.dataPath;

            if (Selection.activeObject != null && Selection.activeObject != Selection.activeGameObject)
            {
                string selectedFile = AssetDatabase.GetAssetPath(Selection.activeObject);
                filepath = selectedFile.Substring("Assets/".Length);
                filepath = Path.Combine(Application.dataPath, filepath);
            }

            if (!string.IsNullOrEmpty(Path.GetExtension(filepath)))
            {
                // if it was a file that was selected, replace the filename
                filepath  = filepath.Replace(Path.GetFileName(filepath), string.Empty);
                filepath += "MyPackage.nuspec";
            }
            else
            {
                // if it was a directory that was selected, simply add the filename
                filepath += "/MyPackage.nuspec";
            }

            Debug.LogFormat("Creating: {0}", filepath);

            NuspecFile file = new NuspecFile();

            file.Id           = "MyPackage";
            file.Version      = "0.0.1";
            file.Authors      = "Your Name";
            file.Owners       = "Your Name";
            file.LicenseUrl   = "http://your_license_url_here";
            file.ProjectUrl   = "http://your_project_url_here";
            file.Description  = "A description of what this package is and does.";
            file.Summary      = "A brief description of what this package is and does.";
            file.ReleaseNotes = "Notes for this specific release";
            file.Copyright    = "Copyright 2017";
            file.IconUrl      = "https://www.nuget.org/Content/Images/packageDefaultIcon-50x50.png";
            file.Save(filepath);

            AssetDatabase.Refresh();

            // select the newly created .nuspec file
            string dataPath = Application.dataPath.Substring(0, Application.dataPath.Length - "Assets".Length);

            Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(filepath.Replace(dataPath, string.Empty));

            // automatically display the editor with the newly created .nuspec file
            DisplayNuspecEditor();
        }
Example #13
0
        protected static void TestNUPKG()
        {
            var files = Directory.GetFiles("C:/Users/Administrator/AppData/Local/NuGet/Cache", "*.nupkg", SearchOption.AllDirectories);

            foreach (var f in files)
            {
                UnityEngine.Debug.Log("==============>" + Path.GetFileName(f));
                //解析Npkg,剔除无用的dll
                var nupkg = NuspecFile.FromNupkgFile(f);
                foreach (var nf in nupkg.Files)
                {
                    UnityEngine.Debug.Log(nf.Source + "\n" + nf.Target);
                }
            }

            // typeof(int).Assembly.GetReferencedAssemblies()
        }
Example #14
0
        private static void CreateNuspecAndOpenEditor(string filepath)
        {
            if (!string.IsNullOrEmpty(Path.GetExtension(filepath)))
            {
                filepath = Path.GetDirectoryName(filepath);
            }

            if (filepath == null)
            {
                return;
            }

            var packageName = Path.GetFileName(filepath);

            filepath = Path.Combine(filepath, packageName + ".nuspec");

            var file = new NuspecFile
            {
                Id           = string.Format(DefaultFile.Id, packageName.ToLower()),
                Title        = packageName,
                Version      = DefaultFile.Version,
                Authors      = DefaultFile.Authors,
                Owners       = DefaultFile.Owners,
                LicenseUrl   = DefaultFile.LicenseUrl,
                ProjectUrl   = string.Format(DefaultFile.ProjectUrl, packageName),
                Description  = DefaultFile.Description,
                ReleaseNotes = DefaultFile.ReleaseNotes,
                Copyright    = DefaultFile.Copyright,
                IconUrl      = DefaultFile.IconUrl
            };

            file.Save(filepath);

            AssetDatabase.Refresh();

            // select the newly created .nuspec file
            var dataPath = Application.dataPath.Substring(0, Application.dataPath.Length - "Assets".Length);

            Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(filepath.Replace(dataPath, string.Empty));

            // automatically display the editor with the newly created .nuspec file
            DisplayNuspecEditor();
        }
Example #15
0
        /// <summary>
        /// Creates a new <see cref="NugetPackage"/> from the given <see cref="NuspecFile"/>.
        /// </summary>
        /// <param name="nuspec">The <see cref="NuspecFile"/> to use to create the <see cref="NugetPackage"/>.</param>
        /// <returns>The newly created <see cref="NugetPackage"/>.</returns>
        public static NugetPackage FromNuspec(NuspecFile nuspec)
        {
            var package = new NugetPackage
            {
                Id           = nuspec.Id,
                Version      = nuspec.Version,
                Title        = nuspec.Title,
                Description  = nuspec.Description,
                ReleaseNotes = nuspec.ReleaseNotes,
                LicenseUrl   = nuspec.LicenseUrl,
                ProjectUrl   = nuspec.ProjectUrl,
                Authors      = nuspec.Authors,
                Summary      = nuspec.Summary
            };


            if (!string.IsNullOrEmpty(nuspec.IconUrl))
            {
                SystemProxy.DownloadAndSetIcon(package, nuspec.IconUrl);
            }

            package.RepositoryUrl = nuspec.RepositoryUrl;

            try
            {
                package.RepositoryType = (RepositoryType)Enum.Parse(typeof(RepositoryType), nuspec.RepositoryType, true);
            }
            catch (Exception) { }

            package.RepositoryBranch = nuspec.RepositoryBranch;
            package.RepositoryCommit = nuspec.RepositoryCommit;

            // if there is no title, just use the ID as the title
            if (string.IsNullOrEmpty(package.Title))
            {
                package.Title = package.Id;
            }

            package.Dependencies = nuspec.Dependencies;

            return(package);
        }
Example #16
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();

            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.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();

                        dependency.Id      = (string)dependencyElement.Attribute("id") ?? string.Empty;
                        dependency.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();
                        dependency.Id      = (string)dependencyElement.Attribute("id") ?? string.Empty;
                        dependency.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();
                    file.Source = (string)fileElement.Attribute("src") ?? string.Empty;
                    file.Target = (string)fileElement.Attribute("target") ?? string.Empty;
                    nuspec.Files.Add(file);
                }
            }

            return(nuspec);
        }
Example #17
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();

            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;
        }
Example #18
0
        /// <summary>
        /// Loads the .nuspec file inside the .nupkg file at the given filepath.
        /// </summary>
        /// <param name="nupkgFilepath">The filepath to the .nupkg file to load.</param>
        /// <returns>The .nuspec file loaded from inside the .nupkg file.</returns>
        public static NuspecFile FromNupkgFile(string nupkgFilepath)
        {
            NuspecFile nuspec = new NuspecFile();

            if (File.Exists(nupkgFilepath))
            {
                // get the .nuspec file from inside the .nupkg
                using (ZipFile zip = ZipFile.Read(nupkgFilepath))
                {
                    //var entry = zip[string.Format("{0}.nuspec", packageId)];
                    var entry = zip.First(x => x.FileName.EndsWith(".nuspec"));

                    using (MemoryStream stream = new MemoryStream())
                    {
                        entry.Extract(stream);
                        stream.Position = 0;

                        nuspec = Load(stream);
                    }
                }
            }
            else
            {
                UnityEngine.Debug.LogErrorFormat("Package could not be read: {0}", nupkgFilepath);

                //nuspec.Id = packageId;
                //nuspec.Version = packageVersion;
                nuspec.Description = string.Format("COULD NOT LOAD {0}", nupkgFilepath);
            }

            return nuspec;
        }
Example #19
0
        /// <summary>
        /// Calls "nuget.exe push" to push a .nupkf file to the the server location defined in the NuGet.config file.
        /// Note: This differs slightly from NuGet's Push command by automatically calling Pack if the .nupkg doesn't already exist.
        /// </summary>
        /// <param name="nuspec">The NuspecFile which defines the package to push.  Only the ID and Version are used.</param>
        /// <param name="nuspecFilePath">The full filepath to the .nuspec file to use.  This is required by NuGet's Push command.</param>
        /// /// <param name="apiKey">The API key to use when pushing a package to the server.  This is optional.</param>
        public static void Push(NuspecFile nuspec, string nuspecFilePath, string apiKey = "")
        {
            string packagePath = Path.Combine(PackOutputDirectory, string.Format("{0}.{1}.nupkg", nuspec.Id, nuspec.Version));
            if (!File.Exists(packagePath))
            {
                LogVerbose("Attempting to Pack.");
                Pack(nuspecFilePath);

                if (!File.Exists(packagePath))
                {
                    Debug.LogErrorFormat("NuGet package not found: {0}", packagePath);
                    return;
                }
            }

            string arguments = string.Format("push \"{0}\" {1} -configfile \"{2}\"", packagePath, apiKey, NugetConfigFilePath);

            RunNugetProcess(arguments);
        }
Example #20
0
        protected static void CreateNuspecFile()
        {
            string filepath = Application.dataPath;

            if (Selection.activeObject != null && Selection.activeObject != Selection.activeGameObject)
            {
                string selectedFile = AssetDatabase.GetAssetPath(Selection.activeObject);
                filepath = selectedFile.Substring("Assets/".Length);
                filepath = Path.Combine(Application.dataPath, filepath);
            }

            if (!string.IsNullOrEmpty(Path.GetExtension(filepath)))
            {
                // if it was a file that was selected, replace the filename
                filepath = filepath.Replace(Path.GetFileName(filepath), string.Empty);
                filepath += "MyPackage.nuspec";
            }
            else
            {
                // if it was a directory that was selected, simply add the filename
                filepath += "/MyPackage.nuspec";
            }

            Debug.LogFormat("Creating: {0}", filepath);

            NuspecFile file = new NuspecFile();
            file.Id = "MyPackage";
            file.Version = "0.0.1";
            file.Authors = "Your Name";
            file.Owners = "Your Name";
            file.LicenseUrl = "http://your_license_url_here";
            file.ProjectUrl = "http://your_project_url_here";
            file.Description = "A description of what this packages is and does.";
            file.ReleaseNotes = "Notes for this specific release";
            file.Copyright = "Copyright 2016";
            file.IconUrl = "https://www.nuget.org/Content/Images/packageDefaultIcon-50x50.png";
            file.Save(filepath);

            AssetDatabase.Refresh();
        }