Skip to content

Commit 813e6a1

Browse files
authored
Refactor to use WorkingDirectoryRoot over Root. (#791)
* Refactor to use `WorkingDirectoryRoot` over `Root`. Replaced references to `Paths.Root` with `Paths.WorkingDirectoryRoot` across the project to improve clarity and alignment with current working directory semantics. Adjusted related logic to handle directory determination and path relations more effectively. Introduced `BuildContext.DocumentationCheckoutDirectory?` to centrally determine the checkout folder. * Fix test
1 parent c2f0902 commit 813e6a1

File tree

21 files changed

+67
-40
lines changed

21 files changed

+67
-40
lines changed

src/Elastic.Markdown/BuildContext.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ public record BuildContext
1616
public IFileSystem ReadFileSystem { get; }
1717
public IFileSystem WriteFileSystem { get; }
1818

19+
public IDirectoryInfo? DocumentationCheckoutDirectory { get; }
1920
public IDirectoryInfo DocumentationSourceDirectory { get; }
2021
public IDirectoryInfo DocumentationOutputDirectory { get; }
2122

22-
public ConfigurationFile Configuration { get; set; }
23+
public ConfigurationFile Configuration { get; }
2324

2425
public IFileInfo ConfigurationPath { get; }
2526

@@ -61,19 +62,21 @@ public BuildContext(DiagnosticsCollector collector, IFileSystem readFileSystem,
6162

6263
var rootFolder = !string.IsNullOrWhiteSpace(source)
6364
? ReadFileSystem.DirectoryInfo.New(source)
64-
: ReadFileSystem.DirectoryInfo.New(Path.Combine(Paths.Root.FullName));
65+
: ReadFileSystem.DirectoryInfo.New(Path.Combine(Paths.WorkingDirectoryRoot.FullName));
6566

6667
(DocumentationSourceDirectory, ConfigurationPath) = FindDocsFolderFromRoot(rootFolder);
6768

69+
DocumentationCheckoutDirectory = Paths.DetermineSourceDirectoryRoot(DocumentationSourceDirectory);
70+
6871
DocumentationOutputDirectory = !string.IsNullOrWhiteSpace(output)
6972
? WriteFileSystem.DirectoryInfo.New(output)
70-
: WriteFileSystem.DirectoryInfo.New(Path.Combine(Paths.Root.FullName, Path.Combine(".artifacts", "docs", "html")));
73+
: WriteFileSystem.DirectoryInfo.New(Path.Combine(Paths.WorkingDirectoryRoot.FullName, Path.Combine(".artifacts", "docs", "html")));
7174

7275
if (ConfigurationPath.FullName != DocumentationSourceDirectory.FullName)
7376
DocumentationSourceDirectory = ConfigurationPath.Directory!;
7477

7578

76-
Git = GitCheckoutInformation.Create(DocumentationSourceDirectory, ReadFileSystem);
79+
Git = GitCheckoutInformation.Create(DocumentationCheckoutDirectory, ReadFileSystem);
7780
Configuration = new ConfigurationFile(this);
7881
}
7982

src/Elastic.Markdown/IO/Discovery/GitCheckoutInformation.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Elastic.Markdown.IO.Discovery;
1111

1212
public record GitCheckoutInformation
1313
{
14-
private static GitCheckoutInformation Unavailable { get; } = new()
14+
public static GitCheckoutInformation Unavailable { get; } = new()
1515
{
1616
Branch = "unavailable",
1717
Remote = "unavailable",
@@ -32,8 +32,11 @@ public record GitCheckoutInformation
3232
public string? RepositoryName { get; init; }
3333

3434
// manual read because libgit2sharp is not yet AOT ready
35-
public static GitCheckoutInformation Create(IDirectoryInfo source, IFileSystem fileSystem, ILogger? logger = null)
35+
public static GitCheckoutInformation Create(IDirectoryInfo? source, IFileSystem fileSystem, ILogger? logger = null)
3636
{
37+
if (source is null)
38+
return Unavailable;
39+
3740
if (fileSystem is not FileSystem)
3841
{
3942
return new GitCheckoutInformation

src/Elastic.Markdown/IO/DocumentationSet.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ public class DocumentationSet : INavigationLookups
4141
public IDirectoryInfo SourceDirectory { get; }
4242
public IDirectoryInfo OutputDirectory { get; }
4343

44-
public string RelativeSourcePath { get; }
45-
4644
public DateTimeOffset LastWrite { get; }
4745

4846
public ConfigurationFile Configuration { get; }
@@ -68,7 +66,6 @@ public DocumentationSet(BuildContext build, ILoggerFactory logger, ICrossLinkRes
6866
Build = build;
6967
SourceDirectory = build.DocumentationSourceDirectory;
7068
OutputDirectory = build.DocumentationOutputDirectory;
71-
RelativeSourcePath = Path.GetRelativePath(Paths.Root.FullName, SourceDirectory.FullName);
7269
LinkResolver =
7370
linkResolver ?? new CrossLinkResolver(new ConfigurationCrossLinkFetcher(build.Configuration, logger));
7471
Configuration = build.Configuration;

src/Elastic.Markdown/IO/Paths.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
// Licensed to Elasticsearch B.V under one or more agreements.
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
4+
5+
using System.IO.Abstractions;
6+
47
namespace Elastic.Markdown.IO;
58

69
public static class Paths
710
{
8-
public static readonly DirectoryInfo Root = RootDirectoryInfo();
11+
public static readonly DirectoryInfo WorkingDirectoryRoot = DetermineWorkingDirectoryRoot();
912

1013
public static readonly DirectoryInfo ApplicationData = GetApplicationFolder();
1114

12-
private static DirectoryInfo RootDirectoryInfo()
15+
private static DirectoryInfo DetermineWorkingDirectoryRoot()
1316
{
1417
var directory = new DirectoryInfo(Directory.GetCurrentDirectory());
1518
while (directory != null &&
@@ -18,6 +21,16 @@ private static DirectoryInfo RootDirectoryInfo()
1821
return directory ?? new DirectoryInfo(Directory.GetCurrentDirectory());
1922
}
2023

24+
public static IDirectoryInfo? DetermineSourceDirectoryRoot(IDirectoryInfo sourceDirectory)
25+
{
26+
IDirectoryInfo? sourceRoot = null;
27+
var directory = sourceDirectory;
28+
while (directory != null && directory.GetDirectories(".git").Length == 0)
29+
directory = directory.Parent;
30+
sourceRoot ??= directory;
31+
return sourceRoot;
32+
}
33+
2134
/// Used in debug to locate static folder so we can change js/css files while the server is running
2235
public static DirectoryInfo? GetSolutionDirectory()
2336
{

src/Elastic.Markdown/InboundLinks/LinkIndexLinkChecker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public async Task<int> CheckWithLocalLinksJson(DiagnosticsCollector collector, s
6363
_logger.LogInformation("Checking '{Repository}' with local '{LocalLinksJson}'", repository, localLinksJson);
6464

6565
if (!Path.IsPathRooted(localLinksJson))
66-
localLinksJson = Path.Combine(Paths.Root.FullName, localLinksJson);
66+
localLinksJson = Path.Combine(Paths.WorkingDirectoryRoot.FullName, localLinksJson);
6767

6868
try
6969
{

src/Elastic.Markdown/Slices/HtmlWriter.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Concurrent;
66
using System.IO.Abstractions;
77
using Elastic.Markdown.IO;
8+
using Elastic.Markdown.IO.Discovery;
89
using Elastic.Markdown.IO.Navigation;
910
using Markdig.Syntax;
1011
using RazorSlices;
@@ -60,7 +61,11 @@ private NavigationViewModel CreateNavigationModel(INavigation navigation)
6061
}
6162
}
6263

63-
public class HtmlWriter(DocumentationSet documentationSet, IFileSystem writeFileSystem, IDescriptionGenerator descriptionGenerator, INavigationHtmlWriter? navigationHtmlWriter = null)
64+
public class HtmlWriter(
65+
DocumentationSet documentationSet,
66+
IFileSystem writeFileSystem,
67+
IDescriptionGenerator descriptionGenerator,
68+
INavigationHtmlWriter? navigationHtmlWriter = null)
6469
{
6570
private DocumentationSet DocumentationSet { get; } = documentationSet;
6671
public INavigationHtmlWriter NavigationHtmlWriter { get; } = navigationHtmlWriter ?? new IsolatedBuildNavigationHtmlWriter(documentationSet);
@@ -85,8 +90,14 @@ private async Task<string> RenderLayout(MarkdownFile markdown, MarkdownDocument
8590

8691
var remote = DocumentationSet.Build.Git.RepositoryName;
8792
var branch = DocumentationSet.Build.Git.Branch;
88-
var path = Path.Combine(DocumentationSet.RelativeSourcePath, markdown.RelativePath);
89-
var editUrl = $"https://github.com/elastic/{remote}/edit/{branch}/{path}";
93+
string? editUrl = null;
94+
if (DocumentationSet.Build.Git != GitCheckoutInformation.Unavailable && DocumentationSet.Build.DocumentationCheckoutDirectory is { } checkoutDirectory)
95+
{
96+
var relativeSourcePath = Path.GetRelativePath(checkoutDirectory.FullName, DocumentationSet.Build.DocumentationSourceDirectory.FullName);
97+
var path = Path.Combine(relativeSourcePath, markdown.RelativePath);
98+
editUrl = $"https://github.com/elastic/{remote}/edit/{branch}/{path}";
99+
}
100+
90101
var slice = Index.Create(new IndexViewModel
91102
{
92103
Title = markdown.Title ?? "[TITLE NOT SET]",

src/docs-assembler/AssembleContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ public AssembleContext(
4444
ReadFileSystem = readFileSystem;
4545
WriteFileSystem = writeFileSystem;
4646

47-
var configPath = Path.Combine(Paths.Root.FullName, "src", "docs-assembler", "assembler.yml");
47+
var configPath = Path.Combine(Paths.WorkingDirectoryRoot.FullName, "src", "docs-assembler", "assembler.yml");
4848
// temporarily fallback to embedded assembler.yml
4949
// This will live in docs-content soon
5050
if (!ReadFileSystem.File.Exists(configPath))
5151
ExtractAssemblerConfiguration(configPath);
5252
ConfigurationPath = ReadFileSystem.FileInfo.New(configPath);
5353
Configuration = AssemblyConfiguration.Deserialize(ReadFileSystem.File.ReadAllText(ConfigurationPath.FullName));
5454

55-
var navigationPath = Path.Combine(Paths.Root.FullName, "src", "docs-assembler", "navigation.yml");
55+
var navigationPath = Path.Combine(Paths.WorkingDirectoryRoot.FullName, "src", "docs-assembler", "navigation.yml");
5656
if (!ReadFileSystem.File.Exists(navigationPath))
5757
ExtractAssemblerConfiguration(navigationPath);
5858
NavigationPath = ReadFileSystem.FileInfo.New(navigationPath);

src/docs-assembler/Cli/InboundLinkCommands.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public async Task<int> ValidateRepoInboundLinks(string? from = null, string? to
4545
{
4646
AssignOutputLogger();
4747
var fs = new FileSystem();
48-
var root = fs.DirectoryInfo.New(Paths.Root.FullName);
48+
var root = fs.DirectoryInfo.New(Paths.WorkingDirectoryRoot.FullName);
4949
if (from == null && to == null)
5050
{
5151
from ??= GitCheckoutInformation.Create(root, new FileSystem(), logger.CreateLogger(nameof(GitCheckoutInformation))).RepositoryName;
@@ -67,7 +67,7 @@ public async Task<int> ValidateLocalLinkReference([Argument] string? file = null
6767
AssignOutputLogger();
6868
file ??= ".artifacts/docs/html/links.json";
6969
var fs = new FileSystem();
70-
var root = fs.DirectoryInfo.New(Paths.Root.FullName);
70+
var root = fs.DirectoryInfo.New(Paths.WorkingDirectoryRoot.FullName);
7171
var repository = GitCheckoutInformation.Create(root, new FileSystem(), logger.CreateLogger(nameof(GitCheckoutInformation))).RepositoryName
7272
?? throw new Exception("Unable to determine repository name");
7373

src/docs-assembler/Sourcing/RepositorySourcesFetcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private Checkout CloneOrUpdateRepository(Repository repository, string name, Con
7474
{
7575
var fs = context.ReadFileSystem;
7676
var checkoutFolder = fs.DirectoryInfo.New(Path.Combine(context.CheckoutDirectory.FullName, name));
77-
var relativePath = Path.GetRelativePath(Paths.Root.FullName, checkoutFolder.FullName);
77+
var relativePath = Path.GetRelativePath(Paths.WorkingDirectoryRoot.FullName, checkoutFolder.FullName);
7878
var sw = Stopwatch.StartNew();
7979
_ = dict.AddOrUpdate(name, sw, (_, _) => sw);
8080
var head = string.Empty;

src/docs-builder/Cli/Commands.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public async Task<int> Generate(
124124
{
125125
var outputDirectory = !string.IsNullOrWhiteSpace(output)
126126
? fileSystem.DirectoryInfo.New(output)
127-
: fileSystem.DirectoryInfo.New(Path.Combine(Paths.Root.FullName, ".artifacts/docs/html"));
127+
: fileSystem.DirectoryInfo.New(Path.Combine(Paths.WorkingDirectoryRoot.FullName, ".artifacts/docs/html"));
128128
// we temporarily do not error when pointed to a non documentation folder.
129129
_ = fileSystem.Directory.CreateDirectory(outputDirectory.FullName);
130130

0 commit comments

Comments
 (0)