2
// TextTemplatingTool.cs
5
// Michael Hutchinson <mhutchinson@novell.com>
7
// Copyright (c) 2010 Novell, Inc.
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
using MonoDevelop.Ide.CustomTools;
29
using System.CodeDom.Compiler;
30
using MonoDevelop.Projects;
32
using Mono.TextTemplating;
33
using MonoDevelop.Core;
34
using System.Threading;
36
namespace MonoDevelop.TextTemplating
38
public class TextTemplatingTool : ISingleFileCustomTool
40
public IAsyncOperation Generate (IProgressMonitor monitor, ProjectFile file, SingleFileCustomToolResult result)
42
return new ThreadAsyncOperation (delegate {
43
using (var h = TextTemplatingService.GetTemplatingDomain ()) {
44
var host = (MonoDevelopTemplatingHost) h.Domain.CreateInstanceAndUnwrap (
45
typeof (MonoDevelopTemplatingHost).Assembly.FullName,
46
typeof (MonoDevelopTemplatingHost).FullName);
47
var defaultOutputName = file.FilePath.ChangeExtension (".cs"); //cs extension for VS compat
48
host.ProcessTemplate (file.FilePath, defaultOutputName);
49
result.GeneratedFilePath = host.OutputFile;
50
result.Errors.AddRange (host.Errors);
51
foreach (var err in host.Errors)
52
monitor.Log.WriteLine (err.ToString ());
58
public class ThreadAsyncOperation : IAsyncOperation
62
SingleFileCustomToolResult result;
65
public ThreadAsyncOperation (Action task, SingleFileCustomToolResult result)
68
throw new ArgumentNullException ("result");
72
thread = new Thread (Run);
80
} catch (ThreadAbortException ex) {
81
result.UnhandledException = ex;
83
} catch (Exception ex) {
84
result.UnhandledException = ex;
86
if (Completed != null)
90
public event OperationHandler Completed;
97
public void WaitForCompleted ()
102
public bool IsCompleted {
103
get { return !thread.IsAlive; }
106
public bool Success {
107
get { return !cancelled && result.Success; }
110
public bool SuccessWithWarnings {
111
get { return !cancelled && result.SuccessWithWarnings; }
115
class MonoDevelopTemplatingHost : TemplateGenerator