diff --git a/src/docs-builder/Cli/Commands.cs b/src/docs-builder/Cli/Commands.cs index 087e6f58a..bbd161af5 100644 --- a/src/docs-builder/Cli/Commands.cs +++ b/src/docs-builder/Cli/Commands.cs @@ -16,6 +16,13 @@ namespace Documentation.Builder.Cli; internal class Commands(ILoggerFactory logger, ICoreService githubActionsService) { + private void AssignOutputLogger() + { + var log = logger.CreateLogger(); + ConsoleApp.Log = msg => log.LogInformation(msg); + ConsoleApp.LogError = msg => log.LogError(msg); + } + /// /// Continuously serve a documentation folder at http://localhost:3000. /// File systems changes will be reflected without having to restart the server. @@ -29,6 +36,7 @@ internal class Commands(ILoggerFactory logger, ICoreService githubActionsService [ConsoleAppFilter] public async Task Serve(string? path = null, int port = 3000, Cancel ctx = default) { + AssignOutputLogger(); var host = new DocumentationWebHost(path, port, logger, new FileSystem()); await host.RunAsync(ctx); await host.StopAsync(ctx); @@ -58,6 +66,7 @@ public async Task Generate( Cancel ctx = default ) { + AssignOutputLogger(); pathPrefix ??= githubActionsService.GetInput("prefix"); var fileSystem = new FileSystem(); var context = new BuildContext(fileSystem, fileSystem, path, output) @@ -115,13 +124,14 @@ public async Task GenerateDefault( /// [Command("mv")] public async Task Move( - [Argument] string? source = null, - [Argument] string? target = null, + [Argument] string source, + [Argument] string target, bool? dryRun = null, string? path = null, Cancel ctx = default ) { + AssignOutputLogger(); var fileSystem = new FileSystem(); var context = new BuildContext(fileSystem, fileSystem, path, null) { diff --git a/src/docs-builder/Program.cs b/src/docs-builder/Program.cs index 0fed8f7ec..97a1dd120 100644 --- a/src/docs-builder/Program.cs +++ b/src/docs-builder/Program.cs @@ -28,14 +28,8 @@ services.AddSingleton(); await using var serviceProvider = services.BuildServiceProvider(); -var logger = serviceProvider.GetRequiredService>(); ConsoleApp.ServiceProvider = serviceProvider; -var isHelp = args.Contains("-h") || args.Contains("--help"); -if (!isHelp) - ConsoleApp.Log = msg => logger.LogInformation(msg); -ConsoleApp.LogError = msg => logger.LogError(msg); - var app = ConsoleApp.Create(); app.Add(); diff --git a/src/docs-mover/Move.cs b/src/docs-mover/Move.cs index 46a21f2a6..9ff7916c3 100644 --- a/src/docs-mover/Move.cs +++ b/src/docs-mover/Move.cs @@ -23,19 +23,18 @@ public record LinkModification(string OldLink, string NewLink, string SourceFile public ReadOnlyCollection LinkModifications => _linkModifications.AsReadOnly(); - public async Task Execute(string? source, string? target, bool isDryRun, Cancel ctx = default) + public async Task Execute(string source, string target, bool isDryRun, Cancel ctx = default) { if (isDryRun) _logger.LogInformation("Running in dry-run mode"); - if (!ValidateInputs(source, target)) - { + if (!ValidateInputs(source, target, out var from, out var to)) return 1; - } + var sourcePath = from.FullName; + var targetPath = to.FullName; - var sourcePath = Path.GetFullPath(source!); - var targetPath = Path.GetFullPath(target!); + _logger.LogInformation($"Requested to move from '{from}' to '{to}"); var sourceContent = await readFileSystem.File.ReadAllTextAsync(sourcePath, ctx); @@ -119,40 +118,33 @@ await ProcessMarkdownFile( return 0; } - private bool ValidateInputs(string? source, string? target) + private bool ValidateInputs(string source, string target, out IFileInfo from, out IFileInfo to) { + from = readFileSystem.FileInfo.New(source); + to = readFileSystem.FileInfo.New(target); - if (string.IsNullOrEmpty(source)) - { - _logger.LogError("Source path is required"); - return false; - } - - if (string.IsNullOrEmpty(target)) - { - _logger.LogError("Target path is required"); - return false; - } - - if (!Path.GetExtension(source).Equals(".md", StringComparison.OrdinalIgnoreCase)) + if (!from.Extension.Equals(".md", StringComparison.OrdinalIgnoreCase)) { _logger.LogError("Source path must be a markdown file. Directory paths are not supported yet"); return false; } - if (!Path.GetExtension(target).Equals(".md", StringComparison.OrdinalIgnoreCase)) + if (to.Extension == string.Empty) + to = readFileSystem.FileInfo.New(Path.Combine(to.FullName, from.Name)); + + if (!to.Extension.Equals(".md", StringComparison.OrdinalIgnoreCase)) { - _logger.LogError("Target path must be a markdown file. Directory paths are not supported yet"); + _logger.LogError($"Target path '{to.FullName}' must be a markdown file."); return false; } - if (!readFileSystem.File.Exists(source)) + if (!from.Exists) { _logger.LogError($"Source file {source} does not exist"); return false; } - if (readFileSystem.File.Exists(target)) + if (to.Exists) { _logger.LogError($"Target file {target} already exists"); return false; diff --git a/tests/Elastic.Markdown.Tests/Mover/MoverTests.cs b/tests/Elastic.Markdown.Tests/Mover/MoverTests.cs index ea4d68d81..e14191d57 100644 --- a/tests/Elastic.Markdown.Tests/Mover/MoverTests.cs +++ b/tests/Elastic.Markdown.Tests/Mover/MoverTests.cs @@ -35,4 +35,27 @@ public async Task RelativeLinks() mover.LinkModifications[2].OldLink.Should().Be("[Absolut link to first page](/testing/mover/first-page.md)"); mover.LinkModifications[2].NewLink.Should().Be("[Absolut link to first page](/new-folder/hello-world.md)"); } + + [Fact] + public async Task MoveToFolder() + { + var workingDirectory = Set.Configuration.SourceFile.DirectoryName; + Directory.SetCurrentDirectory(workingDirectory!); + + var mover = new Move(ReadFileSystem, WriteFileSystem, Set, LoggerFactory); + await mover.Execute("testing/mover/first-page.md", "new-folder", true); + mover.LinkModifications.Should().HaveCount(3); + + Path.GetRelativePath(".", mover.LinkModifications[0].SourceFile).Should().Be("testing/mover/first-page.md"); + mover.LinkModifications[0].OldLink.Should().Be("[Link to second page](second-page.md)"); + mover.LinkModifications[0].NewLink.Should().Be("[Link to second page](../testing/mover/second-page.md)"); + + Path.GetRelativePath(".", mover.LinkModifications[1].SourceFile).Should().Be("testing/mover/second-page.md"); + mover.LinkModifications[1].OldLink.Should().Be("[Link to first page](first-page.md)"); + mover.LinkModifications[1].NewLink.Should().Be("[Link to first page](../../new-folder/first-page.md)"); + + Path.GetRelativePath(".", mover.LinkModifications[2].SourceFile).Should().Be("testing/mover/second-page.md"); + mover.LinkModifications[2].OldLink.Should().Be("[Absolut link to first page](/testing/mover/first-page.md)"); + mover.LinkModifications[2].NewLink.Should().Be("[Absolut link to first page](/new-folder/first-page.md)"); + } }