Permalink
Browse files

Expand environment variables in T4 assembly directives.

  • Loading branch information...
1 parent a22a110 commit 07b92510e2de4a0d7ae12d50d9ed784ea0985141 @mrward mrward committed Sep 25, 2011
@@ -0,0 +1,12 @@
+// 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 interface ITextTemplatingEnvironment
+ {
+ string ExpandEnvironmentVariables(string name);
+ }
+}
@@ -0,0 +1,12 @@
+// 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 interface ITextTemplatingPathResolver
+ {
+ string ResolvePath(string path);
+ }
+}
@@ -11,29 +11,29 @@ public class TextTemplatingAssemblyResolver : ITextTemplatingAssemblyResolver
{
IProject project;
IAssemblyParserService assemblyParserService;
- ITextTemplatingVariables templatingVariables;
+ ITextTemplatingPathResolver pathResolver;
public TextTemplatingAssemblyResolver(
IProject project,
IAssemblyParserService assemblyParserService,
- ITextTemplatingVariables templatingVariables)
+ ITextTemplatingPathResolver pathResolver)
{
this.project = project;
this.assemblyParserService = assemblyParserService;
- this.templatingVariables = templatingVariables;
+ this.pathResolver = pathResolver;
}
public TextTemplatingAssemblyResolver(IProject project)
: this(
project,
new TextTemplatingAssemblyParserService(),
- new TextTemplatingVariables())
+ new TextTemplatingPathResolver())
{
}
public string Resolve(string assemblyReference)
{
- assemblyReference = ExpandVariables(assemblyReference);
+ assemblyReference = ResolvePath(assemblyReference);
if (Path.IsPathRooted(assemblyReference)) {
return assemblyReference;
}
@@ -48,9 +48,9 @@ public string Resolve(string assemblyReference)
return assemblyReference;
}
- string ExpandVariables(string assemblyReference)
+ string ResolvePath(string assemblyReference)
{
- return templatingVariables.Expand(assemblyReference);
+ return pathResolver.ResolvePath(assemblyReference);
}
string ResolveAssemblyFromProject(string assemblyReference)
@@ -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 TextTemplatingEnvironment : ITextTemplatingEnvironment
+ {
+ public string ExpandEnvironmentVariables(string name)
+ {
+ return Environment.ExpandEnvironmentVariables(name);
+ }
+ }
+}
@@ -0,0 +1,32 @@
+// 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 TextTemplatingPathResolver : ITextTemplatingPathResolver
+ {
+ ITextTemplatingVariables templatingVariables;
+ ITextTemplatingEnvironment environment;
+
+ public TextTemplatingPathResolver()
+ : this(new TextTemplatingVariables(), new TextTemplatingEnvironment())
+ {
+ }
+
+ public TextTemplatingPathResolver(
+ ITextTemplatingVariables templatingVariables,
+ ITextTemplatingEnvironment environment)
+ {
+ this.templatingVariables = templatingVariables;
+ this.environment = environment;
+ }
+
+ public string ResolvePath(string path)
+ {
+ path = environment.ExpandEnvironmentVariables(path);
+ return templatingVariables.Expand(path);
+ }
+ }
+}
@@ -57,9 +57,11 @@
<Compile Include="Src\ITextTemplatingAppDomainFactory.cs" />
<Compile Include="Src\ITextTemplatingAssemblyResolver.cs" />
<Compile Include="Src\ITextTemplatingCustomToolContext.cs" />
+ <Compile Include="Src\ITextTemplatingEnvironment.cs" />
<Compile Include="Src\ITextTemplatingFilePreprocessor.cs" />
<Compile Include="Src\ITextTemplatingHost.cs" />
<Compile Include="Src\ITextTemplatingFileGenerator.cs" />
+ <Compile Include="Src\ITextTemplatingPathResolver.cs" />
<Compile Include="Src\ITextTemplatingStringParser.cs" />
<Compile Include="Src\ITextTemplatingVariables.cs" />
<Compile Include="Src\NamespaceHint.cs" />
@@ -71,13 +73,15 @@
<Compile Include="Src\TextTemplatingCustomTool.cs" />
<Compile Include="Src\TextTemplatingCustomToolContext.cs" />
<Compile Include="Src\TextTemplatingDirectoryVariable.cs" />
+ <Compile Include="Src\TextTemplatingEnvironment.cs" />
<Compile Include="Src\TextTemplatingFileGenerator.cs" />
<Compile Include="Src\TextTemplatingFileGeneratorCustomTool.cs" />
<Compile Include="Configuration\AssemblyInfo.cs" />
<Compile Include="Src\TextTemplatingFilePreprocessor.cs" />
<Compile Include="Src\TextTemplatingFilePreprocessorCustomTool.cs" />
<Compile Include="Src\TextTemplatingFileProcessor.cs" />
<Compile Include="Src\TextTemplatingHost.cs" />
+ <Compile Include="Src\TextTemplatingPathResolver.cs" />
<Compile Include="Src\TextTemplatingReflectionProjectContent.cs" />
<Compile Include="Src\TextTemplatingStringParser.cs" />
<Compile Include="Src\TextTemplatingVariableLocation.cs" />
@@ -0,0 +1,29 @@
+// 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;
+using ICSharpCode.TextTemplating;
+
+namespace TextTemplating.Tests.Helpers
+{
+ public class FakeTextTemplatingEnvironment : ITextTemplatingEnvironment
+ {
+ public Dictionary<string, string> Variables = new Dictionary<string, string>();
+
+ public string ExpandEnvironmentVariables(string name)
+ {
+ string value = null;
+ if (Variables.TryGetValue(name, out value)) {
+ return value;
+ }
+ return name;
+ }
+
+ public void AddVariable(string name, string value)
+ {
+ name = "%" + name + "%";
+ Variables.Add(name, value);
+ }
+ }
+}
@@ -0,0 +1,28 @@
+// 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;
+using ICSharpCode.TextTemplating;
+
+namespace TextTemplating.Tests.Helpers
+{
+ public class FakeTextTemplatingPathResolver : ITextTemplatingPathResolver
+ {
+ public Dictionary<string, string> Paths = new Dictionary<string, string>();
+
+ public string ResolvePath(string path)
+ {
+ string resolvedPath = null;
+ if (Paths.TryGetValue(path, out resolvedPath)) {
+ return resolvedPath;
+ }
+ return path;
+ }
+
+ public void AddPath(string path, string resolvedPath)
+ {
+ Paths.Add(path, resolvedPath);
+ }
+ }
+}
@@ -13,6 +13,7 @@ public class FakeTextTemplatingVariables : ITextTemplatingVariables
public void AddVariable(string name, string value)
{
+ name = "$(" + name + ")";
Variables.Add(name, value);
}
@@ -16,14 +16,14 @@ public class TextTemplatingAssemblyResolverTests
TextTemplatingAssemblyResolver resolver;
IProject project;
FakeAssemblyParserService fakeAssemblyParserService;
- FakeTextTemplatingVariables fakeTemplatingVariables;
+ FakeTextTemplatingPathResolver fakePathResolver;
void CreateResolver()
{
project = ProjectHelper.CreateProject();
fakeAssemblyParserService = new FakeAssemblyParserService();
- fakeTemplatingVariables = new FakeTextTemplatingVariables();
- resolver = new TextTemplatingAssemblyResolver(project, fakeAssemblyParserService, fakeTemplatingVariables);
+ fakePathResolver = new FakeTextTemplatingPathResolver();
+ resolver = new TextTemplatingAssemblyResolver(project, fakeAssemblyParserService, fakePathResolver);
}
ReferenceProjectItem AddReferenceToProject(string referenceName)
@@ -153,20 +153,24 @@ public void Resolve_ProjectHasNoReferencesAndAssemblyReferenceNotFoundInGac_Retu
public void Resolve_AssemblyReferenceHasTemplateVariable_ReturnsExpandedAssemblyReferenceFileName()
{
CreateResolver();
- fakeTemplatingVariables.AddVariable("$(SolutionDir)", @"d:\projects\MyProject\");
+ string path = @"$(SolutionDir)lib\Test.dll";
+ string expectedPath = @"d:\projects\MyProject\lib\Test.dll";
+ fakePathResolver.AddPath(path, expectedPath);
- string result = resolver.Resolve(@"$(SolutionDir)lib\Test.dll");
+ string resolvedPath = resolver.Resolve(path);
- Assert.AreEqual(@"d:\projects\MyProject\lib\Test.dll", result);
+ Assert.AreEqual(expectedPath, resolvedPath);
}
[Test]
public void Resolve_AssemblyReferenceHasTemplateVariable_AssemblyParserServiceIsNotUsed()
{
CreateResolver();
- fakeTemplatingVariables.AddVariable("$(SolutionDir)", @"d:\projects\MyProject\");
+ string path = @"$(SolutionDir)lib\Test.dll";
+ string expectedPath = @"d:\projects\MyProject\lib\Test.dll";
+ fakePathResolver.AddPath(path, expectedPath);
- string result = resolver.Resolve(@"$(SolutionDir)lib\Test.dll");
+ string result = resolver.Resolve(path);
Assert.IsFalse(fakeAssemblyParserService.IsGetReflectionProjectContentForReferenceCalled);
}
@@ -123,7 +123,7 @@ public void ResolveAssemblyReference_PassedMyAssemblyReference_ReturnsFileNameRe
public void ResolvePath_PathContainsSolutionDirProperty_SolutionDirExpanded()
{
CreateHost();
- AddTemplateVariableValue("$(SolutionDir)", @"d:\projects\MySolution\");
+ AddTemplateVariableValue("SolutionDir", @"d:\projects\MySolution\");
string path = host.CallResolvePath("$(SolutionDir)");
Assert.AreEqual(@"d:\projects\MySolution\", path);
@@ -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.TextTemplating;
+using NUnit.Framework;
+using TextTemplating.Tests.Helpers;
+
+namespace TextTemplating.Tests
+{
+ [TestFixture]
+ public class TextTemplatingPathResolverTests
+ {
+ TextTemplatingPathResolver pathResolver;
+ FakeTextTemplatingVariables fakeTemplatingVariables;
+ FakeTextTemplatingEnvironment fakeEnvironment;
+
+ void CreatePathResolver()
+ {
+ fakeTemplatingVariables = new FakeTextTemplatingVariables();
+ fakeEnvironment = new FakeTextTemplatingEnvironment();
+ pathResolver = new TextTemplatingPathResolver(fakeTemplatingVariables, fakeEnvironment);
+ }
+
+ void AddEnvironmentVariable(string name, string value)
+ {
+ fakeEnvironment.AddVariable(name, value);
+ }
+
+ void AddTemplateVariable(string name, string value)
+ {
+ fakeTemplatingVariables.AddVariable(name, value);
+ }
+
+ [Test]
+ public void ResolvePath_EnvironmentVariableInPath_ReturnsPathWithEnvironmentVariableExpanded()
+ {
+ CreatePathResolver();
+ AddEnvironmentVariable("windir", @"c:\windows");
+
+ string path = pathResolver.ResolvePath("%windir%");
+
+ Assert.AreEqual(@"c:\windows", path);
+ }
+
+ [Test]
+ public void ResolvePath_TemplateVariableInPath_ReturnsPathWithTemplateVariableExpanded()
+ {
+ CreatePathResolver();
+ AddTemplateVariable("SolutionDir", @"d:\projects\MyApp\");
+
+ string path = pathResolver.ResolvePath("$(SolutionDir)");
+
+ Assert.AreEqual(@"d:\projects\MyApp\", path);
+ }
+ }
+}
@@ -61,9 +61,11 @@
<Compile Include="Helpers\FakeTextTemplatingAppDomainFactory.cs" />
<Compile Include="Helpers\FakeTextTemplatingAssemblyResolver.cs" />
<Compile Include="Helpers\FakeTextTemplatingCustomToolContext.cs" />
+ <Compile Include="Helpers\FakeTextTemplatingEnvironment.cs" />
<Compile Include="Helpers\FakeTextTemplatingFilePreprocessor.cs" />
<Compile Include="Helpers\FakeTextTemplatingHost.cs" />
<Compile Include="Helpers\FakeTextTemplatingFileGenerator.cs" />
+ <Compile Include="Helpers\FakeTextTemplatingPathResolver.cs" />
<Compile Include="Helpers\FakeTextTemplatingStringParser.cs" />
<Compile Include="Helpers\FakeTextTemplatingVariables.cs" />
<Compile Include="Helpers\ProjectHelper.cs" />
@@ -79,6 +81,7 @@
<Compile Include="Src\TextTemplatingFileGeneratorTests.cs" />
<Compile Include="Src\TextTemplatingFilePreprocessorCustomToolTests.cs" />
<Compile Include="Src\TextTemplatingHostTests.cs" />
+ <Compile Include="Src\TextTemplatingPathResolverTests.cs" />
<Compile Include="Src\TextTemplatingPreprocessorTests.cs" />
<Compile Include="Src\TextTemplatingReflectionProjectContentTests.cs" />
<Compile Include="Src\TextTemplatingVariableLocationTests.cs" />

0 comments on commit 07b9251

Please sign in to comment.