From 9354fb087fe6c567c5890cb929b6ec3ac73aa4d7 Mon Sep 17 00:00:00 2001 From: Alexandru Neamtu Date: Sat, 3 Feb 2024 22:17:31 -0500 Subject: [PATCH] Added the command to download latest release with a path. --- src/Velopack.Deployment/PathRepository.cs | 17 ++++++++++ .../Commands/PathDownloadCommand.cs | 15 +++++++++ src/Velopack.Vpk/OptionMapper.cs | 1 + src/Velopack.Vpk/Program.cs | 1 + .../Commands/PathDownloadCommandTests.cs | 32 +++++++++++++++++++ 5 files changed, 66 insertions(+) create mode 100644 src/Velopack.Deployment/PathRepository.cs create mode 100644 src/Velopack.Vpk/Commands/PathDownloadCommand.cs create mode 100644 test/Velopack.CommandLine.Tests/Commands/PathDownloadCommandTests.cs diff --git a/src/Velopack.Deployment/PathRepository.cs b/src/Velopack.Deployment/PathRepository.cs new file mode 100644 index 00000000..623bf52f --- /dev/null +++ b/src/Velopack.Deployment/PathRepository.cs @@ -0,0 +1,17 @@ +using Microsoft.Extensions.Logging; +using Velopack.Sources; + +namespace Velopack.Deployment; + +public class PathDownloadOptions : RepositoryOptions +{ + public DirectoryInfo Path { get; set; } +} + +public class PathRepository(ILogger logger) : SourceRepository(logger) +{ + public override SimpleFileSource CreateSource(PathDownloadOptions options) + { + return new SimpleFileSource(options.Path); + } +} diff --git a/src/Velopack.Vpk/Commands/PathDownloadCommand.cs b/src/Velopack.Vpk/Commands/PathDownloadCommand.cs new file mode 100644 index 00000000..1d694986 --- /dev/null +++ b/src/Velopack.Vpk/Commands/PathDownloadCommand.cs @@ -0,0 +1,15 @@ +namespace Velopack.Vpk.Commands; + +public class PathDownloadCommand : OutputCommand +{ + public DirectoryInfo Path { get; private set; } + + public PathDownloadCommand() + : base("path", "Download latest release from a specific path source.") + { + AddOption((p) => Path = p, "--path") + .SetDescription("Path to download releases from.") + .MustExist() + .SetRequired(); + } +} diff --git a/src/Velopack.Vpk/OptionMapper.cs b/src/Velopack.Vpk/OptionMapper.cs index 3227ca0e..d652c640 100644 --- a/src/Velopack.Vpk/OptionMapper.cs +++ b/src/Velopack.Vpk/OptionMapper.cs @@ -21,6 +21,7 @@ public static partial class OptionMapper public static partial GitHubDownloadOptions ToOptions(this GitHubDownloadCommand cmd); public static partial GitHubUploadOptions ToOptions(this GitHubUploadCommand cmd); public static partial HttpDownloadOptions ToOptions(this HttpDownloadCommand cmd); + public static partial PathDownloadOptions ToOption(this PathDownloadCommand cmd); public static partial S3DownloadOptions ToOptions(this S3DownloadCommand cmd); public static partial S3UploadOptions ToOptions(this S3UploadCommand cmd); public static partial DeltaGenOptions ToOptions(this DeltaGenCommand cmd); diff --git a/src/Velopack.Vpk/Program.cs b/src/Velopack.Vpk/Program.cs index c3bfcc51..192ad48c 100644 --- a/src/Velopack.Vpk/Program.cs +++ b/src/Velopack.Vpk/Program.cs @@ -83,6 +83,7 @@ public class Program downloadCommand.AddRepositoryDownload(provider); downloadCommand.AddRepositoryDownload(provider); downloadCommand.AddRepositoryDownload(provider); + downloadCommand.AddRepositoryDownload(provider); rootCommand.Add(downloadCommand); var uploadCommand = new CliCommand("upload", "Upload local package(s) to a remote update source."); diff --git a/test/Velopack.CommandLine.Tests/Commands/PathDownloadCommandTests.cs b/test/Velopack.CommandLine.Tests/Commands/PathDownloadCommandTests.cs new file mode 100644 index 00000000..59fd9782 --- /dev/null +++ b/test/Velopack.CommandLine.Tests/Commands/PathDownloadCommandTests.cs @@ -0,0 +1,32 @@ +using System.CommandLine; +using Velopack.Vpk.Commands; + +namespace Velopack.CommandLine.Tests.Commands; +public class PathDownloadCommandTests : BaseCommandTests +{ + [Fact] + public void Path_WithPath_ParsesValue() + { + var command = new PathDownloadCommand(); + + DirectoryInfo directory = Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "releases")); + ParseResult parseResult = command.ParseAndApply($"--path {directory.FullName}"); + + Assert.Empty(parseResult.Errors); + Assert.Equal(directory.FullName, command.Path.FullName); + + Directory.Delete(directory.FullName); + } + + [Fact] + public void Path_WithNonExistingDirectory_ShowsError() + { + var command = new PathDownloadCommand(); + + // Parse with a fake path + ParseResult parseResult = command.ParseAndApply($"--path \"E:\releases\""); + + Assert.Equal(1, parseResult.Errors.Count); + Assert.StartsWith("--path directory is not found, but must exist", parseResult.Errors[0].Message); + } +}