/// <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); }
/// <summary> /// Compares two version numbers in the form "1.2". Also supports an optional 3rd and 4th number as well as a prerelease tag, such as "1.3.0.1-alpha2". /// Returns: /// -1 if versionA is less than versionB /// 0 if versionA is equal to versionB /// +1 if versionA is greater than versionB /// </summary> /// <param name="versionA">The first version number to compare.</param> /// <param name="versionB">The second version number to compare.</param> /// <returns>-1 if versionA is less than versionB. 0 if versionA is equal to versionB. +1 if versionA is greater than versionB</returns> private static int CompareVersions(string versionA, string versionB) { try { var splitStringsA = versionA.Split('-'); versionA = splitStringsA[0]; var prereleaseA = "\uFFFF"; if (splitStringsA.Length > 1) { prereleaseA = splitStringsA[1]; for (var i = 2; i < splitStringsA.Length; i++) { prereleaseA += "-" + splitStringsA[i]; } } var splitA = versionA.Split('.'); var majorA = int.Parse(splitA[0]); var minorA = int.Parse(splitA[1]); var patchA = 0; if (splitA.Length >= 3) { patchA = int.Parse(splitA[2]); } var buildA = 0; if (splitA.Length >= 4) { buildA = int.Parse(splitA[3]); } var splitStringsB = versionB.Split('-'); versionB = splitStringsB[0]; var prereleaseB = "\uFFFF"; if (splitStringsB.Length > 1) { prereleaseB = splitStringsB[1]; for (var i = 2; i < splitStringsB.Length; i++) { prereleaseB += "-" + splitStringsB[i]; } } var splitB = versionB.Split('.'); var majorB = int.Parse(splitB[0]); var minorB = int.Parse(splitB[1]); var patchB = 0; if (splitB.Length >= 3) { patchB = int.Parse(splitB[2]); } var buildB = 0; if (splitB.Length >= 4) { buildB = int.Parse(splitB[3]); } var major = majorA <majorB ? -1 : majorA> majorB ? 1 : 0; var minor = minorA <minorB ? -1 : minorA> minorB ? 1 : 0; var patch = patchA <patchB ? -1 : patchA> patchB ? 1 : 0; var build = buildA <buildB ? -1 : buildA> buildB ? 1 : 0; var prerelease = string.CompareOrdinal(prereleaseA, prereleaseB); if (major != 0) { return(major); } if (minor != 0) { return(minor); } if (patch != 0) { return(patch); } if (build != 0) { return(build); } return(prerelease); } catch (Exception) { SystemProxy.LogError($"Compare Error: {versionA} {versionB}"); return(-1); } }