Skip to content

Commit dc12ab6

Browse files
authored
Device list CLI now uses nuspec to list NuGet package (#1295)
1 parent f7c1b06 commit dc12ab6

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

src/device-listing/DeviceInfo.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.IO;
7-
using System.Text;
8-
using System.Threading;
7+
using System.Linq;
98

109
namespace Iot.Tools.DeviceListing
1110
{
@@ -17,6 +16,7 @@ class DeviceInfo : IComparable<DeviceInfo>
1716
public HashSet<string> Categories { get; private set; } = new HashSet<string>();
1817
public string CategoriesFilePath { get; private set; }
1918
public bool CategoriesFileExists { get; private set; }
19+
public string NuGetPackageId { get; private set; }
2020

2121
public DeviceInfo(string readmePath, string categoriesFilePath)
2222
{
@@ -25,6 +25,7 @@ public DeviceInfo(string readmePath, string categoriesFilePath)
2525
Title = GetTitle(readmePath) ?? "Error";
2626
CategoriesFilePath = categoriesFilePath;
2727
CategoriesFileExists = File.Exists(categoriesFilePath);
28+
NuGetPackageId = GetNugetPackageId(readmePath);
2829

2930
ImportCategories();
3031
}
@@ -66,5 +67,34 @@ private void ImportCategories()
6667

6768
return null;
6869
}
70+
71+
private string GetNugetPackageId(string readmePath)
72+
{
73+
// find nuspec file
74+
var nuspecFiles = Directory.EnumerateFiles(Path.GetDirectoryName(readmePath)!, "*.nuspec");
75+
76+
// should have only one nuspec file
77+
if (nuspecFiles is null || !nuspecFiles.Any())
78+
{
79+
Console.WriteLine($"Error: Can't find nuspec file for device {Name}");
80+
return string.Empty;
81+
}
82+
83+
// read nuspec file
84+
string[] nuspecLines = File.ReadAllLines(nuspecFiles.First());
85+
86+
// find package id
87+
foreach (string line in nuspecLines)
88+
{
89+
if (line.Contains("<id>") && line.Contains("</id>"))
90+
{
91+
return line.Split(new[] { '<', '>' }, StringSplitOptions.None)[2];
92+
}
93+
}
94+
95+
// shouldn't reach here
96+
Console.WriteLine($"Error: Can't find package id in nuspec file for device {Name}");
97+
return string.Empty;
98+
}
6999
}
70100
}

src/device-listing/Program.cs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -111,25 +111,7 @@ string GetDeviceListing(string devicesPath, IEnumerable<DeviceInfo> devices)
111111
var deviceListing = new StringBuilder();
112112
foreach (DeviceInfo device in devices)
113113
{
114-
if (device.Name.StartsWith("System"))
115-
{
116-
deviceListing.AppendLine($"* [![NuGet](https://img.shields.io/nuget/v/nanoFramework.{device.Name}.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.{device.Name}/) [{device.Title}]({CreateMarkdownLinkFromPath(device.ReadmePath, devicesPath)})");
117-
}
118-
else
119-
{
120-
if ((device.Name == "NumberHelper") || (device.Name == "WeatherHelper"))
121-
{
122-
deviceListing.AppendLine($"* [![NuGet](https://img.shields.io/nuget/v/nanoFramework.IoT.Device.Common.{device.Name}.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.IoT.Device.Common.{device.Name}/) [{device.Title}]({CreateMarkdownLinkFromPath(device.ReadmePath, devicesPath)})");
123-
}
124-
else if (device.Name == "Card")
125-
{
126-
deviceListing.AppendLine($"* [![NuGet](https://img.shields.io/nuget/v/nanoFramework.IoT.Device.Card.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.IoT.Device.Card/) [{device.Title}]({CreateMarkdownLinkFromPath(device.ReadmePath, devicesPath)})");
127-
}
128-
else
129-
{
130-
deviceListing.AppendLine($"* [![NuGet](https://img.shields.io/nuget/v/nanoFramework.IoT.Device.{device.Name}.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.IoT.Device.{device.Name}/) [{device.Title}]({CreateMarkdownLinkFromPath(device.ReadmePath, devicesPath)})");
131-
}
132-
}
114+
deviceListing.AppendLine($"* [![NuGet](https://img.shields.io/nuget/v/{device.NuGetPackageId}.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/{device.NuGetPackageId}/) [{device.Title}]({CreateMarkdownLinkFromPath(device.ReadmePath, devicesPath)})");
133115
}
134116

135117
return deviceListing.ToString();

0 commit comments

Comments
 (0)