From 07b92510e2de4a0d7ae12d50d9ed784ea0985141 Mon Sep 17 00:00:00 2001 From: Matt Ward Date: Sun, 25 Sep 2011 19:08:02 +0100 Subject: [PATCH] Expand environment variables in T4 assembly directives. --- .../Project/Src/ITextTemplatingEnvironment.cs | 12 ++++ .../Src/ITextTemplatingPathResolver.cs | 12 ++++ .../Src/TextTemplatingAssemblyResolver.cs | 14 ++--- .../Project/Src/TextTemplatingEnvironment.cs | 15 +++++ .../Project/Src/TextTemplatingPathResolver.cs | 32 +++++++++++ .../Project/TextTemplating.csproj | 4 ++ .../Helpers/FakeTextTemplatingEnvironment.cs | 29 ++++++++++ .../Helpers/FakeTextTemplatingPathResolver.cs | 28 +++++++++ .../Helpers/FakeTextTemplatingVariables.cs | 1 + .../TextTemplatingAssemblyResolverTests.cs | 20 ++++--- .../Test/Src/TextTemplatingHostTests.cs | 2 +- .../Src/TextTemplatingPathResolverTests.cs | 57 +++++++++++++++++++ .../Test/TextTemplating.Tests.csproj | 3 + 13 files changed, 213 insertions(+), 16 deletions(-) create mode 100644 src/AddIns/Misc/TextTemplating/Project/Src/ITextTemplatingEnvironment.cs create mode 100644 src/AddIns/Misc/TextTemplating/Project/Src/ITextTemplatingPathResolver.cs create mode 100644 src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingEnvironment.cs create mode 100644 src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingPathResolver.cs create mode 100644 src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingEnvironment.cs create mode 100644 src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingPathResolver.cs create mode 100644 src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingPathResolverTests.cs diff --git a/src/AddIns/Misc/TextTemplating/Project/Src/ITextTemplatingEnvironment.cs b/src/AddIns/Misc/TextTemplating/Project/Src/ITextTemplatingEnvironment.cs new file mode 100644 index 0000000000..2c6d014cd8 --- /dev/null +++ b/src/AddIns/Misc/TextTemplating/Project/Src/ITextTemplatingEnvironment.cs @@ -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); + } +} diff --git a/src/AddIns/Misc/TextTemplating/Project/Src/ITextTemplatingPathResolver.cs b/src/AddIns/Misc/TextTemplating/Project/Src/ITextTemplatingPathResolver.cs new file mode 100644 index 0000000000..62297562b0 --- /dev/null +++ b/src/AddIns/Misc/TextTemplating/Project/Src/ITextTemplatingPathResolver.cs @@ -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); + } +} diff --git a/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingAssemblyResolver.cs b/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingAssemblyResolver.cs index 2d63c639d1..a47bc89ded 100644 --- a/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingAssemblyResolver.cs +++ b/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingAssemblyResolver.cs @@ -11,29 +11,29 @@ namespace ICSharpCode.TextTemplating { 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 @@ namespace ICSharpCode.TextTemplating return assemblyReference; } - string ExpandVariables(string assemblyReference) + string ResolvePath(string assemblyReference) { - return templatingVariables.Expand(assemblyReference); + return pathResolver.ResolvePath(assemblyReference); } string ResolveAssemblyFromProject(string assemblyReference) diff --git a/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingEnvironment.cs b/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingEnvironment.cs new file mode 100644 index 0000000000..52be4c7aa3 --- /dev/null +++ b/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingEnvironment.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 TextTemplatingEnvironment : ITextTemplatingEnvironment + { + public string ExpandEnvironmentVariables(string name) + { + return Environment.ExpandEnvironmentVariables(name); + } + } +} diff --git a/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingPathResolver.cs b/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingPathResolver.cs new file mode 100644 index 0000000000..7da0491838 --- /dev/null +++ b/src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingPathResolver.cs @@ -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); + } + } +} diff --git a/src/AddIns/Misc/TextTemplating/Project/TextTemplating.csproj b/src/AddIns/Misc/TextTemplating/Project/TextTemplating.csproj index b48c0f09ad..67fee77c77 100644 --- a/src/AddIns/Misc/TextTemplating/Project/TextTemplating.csproj +++ b/src/AddIns/Misc/TextTemplating/Project/TextTemplating.csproj @@ -57,9 +57,11 @@ + + @@ -71,6 +73,7 @@ + @@ -78,6 +81,7 @@ + diff --git a/src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingEnvironment.cs b/src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingEnvironment.cs new file mode 100644 index 0000000000..3bc9938fa7 --- /dev/null +++ b/src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingEnvironment.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 Variables = new Dictionary(); + + 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); + } + } +} diff --git a/src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingPathResolver.cs b/src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingPathResolver.cs new file mode 100644 index 0000000000..ba6d118acc --- /dev/null +++ b/src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingPathResolver.cs @@ -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 Paths = new Dictionary(); + + 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); + } + } +} diff --git a/src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingVariables.cs b/src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingVariables.cs index 6abca76503..a8b72fe69f 100644 --- a/src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingVariables.cs +++ b/src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingVariables.cs @@ -13,6 +13,7 @@ namespace TextTemplating.Tests.Helpers public void AddVariable(string name, string value) { + name = "$(" + name + ")"; Variables.Add(name, value); } diff --git a/src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingAssemblyResolverTests.cs b/src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingAssemblyResolverTests.cs index 55901f45a6..06d82167e3 100644 --- a/src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingAssemblyResolverTests.cs +++ b/src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingAssemblyResolverTests.cs @@ -16,14 +16,14 @@ namespace TextTemplating.Tests 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 @@ namespace TextTemplating.Tests 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); } diff --git a/src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingHostTests.cs b/src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingHostTests.cs index 8f4cc8c72f..f67d1c4dc6 100644 --- a/src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingHostTests.cs +++ b/src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingHostTests.cs @@ -123,7 +123,7 @@ namespace TextTemplating.Tests 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); diff --git a/src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingPathResolverTests.cs b/src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingPathResolverTests.cs new file mode 100644 index 0000000000..b3cc906939 --- /dev/null +++ b/src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingPathResolverTests.cs @@ -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); + } + } +} diff --git a/src/AddIns/Misc/TextTemplating/Test/TextTemplating.Tests.csproj b/src/AddIns/Misc/TextTemplating/Test/TextTemplating.Tests.csproj index dacaa08e62..6e665604eb 100644 --- a/src/AddIns/Misc/TextTemplating/Test/TextTemplating.Tests.csproj +++ b/src/AddIns/Misc/TextTemplating/Test/TextTemplating.Tests.csproj @@ -61,9 +61,11 @@ + + @@ -79,6 +81,7 @@ +