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

RequiresProvidesDirectiveProcessor.cs « Microsoft.VisualStudio.TextTemplating « Mono.TextTemplating « TextTemplating « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bb48357c32a670619e70eb6fa540e079bd7c146f (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// 
// RequiresProvidesDirectiveProcessor.cs
//  
// Author:
//       Michael Hutchinson <mhutchinson@novell.com>
// 
// Copyright (c) 2009 Novell, Inc. (http://www.novell.com)
// 
// 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.CodeDom.Compiler;
using System.Text;

namespace Microsoft.VisualStudio.TextTemplating
{
	
	
	public abstract class RequiresProvidesDirectiveProcessor : DirectiveProcessor
	{
		bool isInProcessingRun;
		ITextTemplatingEngineHost host;
		StringBuilder preInitBuffer = new StringBuilder ();
		StringBuilder postInitBuffer = new StringBuilder ();
		StringBuilder codeBuffer = new StringBuilder ();
		CodeDomProvider languageProvider;
		
		protected RequiresProvidesDirectiveProcessor ()
		{
		}
		
		public override void Initialize (ITextTemplatingEngineHost host)
		{
			base.Initialize (host);
			this.host = host;
		}
		
		protected abstract void InitializeProvidesDictionary (string directiveName, IDictionary<string, string> providesDictionary);
		protected abstract void InitializeRequiresDictionary (string directiveName, IDictionary<string, string> requiresDictionary);
		protected abstract string FriendlyName { get; }
		
		protected abstract void GeneratePostInitializationCode (string directiveName, StringBuilder codeBuffer, CodeDomProvider languageProvider, 
			IDictionary<string, string> requiresArguments, IDictionary<string, string> providesArguments);
		protected abstract void GeneratePreInitializationCode (string directiveName, StringBuilder codeBuffer, CodeDomProvider languageProvider,
			IDictionary<string, string> requiresArguments, IDictionary<string, string> providesArguments);
		protected abstract void GenerateTransformCode (string directiveName, StringBuilder codeBuffer, CodeDomProvider languageProvider,
			IDictionary<string, string> requiresArguments, IDictionary<string, string> providesArguments);
		
		protected virtual void PostProcessArguments (string directiveName, IDictionary<string, string> requiresArguments,
			IDictionary<string, string> providesArguments)
		{
		}
		
		public override string GetClassCodeForProcessingRun ()
		{
			AssertNotProcessing ();
			return codeBuffer.ToString ();
		}
		
		public override string[] GetImportsForProcessingRun ()
		{
			AssertNotProcessing ();
			return null;
		}
		
		public override string[] GetReferencesForProcessingRun ()
		{
			AssertNotProcessing ();
			return null;
		}
		
		public override string GetPostInitializationCodeForProcessingRun ()
		{
			AssertNotProcessing ();
			return postInitBuffer.ToString ();
		}
		
		public override string GetPreInitializationCodeForProcessingRun ()
		{
			AssertNotProcessing ();
			return preInitBuffer.ToString ();
		}
		
		public override void StartProcessingRun (CodeDomProvider languageProvider, string templateContents, CompilerErrorCollection errors)
		{
			AssertNotProcessing ();
			isInProcessingRun = true;
			base.StartProcessingRun (languageProvider, templateContents, errors);
			
			this.languageProvider = languageProvider;
			codeBuffer.Length = 0;
			preInitBuffer.Length = 0;
			postInitBuffer.Length = 0;
		}
		
		public override void FinishProcessingRun ()
		{
			isInProcessingRun = false;
		}
		
		void AssertNotProcessing ()
		{
			if (isInProcessingRun)
				throw new InvalidOperationException ();
		}
		
		//FIXME: handle escaping
		IEnumerable<KeyValuePair<string,string>> ParseArgs (string args)
		{
			var pairs = args.Split (';');
			foreach (var p in pairs) {
				int eq = p.IndexOf ('=');
				var k = p.Substring (0, eq);
				var v = p.Substring (eq);
				yield return new KeyValuePair<string, string> (k, v);
			}
		}
		
		public override void ProcessDirective (string directiveName, IDictionary<string, string> arguments)
		{
			if (directiveName == null)
				throw new ArgumentNullException ("directiveName");
			if (arguments == null)
				throw new ArgumentNullException ("arguments");
			
			var providesDictionary = new Dictionary<string,string> ();
			var requiresDictionary = new Dictionary<string,string> ();
			
			string provides;
			if (arguments.TryGetValue ("provides", out provides)) {
				foreach (var arg in ParseArgs (provides)) {
					providesDictionary.Add (arg.Key, arg.Value);
				}
			}
			
			string requires;
			if (arguments.TryGetValue ("requires", out requires)) {
				foreach (var arg in ParseArgs (requires)) {
					requiresDictionary.Add (arg.Key, arg.Value);
				}
			}
			
			InitializeRequiresDictionary (directiveName, requiresDictionary);
			InitializeProvidesDictionary (directiveName, providesDictionary);
			
			var id = ProvideUniqueId (directiveName, arguments, requiresDictionary, providesDictionary);
			
			foreach (var req in requiresDictionary) {
				var val = host.ResolveParameterValue (id, FriendlyName, req.Key);
				if (val != null)
					requiresDictionary[req.Key] = val; 
				else if (req.Value == null)
					throw new DirectiveProcessorException ("Could not resolve required value '" + req.Key + "'");
			}
			
			foreach (var req in providesDictionary) {
				var val = host.ResolveParameterValue (id, FriendlyName, req.Key);
				if (val != null)
					providesDictionary[req.Key] = val;
			}
			
			PostProcessArguments (directiveName, requiresDictionary, providesDictionary);
			
			GeneratePreInitializationCode (directiveName, preInitBuffer, languageProvider, requiresDictionary, providesDictionary);
			GeneratePostInitializationCode (directiveName, postInitBuffer, languageProvider, requiresDictionary, providesDictionary);
			GenerateTransformCode (directiveName, codeBuffer, languageProvider, requiresDictionary, providesDictionary);
		}
		
		protected virtual string ProvideUniqueId (string directiveName, IDictionary<string, string> arguments,
			IDictionary<string, string> requiresArguments, IDictionary<string, string> providesArguments)
		{
			return directiveName;
		}
		
		protected ITextTemplatingEngineHost Host {
			get { return host; }
		}
	}
}