Click here to Skip to main content
12,354,543 members (59,598 online)
Click here to Skip to main content
Articles » Languages » C# » Attributes » Downloads

Stats

100K views
601 downloads
42 bookmarked
Posted

Make NDoc compile the code examples contained in your documentation using NLiterate

, 26 Apr 2004
An utility that merges and recompiles the examples in your documentation using NDoc.
// The MIT License
// Copyright (c) 2004 Jonathan de Halleux
//
// Permission is hereby granted, free of charge, to any person obtaining a 
// copy of this software and associated documentation files (the "Software"), 
// to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the 
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in 
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.

namespace NLiterate
{
	using System;
	using System.CodeDom.Compiler;
	using System.Reflection;
	using Microsoft.CSharp;
	using Microsoft.VisualBasic;
	using System.Diagnostics;
	
	/// <summary>
	/// Helper class for the <see cref="CodeSnippet"/> compilation.
	/// </summary>
	public class CodeSnippetCompiler
	{
		#region Private Fields
		private CSharpCodeProvider csProvider = new CSharpCodeProvider();
		private VBCodeProvider vbProvider = new VBCodeProvider();
		#endregion

		#region Events

		/// <summary>
		/// Raised when compilation starts.
		/// </summary>
		public event CodeSnippetEventHandler BeginCompile;

		/// <summary>
		/// Raises the <see cref="BeginCompile"/> event.
		/// </summary>
		/// <param name="snippet">
		/// <see cref="CodeSnippet"/> instance to attach to the event.
		/// </param>
		protected void OnBeginCompile(CodeSnippet snippet)
		{
			if (this.BeginCompile!=null)
				this.BeginCompile(this,new CodeSnippetEventArgs(snippet));
		}

		/// <summary>
		/// Raised when compilation is finished, with errors.
		/// </summary>
		public event CodeSnippetEventHandler CompileFailure;

		/// <summary>
		/// Raises the <see cref="CompileFailure"/> event.
		/// </summary>
		/// <param name="snippet">
		/// <see cref="CodeSnippet"/> instance to attach to the event.
		/// </param>
		protected void OnCompileFailure(CodeSnippet snippet)
		{
			if (this.CompileFailure!=null)
				this.CompileFailure(this,new CodeSnippetEventArgs(snippet));
		}

		/// <summary>
		/// Raised when compilation is finished, without errors.
		/// </summary>
		public event CodeSnippetEventHandler CompileSuccess;

		/// <summary>
		/// Raises the <see cref="CompileSuccess"/> event.
		/// </summary>
		/// <param name="snippet">
		/// <see cref="CodeSnippet"/> instance to attach to the event.
		/// </param>
		protected void OnCompileSuccess(CodeSnippet snippet)
		{
			if (this.CompileSuccess!=null)
				this.CompileSuccess(this,new CodeSnippetEventArgs(snippet));
		}

		/// <summary>
		/// Raised when compilation starts.
		/// </summary>
		public event CodeSnippetEventHandler BeginRun;

		/// <summary>
		/// Raises the <see cref="BeginRun"/> event.
		/// </summary>
		/// <param name="snippet">
		/// <see cref="CodeSnippet"/> instance to attach to the event.
		/// </param>
		protected void OnBeginRun(CodeSnippet snippet)
		{
			if (this.BeginRun!=null)
				this.BeginRun(this,new CodeSnippetEventArgs(snippet));
		}
		#endregion

		/// <summary>
		/// Compiles and runs the snippet.
		/// </summary>
		/// <param name="snippet">
		/// <see cref="CodeSnippet"/> instance to process.
		/// </param>
		/// <exception cref="ArgumentNullException">
		/// <paramref name="snippet"/> is a null reference (Nothing in Visual Basic)
		/// </exception>
		public void Process(CodeSnippet snippet)
		{
			if (snippet==null)
				throw new ArgumentNullException("snippet");

			this.compile(snippet);
			if (snippet.Results.Errors.HasErrors)
			{
				this.OnCompileFailure(snippet);
				return;
			}
			this.OnCompileSuccess(snippet);

			if (snippet.EntryPoint)
			{
				this.OnBeginRun(snippet);
				this.run(snippet);
			}
		}

		private void compile(CodeSnippet snippet)
		{
			OnBeginCompile(snippet);
			// getting compiler
			ICodeCompiler cc = null;
			switch(snippet.Language)
			{
				case CodeLanguage.CS: cc = csProvider.CreateCompiler();break;
				case CodeLanguage.VB: cc = vbProvider.CreateCompiler();break;
				default:
					throw new ArgumentException("Unknown language");
			}

			// loading additional assemblies
			CompilerParameters options = new CompilerParameters();
			// in memory generation
			options.GenerateInMemory = true;
			options.GenerateExecutable = false;
			// adding assemblies
			foreach(string a in snippet.References)
				options.ReferencedAssemblies.Add(a);

			// compile
			snippet.Results = cc.CompileAssemblyFromSource(options,snippet.GetSource());
		}

		private void run(CodeSnippet snippet)
		{
			Debug.Assert(snippet!=null);
			Debug.Assert(snippet.Results!=null);
			Debug.Assert(!snippet.Results.Errors.HasErrors);
			Debug.Assert(snippet.EntryPoint);

			//loop through each class that was defined and look for the first occurrance of the entry point method
			foreach (Type type in snippet.Results.CompiledAssembly.GetExportedTypes())
			{
				MethodInfo mi = type.GetMethod("Main", BindingFlags.Public | BindingFlags.Static);
				if (mi != null)
				{
					snippet.ExecutionResults = ExecutionResults.Execute(mi);
					return;
				}
			}
			snippet.ExecutionResults = ExecutionResults.NoEntryPoint();
		}
		
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Jonathan de Halleux
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 27 Apr 2004
Article Copyright 2004 by Jonathan de Halleux
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid