4 using System.CodeDom.Compiler;
5 using System.Collections.Generic;
9 using System.Reflection;
11 using Microsoft.CSharp.RuntimeBinder;
12 using Microsoft.VisualStudio.TextTemplating;
13 using SiliconStudio.Core;
14 using SiliconStudio.Core.Diagnostics;
15 using SiliconStudio.Core.IO;
17 namespace SiliconStudio.ProjectTemplating
19 internal class ProjectTemplatingHost : Microsoft.VisualStudio.TextTemplating.ITextTemplatingEngineHost,
ITextTemplatingSessionHost
21 public ProjectTemplatingHost(
ILogger log,
string templateFile,
string rootDirectory, ExpandoObject expando,
IEnumerable<string> assemblies)
23 if (log == null)
throw new ArgumentNullException(
"log");
24 if (templateFile == null)
throw new ArgumentNullException(
"templateFile");
25 if (rootDirectory == null)
throw new ArgumentNullException(
"rootDirectory");
26 if (expando == null)
throw new ArgumentNullException(
"expando");
28 this.TemplateFile = templateFile;
29 this.rootDirectory = rootDirectory;
31 Session =
new CustomTemplatingSession(expando);
33 var assembliesToLoad =
new List<string>()
36 typeof(RuntimeBinderException).Assembly.FullName,
37 "Mono.TextTemplating",
39 typeof(
UPath).Assembly.FullName,
40 "SiliconStudio.ProjectTemplating"
42 assembliesToLoad.AddRange(assemblies);
43 StandardAssemblyReferences = assembliesToLoad;
45 StandardImports =
new List<string>()
49 "System.Collections.Generic",
51 "SiliconStudio.ProjectTemplating"
55 public IList<string> StandardAssemblyReferences {
get;
private set; }
57 public IList<string> StandardImports {
get;
private set; }
59 public string TemplateFile {
get; set; }
61 private readonly
string rootDirectory;
65 public object GetHostOption(
string optionName)
70 Session.TryGetValue(optionName, out value);
75 public bool LoadIncludeText(
string requestFileName, out
string content, out
string location)
78 location = Path.Combine(rootDirectory, requestFileName);
79 if (
File.Exists(location))
81 content = File.ReadAllText(location);
87 public void LogErrors(CompilerErrorCollection errors)
89 for (
int i = 0; i < errors.Count; i++)
91 var error = errors[i];
93 var msg = error.FileName == null ? error.ErrorText : error.ToString();
98 public AppDomain ProvideTemplatingAppDomain(
string content)
100 return AppDomain.CurrentDomain;
103 public string ResolveAssemblyReference(
string assemblyReference)
105 var loadedAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(x => x.GetName().Name.Equals(assemblyReference, StringComparison.Ordinal) || x.GetName().FullName.Equals(assemblyReference, StringComparison.Ordinal));
106 if (loadedAssembly != null)
107 return loadedAssembly.Location;
113 Assembly assembly = Assembly.Load(assemblyReference);
114 if (assembly != null)
116 return assembly.Location;
119 catch (FileNotFoundException) { }
120 catch (FileLoadException) { }
121 catch (BadImageFormatException) { }
126 public Type ResolveDirectiveProcessor(
string processorName)
128 throw new NotImplementedException();
131 public string ResolveParameterValue(
string directiveId,
string processorName,
string parameterName)
133 throw new NotImplementedException();
136 public string ResolvePath(
string path)
138 throw new NotImplementedException();
141 public void SetFileExtension(
string extension)
145 public void SetOutputEncoding(
Encoding encoding,
bool fromOutputDirective)
151 throw new NotImplementedException();
PlatformType
Describes the platform operating system.
System.Text.Encoding Encoding
Base class that describes a uniform path and provides method to manipulate them. Concrete class are U...
The type of the serialized type will be passed as a generic arguments of the serializer. Example: serializer of A becomes instantiated as Serializer{A}.
The template can be applied to an existing PackageSession.