ServiceStack/ServiceStack — pull request #904 by Tyst (2014-04-17 09:17:27+00:00)
Title: Razor MSBuild Task

## Usage

1. Install NuGet Package / Add ServiceStack.Razor.BuildTask.targets import to proj

2. Add the RazorFormat plugin, configuring which assemblies to look for Razor View Files

*AppHost.cs*

Plugins.Add(new RazorFormat { ScanAssemblies = new[] { typeof(Rockstar).Assembly } });

3. (Optional) Configure which build configuration(s) the Razor Build Task should build for
- Leaving this out causes the Razor Build task to build for every configuration

*Host Project's Web/App.Config:*

*Note: Having task enabled for DEBUG can be useful, as you get build-time compilation errors for Razor Views*

4. Set Build Action = Content for all the Razor View Files

**Done:** Razor Views are now loaded from the Assembly(ies)

![razorbuildtask](https://cloud.githubusercontent.com/assets/1469708/2729915/ed68c320-c610-11e3-9962-b16ae6090586.png)

## How it Works

- SS.Razor modified to prefer loading from ScanAssemblies over using its IVirtualPathProvider
- Compiled Razor View classes are located by searching for the attribute: [GeneratedCode].Tool == "RazorGenerator"
- Razor View route paths are inferred from [VirtualPath].VirtualPath like normal

- Razor Build Task generates the source-code in obj\$(Configuration)
- Output of Razor Build Task is added to Host Project's "Compile" items
- Generated source is built with Host Project, so any dependencies it requires are there at build time
- To prevent, filename and namespace collisions, filename\namespaces are now automatically inferred from the $(RootNamespace) and the VirtualPath (see src\ServiceStack.Razor\RazorViewManager GetNamespace())
- Uses the Host Project's Web/App.Config (see src\ServiceStack.Razor.BuildTask\AppConfigScope.cs)
- Host Project's section is made available to Razor Build Task
- Namespaces, PageBaseType, etc
- First tries to resolve web.config, then app.config, then .config

## Dev Notes
- Note-worthy files:
- src\ServiceStack.Razor.BuildTask\RazorGeneratorBuildTask.cs
- tests\ServiceStack.Razor.BuildTask.Tests\RazorBuildTaskTests.cs
- tests\ServiceStack.Razor.BuildTask.IntegrationTests\RazorBuildTaskIntegrationTests.cs

- Minimal dependencies (SS.interfaces, SS.Text, System.Web.Razor)
- A lot of the project files are links similar to the Razorv4 project
- Some of the Razor compilation currently depends on things like HostContext
- Some work towards separting the compliation from the rest of SS projects has been made
- Would result in no dependencies for Build Task
- Currently, the few places that have dependencies outside the above 3 DLLs have been added as "polyfills" in ServiceStack.Razor.BuildTask\Support to keep the project's number of dependencies low
- Sometimes this was advantageous, as slightly different functionality of these parts made the MSBuildTask easier to work with