public void GetHashCodeTest() { PreRelease p1 = new PreRelease(PreReleaseStage.RC, 1); PreRelease p2 = new PreRelease(PreReleaseStage.RC, 1); Assert.Equal(p1.GetHashCode(), p2.GetHashCode()); }
public int CompareTo(Semver2Version other) { var majorMinorComparison = base.CompareTo(other); if (majorMinorComparison == 0) { var patchComparison = Patch.CompareTo(other.Patch); if (patchComparison == 0) { var preReleaseComparison = PreRelease.CompareTo(other.PreRelease); if (preReleaseComparison == 0) { return(Build.CompareTo(other.Build)); } else { return(preReleaseComparison); } } return(patchComparison); } return(majorMinorComparison); }
public int CompareTo(SemanticVersion other) { var result = Major.CompareTo(other.Major); if (result != 0) { return(result); } result = Minor.CompareTo(other.Minor); if (result != 0) { return(result); } result = Patch.CompareTo(other.Patch); if (result != 0) { return(result); } result = PreRelease.CompareTo(other.PreRelease); if (result != 0) { return(result); } return(Build.CompareTo(other.Build)); }
public int CompareTo(SematicVersion sematicVersion) { if (sematicVersion != null) { int result = Major.CompareTo(sematicVersion.Major); if (result == 0) { result = Minor.CompareTo(sematicVersion.Minor); if (result == 0) { result = Patch.CompareTo(sematicVersion.Patch); if (result == 0) { if (HasPreRelease == false && sematicVersion.HasPreRelease == false) { result = 0; } else if (HasPreRelease && !sematicVersion.HasPreRelease) { result = -1; } else if (!HasPreRelease && sematicVersion.HasPreRelease) { result = 1; } else { string[] a = PreRelease.ToArray(); string[] b = sematicVersion.PreRelease.ToArray(); for (int i = 0; i < a.Length && i < b.Length; i++) { result = a[i].CompareTo(b[i]); if (result != 0) { break; } } if (result == 0) { if (a.Length > b.Length) { result = 1; } else if (a.Length < b.Length) { result = -1; } } } } } } return(result); } else { throw new ArgumentNullException(); } }
public void FullCtorTest() { PreRelease pre = new PreRelease(PreReleaseStage.BETA, 5); SemanticVersion v = new SemanticVersion(0, 2, 1, pre, 1000); Assert.Equal("0.2.1-BETA.5+1000", v.ToString()); Assert.Equal("0.2.1-BETA.5", v.ToString("C")); Assert.Equal("0.2.1", v.ToString("G")); }
public void ComparableCtorTest() { PreRelease pre = new PreRelease(PreReleaseStage.ALPHA); SemanticVersion v = new SemanticVersion(0, 2, 1, pre); Assert.Equal("0.2.1-ALPHA", v.ToString("")); Assert.Equal("0.2.1-ALPHA", v.ToString("C")); Assert.Equal("0.2.1", v.ToString("G")); }
public override int GetHashCode() { return (Major + Minor + Patch + (PreRelease.HasValue? PreRelease.GetHashCode(): 0)); }
public int CompareTo(SemVersion other) { if (other is null) { throw new ArgumentNullException(nameof(other)); } // Compare revision numbers. int revisionNumbersComparisonResult = Version.Compare(this.revisionNumbers, other.revisionNumbers); if (revisionNumbersComparisonResult != 0) { return(revisionNumbersComparisonResult); } // If we get here, the major/minor/patch are all equal. if (IsPreRelease && !other.IsPreRelease) { return(-1); } if (!IsPreRelease && other.IsPreRelease) { return(1); } // If we get here, both versions are pre-release versions. string[] preReleaseParts = PreRelease?.Split('.') ?? Enumerable.Empty <string>().ToArray(); string[] otherPreReleaseParts = other.PreRelease?.Split('.') ?? Enumerable.Empty <string>().ToArray(); for (int i = 0; i < preReleaseParts.Count() && i < otherPreReleaseParts.Count(); ++i) { int compareResult = ComparePreReleaseIdentifiers(preReleaseParts[i], otherPreReleaseParts[i]); if (compareResult != 0) { return(compareResult); } } // If we get here, all pre-release identifiers were identical. if (preReleaseParts.Count() > otherPreReleaseParts.Count()) { return(1); } else if (preReleaseParts.Count() < otherPreReleaseParts.Count()) { return(-1); } // If we get here, the versions were exactly equal. return(0); }
/// <summary> /// Merges another packageIndex into this package index. For any overlapping /// data 'other' has precedence. /// </summary> /// <param name="other"></param> public void Merge(PackageIndex other) { if (other.IndexSources.IsSubsetOf(IndexSources)) { return; } // if pre-release is set on this index and different than the other // move pre-release to individual infos if (PreRelease != null && !PreRelease.Equals(other.PreRelease)) { foreach (var info in Packages.Values) { if (info.PreRelease == null) { info.PreRelease = PreRelease; } } PreRelease = null; } foreach (var otherPackage in other.Packages) { var otherInfo = otherPackage.Value; PackageInfo info; if (Packages.TryGetValue(otherPackage.Key, out info)) { info.Merge(otherInfo); } else { Packages[otherPackage.Key] = info = otherInfo; // if pre-release is set on the other index and doesn't match the value of the info, set it if (other.PreRelease != null && !other.PreRelease.Equals(info.PreRelease)) { info.PreRelease = other.PreRelease; } } } foreach (var otherModuleToPackage in other.ModulesToPackages) { ModulesToPackages[otherModuleToPackage.Key] = otherModuleToPackage.Value; } MetaPackages.Merge(other.MetaPackages); foreach (var otherIndexSource in other.IndexSources) { IndexSources.Add(otherIndexSource); } }
public void OnLoad(UnitOfWork unit) { if (PreRelease != null) { PreRelease.OnLoad(unit); } if (Release != null) { Release.OnLoad(unit); } }
public override int GetHashCode() { unchecked { var result = Major.GetHashCode(); result = result * 31 + Minor.GetHashCode(); result = result * 31 + Patch.GetHashCode(); result = result * 31 + PreRelease.GetHashCode(); result = result * 31 + Build.GetHashCode(); return(result); } }
public override int GetHashCode() { unchecked { var hashCode = Major; hashCode = (hashCode * 397) ^ Minor; hashCode = (hashCode * 397) ^ Patch; hashCode = (hashCode * 397) ^ (PreRelease != null ? PreRelease.GetHashCode() : 0); hashCode = (hashCode * 397) ^ Build; return(hashCode); } }
public void CanDecrement() { SemanticVersion v1 = new SemanticVersion(1, 0, 0, "alpha.1"); SemanticVersion v2 = new SemanticVersion(1, 3, 4, "alpha.2.0"); SemanticVersion v3 = new SemanticVersion(0, 0, 0, "beta"); SemanticVersion v4 = new SemanticVersion(0, 0, 0, "beta.x.0"); PreRelease v11 = --(v1.PreRelease); PreRelease v21 = --(v2.PreRelease); Assert.Equal(v11.Count, 1); Assert.Equal(v11.ToString(), "alpha"); Assert.Equal(v21.ToString(), "alpha.1"); Assert.Equal((--v3).ToString(), "0.0.0"); Assert.Equal((--v4).ToString(), "0.0.0.beta"); }
/// <summary> /// Calculate a hash code for the version. /// </summary> public override int GetHashCode() { // The build version isn't included when calculating the hash, // as two versions with equal properties except for the build // are considered equal. unchecked // Allow integer overflow with wrapping { int hash = 17; hash = hash * 23 + Major.GetHashCode(); hash = hash * 23 + Minor.GetHashCode(); hash = hash * 23 + Patch.GetHashCode(); hash = hash * 23 + PreRelease.GetHashCode(); return(hash); } }
/// <summary> /// Determines whether two <see cref="SemanticVersion"/> instances are equal. /// </summary> /// <param name="obj">The object to compare with the current <see cref="SemanticVersion"/>.</param> /// <returns>'true' if the specified object is equal to the current <see cref="SemanticVersion"/>; otherwise, 'false'.</returns> public override bool Equals(object obj) { if (obj is SemanticVersion other) { bool sameVersions = Major == other.Major && Minor == other.Minor && Patch == other.Patch; if (sameVersions && HasPreRelease && other.HasPreRelease) { if (PreRelease.Equals(other.PreRelease) && HasBuildMetadata && other.HasBuildMetadata) { return(BuildMetadata.Equals(other.BuildMetadata)); } } } return(false); }
/// <summary> /// Generates a hash of this object data. /// </summary> /// <returns>Hash of this object.</returns> public override int GetHashCode() { int hash = 0; hash ^= Major; hash ^= Minor; hash ^= Patch; if (HasPreRelease) { hash ^= PreRelease.GetHashCode(); } if (HasBuildMetadata) { hash ^= BuildMetadata.GetHashCode(); } return(hash); }
private async Task <string> GetLatestVersionAsync(ILibraryInstallationState libraryToUpdate, CancellationToken cancellationToken) { ILibraryCatalog catalog = ManifestDependencies.GetProvider(libraryToUpdate.ProviderId)?.GetCatalog(); if (catalog == null) { throw new InvalidOperationException(PredefinedErrors.LibraryIdIsUndefined().Message); } try { return(await catalog.GetLatestVersion(libraryToUpdate.LibraryId, PreRelease.HasValue(), cancellationToken)); } catch (Exception ex) { throw new InvalidOperationException(string.Format(Resources.UnableToFindLatestVersionForLibrary, libraryToUpdate.LibraryId), ex); } }
public SemanticVersion(uint major, uint minor, uint patch, PreRelease prelease, uint build) { Major = major; Minor = minor; Patch = patch; PreRelease = prelease; Build = build; _generalStr = string.Format("{0}.{1}.{2}", Major.ToString(), Minor.ToString(), Patch.ToString()); if (PreRelease != null) _comparableStr = string.Format("{0}-{1}", _generalStr, PreRelease.ToString()); else _comparableStr = _generalStr; if (Build == 0) _fullStr = _comparableStr; else _fullStr = string.Format("{0}+{1}", _comparableStr, Build.ToString()); }
public override string ToString() { StringBuilder sb = new StringBuilder(); sb.Append(Major).Dot().Append(Minor).Dot().Append(Patch); if (PreRelease?.Count() > 0) { bool first = true; foreach (var item in PreRelease) { if (first) { sb.Hyphen(); first = false; } else { sb.Dot(); } sb.Append(item); } } if (Metadata?.Count() > 0) { bool first = true; foreach (var item in Metadata) { if (first) { sb.Plus(); first = false; } else { sb.Dot(); } sb.Append(item); } } return(sb.ToString()); }
public void CanIncrement() { SemanticVersion v1 = new SemanticVersion(1, 0, 0, "alpha.1"); SemanticVersion v2 = new SemanticVersion(1, 3, 4, "alpha.2.0"); SemanticVersion v3 = new SemanticVersion(0, 0, 0, "beta"); SemanticVersion v4 = new SemanticVersion(0, 0, 0, "beta.x.0"); SemanticVersion bx860new = new SemanticVersion(0, 0, 0, "beta.x86.0.new"); PreRelease v11 = ++(v1.PreRelease); PreRelease v21 = ++(v2.PreRelease); Assert.Equal(v11.ToString(), "alpha.2"); Assert.Equal(v11[1], "2"); Assert.Equal(v21[1], "3"); Assert.Equal(v21.ToString(), "alpha.3"); ++(v3.PreRelease); Assert.Equal(v3.PreRelease.Count, 2); Assert.Equal(v3.PreRelease[1], "1"); ++(v4.PreRelease); Assert.Equal(v4.PreRelease[2], "1"); Assert.Equal((++bx860new).PreRelease.ToString(), "beta.x86.0.new.1"); }
public int CompareTo(Tag tag) { int value = 0; value = MajorInt32.CompareTo(tag.MajorInt32); if (value != 0) { return(value); } value = MinorInt32.CompareTo(tag.MinorInt32); if (value != 0) { return(value); } value = PatchInt32.CompareTo(tag.PatchInt32); if (value != 0) { return(value); } if (PreRelease == null && tag.PreRelease == null) { return(0); } if (PreRelease == null) { return(-1); } if (tag.PreRelease == null) { return(1); } value = PreRelease.CompareTo(tag.PreRelease); return(value); }
public SemanticVersion(uint major, uint minor, uint patch, PreRelease prelease) : this(major, minor, patch, prelease, 0) { }
/// <summary> /// Compare FullVersion accroding to semver 2.0 /// <para>http://semver.org/</para> /// </summary> /// <param name="other"></param> /// <returns></returns> public int CompareTo(SemVer other) { if (ReferenceEquals(other, null)) { return(1); } if (FullVersion == other.FullVersion) { return(0); } if (Major > other.Major) { return(1); } if (Major < other.Major) { return(-1); } // Major parts are equal if (Minor > other.Minor) { return(1); } if (Minor < other.Minor) { return(-1); } // Minor parts are equal if (Patch > other.Patch) { return(1); } if (Patch < other.Patch) { return(-1); } // Patch parts are equal // Precedence refers to how versions are compared to each other when ordered. Precedence MUST be // calculated by separating the version into major, minor, patch and pre-release identifiers in that order // (Build metadata does not figure into precedence). Precedence is determined by the first difference when // comparing each of these identifiers from left to right as follows: Major, minor, and patch versions are always // compared numerically. Example: 1.0.0 < 2.0.0 < 2.1.0 < 2.1.1. When major, minor, and patch are equal, a // pre-release version has lower precedence than a normal version. Example: 1.0.0-alpha < 1.0.0. Precedence // for two pre-release versions with the same major, minor, and patch version MUST be determined by // comparing each dot separated identifier from left to right until a difference is found as follows: identifiers // consisting of only digits are compared numerically and identifiers with letters or hyphens are compared // lexically in ASCII sort order. Numeric identifiers always have lower precedence than non-numeric identifiers. // A larger set of pre-release fields has a higher precedence than a smaller set, if all of the preceding identifiers // are equal. Example: 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta < 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0. // Source: http://semver.org/ if (PreRelease == other.PreRelease) { return(0); } if (!string.IsNullOrWhiteSpace(PreRelease) && string.IsNullOrWhiteSpace(other.PreRelease)) { return(-1); } if (string.IsNullOrWhiteSpace(PreRelease) && !string.IsNullOrWhiteSpace(other.PreRelease)) { return(1); } var parts = PreRelease.Split('.'); var otherParts = other.PreRelease.Split('.'); int commonLength = Math.Min(parts.Length, otherParts.Length); for (int i = 0; i < commonLength; i++) { int part; int otherPart; bool partIsInt = int.TryParse(parts[i], out part); bool otherPartIsInt = int.TryParse(otherParts[i], out otherPart); // Numeric identifiers always have lower precedence than non-numeric identifiers if (!partIsInt && otherPartIsInt) { return(1); } if (partIsInt && !otherPartIsInt) { return(-1); } // they are both the same type if (partIsInt) { // numeric if (part > otherPart) { return(1); } if (part < otherPart) { return(-1); } } else { // non-numeric int stringCompare = string.Compare(parts[i], otherParts[i], StringComparison.Ordinal); if (stringCompare != 0) { return(stringCompare); } } } // common parts are equal if (parts.Length > otherParts.Length) { return(1); } if (parts.Length < otherParts.Length) { return(-1); } return(0); }
public override string ToString() { return(PreRelease.HasValue? $"{Major}.{Minor}.{Patch}-{PreRelease.ToString()}": $"{Major}.{Minor}.{Patch}"); }
public int CompareTo(PackageVersion other) { Exception error; if (!Validate(out error)) { throw error; } if (!other.Validate(out error)) { throw new ArgumentException("Invalid version", "other", error); } int c = Epoch.CompareTo(other.Epoch); if (c != 0) { return(c); } if (Release != null && other.Release != null) { for (int i = 0; i < Release.Count || i < other.Release.Count; ++i) { c = Release.ElementAtOrDefault(i).CompareTo(other.Release.ElementAtOrDefault(i)); if (c != 0) { return(c); } } } else if (Release == null) { // No release, so we sort earlier if other has one return(other.Release == null ? 0 : -1); } else { // We have a release and other doesn't, so we sort later return(1); } if (PreReleaseName != other.PreReleaseName) { // Regular comparison mishandles None if (PreReleaseName == PackageVersionPreReleaseName.None) { return(1); } else if (other.PreReleaseName == PackageVersionPreReleaseName.None) { return(-1); } // Neither value is None, so CompareTo will be correct return(PreReleaseName.CompareTo(other.PreReleaseName)); } c = PreRelease.CompareTo(other.PreRelease); if (c != 0) { return(c); } c = PostRelease.CompareTo(other.PostRelease); if (c != 0) { return(c); } c = DevRelease.CompareTo(other.DevRelease); if (c != 0) { if (DevRelease == 0 || other.DevRelease == 0) { // When either DevRelease is zero, the sort order needs to // be reversed. return(-c); } return(c); } if (string.IsNullOrEmpty(LocalVersion)) { if (string.IsNullOrEmpty(other.LocalVersion)) { // No local versions, so we are equal return(0); } // other has a local version, so we sort earlier return(-1); } else if (string.IsNullOrEmpty(other.LocalVersion)) { // we have a local version, so we sort later return(1); } var lv1 = LocalVersion.Split('.'); var lv2 = other.LocalVersion.Split('.'); for (int i = 0; i < lv1.Length || i < lv2.Length; ++i) { if (i >= lv1.Length) { // other has a longer local version, so we sort earlier return(-1); } else if (i >= lv2.Length) { // we have a longer local version, so we sort later return(1); } var p1 = lv1[i]; var p2 = lv2[i]; int i1, i2; if (int.TryParse(p1, out i1)) { if (int.TryParse(p2, out i2)) { c = i1.CompareTo(i2); } else { // we have a number and other doesn't, so we sort later return(1); } } else if (int.TryParse(p2, out i2)) { // other has a number and we don't, so we sort earlier return(-1); } else { c = p1.CompareTo(p2); } if (c != 0) { return(c); } } // After all that, we are equal! return(0); }
private async System.Threading.Tasks.Task Upload() { using (var httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue("msbuild", "4.0")); httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Token", GithubOAuthToken); HttpResponseMessage createReleaseResponse; string postUrl = @"https://api.github.com/repos/" + GithubUser + @"/" + GithubRepo + @"/releases"; using (var request = new HttpRequestMessage(new HttpMethod("POST"), postUrl)) { string json = @"{ ""tag_name"": """; json += ReleaseTag; json += @""", ""target_commitish"": ""master"", ""name"": """; json += ReleaseName; json += @""", ""body"": """; json += HttpUtility.JavaScriptStringEncode(ReleaseDescription, false); json += @""", ""draft"": "; json += Draft.ToString().ToLower(); json += @", ""prerelease"": "; json += PreRelease.ToString().ToLower(); json += @"}"; //string json = @"{ ""tag_name"": ""v11.0.0"", ""target_commitish"": ""master"", ""name"": ""release name v11.0.0"", ""body"": ""Description of the release"", ""draft"": true, ""prerelease"": false}"; request.Content = new StringContent(json, Encoding.UTF8, "application/json"); createReleaseResponse = await httpClient.SendAsync(request); } string createReleaseOutput = await createReleaseResponse.Content.ReadAsStringAsync(); if (!createReleaseResponse.IsSuccessStatusCode) { Log.LogMessage(MessageImportance.High, createReleaseResponse.ToString()); Log.LogMessage(MessageImportance.High, createReleaseOutput.ToString()); Log.LogError("[UploadGitHubRelease] Failed to create release with the GitHub API V3"); return; } var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(createReleaseOutput), new System.Xml.XmlDictionaryReaderQuotas()); var root = XElement.Load(jsonReader); string uploadURL = root.XPathSelectElement("//upload_url").Value; uploadURL = uploadURL.Replace("{?name,label}", "?name="); Log.LogMessage(MessageImportance.High, "[UploadGitHubRelease] Release '" + ReleaseName + "' created with tag '" + ReleaseTag + "'"); if (ZipFilesToUpload == null) { return; } foreach (ITaskItem zipItem in ZipFilesToUpload) { HttpResponseMessage uploadZipResponse; string zipname = Path.GetFileName(zipItem.ItemSpec); Log.LogMessage(MessageImportance.High, "[UploadGitHubRelease] Uploading '" + zipname + "'..."); using (var request = new HttpRequestMessage(new HttpMethod("POST"), uploadURL + zipname)) { request.Content = new ByteArrayContent(File.ReadAllBytes(zipItem.ItemSpec)); request.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/zip"); uploadZipResponse = await httpClient.SendAsync(request); } if (!uploadZipResponse.IsSuccessStatusCode) { Log.LogMessage(MessageImportance.High, uploadZipResponse.ToString()); Log.LogMessage(MessageImportance.High, await uploadZipResponse.Content.ReadAsStringAsync()); Log.LogError("[UploadGitHubRelease] Failed to upload zip file with the GitHub API V3 : " + zipItem.ItemSpec); break; } Log.LogMessage(MessageImportance.High, "[UploadGitHubRelease] " + zipname + " uploaded successfully"); } } }