Skip to content

Commit 6219522

Browse files
russcamMpdreamz
authored andcommitted
Generate asciidoc generation as part of build (#2486)
(cherry-picked from commit 46a5389)
1 parent e87a4ef commit 6219522

File tree

9 files changed

+151
-40
lines changed

9 files changed

+151
-40
lines changed

build/scripts/Documentation.fsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,30 @@ open System
88
open Fake
99

1010
open Paths
11+
open System.IO
1112

1213
module Documentation =
1314

14-
let RunLitterateur = fun _ ->
15-
let litterateur = "src/CodeGeneration/Nest.Litterateur/bin/Release/Nest.Litterateur.exe"
15+
let Generate() =
16+
let generator = "build/output/v4.6/DocGenerator/DocGenerator.exe"
1617
ExecProcess (fun p ->
17-
p.WorkingDirectory <- "src/CodeGeneration/Nest.Litterateur/bin/Release"
18-
p.FileName <- litterateur
18+
p.WorkingDirectory <- "src/CodeGeneration/DocGenerator"
19+
p.FileName <- generator
1920
)
2021
(TimeSpan.FromMinutes (1.0)) |> ignore
2122

23+
// TODO: hook documentation validation into the process
24+
let Validate() =
25+
let elasticDocsDir = "../elasticsearch-docs"
26+
if (directoryExists elasticDocsDir = false) then
27+
let fullPath = combinePaths currentDirectory elasticDocsDir |> Path.GetFullPath
28+
traceFAKE "No elasticsearch docs repo found at %s. Cannot validate generated documentation" fullPath
29+
//else
30+
// Needs to be able to run the build_docs.pl perl script. The best options on Windows for this
31+
// are Cygwin or Linux Bash for Windows.
32+
//let docBuildScript = combinePaths elasticDocsDir "build_docs.pl"
2233

2334

35+
|> ignore
36+
2437

build/scripts/InheritDoc.fsx

Lines changed: 0 additions & 28 deletions
This file was deleted.

build/scripts/Paths.fsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ module Projects =
2929

3030
type PrivateProject =
3131
| Tests
32-
32+
| DocGenerator
33+
3334
type DotNetProject =
3435
| Project of Project
3536
| PrivateProject of PrivateProject
@@ -53,12 +54,19 @@ module Projects =
5354
| PrivateProject p ->
5455
match p with
5556
| Tests -> "Tests"
57+
| DocGenerator -> "DocGenerator"
5658

5759
static member TryFindName (name: string) =
5860
DotNetProject.All
5961
|> Seq.map(fun p -> p.Name)
6062
|> Seq.tryFind(fun p -> p.ToLowerInvariant() = name.ToLowerInvariant())
6163

64+
type DotNetFrameworkProject = { framework: DotNetFramework; project: DotNetProject }
65+
let AllPublishableProjectsWithSupportedFrameworks = seq {
66+
for framework in DotNetFramework.All do
67+
for project in DotNetProject.AllPublishable do
68+
yield { framework = framework; project= project}
69+
}
6270

6371
module Paths =
6472
open Projects
@@ -71,7 +79,7 @@ module Paths =
7179

7280
let ProjectOutputFolder (project:DotNetProject) (framework:DotNetFramework) =
7381
sprintf "%s/%s/%s" BuildOutput framework.Identifier.MSBuild project.Name
74-
82+
7583
let Tool tool = sprintf "packages/build/%s" tool
7684
let CheckedInToolsFolder = "build/Tools"
7785
let KeysFolder = sprintf "%s/keys" BuildFolder

build/scripts/Targets.fsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#load @"Testing.fsx"
44
#load @"Signing.fsx"
55
#load @"Building.fsx"
6+
#load @"XmlDocPatcher.fsx"
67
#load @"Documentation.fsx"
78
#load @"Releasing.fsx"
89
#load @"Profiling.fsx"
@@ -17,6 +18,8 @@ open Signing
1718
open Versioning
1819
open Releasing
1920
open Profiling
21+
open Documentation
22+
open XmlDocPatcher
2023

2124
// Default target
2225
Target "Build" <| fun _ -> traceHeader "STARTING BUILD"
@@ -26,13 +29,19 @@ Target "Clean" <| fun _ -> Build.Clean()
2629
Target "BuildApp" <| fun _ -> Build.Compile()
2730

2831
Target "Test" <| fun _ -> Tests.RunUnitTests()
29-
32+
33+
Target "InheritDoc" <| fun _ -> InheritDoc.patchInheritDocs()
34+
35+
Target "TestForever" <| fun _ -> Tests.RunUnitTestsForever()
36+
3037
Target "Integrate" <| fun _ -> Tests.RunIntegrationTests (getBuildParamOrDefault "esversions" "") (getBuildParamOrDefault "escluster" "") (getBuildParamOrDefault "testfilter" "")
3138

3239
Target "Profile" <| fun _ -> Profiler.Run()
3340

3441
Target "Benchmark" <| fun _ -> Benchmarker.Run()
3542

43+
Target "Documentation" <| fun _ -> Documentation.Generate()
44+
3645
Target "Version" <| fun _ ->
3746
Versioning.PatchAssemblyInfos()
3847
Versioning.PatchProjectJsons()
@@ -53,8 +62,14 @@ Target "Canary" <| fun _ ->
5362
=?> ("Version", hasBuildParam "version")
5463
==> "BuildApp"
5564
=?> ("Test", (not ((getBuildParam "skiptests") = "1")))
65+
==> "InheritDoc"
66+
==> "Documentation"
5667
==> "Build"
5768

69+
"Clean"
70+
==> "BuildApp"
71+
==> "TestForever"
72+
5873
"Clean"
5974
==> "BuildApp"
6075
==> "Profile"

build/scripts/Testing.fsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ module Tests =
2020
let testDll = Path.Combine(folder, "Tests.dll")
2121
Tooling.XUnit.Exec [testDll; "-parallel"; parallelization; "-xml"; Paths.Output("TestResults-Desktop-Clr.xml")]
2222
|> ignore
23-
2423

24+
let RunUnitTestsForever() =
25+
while true do testDesktopClr "all"
26+
2527
let RunUnitTests() =
2628
testDesktopClr "all"
2729
testProjectJson "all"

build/scripts/XmlDocPatcher.fsx

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#I @"../../packages/build/FAKE/tools"
2+
#r @"FakeLib.dll"
3+
#r "System.Xml.Linq.dll"
4+
5+
#load @"Paths.fsx"
6+
7+
namespace XmlDocPatcher
8+
9+
open System.Linq
10+
open System.Text.RegularExpressions
11+
open System.Xml
12+
open System.Xml.Linq
13+
open System.Xml.XPath
14+
15+
open Paths
16+
open Projects
17+
open Fake
18+
19+
module InheritDoc =
20+
21+
let private apiName n = Regex.Replace(n, @"^\w\:(.+?)(?:\(.+$|$)", "$1")
22+
let private relatedInterface n = Regex.Replace(n, @"\.([^.]+\.[^.]+)$", ".I$1")
23+
let private relatedInterfaceAsync n = (relatedInterface n).Replace("Async", "")
24+
let private relatedInterfaceDescriptor n = Regex.Replace(n, @"\.([^.]+)Descriptor\.([^.]+)$", ".I$1.$2")
25+
let private relatedInterfaceDescriptorRequest n = Regex.Replace(n, @"\.([^.]+)Descriptor\.([^.]+)$", ".I$1Request.$2")
26+
let private relatedInterfaceDescriptorGeneric n = Regex.Replace(n, @"\.([^.]+)Descriptor[^.]+.([^.]+)$", ".I$1.$2")
27+
let private manualMapping (n : string) =
28+
//this sucks but untill roslyn gets coderewriting API's this is the best we got without making it
29+
//a bigger thing than it already is
30+
match n with
31+
| n when n.Contains("PutMapping") -> n.Replace("PutMapping", "TypeMapping")
32+
| _ -> n
33+
let private relatedApiLookups = [
34+
relatedInterface;
35+
relatedInterfaceAsync;
36+
relatedInterfaceDescriptor;
37+
relatedInterfaceDescriptorRequest;
38+
relatedInterfaceDescriptorGeneric;
39+
manualMapping
40+
];
41+
42+
let private documentedApis = fun (file : string) ->
43+
use reader = XmlReader.Create file
44+
seq {
45+
while reader.ReadToFollowing("member") do
46+
let name = apiName(reader.GetAttribute("name"))
47+
let innerXml = reader.ReadInnerXml().Trim();
48+
if (isNotNullOrEmpty innerXml && not (innerXml.Contains("<inheritdoc"))) then
49+
let xdoc = XDocument.Parse("<x>" + innerXml + "</x>")
50+
yield (name, xdoc)
51+
} |> Map.ofSeq
52+
53+
let private patchInheritDoc = fun file ->
54+
traceFAKE "Rewriting xmldoc: %s" file
55+
56+
let mapOfDocumentedApis = documentedApis file
57+
58+
let findDocumentation (apiElement:string) =
59+
relatedApiLookups
60+
|> Seq.map (fun f -> f apiElement)
61+
|> (Seq.map <| mapOfDocumentedApis.TryFind)
62+
|> Seq.choose id
63+
|> Seq.tryHead
64+
65+
let xml = XDocument.Load file
66+
67+
xml.XPathSelectElements("//inheritdoc")
68+
|> Seq.iter (fun inheritDocElement ->
69+
let parent =inheritDocElement.Parent
70+
let name = apiName (parent.Attribute(XName.Get "name").Value)
71+
let documentation = findDocumentation name
72+
73+
match documentation with
74+
| Some d ->
75+
let elements = d.Element(XName.Get("x")).Elements().ToList();
76+
inheritDocElement.AddBeforeSelf(elements)
77+
| _ ->
78+
//unignore the following to find undocumented/badly inherited methods
79+
//tracefn "not inherited: %s" apiElement
80+
ignore()
81+
)
82+
83+
use writer = new XmlTextWriter(file,null)
84+
writer.Formatting <- Formatting.Indented;
85+
xml.Save(writer);
86+
87+
let patchInheritDocs = fun () ->
88+
AllPublishableProjectsWithSupportedFrameworks
89+
|> Seq.map (fun p ->
90+
let folder = Paths.ProjectOutputFolder p.project p.framework
91+
folder @@ p.project.Name + ".xml"
92+
)
93+
|> Seq.filter fileExists
94+
|> Seq.iter patchInheritDoc
95+

src/CodeGeneration/DocGenerator/DocGenerator.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<DebugSymbols>true</DebugSymbols>
2121
<DebugType>full</DebugType>
2222
<Optimize>false</Optimize>
23-
<OutputPath>..\Nest.Litterateur\bin\Net45\Debug\</OutputPath>
23+
<OutputPath>bin\Debug\</OutputPath>
2424
<DefineConstants>DEBUG;TRACE</DefineConstants>
2525
<ErrorReport>prompt</ErrorReport>
2626
<WarningLevel>4</WarningLevel>
@@ -29,7 +29,7 @@
2929
<PlatformTarget>AnyCPU</PlatformTarget>
3030
<DebugType>pdbonly</DebugType>
3131
<Optimize>true</Optimize>
32-
<OutputPath>..\Nest.Litterateur\bin\Net45\Release\</OutputPath>
32+
<OutputPath>bin\Release\</OutputPath>
3333
<DefineConstants>TRACE</DefineConstants>
3434
<ErrorReport>prompt</ErrorReport>
3535
<WarningLevel>4</WarningLevel>

src/CodeGeneration/DocGenerator/LitUp.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,15 @@ public static void Go(string[] args)
3939
file.SaveToDocumentationFolder();
4040
}
4141

42+
Console.ForegroundColor = ConsoleColor.Green;
43+
Console.WriteLine("Documentation generated.");
44+
Console.ResetColor();
45+
4246
if (Debugger.IsAttached)
47+
{
4348
Console.WriteLine("Press any key to continue...");
4449
Console.ReadKey();
50+
}
4551
}
4652
}
4753
}

src/CodeGeneration/DocGenerator/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ static Program()
1414
}
1515
else
1616
{
17-
InputDirPath = @"..\..\..\..\..\..\src\Tests";
18-
OutputDirPath = @"..\..\..\..\..\..\docs";
17+
InputDirPath = @"..\..\..\..\..\src\Tests";
18+
OutputDirPath = @"..\..\..\..\..\docs";
1919
}
2020
}
2121

0 commit comments

Comments
 (0)