Skip to content

Commit c781dcd

Browse files
authored
Add move to target folder support to mv command (#383)
1 parent 2d5c0c0 commit c781dcd

File tree

4 files changed

+51
-32
lines changed

4 files changed

+51
-32
lines changed

src/docs-builder/Cli/Commands.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ namespace Documentation.Builder.Cli;
1616

1717
internal class Commands(ILoggerFactory logger, ICoreService githubActionsService)
1818
{
19+
private void AssignOutputLogger()
20+
{
21+
var log = logger.CreateLogger<Program>();
22+
ConsoleApp.Log = msg => log.LogInformation(msg);
23+
ConsoleApp.LogError = msg => log.LogError(msg);
24+
}
25+
1926
/// <summary>
2027
/// Continuously serve a documentation folder at http://localhost:3000.
2128
/// File systems changes will be reflected without having to restart the server.
@@ -29,6 +36,7 @@ internal class Commands(ILoggerFactory logger, ICoreService githubActionsService
2936
[ConsoleAppFilter<CheckForUpdatesFilter>]
3037
public async Task Serve(string? path = null, int port = 3000, Cancel ctx = default)
3138
{
39+
AssignOutputLogger();
3240
var host = new DocumentationWebHost(path, port, logger, new FileSystem());
3341
await host.RunAsync(ctx);
3442
await host.StopAsync(ctx);
@@ -58,6 +66,7 @@ public async Task<int> Generate(
5866
Cancel ctx = default
5967
)
6068
{
69+
AssignOutputLogger();
6170
pathPrefix ??= githubActionsService.GetInput("prefix");
6271
var fileSystem = new FileSystem();
6372
var context = new BuildContext(fileSystem, fileSystem, path, output)
@@ -115,13 +124,14 @@ public async Task<int> GenerateDefault(
115124
/// <param name="ctx"></param>
116125
[Command("mv")]
117126
public async Task<int> Move(
118-
[Argument] string? source = null,
119-
[Argument] string? target = null,
127+
[Argument] string source,
128+
[Argument] string target,
120129
bool? dryRun = null,
121130
string? path = null,
122131
Cancel ctx = default
123132
)
124133
{
134+
AssignOutputLogger();
125135
var fileSystem = new FileSystem();
126136
var context = new BuildContext(fileSystem, fileSystem, path, null)
127137
{

src/docs-builder/Program.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,8 @@
2828
services.AddSingleton<DiagnosticsCollector>();
2929

3030
await using var serviceProvider = services.BuildServiceProvider();
31-
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
3231
ConsoleApp.ServiceProvider = serviceProvider;
3332

34-
var isHelp = args.Contains("-h") || args.Contains("--help");
35-
if (!isHelp)
36-
ConsoleApp.Log = msg => logger.LogInformation(msg);
37-
ConsoleApp.LogError = msg => logger.LogError(msg);
38-
3933
var app = ConsoleApp.Create();
4034
app.Add<Commands>();
4135

src/docs-mover/Move.cs

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,18 @@ public record LinkModification(string OldLink, string NewLink, string SourceFile
2323

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

26-
public async Task<int> Execute(string? source, string? target, bool isDryRun, Cancel ctx = default)
26+
public async Task<int> Execute(string source, string target, bool isDryRun, Cancel ctx = default)
2727
{
2828
if (isDryRun)
2929
_logger.LogInformation("Running in dry-run mode");
3030

31-
if (!ValidateInputs(source, target))
32-
{
31+
if (!ValidateInputs(source, target, out var from, out var to))
3332
return 1;
34-
}
3533

34+
var sourcePath = from.FullName;
35+
var targetPath = to.FullName;
3636

37-
var sourcePath = Path.GetFullPath(source!);
38-
var targetPath = Path.GetFullPath(target!);
37+
_logger.LogInformation($"Requested to move from '{from}' to '{to}");
3938

4039
var sourceContent = await readFileSystem.File.ReadAllTextAsync(sourcePath, ctx);
4140

@@ -119,40 +118,33 @@ await ProcessMarkdownFile(
119118
return 0;
120119
}
121120

122-
private bool ValidateInputs(string? source, string? target)
121+
private bool ValidateInputs(string source, string target, out IFileInfo from, out IFileInfo to)
123122
{
123+
from = readFileSystem.FileInfo.New(source);
124+
to = readFileSystem.FileInfo.New(target);
124125

125-
if (string.IsNullOrEmpty(source))
126-
{
127-
_logger.LogError("Source path is required");
128-
return false;
129-
}
130-
131-
if (string.IsNullOrEmpty(target))
132-
{
133-
_logger.LogError("Target path is required");
134-
return false;
135-
}
136-
137-
if (!Path.GetExtension(source).Equals(".md", StringComparison.OrdinalIgnoreCase))
126+
if (!from.Extension.Equals(".md", StringComparison.OrdinalIgnoreCase))
138127
{
139128
_logger.LogError("Source path must be a markdown file. Directory paths are not supported yet");
140129
return false;
141130
}
142131

143-
if (!Path.GetExtension(target).Equals(".md", StringComparison.OrdinalIgnoreCase))
132+
if (to.Extension == string.Empty)
133+
to = readFileSystem.FileInfo.New(Path.Combine(to.FullName, from.Name));
134+
135+
if (!to.Extension.Equals(".md", StringComparison.OrdinalIgnoreCase))
144136
{
145-
_logger.LogError("Target path must be a markdown file. Directory paths are not supported yet");
137+
_logger.LogError($"Target path '{to.FullName}' must be a markdown file.");
146138
return false;
147139
}
148140

149-
if (!readFileSystem.File.Exists(source))
141+
if (!from.Exists)
150142
{
151143
_logger.LogError($"Source file {source} does not exist");
152144
return false;
153145
}
154146

155-
if (readFileSystem.File.Exists(target))
147+
if (to.Exists)
156148
{
157149
_logger.LogError($"Target file {target} already exists");
158150
return false;

tests/Elastic.Markdown.Tests/Mover/MoverTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,27 @@ public async Task RelativeLinks()
3535
mover.LinkModifications[2].OldLink.Should().Be("[Absolut link to first page](/testing/mover/first-page.md)");
3636
mover.LinkModifications[2].NewLink.Should().Be("[Absolut link to first page](/new-folder/hello-world.md)");
3737
}
38+
39+
[Fact]
40+
public async Task MoveToFolder()
41+
{
42+
var workingDirectory = Set.Configuration.SourceFile.DirectoryName;
43+
Directory.SetCurrentDirectory(workingDirectory!);
44+
45+
var mover = new Move(ReadFileSystem, WriteFileSystem, Set, LoggerFactory);
46+
await mover.Execute("testing/mover/first-page.md", "new-folder", true);
47+
mover.LinkModifications.Should().HaveCount(3);
48+
49+
Path.GetRelativePath(".", mover.LinkModifications[0].SourceFile).Should().Be("testing/mover/first-page.md");
50+
mover.LinkModifications[0].OldLink.Should().Be("[Link to second page](second-page.md)");
51+
mover.LinkModifications[0].NewLink.Should().Be("[Link to second page](../testing/mover/second-page.md)");
52+
53+
Path.GetRelativePath(".", mover.LinkModifications[1].SourceFile).Should().Be("testing/mover/second-page.md");
54+
mover.LinkModifications[1].OldLink.Should().Be("[Link to first page](first-page.md)");
55+
mover.LinkModifications[1].NewLink.Should().Be("[Link to first page](../../new-folder/first-page.md)");
56+
57+
Path.GetRelativePath(".", mover.LinkModifications[2].SourceFile).Should().Be("testing/mover/second-page.md");
58+
mover.LinkModifications[2].OldLink.Should().Be("[Absolut link to first page](/testing/mover/first-page.md)");
59+
mover.LinkModifications[2].NewLink.Should().Be("[Absolut link to first page](/new-folder/first-page.md)");
60+
}
3861
}

0 commit comments

Comments
 (0)