//---------------------------------------------------------------------------------------------------------------------- /// <summary> /// Parse a semantic versioned string to a PackageVersion class /// </summary> /// <param name="semanticVer">Semantic versioned input string</param> /// <param name="packageVersion">The detected PackageVersion. Set to null when the parsing fails</param> /// <returns>true if successful, false otherwise</returns> public static bool TryParse(string semanticVer, out PackageVersion packageVersion) { packageVersion = new PackageVersion(); string[] tokens = semanticVer.Split('.'); if (tokens.Length <= 2) { return(false); } if (!TryParseSingleVer(tokens[0], out packageVersion.m_major)) { return(false); } if (!TryParseSingleVer(tokens[1], out packageVersion.m_minor)) { return(false); } //Find patch and lifecycle string[] patches = tokens[2].Split('-'); if (!TryParseSingleVer(patches[0], out packageVersion.m_patch)) { return(false); } PackageLifecycle lifecycle = PackageLifecycle.Released; if (patches.Length > 1) { string lifecycleStr = patches[1].ToLower(); switch (lifecycleStr) { case "experimental": lifecycle = PackageLifecycle.Experimental; break; case "preview": lifecycle = PackageLifecycle.Preview; break; case "pre": lifecycle = PackageLifecycle.Prerelease; break; default: lifecycle = PackageLifecycle.Invalid; break; } } packageVersion.m_lifecycle = lifecycle; const int METADATA_INDEX = 3; if (tokens.Length > METADATA_INDEX) { packageVersion.m_additionalMetadata = String.Join(".", tokens, METADATA_INDEX, tokens.Length - METADATA_INDEX); } return(true); }
//---------------------------------------------------------------------------------------------------------------------- private void ParseAndVerifyVersion(string semanticVer, int? major, int? minor, int? patch, PackageLifecycle lifecycle, string additionalMetadata) { bool result = PackageVersion.TryParse(semanticVer, out PackageVersion packageVersion); UnityEngine.Assertions.Assert.IsTrue(result); Assert.AreEqual(major, packageVersion.GetMajor()); Assert.AreEqual(minor, packageVersion.GetMinor()); Assert.AreEqual(patch, packageVersion.GetPatch()); Assert.AreEqual(lifecycle, packageVersion.GetLifeCycle()); Assert.AreEqual(additionalMetadata, packageVersion.GetMetadata()); Assert.AreEqual(semanticVer, packageVersion.ToString()); }
//---------------------------------------------------------------------------------------------------------------------- private void CreateAndCheckPackageVersion(string semanticVer, int major, int minor, int patch, PackageLifecycle lifecycle, string additionalMetadata) { PackageVersion packageVersion = new PackageVersion(semanticVer); Assert.AreEqual(major, packageVersion.Major); Assert.AreEqual(minor, packageVersion.Minor); Assert.AreEqual(patch, packageVersion.Patch); Assert.AreEqual(lifecycle, packageVersion.Lifecycle); Assert.AreEqual(additionalMetadata, packageVersion.AdditionalMetadata); Assert.AreEqual(semanticVer, packageVersion.ToString()); }
//---------------------------------------------------------------------------------------------------------------------- /// <summary> /// Parse a semantic versioned string to a PackageVersion class /// </summary> /// <param name="semanticVer">Semantic versioned input string</param> /// <param name="packageVersion">The detected PackageVersion. Set to null when the parsing fails</param> /// <returns>true if successful, false otherwise</returns> public static bool TryParse(string semanticVer, out PackageVersion packageVersion) { packageVersion = null; string[] tokens = semanticVer.Split('.'); if (tokens.Length <= 2) { return(false); } if (!int.TryParse(tokens[0], out int major)) { return(false); } if (!int.TryParse(tokens[1], out int minor)) { return(false); } //Find patch and lifecycle string[] patches = tokens[2].Split('-'); if (!int.TryParse(patches[0], out int patch)) { return(false); } PackageLifecycle lifecycle = PackageLifecycle.INVALID; if (patches.Length > 1) { string lifecycleStr = patches[1].ToLower(); switch (lifecycleStr) { case "experimental": lifecycle = PackageLifecycle.EXPERIMENTAL; break; case "preview": lifecycle = PackageLifecycle.PREVIEW; break; case "pre": lifecycle = PackageLifecycle.PRERELEASE; break; default: lifecycle = PackageLifecycle.INVALID; break; } } else { lifecycle = PackageLifecycle.RELEASED; } packageVersion = new PackageVersion() { Major = major, Minor = minor, Patch = patch, Lifecycle = lifecycle }; const int METADATA_INDEX = 3; if (tokens.Length > METADATA_INDEX) { packageVersion.AdditionalMetadata = String.Join(".", tokens, METADATA_INDEX, tokens.Length - METADATA_INDEX); } return(true); }