/// <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 = $"COULD NOT LOAD {nupkgFilepath}"; } return(nuspec); }
/// <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); }
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(); }
/// <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; if (!string.IsNullOrEmpty(nuspec.IconUrl)) { package.Icon = NugetHelper.DownloadImage(nuspec.IconUrl); } package.RepositoryUrl = nuspec.RepositoryUrl; try { package.RepositoryType = (RepositoryType)Enum.Parse(typeof(RepositoryType), nuspec.RepositoryType, true); } catch (Exception) { // ignored } 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); }
/// <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); string dataPath = Application.dataPath.Substring(0, Application.dataPath.Length - "Assets".Length); assetFilepath = Path.Combine(dataPath, assetFilepath); var isNuspec = Path.GetExtension(assetFilepath) == ".nuspec"; if (isNuspec) { _filepath = assetFilepath; _nuspec = NuspecFile.Load(_filepath); titleContent = new GUIContent(Path.GetFileNameWithoutExtension(_filepath)); // force a repaint Repaint(); } } }
/// <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); }