/// <summary> /// Initializes a new instance of the <see cref="PackageVersionRange" /> class. /// </summary> /// <param name="minVersion">The minimum version.</param> /// <param name="minVersionInclusive">if set to <c>true</c> the minimum version is inclusive</param> /// <param name="maxVersion">The maximum version.</param> /// <param name="maxVersionInclusive">if set to <c>true</c> the maximum version is inclusive</param> public PackageVersionRange(PackageVersion minVersion, bool minVersionInclusive, PackageVersion maxVersion, bool maxVersionInclusive) { IsMinInclusive = minVersionInclusive; IsMaxInclusive = maxVersionInclusive; MinVersion = minVersion; MaxVersion = maxVersion; }
/// <summary> /// Initializes a new instance of the <see cref="PackageVersionRange"/> class with only one possible version. /// </summary> /// <param name="version">The exact version.</param> public PackageVersionRange(PackageVersion version) : this(version, true, version, true) { }
/// <summary> /// Initializes a new instance of the <see cref="PackageVersionRange" /> class with just a lower bound /// <paramref name="minVersion"/> that can be inclusive or not depending on <paramref name="minVersionInclusive"/>. /// </summary> /// <param name="minVersion">The minimum version.</param> /// <param name="minVersionInclusive">if set to <c>true</c> the minimum version is inclusive</param> public PackageVersionRange(PackageVersion minVersion, bool minVersionInclusive) { IsMinInclusive = minVersionInclusive; MinVersion = minVersion; }
/// <summary> /// Tries to parse a version dependency. /// </summary> /// <param name="value">The version dependency as a string.</param> /// <param name="result">The parsed result.</param> /// <returns><c>true</c> if successfuly parsed, <c>false</c> otherwise.</returns> /// <exception cref="System.ArgumentNullException">value</exception> public static bool TryParse(string value, out PackageVersionRange result) { if (value == null) { throw new ArgumentNullException(nameof(value)); } var versionSpec = new PackageVersionRange(); value = value.Trim(); // First, try to parse it as a plain version string PackageVersion version; if (PackageVersion.TryParse(value, out version)) { // A plain version is treated as an inclusive minimum range result = new PackageVersionRange { MinVersion = version, IsMinInclusive = true, }; return(true); } // It's not a plain version, so it must be using the bracket arithmetic range syntax result = null; // Fail early if the string is too short to be valid if (value.Length < 3) { return(false); } // The first character must be [ ot ( switch (value.First()) { case '[': versionSpec.IsMinInclusive = true; break; case '(': versionSpec.IsMinInclusive = false; break; default: return(false); } // The last character must be ] ot ) switch (value.Last()) { case ']': versionSpec.IsMaxInclusive = true; break; case ')': versionSpec.IsMaxInclusive = false; break; default: return(false); } // Get rid of the two brackets value = value.Substring(1, value.Length - 2); // Split by comma, and make sure we don't get more than two pieces string[] parts = value.Split(','); if (parts.Length > 2) { return(false); } if (parts.All(string.IsNullOrEmpty)) { // If all parts are empty, then neither of upper or lower bounds were specified. Version spec is of the format (,] return(false); } // If there is only one piece, we use it for both min and max string minVersionString = parts[0]; string maxVersionString = (parts.Length == 2) ? parts[1] : parts[0]; // Only parse the min version if it's non-empty if (!string.IsNullOrWhiteSpace(minVersionString)) { if (!PackageVersion.TryParse(minVersionString, out version)) { return(false); } versionSpec.MinVersion = version; } // Same deal for max if (!string.IsNullOrWhiteSpace(maxVersionString)) { if (!PackageVersion.TryParse(maxVersionString, out version)) { return(false); } versionSpec.MaxVersion = version; } // Successful parse! result = versionSpec; return(true); }