Skip to content
This repository has been archived by the owner on Oct 16, 2020. It is now read-only.

Commit

Permalink
Refactor T4 templating host.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrward committed Sep 29, 2011
1 parent 2658bc5 commit 2b2e716
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ protected TextTemplatingHost CreateTextTemplatingHost(IProject project)
var assemblyResolver = new TextTemplatingAssemblyResolver(project);
var textTemplatingVariables = new TextTemplatingVariables();
var serviceProvider = new TextTemplatingServiceProvider();
var host = new TextTemplatingHost(appDomainFactory, assemblyResolver, textTemplatingVariables, serviceProvider, applicationBase);
return host;
var context = new TextTemplatingHostContext(project);
return new TextTemplatingHost(context, applicationBase);
}

string GetAssemblyBaseLocation()
Expand Down
25 changes: 7 additions & 18 deletions src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,13 @@ namespace ICSharpCode.TextTemplating
{
public class TextTemplatingHost : TemplateGenerator, ITextTemplatingHost, IServiceProvider
{
ITextTemplatingAppDomainFactory appDomainFactory;
ITextTemplatingAppDomain templatingAppDomain;
ITextTemplatingAssemblyResolver assemblyResolver;
ITextTemplatingVariables templatingVariables;
IServiceProvider serviceProvider;
TextTemplatingHostContext context;
string applicationBase;

public TextTemplatingHost(
ITextTemplatingAppDomainFactory appDomainFactory,
ITextTemplatingAssemblyResolver assemblyResolver,
ITextTemplatingVariables templatingVariables,
IServiceProvider serviceProvider,
string applicationBase)
public TextTemplatingHost(TextTemplatingHostContext context, string applicationBase)
{
this.appDomainFactory = appDomainFactory;
this.assemblyResolver = assemblyResolver;
this.templatingVariables = templatingVariables;
this.serviceProvider = serviceProvider;
this.context = context;
this.applicationBase = applicationBase;
}

Expand All @@ -47,12 +36,12 @@ public override AppDomain ProvideTemplatingAppDomain(string content)

void CreateAppDomain()
{
templatingAppDomain = appDomainFactory.CreateTextTemplatingAppDomain(applicationBase);
templatingAppDomain = context.CreateTextTemplatingAppDomain(applicationBase);
}

protected override string ResolveAssemblyReference(string assemblyReference)
{
return assemblyResolver.Resolve(assemblyReference);
return context.ResolveAssemblyReference(assemblyReference);
}

protected override string ResolvePath(string path)
Expand All @@ -63,12 +52,12 @@ protected override string ResolvePath(string path)

string ExpandPath(string path)
{
return templatingVariables.ExpandVariables(path);
return context.ExpandTemplateVariables(path);
}

object IServiceProvider.GetService(Type serviceType)
{
return serviceProvider.GetService(serviceType);
return context.GetService(serviceType);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)

using System;
using ICSharpCode.SharpDevelop.Project;

namespace ICSharpCode.TextTemplating
{
public class TextTemplatingHostContext
{
ITextTemplatingAppDomainFactory appDomainFactory;
ITextTemplatingAssemblyResolver assemblyResolver;
ITextTemplatingVariables templatingVariables;
IServiceProvider serviceProvider;

public TextTemplatingHostContext(IProject project)
: this(
new TextTemplatingAppDomainFactory(),
new TextTemplatingAssemblyResolver(project),
new TextTemplatingVariables(),
new TextTemplatingServiceProvider())
{
}

public TextTemplatingHostContext(
ITextTemplatingAppDomainFactory appDomainFactory,
ITextTemplatingAssemblyResolver assemblyResolver,
ITextTemplatingVariables templatingVariables,
IServiceProvider serviceProvider)
{
this.appDomainFactory = appDomainFactory;
this.assemblyResolver = assemblyResolver;
this.templatingVariables = templatingVariables;
this.serviceProvider = serviceProvider;
}

public object GetService(Type serviceType)
{
return serviceProvider.GetService(serviceType);
}

public string ExpandTemplateVariables(string name)
{
return templatingVariables.ExpandVariables(name);
}

public ITextTemplatingAppDomain CreateTextTemplatingAppDomain(string applicationBase)
{
return appDomainFactory.CreateTextTemplatingAppDomain(applicationBase);
}

public string ResolveAssemblyReference(string assemblyReference)
{
return assemblyResolver.Resolve(assemblyReference);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<Compile Include="Src\TextTemplatingFilePreprocessorCustomTool.cs" />
<Compile Include="Src\TextTemplatingFileProcessor.cs" />
<Compile Include="Src\TextTemplatingHost.cs" />
<Compile Include="Src\TextTemplatingHostContext.cs" />
<Compile Include="Src\TextTemplatingPathResolver.cs" />
<Compile Include="Src\TextTemplatingReflectionProjectContent.cs" />
<Compile Include="Src\TextTemplatingServiceProvider.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class TestableTextTemplatingHost : TextTemplatingHost
public FakeTextTemplatingAssemblyResolver FakeTextTemplatingAssemblyResolver;
public FakeTextTemplatingVariables FakeTextTemplatingVariables;
public FakeServiceProvider FakeServiceProvider;
public TextTemplatingHostContext HostContext;

public TestableTextTemplatingHost(string applicationBase)
: this(
Expand All @@ -29,13 +30,20 @@ public TestableTextTemplatingHost(
FakeTextTemplatingVariables textTemplatingVariables,
FakeServiceProvider fakeServiceProvider,
string applicationBase)
: base(appDomainFactory, assemblyResolver, textTemplatingVariables, fakeServiceProvider, applicationBase)
: this(
new TextTemplatingHostContext(appDomainFactory, assemblyResolver, textTemplatingVariables, fakeServiceProvider),
applicationBase)
{
FakeTextTemplatingAppDomainFactory = appDomainFactory;
FakeTextTemplatingAssemblyResolver = assemblyResolver;
FakeTextTemplatingVariables = textTemplatingVariables;
FakeServiceProvider = fakeServiceProvider;
}

public TestableTextTemplatingHost(TextTemplatingHostContext context, string applicationBase)
: base(context, applicationBase)
{
}

public string CallResolveAssemblyReference(string assemblyReference)
{
Expand Down

0 comments on commit 2b2e716

Please sign in to comment.