Skip to content

Commit

Permalink
Change "avail" command so it can limit results by prefix instead of b…
Browse files Browse the repository at this point in the history
…y minimum version. Should be more useful this way.
  • Loading branch information
simshaun committed Jul 9, 2021
1 parent b292bd2 commit 8ea9494
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
12 changes: 7 additions & 5 deletions NodeSwap.Tests/NodeJsWebApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ public void GetLatestNodeVersion_ShouldWorkWithPrefix()
public void GetInstallableNodeVersions_ShouldWork()
{
var api = GetNodeJsWebApi();
api.GetInstallableNodeVersions().Count.ShouldBe(10);
api.GetInstallableNodeVersions().Count.ShouldBe(11);
}

[TestMethod]
public void GetInstallableNodeVersions_ShouldWorkWithMinVersion()
public void GetInstallableNodeVersions_ShouldWorkWithPrefix()
{
var api = GetNodeJsWebApi();
api.GetInstallableNodeVersions("15").Count.ShouldBe(5);
api.GetInstallableNodeVersions("14.16.1").Count.ShouldBe(1);
api.GetInstallableNodeVersions("14.16").Count.ShouldBe(2);
api.GetInstallableNodeVersions("14").Count.ShouldBe(6);
}

private static NodeJsWebApi GetNodeJsWebApi()
Expand All @@ -55,8 +57,8 @@ protected override Stream NodeVersionsStream()
{
return GenerateStreamFromString("version\tdate\nv16.0.0\t2021-04-20\nv15.14.0\t2021-04-06" +
"\nv15.10.0\t2021-02-23\nv15.9.0\t2021-02-18\nv15.0.0\t2020-10-20" +
"\nv14.16.1\t2021-04-06\nv14.14.0\t2020-10-15\nv14.10.0\t2020-09-08" +
"\nv14.9.0\t2020-08-27\nv14.8.0\t2020-08-11\n");
"\nv14.16.1\t2021-04-06\nv14.16.0\t2021-02-23\nv14.14.0\t2020-10-15" +
"\nv14.10.0\t2020-09-08\nv14.9.0\t2020-08-27\nv14.8.0\t2020-08-11\n");
}

private static Stream GenerateStreamFromString(string s)
Expand Down
4 changes: 2 additions & 2 deletions NodeSwap/Commands/AvailCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public AvailCommand(NodeJsWebApi nodeWeb)

public Task<int> InvokeAsync(InvocationContext context)
{
var minVersion = context.ParseResult.ValueForArgument("min");
var versionPrefix = context.ParseResult.ValueForArgument("prefix");

try
{
var versions = _nodeWeb.GetInstallableNodeVersions(minVersion?.ToString());
var versions = _nodeWeb.GetInstallableNodeVersions(versionPrefix?.ToString());
if (versions.Count == 0)
{
Console.WriteLine("None found");
Expand Down
11 changes: 8 additions & 3 deletions NodeSwap/NodeJsWebApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ public Version GetLatestNodeVersion(string prefix)
/// <summary>
/// Get the versions of Node that may be installed.
/// </summary>
public List<Version> GetInstallableNodeVersions(string min = "")
public List<Version> GetInstallableNodeVersions(string prefix = "")
{
var minVersion = VersionParser.Parse(min != "" ? min : "0.0.0");
var minVersion = VersionParser.Parse(prefix != "" ? prefix : "0.0.0");

var versions = new List<Version>();
var reader = NodeVersionsStreamReader();
string line;
while ((line = reader.ReadLine()) != null)
{
var lineVersion = VersionParser.Parse(line.Split("\t")[0]);
if (lineVersion >= minVersion)
if (lineVersion >= minVersion && (prefix == "" || lineVersion.ToString().StartsWith(prefix)))
{
versions.Add(lineVersion);
}
Expand Down Expand Up @@ -98,6 +98,11 @@ private StreamReader NodeVersionsStreamReader()
{
if (_nodeVersionsStreamReader != null)
{
// Reset reader to beginning on each access
_nodeVersionsStreamReader.DiscardBufferedData();
_nodeVersionsStreamReader.BaseStream.Seek(0, SeekOrigin.Begin);
_nodeVersionsStreamReader.ReadLine(); // skip first line

return _nodeVersionsStreamReader;
}

Expand Down
2 changes: 1 addition & 1 deletion NodeSwap/NodeSwap.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<Version>1.1.2</Version>
<Version>1.2.0</Version>
<!-- Single file app – https://docs.microsoft.com/en-us/dotnet/core/deploying/single-file -->
<PublishSingleFile>true</PublishSingleFile>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
Expand Down
10 changes: 6 additions & 4 deletions NodeSwap/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ private static int Main(string[] args)
Console.Error.WriteLine($"Missing {StorageEnv} ENV var. It should exist and contain a folder path.");
return 1;
}

if (!Directory.Exists(globalContext.StoragePath))
{
Console.Error.WriteLine($"The directory specified by the {StorageEnv} ENV var does not exist.");
Expand All @@ -59,10 +60,11 @@ private static int Main(string[] args)
};
rootCommand.Add(listCommand);

var availMinArg = new Argument("min");
availMinArg.SetDefaultValue("0.0.0");
var availCommand = new Command("avail") {availMinArg};
availCommand.Description = "List the versions available for download.";
var availPrefixArg = new Argument("prefix");
availPrefixArg.SetDefaultValue("");
var availCommand = new Command("avail") {availPrefixArg};
availCommand.Description =
"List versions available for download. Prefix can be specific like `14.16.1`, or fuzzy like `14.16` or `14`.";
availCommand.Handler = Container.GetInstance<AvailCommand>();
rootCommand.Add(availCommand);

Expand Down

0 comments on commit 8ea9494

Please sign in to comment.