Browse Source

Expand environment variables in T4 assembly directives.

pull/23/head
Matt Ward 15 years ago
parent
commit
07b92510e2
  1. 12
      src/AddIns/Misc/TextTemplating/Project/Src/ITextTemplatingEnvironment.cs
  2. 12
      src/AddIns/Misc/TextTemplating/Project/Src/ITextTemplatingPathResolver.cs
  3. 14
      src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingAssemblyResolver.cs
  4. 15
      src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingEnvironment.cs
  5. 32
      src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingPathResolver.cs
  6. 4
      src/AddIns/Misc/TextTemplating/Project/TextTemplating.csproj
  7. 29
      src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingEnvironment.cs
  8. 28
      src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingPathResolver.cs
  9. 1
      src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingVariables.cs
  10. 20
      src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingAssemblyResolverTests.cs
  11. 2
      src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingHostTests.cs
  12. 57
      src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingPathResolverTests.cs
  13. 3
      src/AddIns/Misc/TextTemplating/Test/TextTemplating.Tests.csproj

12
src/AddIns/Misc/TextTemplating/Project/Src/ITextTemplatingEnvironment.cs

@ -0,0 +1,12 @@ @@ -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);
}
}

12
src/AddIns/Misc/TextTemplating/Project/Src/ITextTemplatingPathResolver.cs

@ -0,0 +1,12 @@ @@ -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);
}
}

14
src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingAssemblyResolver.cs

@ -11,29 +11,29 @@ namespace ICSharpCode.TextTemplating @@ -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 @@ -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)

15
src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingEnvironment.cs

@ -0,0 +1,15 @@ @@ -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);
}
}
}

32
src/AddIns/Misc/TextTemplating/Project/Src/TextTemplatingPathResolver.cs

@ -0,0 +1,32 @@ @@ -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);
}
}
}

4
src/AddIns/Misc/TextTemplating/Project/TextTemplating.csproj

@ -57,9 +57,11 @@ @@ -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,6 +73,7 @@ @@ -71,6 +73,7 @@
<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" />
@ -78,6 +81,7 @@ @@ -78,6 +81,7 @@
<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" />

29
src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingEnvironment.cs

@ -0,0 +1,29 @@ @@ -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);
}
}
}

28
src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingPathResolver.cs

@ -0,0 +1,28 @@ @@ -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);
}
}
}

1
src/AddIns/Misc/TextTemplating/Test/Helpers/FakeTextTemplatingVariables.cs

@ -13,6 +13,7 @@ namespace TextTemplating.Tests.Helpers @@ -13,6 +13,7 @@ namespace TextTemplating.Tests.Helpers
public void AddVariable(string name, string value)
{
name = "$(" + name + ")";
Variables.Add(name, value);
}

20
src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingAssemblyResolverTests.cs

@ -16,14 +16,14 @@ namespace TextTemplating.Tests @@ -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 @@ -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);
}

2
src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingHostTests.cs

@ -123,7 +123,7 @@ namespace TextTemplating.Tests @@ -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);

57
src/AddIns/Misc/TextTemplating/Test/Src/TextTemplatingPathResolverTests.cs

@ -0,0 +1,57 @@ @@ -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);
}
}
}

3
src/AddIns/Misc/TextTemplating/Test/TextTemplating.Tests.csproj

@ -61,9 +61,11 @@ @@ -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 @@ @@ -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" />

Loading…
Cancel
Save