Skip to content
Browse files

T4 text templating host now implements IServiceProvider.

  • Loading branch information...
1 parent 1cc53f1 commit 2658bc5edd523cf4f19bb3a0c0a25edbbcd06f29 @mrward mrward committed Sep 29, 2011
View
3 src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingCustomTool.cs
@@ -17,7 +17,8 @@ protected TextTemplatingHost CreateTextTemplatingHost(IProject project)
string applicationBase = GetAssemblyBaseLocation();
var assemblyResolver = new TextTemplatingAssemblyResolver(project);
var textTemplatingVariables = new TextTemplatingVariables();
- var host = new TextTemplatingHost(appDomainFactory, assemblyResolver, textTemplatingVariables, applicationBase);
+ var serviceProvider = new TextTemplatingServiceProvider();
+ var host = new TextTemplatingHost(appDomainFactory, assemblyResolver, textTemplatingVariables, serviceProvider, applicationBase);
return host;
}
View
10 src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingHost.cs
@@ -6,23 +6,26 @@
namespace ICSharpCode.TextTemplating
{
- public class TextTemplatingHost : TemplateGenerator, ITextTemplatingHost
+ public class TextTemplatingHost : TemplateGenerator, ITextTemplatingHost, IServiceProvider
{
ITextTemplatingAppDomainFactory appDomainFactory;
ITextTemplatingAppDomain templatingAppDomain;
ITextTemplatingAssemblyResolver assemblyResolver;
ITextTemplatingVariables templatingVariables;
+ IServiceProvider serviceProvider;
string applicationBase;
public TextTemplatingHost(
ITextTemplatingAppDomainFactory appDomainFactory,
ITextTemplatingAssemblyResolver assemblyResolver,
ITextTemplatingVariables templatingVariables,
+ IServiceProvider serviceProvider,
string applicationBase)
{
this.appDomainFactory = appDomainFactory;
this.assemblyResolver = assemblyResolver;
this.templatingVariables = templatingVariables;
+ this.serviceProvider = serviceProvider;
this.applicationBase = applicationBase;
}
@@ -62,5 +65,10 @@ string ExpandPath(string path)
{
return templatingVariables.ExpandVariables(path);
}
+
+ object IServiceProvider.GetService(Type serviceType)
+ {
+ return serviceProvider.GetService(serviceType);
+ }
}
}
View
15 src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingServiceProvider.cs
@@ -0,0 +1,15 @@
+// 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;
+
+namespace ICSharpCode.TextTemplating
+{
+ public class TextTemplatingServiceProvider : IServiceProvider
+ {
+ public object GetService(Type serviceType)
+ {
+ return serviceType.Assembly.CreateInstance(serviceType.FullName);
+ }
+ }
+}
View
1 src/AddIns/Misc/TextTemplating/Project/TextTemplating.csproj
@@ -83,6 +83,7 @@
<Compile Include="Src\TextTemplatingHost.cs" />
<Compile Include="Src\TextTemplatingPathResolver.cs" />
<Compile Include="Src\TextTemplatingReflectionProjectContent.cs" />
+ <Compile Include="Src\TextTemplatingServiceProvider.cs" />
<Compile Include="Src\TextTemplatingStringParser.cs" />
<Compile Include="Src\TextTemplatingVariableLocation.cs" />
<Compile Include="Src\TextTemplatingVariables.cs" />
View
25 src/AddIns/Misc/TextTemplating/Test/Helpers/FakeServiceProvider.cs
@@ -0,0 +1,25 @@
+// 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 System.Collections.Generic;
+
+namespace TextTemplating.Tests.Helpers
+{
+ public class FakeServiceProvider : IServiceProvider
+ {
+ public Dictionary<Type, object> Services = new Dictionary<Type, object>();
+
+ public void AddService(Type serviceType, object service)
+ {
+ Services.Add(serviceType, service);
+ }
+
+ public object GetService(Type serviceType)
+ {
+ object service = null;
+ Services.TryGetValue(serviceType, out service);
+ return service;
+ }
+ }
+}
View
6 src/AddIns/Misc/TextTemplating/Test/Helpers/TestableTextTemplatingHost.cs
@@ -11,12 +11,14 @@ public class TestableTextTemplatingHost : TextTemplatingHost
public FakeTextTemplatingAppDomainFactory FakeTextTemplatingAppDomainFactory;
public FakeTextTemplatingAssemblyResolver FakeTextTemplatingAssemblyResolver;
public FakeTextTemplatingVariables FakeTextTemplatingVariables;
+ public FakeServiceProvider FakeServiceProvider;
public TestableTextTemplatingHost(string applicationBase)
: this(
new FakeTextTemplatingAppDomainFactory(),
new FakeTextTemplatingAssemblyResolver(),
new FakeTextTemplatingVariables(),
+ new FakeServiceProvider(),
applicationBase)
{
}
@@ -25,12 +27,14 @@ public TestableTextTemplatingHost(string applicationBase)
FakeTextTemplatingAppDomainFactory appDomainFactory,
FakeTextTemplatingAssemblyResolver assemblyResolver,
FakeTextTemplatingVariables textTemplatingVariables,
+ FakeServiceProvider fakeServiceProvider,
string applicationBase)
- : base(appDomainFactory, assemblyResolver, textTemplatingVariables, applicationBase)
+ : base(appDomainFactory, assemblyResolver, textTemplatingVariables, fakeServiceProvider, applicationBase)
{
FakeTextTemplatingAppDomainFactory = appDomainFactory;
FakeTextTemplatingAssemblyResolver = assemblyResolver;
FakeTextTemplatingVariables = textTemplatingVariables;
+ FakeServiceProvider = fakeServiceProvider;
}
public string CallResolveAssemblyReference(string assemblyReference)
View
17 src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingHostTests.cs
@@ -17,6 +17,7 @@ public class TextTemplatingHostTests
FakeTextTemplatingAppDomain fakeTextTemplatingAppDomain;
FakeTextTemplatingAssemblyResolver fakeAssemblyResolver;
FakeTextTemplatingVariables fakeTextTemplatingVariables;
+ FakeServiceProvider fakeServiceProvider;
void CreateHost()
{
@@ -30,6 +31,7 @@ void CreateHost(string applicationBase)
fakeTextTemplatingAppDomain = fakeTextTemplatingAppDomainFactory.FakeTextTemplatingAppDomain;
fakeAssemblyResolver = host.FakeTextTemplatingAssemblyResolver;
fakeTextTemplatingVariables = host.FakeTextTemplatingVariables;
+ fakeServiceProvider = host.FakeServiceProvider;
}
void AddTemplateVariableValue(string variableName, string variableValue)
@@ -127,6 +129,19 @@ public void ResolvePath_PathContainsSolutionDirProperty_SolutionDirExpanded()
string path = host.CallResolvePath("$(SolutionDir)");
Assert.AreEqual(@"d:\projects\MySolution\", path);
- }
+ }
+
+ [Test]
+ public void GetService_HostPassedFakeServiceProvider_ReturnsServiceFromFakeServiceProvider()
+ {
+ CreateHost();
+ var expectedService = new StringWriter();
+ fakeServiceProvider.AddService(typeof(StringWriter), expectedService);
+
+ var hostServiceProvider = host as IServiceProvider;
+ StringWriter service = hostServiceProvider.GetService(typeof(StringWriter)) as StringWriter;
+
+ Assert.AreEqual(expectedService, service);
+ }
}
}
View
30 src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingServiceProviderTests.cs
@@ -0,0 +1,30 @@
+// 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.TextTemplating;
+using NUnit.Framework;
+using TextTemplating.Tests.Helpers;
+
+namespace TextTemplating.Tests
+{
+ [TestFixture]
+ public class TextTemplatingServiceProviderTests
+ {
+ TextTemplatingServiceProvider serviceProvider;
+
+ void CreateServiceProvider()
+ {
+ serviceProvider = new TextTemplatingServiceProvider();
+ }
+
+ [Test]
+ public void GetService_TypeOfFakeServiceProvider_ReturnsNewFakeServiceProvider()
+ {
+ CreateServiceProvider();
+ FakeServiceProvider service = serviceProvider.GetService(typeof(FakeServiceProvider)) as FakeServiceProvider;
+
+ Assert.IsNotNull(service);
+ }
+ }
+}
View
2 src/AddIns/Misc/TextTemplating/Test/TextTemplating.Tests.csproj
@@ -57,6 +57,7 @@
<Compile Include="Configuration\AssemblyInfo.cs" />
<Compile Include="Helpers\FakeAssemblyParserService.cs" />
<Compile Include="Helpers\FakeReflectionProjectContent.cs" />
+ <Compile Include="Helpers\FakeServiceProvider.cs" />
<Compile Include="Helpers\FakeTextTemplatingAppDomain.cs" />
<Compile Include="Helpers\FakeTextTemplatingAppDomainFactory.cs" />
<Compile Include="Helpers\FakeTextTemplatingAssemblyResolver.cs" />
@@ -84,6 +85,7 @@
<Compile Include="Src\TextTemplatingPathResolverTests.cs" />
<Compile Include="Src\TextTemplatingPreprocessorTests.cs" />
<Compile Include="Src\TextTemplatingReflectionProjectContentTests.cs" />
+ <Compile Include="Src\TextTemplatingServiceProviderTests.cs" />
<Compile Include="Src\TextTemplatingVariableLocationTests.cs" />
<Compile Include="Src\TextTemplatingVariablesTests.cs" />
</ItemGroup>

0 comments on commit 2658bc5

Please sign in to comment.
Something went wrong with that request. Please try again.