This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 771
/
TextTemplatingAssemblyResolver.cs
91 lines (79 loc) · 2.69 KB
/
TextTemplatingAssemblyResolver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// 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.IO;
using ICSharpCode.SharpDevelop.Project;
namespace ICSharpCode.TextTemplating
{
public class TextTemplatingAssemblyResolver : ITextTemplatingAssemblyResolver
{
IProject project;
IAssemblyParserService assemblyParserService;
ITextTemplatingVariables templatingVariables;
public TextTemplatingAssemblyResolver(
IProject project,
IAssemblyParserService assemblyParserService,
ITextTemplatingVariables templatingVariables)
{
this.project = project;
this.assemblyParserService = assemblyParserService;
this.templatingVariables = templatingVariables;
}
public TextTemplatingAssemblyResolver(IProject project)
: this(
project,
new TextTemplatingAssemblyParserService(),
new TextTemplatingVariables())
{
}
public string Resolve(string assemblyReference)
{
assemblyReference = ExpandVariables(assemblyReference);
if (Path.IsPathRooted(assemblyReference)) {
return assemblyReference;
}
string resolvedAssemblyFileName = ResolveAssemblyFromProject(assemblyReference);
if (resolvedAssemblyFileName == null) {
resolvedAssemblyFileName = ResolveAssemblyFromGac(assemblyReference);
}
if (resolvedAssemblyFileName != null) {
return resolvedAssemblyFileName;
}
return assemblyReference;
}
string ExpandVariables(string assemblyReference)
{
return templatingVariables.Expand(assemblyReference);
}
string ResolveAssemblyFromProject(string assemblyReference)
{
foreach (ReferenceProjectItem refProjectItem in project.GetItemsOfType(ItemType.Reference)) {
if (IsMatch(refProjectItem, assemblyReference)) {
return refProjectItem.FileName;
}
}
return null;
}
bool IsMatch(ReferenceProjectItem refProjectItem, string assemblyReference)
{
return String.Equals(refProjectItem.Include, assemblyReference, StringComparison.InvariantCultureIgnoreCase);
}
string ResolveAssemblyFromGac(string assemblyReference)
{
IReflectionProjectContent projectContent = GetProjectContent(assemblyReference);
if (projectContent != null) {
return projectContent.AssemblyLocation;
}
return null;
}
IReflectionProjectContent GetProjectContent(string assemblyReference)
{
var reference = new ReferenceProjectItem(project, assemblyReference);
return GetProjectContent(reference);
}
IReflectionProjectContent GetProjectContent(ReferenceProjectItem refProjectItem)
{
return assemblyParserService.GetReflectionProjectContentForReference(refProjectItem);
}
}
}