Skip to content

Commit 464013e

Browse files
Update parsing logic for the output of nuget list command (#2732)
1 parent d71a3e8 commit 464013e

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

build/BuildSteps.cs

+20-19
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,9 @@ public static void UpdatePackageVersionForIntegrationTests()
6666
foreach (var package in packagesToUpdate)
6767
{
6868
var packageInfo = GetLatestPackageInfo(name: package.Name, majorVersion: package.MajorVersion, source: AzureFunctionsPreReleaseFeedName);
69+
Shell.Run("dotnet", $"add package {packageInfo.Name} -v {packageInfo.Version} -s {AzureFunctionsPreReleaseFeedName} --no-restore");
6970

70-
if (string.IsNullOrEmpty(packageInfo))
71-
{
72-
throw new Exception($"Failed to get {package} package information from {AzureFunctionsPreReleaseFeedName}.");
73-
}
74-
75-
var parts = packageInfo.Split(" ");
76-
var packageName = parts[0];
77-
var packageVersion = parts[1];
78-
79-
Shell.Run("dotnet", $"add package {packageName} -v {packageVersion} -s {AzureFunctionsPreReleaseFeedName} --no-restore");
80-
81-
buildPackages.Add(packageName, packageVersion);
71+
buildPackages.Add(packageInfo.Name, packageInfo.Version);
8272
}
8373
}
8474
finally
@@ -657,31 +647,42 @@ private static void RemoveLanguageWorkers(string outputPath)
657647
}
658648
}
659649

660-
private static string GetLatestPackageInfo(string name, string majorVersion, string source)
650+
private static PackageInfo GetLatestPackageInfo(string name, string majorVersion, string source)
661651
{
662652
string includeAllVersion = !string.IsNullOrWhiteSpace(majorVersion) ? "-AllVersions" : string.Empty;
663653
string packageInfo = Shell.GetOutput("NuGet", $"list {name} -Source {source} -prerelease {includeAllVersion}");
664654

665-
if (string.IsNullOrEmpty(packageInfo))
655+
if (packageInfo.Contains("No packages found", StringComparison.OrdinalIgnoreCase))
666656
{
667-
throw new Exception($"Failed to get {name} package information from {source}.");
657+
throw new Exception($"Package name {name} not found in {source}.");
668658
}
669659

670660
if (!string.IsNullOrWhiteSpace(majorVersion))
671661
{
672662
foreach (var package in packageInfo.Split(Environment.NewLine))
673663
{
674-
var version = package.Split(" ")[1];
675-
if (version.StartsWith(majorVersion))
664+
var thisPackage = NewPackageInfo(package);
665+
if (thisPackage.Name.Equals(name, StringComparison.OrdinalIgnoreCase) && thisPackage.Version.StartsWith(majorVersion))
676666
{
677-
return package;
667+
return thisPackage;
678668
}
679669
}
680670

681671
throw new Exception($"Failed to find {name} package for major version {majorVersion}.");
682672
}
683673

684-
return packageInfo;
674+
return NewPackageInfo(packageInfo);
675+
}
676+
677+
private static PackageInfo NewPackageInfo(string packageInfo)
678+
{
679+
var parts = packageInfo.Split(" ");
680+
if (parts.Length > 2)
681+
{
682+
throw new Exception($"Invalid package format. The string should only contain 'name<space>version'. Current value: '{packageInfo}'");
683+
}
684+
685+
return new PackageInfo(Name: parts[0], Version: parts[1]);
685686
}
686687
}
687688
}

build/PackageInfo.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Build
6+
{
7+
internal class PackageInfo
8+
{
9+
public PackageInfo(string Name, string Version)
10+
{
11+
this.Name = Name;
12+
this.Version = Version;
13+
}
14+
15+
public string Name { get; set; }
16+
public string Version { get; set; }
17+
}
18+
}

0 commit comments

Comments
 (0)