Skip to content

Commit

Permalink
Merge pull request #3 from dlmelendez/rel/2.0
Browse files Browse the repository at this point in the history
net9 updates
  • Loading branch information
dlmelendez authored Dec 5, 2024
2 parents ff8c203 + 9dee5f6 commit e30679f
Show file tree
Hide file tree
Showing 22 changed files with 145 additions and 120 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ElCamino.DocFx.WebAppRefresh
[DocFx](https://dotnet.github.io/docfx/index.html) build middleware that allows you to setup a .net core web app project and adds a file watcher to your content files. When you change a content file, such as markdown, css, etc, the middleware automatically starts a docFx build in the background regenerating the target html files.

[![NuGet Badge](https://buildstats.info/nuget/ElCamino.DocFx.WebAppRefresh)](https://www.nuget.org/packages/ElCamino.DocFx.WebAppRefresh/)
[![NuGet Badge](https://img.shields.io/nuget/v/ElCamino.DocFx.WebAppRefresh)](https://www.nuget.org/packages/ElCamino.DocFx.WebAppRefresh/)

Project site https://elcamino.cloud/projects/docs/docfxwebapprefresh/.
32 changes: 32 additions & 0 deletions azure-pipelines-sample-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@


trigger:
- firebase-demo

pool:
vmImage: 'windows-2019'

steps:
- task: NodeTool@0
inputs:
versionSpec: '18.x'
displayName: 'Install Node.js'

- task: CmdLine@2
inputs:
script: 'npm install -g firebase-tools'
workingDirectory: '$(Agent.ToolsDirectory)'
displayName: 'install firebase tools'

- task: DotNetCoreCLI@2
inputs:
command: 'build'
projects: '**/ElCamino.DocFx.WebAppRefresh.Sample.sln'
arguments: '-c $(BuildConfiguration)'
displayName: 'build web project'

- task: CmdLine@2
inputs:
script: 'firebase deploy --token "$(FIREBASE_TOKEN)" -m "$(Build.BuildNumber)"'
workingDirectory: '$(System.DefaultWorkingDirectory)\\sample\\ElCamino.DocFx.WebAppRefresh.Sample\\'
displayName: 'firebase publish -m "$(Build.BuildNumber)"'
38 changes: 25 additions & 13 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
@@ -1,32 +1,44 @@


trigger:
- firebase-demo
- master

pool:
vmImage: 'windows-2019'
vmImage: 'ubuntu-latest'

steps:
- task: NodeTool@0
inputs:
versionSpec: '10.x'
versionSpec: '18.x'
displayName: 'Install Node.js'

- task: CmdLine@2
- task: UseDotNet@2
displayName: 'Use .Net Core sdk 8.x'
inputs:
script: 'npm install -g firebase-tools'
workingDirectory: '$(Agent.ToolsDirectory)'
displayName: 'install firebase tools'
version: 8.x

- task: UseDotNet@2
displayName: 'Use .Net Core sdk 9.x'
inputs:
version: 9.x

- task: DotNetCoreCLI@2
inputs:
command: 'build'
projects: '**/ElCamino.DocFx.WebAppRefresh.Sample.sln'
projects: '**/ElCamino.DocFx.WebAppRefresh.sln'
arguments: '-c $(BuildConfiguration)'
displayName: 'build web project'
displayName: 'build projects'

- task: CmdLine@2
- task: CopyFiles@1
displayName: 'Copy Files to: $(Build.ArtifactStagingDirectory)'
inputs:
script: 'firebase deploy --token "$(FIREBASE_TOKEN)" -m "$(Build.BuildNumber)"'
workingDirectory: '$(System.DefaultWorkingDirectory)\\sample\\ElCamino.DocFx.WebAppRefresh.Sample\\'
displayName: 'firebase publish -m "$(Build.BuildNumber)"'
Contents: |
**/*.dll
**/*.nupkg
**/*.snupkg
**/*.zip
**/publish/**
TargetFolder: '$(Build.ArtifactStagingDirectory)'

- task: PublishBuildArtifacts@1
displayName: 'Publish Artifact: drop'
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29411.108
# Visual Studio Version 17
VisualStudioVersion = 17.12.35527.113 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ElCamino.DocFx.WebAppRefresh.Sample", "ElCamino.DocFx.WebAppRefresh.Sample.csproj", "{0ED04DD4-9D04-4A45-A623-A042FF5E97BD}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ElCamino.DocFx.WebAppRefresh.Sample", "ElCamino.DocFx.WebAppRefresh.Sample\ElCamino.DocFx.WebAppRefresh.Sample.csproj", "{797EAB1F-C1CB-4228-9F4D-E6CDFE175E46}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0ED04DD4-9D04-4A45-A623-A042FF5E97BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0ED04DD4-9D04-4A45-A623-A042FF5E97BD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0ED04DD4-9D04-4A45-A623-A042FF5E97BD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0ED04DD4-9D04-4A45-A623-A042FF5E97BD}.Release|Any CPU.Build.0 = Release|Any CPU
{797EAB1F-C1CB-4228-9F4D-E6CDFE175E46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{797EAB1F-C1CB-4228-9F4D-E6CDFE175E46}.Debug|Any CPU.Build.0 = Debug|Any CPU
{797EAB1F-C1CB-4228-9F4D-E6CDFE175E46}.Release|Any CPU.ActiveCfg = Release|Any CPU
{797EAB1F-C1CB-4228-9F4D-E6CDFE175E46}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": 1,
"isRoot": true,
"tools": {
"docfx": {
"version": "2.78.2",
"commands": [
"docfx"
],
"rollForward": false
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>net9</TargetFramework>
<Version>2.0</Version>
</PropertyGroup>

<ItemGroup>
<None Include=".config\dotnet-tools.json" />
</ItemGroup>

<ItemGroup>
<None Include="wwwroot\api\ElCamino.DocFx.WebAppRefresh.Sample.html" />
<None Include="wwwroot\api\ElCamino.DocFx.WebAppRefresh.Sample.Program.html" />
Expand Down Expand Up @@ -34,11 +39,15 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="docfx.console" Version="2.46.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="ElCamino.DocFx.WebAppRefresh" Version="1.0.0" />
<PackageReference Include="ElCamino.DocFx.WebAppRefresh" Version="2.0.0" />
</ItemGroup>

<Target Name="RestoreDocFxTool" BeforeTargets="CreateDocFxJson">
<Exec Command="dotnet tool restore" WorkingDirectory="$(ProjectDir)" />
</Target>
<Target Name="CreateDocFxJson" AfterTargets="Build">
<Exec Command="dotnet docfx docfx.json" WorkingDirectory="$(ProjectDir)" />
</Target>


</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseDeveloperExceptionPage();
// only use in development, not for production
app.UseDocFxBuildRefresh(env.ContentRootPath, env.WebRootPath);
app.UseDocFxBuildRefresh(env.ContentRootPath, env.WebRootPath, excludeFoldersUnderWebRoot:[".vs"]);
}
app.UseDefaultFiles();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This is the **HOMEPAGE**.
Refer to [Markdown](http://daringfireball.net/projects/markdown/) for how to write markdown files.
## Quick Start Notes:
1. Add images to the *images* folder if the file is referencing an image.
1. Add new images to the *images* folder if the file is referencing an image.
114 changes: 32 additions & 82 deletions src/ElCamino.DocFx.WebAppRefresh/DocFxBuildExtension.cs
Original file line number Diff line number Diff line change
@@ -1,93 +1,32 @@
// MIT License Copyright (c) David Melendez. All rights reserved. See License in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting;
using System.IO;
using System.Diagnostics;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

#pragma warning disable IDE0130 // Namespace does not match folder structure
namespace Microsoft.Extensions.DependencyInjection
#pragma warning restore IDE0130 // Namespace does not match folder structure
{
public static class DocFxBuildExtension
{
private static bool IsReloading = false;

private static readonly FileSystemWatcher watcher = new FileSystemWatcher();

public static string GetDocFxExecutablePath(string contentRootPath)
{
// Read the projects.assets.json for nuget path and docFx library version
string assetsJsonPath = Path.Combine(contentRootPath, "obj\\project.assets.json");
string nugetPackPath = null;
string docFxLibPath = null;
using (StreamReader sr = new StreamReader(new FileStream(assetsJsonPath, FileMode.Open, FileAccess.Read)))
{
using (JsonReader reader = new JsonTextReader(sr))
{
JsonSerializer serializer = new JsonSerializer();
JObject assetsObj = (JObject)serializer.Deserialize(reader);
//Get the default nuget package path
nugetPackPath = assetsObj?.Properties()
.Where(pp => pp.Name == "packageFolders")
.Descendants()
.Cast<JObject>()
.FirstOrDefault()?
.Properties()
.FirstOrDefault()?
.Name;
if (string.IsNullOrWhiteSpace(nugetPackPath)
|| !Directory.Exists(nugetPackPath))
{
throw new DirectoryNotFoundException("packageFolders node not found in manifest or invalid nuget package path. Unable to locate default nuget package path");
}


//Get DocFx.console package and version path
var libPath = assetsObj?
.Properties()
.Where(pp => pp.Name == "libraries")
.Descendants()
.Cast<JObject>()
.Properties()
.Where(wp => wp.Name.StartsWith("docfx.console"))
.FirstOrDefault()?
.Name ?? string.Empty;
docFxLibPath = Path.Combine(nugetPackPath, libPath);
if (string.IsNullOrWhiteSpace(libPath)
|| !Directory.Exists(docFxLibPath))
{
throw new DirectoryNotFoundException("Unable to locate docfx.console package directory. Make sure docfx.console package in installed with nuget package manager.");
}

string fullDocFxExePath = Path.Combine(docFxLibPath, "tools\\docfx.exe");
if (File.Exists(fullDocFxExePath))
{
return fullDocFxExePath;
}
else
{
throw new FileNotFoundException("Unable to locate default docfx.exe.");
}
}
}

}
private static readonly FileSystemWatcher watcher = new();

public static IApplicationBuilder UseDocFxBuildRefresh(this IApplicationBuilder app, string contentRootPath, string webRootPath, string docFxJsonFileName = "docfx.json")
public static IApplicationBuilder UseDocFxBuildRefresh(this IApplicationBuilder app,
string contentRootPath,
string webRootPath,
string docFxJsonFileName = "docfx.json",
IEnumerable<string> excludeFoldersUnderWebRoot = null)
{

var logger = GetOrCreateLogger(app, nameof(DocFxBuildExtension));

string docExePath = GetDocFxExecutablePath(contentRootPath);

watcher.Path = contentRootPath;

// Watch for changes in LastAccess and LastWrite times, and
Expand All @@ -100,24 +39,35 @@ public static IApplicationBuilder UseDocFxBuildRefresh(this IApplicationBuilder
watcher.IncludeSubdirectories = true;

var OnChanged = new FileSystemEventHandler((o, args) => {
string lowerPath = args.FullPath.ToLower();
if (!IsReloading && !lowerPath.StartsWith(webRootPath.ToLower()))
if (excludeFoldersUnderWebRoot != null)
{
foreach (var excludeFolder in excludeFoldersUnderWebRoot)
{
if (args.FullPath.StartsWith(Path.Combine(webRootPath, excludeFolder), StringComparison.OrdinalIgnoreCase))
{
return;
}
}
}
if (!IsReloading && !args.FullPath.StartsWith(webRootPath, StringComparison.OrdinalIgnoreCase))
{
IsReloading = true;
using (Process process = new Process())
using (Process process = new())
{
process.StartInfo = new ProcessStartInfo(docExePath,
Path.Combine(contentRootPath, docFxJsonFileName));
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo = new("dotnet", $" docfx {docFxJsonFileName}")
{
WorkingDirectory = contentRootPath,
UseShellExecute = false,
RedirectStandardOutput = true
};
process.OutputDataReceived += new DataReceivedEventHandler((sender, e) =>
{
logger.LogInformation(e.Data);
logger.LogInformation(message: e.Data);
});

process.ErrorDataReceived += new DataReceivedEventHandler((sender, e) =>
{
logger.LogError(e.Data);
logger.LogError(message: e.Data);
});
process.Start();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>net8;net9</TargetFrameworks>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>David Melendez</Authors>
Expand All @@ -17,16 +17,21 @@
<Description>DocFx .net core web app build middleware. Rebuilds docFx assets when files are changed in a web app.</Description>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<Version>1.0.1</Version>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Version>2.0</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
<None Include="projectNugetPic.png" Pack="true" PackagePath="\" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="projectNugetPic.png" Pack="true" PackagePath="\" />
</ItemGroup>

</Project>
Loading

0 comments on commit e30679f

Please sign in to comment.