Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Enable running for path or current directory.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Johnson committed Nov 28, 2021
1 parent 70d58c4 commit eb46be4
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 33 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ $ dotnet tool install --global Umbraco.Docs.Preview.App

```bash
$ umbracodocs # run from UmbracoDocs repo root
$ umbracodocs -h # view help.
```

And you're ready to view the docs by default at http://localhost:5000.
Expand Down
65 changes: 44 additions & 21 deletions src/Umbraco.Docs.Preview.App/Program.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,71 @@
using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO;
using System.Reflection;
using Lamar.Microsoft.DependencyInjection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Umbraco.Docs.Preview.App.Options;

namespace Umbraco.Docs.Preview.App
{
public class Program
{
public static int Main(string[] args)
{
var directory = Directory.GetCurrentDirectory();
if (!File.Exists(Path.Combine(directory, "index.md")))
var rootCommand = new RootCommand
{
Console.Error.WriteLine("Fatal Error: current directory doesn't appear to be the UmbracoDocs repo.");
return -1;
}

CreateHostBuilder(args)
.Build()
.Run();
new Argument
{
Name = "path",
Arity = new ArgumentArity(0, 1),
Description = "Path to UmbracoDocs repository."
}
};
rootCommand.Name = "umbracodocs";
rootCommand.Description = "Run UmbracoDocs preview server";
rootCommand.Handler = CommandHandler.Create<string>(RunServer);

return 0;
return rootCommand.Invoke(args);
}

public static IHostBuilder CreateHostBuilder(string[] args)
private static int RunServer(string path = null)
{
// dotnet tools configure cwd as you would expect, but Host.CreateDefaultBuilder isn't expecting that
// to be miles away from appsettings.json and wwwroot etc.
// So more config, less convention for ContentRoot
var applicationFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
path ??= Directory.GetCurrentDirectory();

var docsAbsolutePath = Path.GetFullPath(path);


if (!File.Exists(Path.Combine(docsAbsolutePath!, "index.md")))
{
Console.Error.WriteLine("Fatal Error: current directory doesn't appear to be the UmbracoDocs repo.");
return -1;
}

return Host.CreateDefaultBuilder(args)
.UseContentRoot(applicationFolder)
Host.CreateDefaultBuilder(new[] { path })
#if !DEBUG
// dotnet tools configures cwd as you would expect, but Host.CreateDefaultBuilder isn't expecting that
// to be miles away from appsettings.json and wwwroot etc.
// So more prefer configuration over convention for ContentRoot (unless debugging).
.UseContentRoot(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))
#endif
.UseLamar()
.UseConsoleLifetime()
.ConfigureWebHostDefaults(webBuilder =>
{
#if !DEBUG
webBuilder.UseEnvironment("Production");
#endif
webBuilder.ConfigureServices(services =>
{
services.Configure<UmbracoDocsOptions>(cfg => cfg.UmbracoDocsRootFolder = docsAbsolutePath);
});
webBuilder.UseStartup<Startup>();
});

})
.Build()
.Run();

return 0;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"profiles": {
"Development": {
"workingDirectory": "../../../UmbracoDocs/",
"commandLineArgs": "../../../UmbracoDocs/",
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
Expand Down
3 changes: 0 additions & 3 deletions src/Umbraco.Docs.Preview.App/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.IO;
using Castle.DynamicProxy;
using Lamar;
using MediatR;
Expand All @@ -11,7 +10,6 @@
using Umbraco.Docs.Preview.App.HostedServices;
using Umbraco.Docs.Preview.App.Interceptors.Caching;
using Umbraco.Docs.Preview.App.MiscellaneousOurStuff;
using Umbraco.Docs.Preview.App.Options;
using Umbraco.Docs.Preview.App.Services;

namespace Umbraco.Docs.Preview.App
Expand All @@ -26,7 +24,6 @@ public void ConfigureContainer(ServiceRegistry services)

services.AddSingleton<IMarkdownService, MarkdownService>();
services.AddSingleton<DocumentationUpdater>();
services.Configure<UmbracoDocsOptions>(cfg => cfg.UmbracoDocsRootFolder = Directory.GetCurrentDirectory());

services.For<IDocumentService>()
.Add<DocumentService>()
Expand Down
5 changes: 3 additions & 2 deletions src/Umbraco.Docs.Preview.App/Umbraco.Docs.Preview.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<PropertyGroup>
<Version>0.1.1</Version>
<Version>0.1.2</Version>
<Company>Umbraco HQ</Company>
<Description>Enables local preview of UmbracoDocs.</Description>
<PackageTags>umbraco</PackageTags>
Expand All @@ -26,11 +26,12 @@
<PackageReference Include="Lamar.Microsoft.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Markdig" Version="0.26.0" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="9.0.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.21308.1" />
<!--<PackageReference Include="Markdig.SyntaxHighlighting" Version="1.1.7" />--> <!--Windows only-->
</ItemGroup>

<ItemGroup>
<None Include="logo-small.png" Pack="true" PackagePath="\"/>
<None Include="logo-small.png" Pack="true" PackagePath="\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@using Microsoft.AspNetCore.Http
@using Umbraco.Docs.Preview.App.Extensions
@model Umbraco.Docs.Preview.App.Models.DocumentationViewModel

@inject IHttpContextAccessor _contextAccessor;
Expand All @@ -7,12 +8,17 @@
public string IsActive(string path, string classname = "active")
{
var requestPath = $"{_contextAccessor?.HttpContext?.Request.Path}";
return requestPath.ToLower().StartsWith(path.ToLower()) ? classname : string.Empty;
return requestPath.ToLower().StartsWith(path.RemoveEmptyFolderParts().ToLower()) ? classname : string.Empty;
}

public string NavUrl(string path)
{
return $"{path}/".RemoveEmptyFolderParts();
}
}

@{
const string root = "/documentation";
const string root = "/documentation/";
}

<nav>
Expand All @@ -28,22 +34,23 @@
&& item.Name != "Cheatsheets")
{
<li class=" @IsActive(root+item.Path, "active open")">
<a href="@(root + item.Path)/"><h3>@item.Name</h3></a>
<a href="@NavUrl(root + item.Path)"><h3>@item.Name</h3></a>

@if (item.HasChildren)
{
<ul class="level-2 @IsActive(root + item.Path, "open")">
@foreach (var itemChild in item.Directories)
{
<li class=" @IsActive(root + itemChild.Path, "active open")">
<a href="@(root + itemChild.Path)/"><h4>@itemChild.Name</h4></a>
<a href="@NavUrl(root + itemChild.Path)"><h4>@itemChild.Name</h4></a>

@if (itemChild.HasChildren)
{
<ul class="level-3 @IsActive(root + item.Path, "open")">
@foreach (var itemGrandChild in itemChild.Directories)
{
<li class=" @IsActive(root + itemGrandChild.Path, "active")"><a href="@(root + itemGrandChild.Path)/"><h5>@itemGrandChild.Name</h5></a></li>
<li class=" @IsActive(root + itemGrandChild.Path, "active")">
<a href="@NavUrl(root + itemGrandChild.Path)"><h5>@itemGrandChild.Name</h5></a></li>
}
</ul>
}
Expand Down
4 changes: 3 additions & 1 deletion umbraco_docs_preview.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=appsettings/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Sitemap/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Umbraco/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Umbraco/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=umbracodocs/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

0 comments on commit eb46be4

Please sign in to comment.