Skip to content

Commit 427ff3e

Browse files
author
davidkline-ms
committed
refactor version parsing and comparison - part 1
1 parent 143d27a commit 427ff3e

File tree

1 file changed

+132
-30
lines changed

1 file changed

+132
-30
lines changed

Assets/MixedRealityToolkit/Utilities/Editor/PackageManifest/PackageManifestUpdater.cs

Lines changed: 132 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
using System;
55
using System.Collections.Generic;
66
using System.IO;
7+
using System.Runtime.CompilerServices;
78
using UnityEditor.PackageManager;
89
using UnityEngine;
10+
using UnityEngine.Networking;
11+
using Version = System.Version;
912

1013
namespace Microsoft.MixedReality.Toolkit.Utilities.Editor
1114
{
@@ -19,7 +22,7 @@ internal static class PackageManifestUpdater
1922
private static string[] MSBuildRegistryScopes = new string[] { "com.microsoft" };
2023

2124
internal const string MSBuildPackageName = "com.microsoft.msbuildforunity";
22-
internal const string MSBuildPackageVersion = "0.9.1";
25+
internal const string MSBuildPackageVersion = "0.9.1-20200131.12";
2326

2427
/// <summary>
2528
/// Finds and returns the fully qualified path to the Unity Package Manager manifest
@@ -42,10 +45,111 @@ private static string GetPackageManifestFilePath()
4245
return manifestPath;
4346
}
4447

48+
private static bool TryGetVersionComponents(
49+
string packageVersion,
50+
out Version version,
51+
out float prerelease)
52+
{
53+
version = null;
54+
prerelease = float.NaN;
55+
56+
char[] trimChars = new char[] { ' ', '\"', ',' };
57+
58+
// Note: The version is in the following format Major.Minor.Revision[-Date.Build]
59+
60+
// Attept to split the version string into version and float components
61+
string[] versionComponents = packageVersion.Split(new char[] { '-' }, 2);
62+
63+
// Parse the version component.
64+
string versionString = versionComponents[0].Trim(trimChars);
65+
if (!Version.TryParse(versionString, out version))
66+
{
67+
// Reset the version out param.
68+
version = null;
69+
return false;
70+
}
71+
72+
if (versionComponents.Length == 2)
73+
{
74+
// Parse the float component
75+
string prereleaseString = versionComponents[1].Trim(trimChars);
76+
if (!float.TryParse(prereleaseString, out prerelease))
77+
{
78+
// Reset both out params.
79+
version = null;
80+
prerelease = float.NaN;
81+
return false;
82+
}
83+
}
84+
85+
return true;
86+
}
87+
4588
/// <summary>
4689
/// Reports whether or not the appropriate version of MSBuild for Unity is specified
4790
/// in the Unity Package Manager manifest.
4891
/// </summary>
92+
/// <param name="packageVersion">The version of the package, as listed in the manifest.</param>
93+
/// <returns>
94+
/// True if an appropriate verson of MS Build for Unity is configured in the manifest, otherwise false.
95+
/// </returns>
96+
private static bool IsAppropriateMBuildVersion(string packageVersion)
97+
{
98+
// Get the version of the package.
99+
// Note: The version is in the following format Major.Minor.Revision[-Date.Build]
100+
101+
Version minVersion;
102+
float minPrerelease;
103+
104+
if (!TryGetVersionComponents(MSBuildPackageVersion, out minVersion, out minPrerelease))
105+
{
106+
// todo
107+
return false;
108+
}
109+
110+
// Get the current version from the manifest
111+
Version currentVersion;
112+
float currentPrerelease;
113+
if (!TryGetVersionComponents(packageVersion, out currentVersion, out currentPrerelease))
114+
{
115+
// todo
116+
return false;
117+
}
118+
119+
// Compare the version
120+
// todo
121+
122+
// Compare the prerelease indicator
123+
// todo
124+
125+
//string[] versionComponents = packageVersion.Split(new char[] { ':' });
126+
//if (splitLine.Length == 2)
127+
//{
128+
// // Ensure correct formatting of the version string, before we attempt to parse it.
129+
// string versionString = splitLine[1].Trim(new char[] { ' ', '\"', ',' });
130+
// bool replaceOnEquals = false;
131+
// if (versionString.Contains("-"))
132+
// {
133+
// // The string references a preview version. Truncate at the '-'.
134+
// versionString = versionString.Substring(0, versionString.IndexOf('-'));
135+
136+
// // We want to update preview versions to the final.
137+
// replaceOnEquals = true;
138+
// }
139+
140+
// Version version;
141+
// if (Version.TryParse(versionString, out version))
142+
// {
143+
// isAppropriateVersion = replaceOnEquals ? (version > minVersion) : (version >= minVersion);
144+
// }
145+
//}
146+
147+
return false;
148+
}
149+
150+
/// <summary>
151+
/// Reports whether or not the MSBuild for Unity is properly enabled in the Unity Package Manager manifest.
152+
/// </summary>
49153
/// <returns>
50154
/// True if an appropriate verson of MS Build for Unity is configured in the manifest, otherwise false.
51155
/// </returns>
@@ -65,9 +169,9 @@ internal static bool IsMSBuildForUnityEnabled()
65169
}
66170

67171
// Read the package manifest a line at a time.
68-
bool msBuildFound = false;
69-
bool isAppropriateVersion = false;
70-
Version minVersion = Version.Parse(MSBuildPackageVersion);
172+
//bool msBuildFound = false;
173+
//bool isAppropriateVersion = false;
174+
//Version minVersion = Version.Parse(MSBuildPackageVersion);
71175
using (FileStream manifestStream = new FileStream(manifestPath, FileMode.Open, FileAccess.Read))
72176
{
73177
using (StreamReader reader = new StreamReader(manifestStream))
@@ -78,38 +182,36 @@ internal static bool IsMSBuildForUnityEnabled()
78182
string line = reader.ReadLine();
79183
if (line.Contains(MSBuildPackageName))
80184
{
81-
msBuildFound = true;
82-
83-
// Next, check the version.
84-
string[] splitLine = line.Split(new char[] { ':' });
85-
if (splitLine.Length == 2)
86-
{
87-
// Ensure correct formatting of the version string, before we attempt to parse it.
88-
string versionString = splitLine[1].Trim(new char[] { ' ', '\"', ',' });
89-
bool replaceOnEquals = false;
90-
if (versionString.Contains("-"))
91-
{
92-
// The string references a preview version. Truncate at the '-'.
93-
versionString = versionString.Substring(0, versionString.IndexOf('-'));
185+
// Split the line into packageName : packageVersion
186+
string[] lineComponents = line.Split(new char[] { ':' }, 2);
187+
188+
return IsAppropriateMBuildVersion(lineComponents[1]);
189+
//if (splitLine.Length == 2)
190+
//{
191+
// // Ensure correct formatting of the version string, before we attempt to parse it.
192+
// string versionString = splitLine[1].Trim(new char[] { ' ', '\"', ',' });
193+
// bool replaceOnEquals = false;
194+
// if (versionString.Contains("-"))
195+
// {
196+
// // The string references a preview version. Truncate at the '-'.
197+
// versionString = versionString.Substring(0, versionString.IndexOf('-'));
94198

95-
// We want to update preview versions to the final.
96-
replaceOnEquals = true;
97-
}
98-
99-
Version version;
100-
if (Version.TryParse(versionString, out version))
101-
{
102-
isAppropriateVersion = replaceOnEquals ? (version > minVersion) : (version >= minVersion);
103-
}
104-
}
105-
106-
break;
199+
// // We want to update preview versions to the final.
200+
// replaceOnEquals = true;
201+
// }
202+
203+
// Version version;
204+
// if (Version.TryParse(versionString, out version))
205+
// {
206+
// isAppropriateVersion = replaceOnEquals ? (version > minVersion) : (version >= minVersion);
207+
// }
208+
//}
107209
}
108210
}
109211
}
110212
}
111213

112-
return (msBuildFound && isAppropriateVersion);
214+
return false;
113215
}
114216

115217
/// <summary>

0 commit comments

Comments
 (0)