Skip to content

Add move to target folder support to mv command #383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/docs-builder/Cli/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ namespace Documentation.Builder.Cli;

internal class Commands(ILoggerFactory logger, ICoreService githubActionsService)
{
private void AssignOutputLogger()
{
var log = logger.CreateLogger<Program>();
ConsoleApp.Log = msg => log.LogInformation(msg);
ConsoleApp.LogError = msg => log.LogError(msg);
}

/// <summary>
/// Continuously serve a documentation folder at http://localhost:3000.
/// File systems changes will be reflected without having to restart the server.
Expand All @@ -29,6 +36,7 @@ internal class Commands(ILoggerFactory logger, ICoreService githubActionsService
[ConsoleAppFilter<CheckForUpdatesFilter>]
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);
Expand Down Expand Up @@ -58,6 +66,7 @@ public async Task<int> Generate(
Cancel ctx = default
)
{
AssignOutputLogger();
pathPrefix ??= githubActionsService.GetInput("prefix");
var fileSystem = new FileSystem();
var context = new BuildContext(fileSystem, fileSystem, path, output)
Expand Down Expand Up @@ -115,13 +124,14 @@ public async Task<int> GenerateDefault(
/// <param name="ctx"></param>
[Command("mv")]
public async Task<int> 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)
{
Expand Down
6 changes: 0 additions & 6 deletions src/docs-builder/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,8 @@
services.AddSingleton<DiagnosticsCollector>();

await using var serviceProvider = services.BuildServiceProvider();
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
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<Commands>();

Expand Down
40 changes: 16 additions & 24 deletions src/docs-mover/Move.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,18 @@ public record LinkModification(string OldLink, string NewLink, string SourceFile

public ReadOnlyCollection<LinkModification> LinkModifications => _linkModifications.AsReadOnly();

public async Task<int> Execute(string? source, string? target, bool isDryRun, Cancel ctx = default)
public async Task<int> 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);

Expand Down Expand Up @@ -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;
Expand Down
23 changes: 23 additions & 0 deletions tests/Elastic.Markdown.Tests/Mover/MoverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)");
}
}