Skip to content

Commit 917d3b3

Browse files
authored
Don't require quotes around release values (#41)
Fixes: #39 The `release` file contained within the [Microsoft OpenJDK Distribution Preview][0] contains an entry which broke the `release` parser within `JdkInfo`, an entry which doesn't contain double quotes around the value: BUILD_NUMBER=9 This causes an exception to be thrown when `JdkInfo` attempts to parse the `release` file: System.ArgumentOutOfRangeException : Length cannot be less than zero. Parameter name: length at System.String.Substring (System.Int32 startIndex, System.Int32 length) [0x0004a] in <7e59c4ad7b424f9eab0db1b78ec1804f>:0 at Xamarin.Android.Tools.JdkInfo.GetReleaseProperties () [0x0006b] in <3975ab851981476abded14492926b261>:0 at Xamarin.Android.Tools.JdkInfo..ctor (System.String homePath) [0x00159] in <3975ab851981476abded14492926b261>:0 Update `JdkInfo.GetReleaseProperties()` so that the double-quotes around values are *optional* and not required, so that this `release` file may be processed without throwing an exception. [0]: https://dl.xamarin.com/OpenJDK/mac/microsoft-dist-openjdk-1.8.0.9.zip
1 parent 7427692 commit 917d3b3

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/Xamarin.Android.Tools.AndroidSdk/JdkInfo.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,21 +144,26 @@ Version GetJavaVersion ()
144144

145145
ReadOnlyDictionary<string, string> GetReleaseProperties ()
146146
{
147+
var props = new Dictionary<string, string> ();
147148
var releasePath = Path.Combine (HomePath, "release");
148149
if (!File.Exists (releasePath))
149-
return new ReadOnlyDictionary<string, string> (new Dictionary<string, string> ());
150+
return new ReadOnlyDictionary<string, string>(props);
150151

151-
var props = new Dictionary<string, string> ();
152152
using (var release = File.OpenText (releasePath)) {
153153
string line;
154154
while ((line = release.ReadLine ()) != null) {
155-
const string PropertyDelim = "=\"";
155+
line = line.Trim ();
156+
const string PropertyDelim = "=";
156157
int delim = line.IndexOf (PropertyDelim, StringComparison.Ordinal);
157158
if (delim < 0) {
158159
props [line] = "";
160+
continue;
161+
}
162+
string key = line.Substring (0, delim).Trim ();
163+
string value = line.Substring (delim + PropertyDelim.Length).Trim ();
164+
if (value.StartsWith ("\"", StringComparison.Ordinal) && value.EndsWith ("\"", StringComparison.Ordinal)) {
165+
value = value.Substring (1, value.Length - 2);
159166
}
160-
string key = line.Substring (0, delim);
161-
string value = line.Substring (delim + PropertyDelim.Length, line.Length - delim - PropertyDelim.Length - 1);
162167
props [key] = value;
163168
}
164169
}

src/Xamarin.Android.Tools.AndroidSdk/Tests/JdkInfoTests.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ internal static void CreateFauxJdk (string dir, string version)
4646

4747
using (var release = new StreamWriter (Path.Combine (dir, "release"))) {
4848
release.WriteLine ($"JAVA_VERSION=\"{version}\"");
49+
release.WriteLine ($"BUILD_NUMBER=42");
50+
release.WriteLine ($"JUST_A_KEY");
4951
}
5052

5153
var bin = Path.Combine (dir, "bin");
@@ -135,8 +137,10 @@ public void ReleaseProperties ()
135137
{
136138
var jdk = new JdkInfo (FauxJdkDir);
137139

138-
Assert.AreEqual (1, jdk.ReleaseProperties.Count);
140+
Assert.AreEqual (3, jdk.ReleaseProperties.Count);
139141
Assert.AreEqual ("1.2.3.4", jdk.ReleaseProperties ["JAVA_VERSION"]);
142+
Assert.AreEqual ("42", jdk.ReleaseProperties ["BUILD_NUMBER"]);
143+
Assert.AreEqual ("", jdk.ReleaseProperties ["JUST_A_KEY"]);
140144
}
141145

142146
[Test]

0 commit comments

Comments
 (0)