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-01 10:23:50 +0300
committerMichael Hutchinson <m.j.hutchinson@gmail.com>2015-02-01 12:10:09 +0300
commite766c859555369406c167c8db511cef51c89ab37 (patch)
tree72ae92c52b4f1329516049ed32d9202282524041 /main/src/addins/TextTemplating
parentd6c561d7c883588592ab36a34c817bb73a0d884c (diff)
[T4] Add T4Template file template type
Diffstat (limited to 'main/src/addins/TextTemplating')
-rw-r--r--main/src/addins/TextTemplating/MonoDevelop.TextTemplating/MonoDevelop.TextTemplating.addin.xml4
-rw-r--r--main/src/addins/TextTemplating/MonoDevelop.TextTemplating/MonoDevelop.TextTemplating.csproj2
-rw-r--r--main/src/addins/TextTemplating/MonoDevelop.TextTemplating/T4FileTemplate.cs106
3 files changed, 112 insertions, 0 deletions
diff --git a/main/src/addins/TextTemplating/MonoDevelop.TextTemplating/MonoDevelop.TextTemplating.addin.xml b/main/src/addins/TextTemplating/MonoDevelop.TextTemplating/MonoDevelop.TextTemplating.addin.xml
index 39e0028b52..a86b1cce6f 100644
--- a/main/src/addins/TextTemplating/MonoDevelop.TextTemplating/MonoDevelop.TextTemplating.addin.xml
+++ b/main/src/addins/TextTemplating/MonoDevelop.TextTemplating/MonoDevelop.TextTemplating.addin.xml
@@ -53,4 +53,8 @@
<CommandItem id = "MonoDevelop.TextTemplating.Commands.Generate" />
</Condition>
</Extension>
+
+ <Extension path = "/MonoDevelop/Ide/FileTemplateTypes">
+ <FileTemplateType name = "T4File" class = "MonoDevelop.TextTemplating.T4FileTemplate" />
+ </Extension>
</ExtensionModel>
diff --git a/main/src/addins/TextTemplating/MonoDevelop.TextTemplating/MonoDevelop.TextTemplating.csproj b/main/src/addins/TextTemplating/MonoDevelop.TextTemplating/MonoDevelop.TextTemplating.csproj
index 89192bbcb1..8fe4ef4c26 100644
--- a/main/src/addins/TextTemplating/MonoDevelop.TextTemplating/MonoDevelop.TextTemplating.csproj
+++ b/main/src/addins/TextTemplating/MonoDevelop.TextTemplating/MonoDevelop.TextTemplating.csproj
@@ -54,6 +54,7 @@
<Compile Include="ProjectFileTemplatingHost.cs" />
<Compile Include="AddinInfo.cs" />
<Compile Include="GenerateCommandHandler.cs" />
+ <Compile Include="T4FileTemplate.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="System" />
@@ -73,6 +74,7 @@
<SpecificVersion>False</SpecificVersion>
</Reference>
<Reference Include="System.Core" />
+ <Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Folder Include="Parser\" />
diff --git a/main/src/addins/TextTemplating/MonoDevelop.TextTemplating/T4FileTemplate.cs b/main/src/addins/TextTemplating/MonoDevelop.TextTemplating/T4FileTemplate.cs
new file mode 100644
index 0000000000..b46624d08d
--- /dev/null
+++ b/main/src/addins/TextTemplating/MonoDevelop.TextTemplating/T4FileTemplate.cs
@@ -0,0 +1,106 @@
+//
+// T4FileTemplate.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 System.IO;
+using System.Xml;
+using MonoDevelop.Core;
+using MonoDevelop.Ide.Templates;
+
+namespace MonoDevelop.TextTemplating
+{
+ public class T4FileTemplate : SingleFileDescriptionTemplate
+ {
+ FilePath srcFile;
+
+ public override void Load (XmlElement filenode, FilePath baseDirectory)
+ {
+ base.Load (filenode, baseDirectory);
+ var srcAtt = filenode.Attributes["src"];
+ if (srcAtt == null) {
+ throw new Exception ("T4FileTemplate is missing src attribute");
+ }
+ srcFile = FileService.MakePathSeparatorsNative (srcAtt.Value);
+ if (srcFile.IsNullOrEmpty)
+ throw new InvalidOperationException ("Template's Src attribute is empty");
+ srcFile = srcFile.ToAbsolute (baseDirectory);
+ }
+
+ public override string CreateContent (string language)
+ {
+ return File.ReadAllText (srcFile);
+ }
+
+ protected override string ProcessContent (string content)
+ {
+ using (var host = new FileTemplateHost (Tags)) {
+ string s = srcFile;
+ string output;
+ host.ProcessTemplate (s, content, ref s, out output);
+ if (host.Errors.HasErrors) {
+ foreach (var err in host.Errors)
+ LoggingService.LogError ("Error in template generator: {0}", err.ToString());
+ throw new Exception ("Failed to generate file");
+ }
+ return output;
+ }
+ }
+ }
+
+ public class FileTemplateHost : MonoDevelopTemplatingHost
+ {
+ readonly Dictionary<string,string> tags;
+
+ public FileTemplateHost ()
+ {
+ AddMonoDevelopHostImport ();
+ Imports.Add (typeof (FileTemplateHost).Namespace);
+ Refs.Add (typeof (FileTemplateHost).Assembly.Location);
+ }
+
+ public FileTemplateHost (Dictionary<string,string> tags)
+ {
+ this.tags = tags;
+ }
+
+ //TODO could we expose the tags better e.g. via a directive processor?
+ public string this[string key] {
+ get {
+ string val;
+ if (tags.TryGetValue (key, out val))
+ return val;
+ return null;
+ }
+ }
+
+ protected override string SubstitutePlaceholders (string s)
+ {
+ return StringParserService.Parse (s, tags);
+ }
+ }
+}
+