This week I will be one of the speakers at BASTA On Tour in Munich. One of the topics I am going to speak about is code documentation with Sandcastle. In this blog post I want to share my slides and summarize the hands-on labs that I am going to go through with the participants.
Although it is likely that you will use one of the sandcastle UI tools that the community provides it is worthful to have an understanding about what happens behind the scenes. Therefore you will use the raw sandcastle command-line api to generate a CHM file for a sime C# sour file in this first hands-on lab.
switch to the folder that you have copied the sandcastle examples to:
cd c:\temp\Sandcastle Examples
Take a look at test.cs. This is the sample C# file that we are going to generate a documentation for.
Compile test.cs and generate XML documentation for the file:
csc /t:library /doc:comments.xml
The a look at comments.xml. The C# compiler has generated this file from the C# source file test.cs because you specified the option /doc when calling csc.
Because of the complexity of the Sandcastle help generation process Sandcastle comes with a sample script that includes all necessary steps to generate the CHM file. The sample script is called build_Sandcastle.bat. Open the file in a text editor and note how MRefBuilder, BuildAssembler and the XSL transformations are used.
Build the documentation for test.cs using the sample script mentioned in the previous step:
build_Sandcastle.bat vs2005 test chm
Open the following files in a text editor and take a look at them. They have been generated by the Sandcastle process.
reflection.xml
manifest.xml
toc.xml
Note how Sandcastle generated the necessary source file for CHM generation:
chm\test.hhc
chm\test.hhk
chm\test.hhp
Content in chm\html, chm\icons, chm\scripts and chm\styles
Open the resulting CHM file chm\test.chm.
Hands-On Lab 2: Sandcastle Helpfile Builder
Prerequisites:
Download and install the July 2010 release of Sandcastle Help File Builder (SHFB) from http://shfb.codeplex.com/
Execute Hands-on lab 1 (see above) so that you have a compiled version of the file test.cs.
Lab step by step description:
Launch Sandcastle Help File Builder.
Create a new project called test.shfbproj.
Add test.dll and comments.xml (both results from lab 1) as a documentation source using the SHFB's project explorer.
Build project using Documentation / Build Project.
Launch generated CHM file using Documentation / View Help File / View HTML Help 1 (.chm) File
Experiment with different project settings; try to generate and launch MSHelpViewer help file format using the project's HelpFileFormat property.
Create a new C# Class Library project called CSharpCodeDoc
Add the following class to the class library:
Make sure that the class library builds correctly.
Right-click on the project in Solution Explorer and open choose StyleCop Settings.
Disable all Documentation Rules.
Run StyleCop on the project using Tools / Run StyleCop and make sure that there are no errors or warning.
Enable all Documentation Rules / Element Documentation rules (do not enable Documentation Rules / File Headers).
Run StyleCop on the project using Tools / Run StyleCop. Note that StyleCop shows warnings because of missing XML code documentation.
Tip: You could integrate this step in your corporate's build process and even treat StyleCop warnings as errors. With this you can make sure that developers do not forget to write code documentation XML.
Add documentation header to class:
Run StyleCop on the project using Tools / Run StyleCop. Note that StyleCop tells you that you have forgotten the period at the end of the summary text.
Correct that error by adding the period
Set the cursor in the name of the property HasReachedLimit.
Auto-generate documentation for the property using GhostDoc by pressing Ctrl + Shift + D or by selecting Tools / GhostDoc / Documentthis.
Note how GhostDoc derives documentation from the property's name and type.
Set the cursor in the name of the method ReadToEnd.
Auto-generate documentation for the method using GhostDoc by pressing Ctrl + Shift + D or by selecting Tools / GhostDoc / Document this.
Note how GhostDoc reads documentation from the base class.
Add and correct the code documentation so that the class file looks like this:
Enable the generation of a code documentation XML file in the project's property window (Build tab) in Visual Studio.
Build your solution using the Debug configuration.
Create a Sandcastle Help File Builder project named CSharpCodeDoc.
Add CSharpCodeDoc.dll and CSharpCodeDoc.XML located in the project's bin/Debug folder to the SHFB project using SHFB's project explorer.
Build your SHFB project and view the generated CHM file.
Hands-On Lab 4: Advanced Features For C# Code Documentation
Prerequisites:
Execute Hands-on lab 3 (see above).
Lab step by step description:
Add file UsageSamples.cswith C# example code to your project:
Change Build Action of UsageSamples.cs from Compile to None.
Change documentation of class SwiftFileReader<T>as follows:
Note how the previously created example code is referenced.
Exclude the property Size from documentation by adding an exclude element:
Add a paragraph to the return section of the documentation for the method ConvertFileContent:
Note how the see langword element is used.
Add a method Dec with two overloads and add documentation to the overloads section in the documentation:
Add file NamespaceDoc.cs with documentation for your namespace to your project:
Build your project and make sure that there are no errors.
Run StyleCop on your project and make sure that there are not errors or warnings.
Build your SHFB project and view the generated CHM file. Especially note how the changes you have done to your documentation during this lab changed the resulting compiled help file.
Open code snippets file in SHFB with a double click; note how the code snippets are defined (we will not change the generated sample code here).
Add conceptual document using SHFB's Content Layout Editor.
Right-click Overview.aml in content layout editor, select Add Sibling Topic / Standard Templates / Conceptual
Add Details.aml
If not already open, open Details.aml in SHFB with a double click
Option: You can open it you favorite XML editor instead
Define the content of the file as follows (don't forget to replace the GUID in the topic id with your guid):
Preview help content in SHFB by pressing F5.
Download the following image into your SHFB project directory:
Add bitmap file using SHFB's Project Explorer
Right-click project name, select Add / New Item / Bitmap
Add image file you downloaded in the previous step
Click on the image in SHFB's Project Explorer and change BuildAction to Image in bitmap properties.
Change ImageId to TitleImage in bitmap properties.
Add image to Introduction section in Overview.aml:
Preview help content in SHFB by pressing F5.
Build your SHFB project and view the generated CHM file. Especially note how the changes you have done to your documentation during this lab changed the resulting compiled help file.
Yesterday, I did a session about Dockerizing .NET applications at TechDays Sweden. In this blog post, I share a recording of my talk and the slides that I used.
September is BASTA! time. It has become a beloved tradition for me to travel in Autumn to Mainz to speak at the BASTA! conference. This year, my workshop, session and keynote topics were .NET, microservices and web dev technology. In this blog post I share my session material and recordings.
Time Cockpit Newsletter
Thanks for your registration! You are almost finished. We need to confirm your email address. To complete the subscription process, please click the link in the email we just sent you.