Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Hutchinson <m.j.hutchinson@gmail.com>2015-02-02 06:42:08 +0300
committerMichael Hutchinson <m.j.hutchinson@gmail.com>2015-02-02 06:42:08 +0300
commit989b2eb1501ed3926d62f5d45bf668f561a28997 (patch)
tree06351da11a75f63e130235509e831830380cf493 /main/src/addins/TextTemplating
parent465ac5068e1b91ec65a4dcbcc3842b5b59005c5a (diff)
[T4] Expose useful host properties for file templates
Diffstat (limited to 'main/src/addins/TextTemplating')
-rw-r--r--main/src/addins/TextTemplating/MonoDevelop.TextTemplating/FileTemplateDirectiveProcessor.cs131
-rw-r--r--main/src/addins/TextTemplating/MonoDevelop.TextTemplating/FileTemplateHost.cs76
-rw-r--r--main/src/addins/TextTemplating/MonoDevelop.TextTemplating/MonoDevelop.TextTemplating.csproj2
-rw-r--r--main/src/addins/TextTemplating/MonoDevelop.TextTemplating/T4FileTemplate.cs28
4 files changed, 210 insertions, 27 deletions
diff --git a/main/src/addins/TextTemplating/MonoDevelop.TextTemplating/FileTemplateDirectiveProcessor.cs b/main/src/addins/TextTemplating/MonoDevelop.TextTemplating/FileTemplateDirectiveProcessor.cs
new file mode 100644
index 0000000000..24cd72e9c8
--- /dev/null
+++ b/main/src/addins/TextTemplating/MonoDevelop.TextTemplating/FileTemplateDirectiveProcessor.cs
@@ -0,0 +1,131 @@
+//
+// FileTemplateDirectiveProcessor.cs
+//
+// Author:
+// Michael Hutchinson <mhutch@xamarin.com>
+//
+// Copyright (c) 2015 Xamarin Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+using System;
+using Microsoft.VisualStudio.TextTemplating;
+using System.CodeDom.Compiler;
+using Mono.TextTemplating;
+using System.CodeDom;
+
+namespace MonoDevelop.TextTemplating
+{
+ //injects helper members into the generated T4 class so templates are cleaner
+ //
+ // bool IsTrue (string key)
+ // - e.g. <# if(IsTrue("Key")){#>
+ //
+ // FileTemplateHost Tags {get; }
+ // - e.g. <#=Tags["Key"]#>
+ //
+ class FileTemplateDirectiveProcessor : DirectiveProcessor, IRecognizeHostSpecific
+ {
+ CodeDomProvider languageProvider;
+
+ void IRecognizeHostSpecific.SetProcessingRunIsHostSpecific (bool hostSpecific)
+ {
+ if (!hostSpecific)
+ throw new InvalidOperationException ();
+ }
+
+ public bool RequiresProcessingRunIsHostSpecific {
+ get { return true; }
+ }
+
+ public override void StartProcessingRun (CodeDomProvider languageProvider, string templateContents, CompilerErrorCollection errors)
+ {
+ this.languageProvider = languageProvider;
+ base.StartProcessingRun (languageProvider, templateContents, errors);
+ }
+
+ public override string GetClassCodeForProcessingRun ()
+ {
+ var hostProp = new CodePropertyReferenceExpression (new CodeThisReferenceExpression (), "Host");
+ return TemplatingEngine.GenerateIndentedClassCode (
+ languageProvider,
+ CreateIsTrueMethod (hostProp),
+ CreateTagsProperty (hostProp)
+ );
+ }
+
+ static CodeTypeMember CreateIsTrueMethod (CodePropertyReferenceExpression hostProp)
+ {
+ var stringTypeRef = new CodeTypeReference (typeof(string));
+ var boolTypeRef = new CodeTypeReference (typeof(bool));
+ var meth = new CodeMemberMethod { Name = "IsTrue" };
+ meth.Parameters.Add (new CodeParameterDeclarationExpression (stringTypeRef, "key"));
+ meth.ReturnType = boolTypeRef;
+ meth.Statements.Add (
+ new CodeMethodReturnStatement (
+ new CodeMethodInvokeExpression (
+ hostProp, "IsTrue", new CodeArgumentReferenceExpression (meth.Parameters[0].Name)
+ )
+ )
+ );
+ return meth;
+ }
+
+ static CodeTypeMember CreateTagsProperty (CodePropertyReferenceExpression hostProp)
+ {
+ var hostTypeRef = new CodeTypeReference (typeof (FileTemplateHost));
+ var prop = new CodeMemberProperty { Name = "Tags", Type = hostTypeRef };
+ prop.GetStatements.Add (new CodeMethodReturnStatement (hostProp));
+ return prop;
+ }
+
+ public override void FinishProcessingRun ()
+ {
+ }
+
+ public override string[] GetImportsForProcessingRun ()
+ {
+ return null;
+ }
+
+ public override string GetPostInitializationCodeForProcessingRun ()
+ {
+ return null;
+ }
+
+ public override string GetPreInitializationCodeForProcessingRun ()
+ {
+ return null;
+ }
+
+ public override string[] GetReferencesForProcessingRun ()
+ {
+ return null;
+ }
+
+ public override bool IsDirectiveSupported (string directiveName)
+ {
+ return true;
+ }
+
+ public override void ProcessDirective (string directiveName, System.Collections.Generic.IDictionary<string, string> arguments)
+ {
+ }
+ }
+}
diff --git a/main/src/addins/TextTemplating/MonoDevelop.TextTemplating/FileTemplateHost.cs b/main/src/addins/TextTemplating/MonoDevelop.TextTemplating/FileTemplateHost.cs
new file mode 100644
index 0000000000..6d609cfcf8
--- /dev/null
+++ b/main/src/addins/TextTemplating/MonoDevelop.TextTemplating/FileTemplateHost.cs
@@ -0,0 +1,76 @@
+//
+// FileTemplateHost.cs
+//
+// Author:
+// Michael Hutchinson <mhutch@xamarin.com>
+//
+// Copyright (c) 2015 Xamarin Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+using System;
+using System.Collections.Generic;
+using Microsoft.VisualStudio.TextTemplating;
+using MonoDevelop.Core;
+using MonoDevelop.Core.StringParsing;
+
+namespace MonoDevelop.TextTemplating
+{
+ public class FileTemplateHost : MonoDevelopTemplatingHost
+ {
+ readonly IStringTagModel tags;
+
+ internal FileTemplateHost (IStringTagModel tags)
+ {
+ this.tags = tags;
+
+ Refs.Add (typeof (FileTemplateHost).Assembly.Location);
+ }
+
+ //TODO should we move this to a tags property?
+ public string this[string key] {
+ get {
+ var val = tags.GetValue (key);
+ return val != null ? val.ToString () : null;
+ }
+ }
+
+ public bool IsTrue (string key)
+ {
+ if (string.Equals (this [key], "true", StringComparison.OrdinalIgnoreCase))
+ return true;
+ return false;
+ }
+
+ protected override string SubstitutePlaceholders (string s)
+ {
+ return StringParserService.Parse (s, tags);
+ }
+
+ public override bool UseSpecificHostType {
+ get { return true; }
+ }
+
+ public override IEnumerable<IDirectiveProcessor> GetAdditionalDirectiveProcessors ()
+ {
+ yield return new FileTemplateDirectiveProcessor ();
+ }
+ }
+
+}
diff --git a/main/src/addins/TextTemplating/MonoDevelop.TextTemplating/MonoDevelop.TextTemplating.csproj b/main/src/addins/TextTemplating/MonoDevelop.TextTemplating/MonoDevelop.TextTemplating.csproj
index 8fe4ef4c26..91b0d65c0a 100644
--- a/main/src/addins/TextTemplating/MonoDevelop.TextTemplating/MonoDevelop.TextTemplating.csproj
+++ b/main/src/addins/TextTemplating/MonoDevelop.TextTemplating/MonoDevelop.TextTemplating.csproj
@@ -55,6 +55,8 @@
<Compile Include="AddinInfo.cs" />
<Compile Include="GenerateCommandHandler.cs" />
<Compile Include="T4FileTemplate.cs" />
+ <Compile Include="FileTemplateDirectiveProcessor.cs" />
+ <Compile Include="FileTemplateHost.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="System" />
diff --git a/main/src/addins/TextTemplating/MonoDevelop.TextTemplating/T4FileTemplate.cs b/main/src/addins/TextTemplating/MonoDevelop.TextTemplating/T4FileTemplate.cs
index 5b2abad2cb..7d0c9eb9c8 100644
--- a/main/src/addins/TextTemplating/MonoDevelop.TextTemplating/T4FileTemplate.cs
+++ b/main/src/addins/TextTemplating/MonoDevelop.TextTemplating/T4FileTemplate.cs
@@ -31,6 +31,7 @@ using System.Xml;
using MonoDevelop.Core;
using MonoDevelop.Ide.Templates;
using MonoDevelop.Core.StringParsing;
+using Microsoft.VisualStudio.TextTemplating;
namespace MonoDevelop.TextTemplating
{
@@ -71,32 +72,5 @@ namespace MonoDevelop.TextTemplating
}
}
}
-
- public class FileTemplateHost : MonoDevelopTemplatingHost
- {
- readonly IStringTagModel tags;
-
- public FileTemplateHost (IStringTagModel tags)
- {
- this.tags = tags;
-
- AddMonoDevelopHostImport ();
- Imports.Add (typeof (FileTemplateHost).Namespace);
- Refs.Add (typeof (FileTemplateHost).Assembly.Location);
- }
-
- //TODO could we expose the tags better e.g. via a directive processor?
- public string this[string key] {
- get {
- var val = tags.GetValue (key);
- return val != null? val.ToString () : null;
- }
- }
-
- protected override string SubstitutePlaceholders (string s)
- {
- return StringParserService.Parse (s, tags);
- }
- }
}