~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//
// SingleFileDescriptionTemplate.cs
//
// Author:
//   Lluis Sanchez Gual
//
// Copyright (C) 2006 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.IO;
using System.Xml;
using System.Collections;
using System.Collections.Generic;

using MonoDevelop.Core;
using MonoDevelop.Projects;
using MonoDevelop.Projects.Text;
using MonoDevelop.Ide.Gui;
using MonoDevelop.Ide.StandardHeader;
using System.Text;
using MonoDevelop.Ide.Gui.Content;
using MonoDevelop.Ide.CodeFormatting;

namespace MonoDevelop.Ide.Templates
{
	public class SingleFileDescriptionTemplate: FileDescriptionTemplate
	{
		string name;
		string defaultName;
		string defaultExtension;
		string generatedFile;
		bool suppressAutoOpen = false;
		bool addStandardHeader = false;
		string dependsOn;
		string buildAction;
		string customTool;
		List<string> references = new List<string> ();
		
		public override void Load (XmlElement filenode, FilePath baseDirectory)
		{
			name = filenode.GetAttribute ("name");
			defaultName = filenode.GetAttribute ("DefaultName");
			defaultExtension = filenode.GetAttribute ("DefaultExtension");
			dependsOn = filenode.GetAttribute ("DependsOn");
			customTool = filenode.GetAttribute ("CustomTool");
			
			buildAction = BuildAction.Compile;
			buildAction = filenode.GetAttribute ("BuildAction");
			if (string.IsNullOrEmpty (buildAction))
				buildAction = null;
			
			string suppressAutoOpenStr = filenode.GetAttribute ("SuppressAutoOpen");
			if (!string.IsNullOrEmpty (suppressAutoOpenStr)) {
				try {
					suppressAutoOpen = bool.Parse (suppressAutoOpenStr);
				} catch (FormatException) {
					throw new InvalidOperationException ("Invalid value for SuppressAutoOpen in template.");
				}
			}
			
			string addStandardHeaderStr = filenode.GetAttribute ("AddStandardHeader");
			if (!string.IsNullOrEmpty (addStandardHeaderStr)) {
				try {
					addStandardHeader = bool.Parse (addStandardHeaderStr);
				} catch (FormatException) {
					throw new InvalidOperationException ("Invalid value for AddStandardHeader in template.");
				}
			}
			
			foreach (XmlElement elem in filenode.SelectNodes ("AssemblyReference")) {
				string aref = elem.InnerText.Trim ();
				if (!string.IsNullOrEmpty (aref))
					references.Add (aref);
			}
		}
		
		public override string Name {
			get { return name; } 
		}
		
		public bool AddStandardHeader {
			get { return addStandardHeader; }
			set { addStandardHeader = value; }
		}
		
		public sealed override bool AddToProject (SolutionItem policyParent, Project project, string language, string directory, string name)
		{
			return AddFileToProject (policyParent, project, language, directory, name) != null;
		}
		
		public ProjectFile AddFileToProject (SolutionItem policyParent, Project project, string language, string directory, string name)
		{
			generatedFile = SaveFile (policyParent, project, language, directory, name);
			if (generatedFile != null) {		
				string buildAction = this.buildAction ?? project.GetDefaultBuildAction (generatedFile);
				ProjectFile projectFile = project.AddFile (generatedFile, buildAction);
				
				if (!string.IsNullOrEmpty (dependsOn)) {
					Dictionary<string,string> tags = new Dictionary<string,string> ();
					ModifyTags (policyParent, project, language, name, generatedFile, ref tags);
					string parsedDepName = StringParserService.Parse (dependsOn, tags);
					if (projectFile.DependsOn != parsedDepName)
						projectFile.DependsOn = parsedDepName;
				}
				
				if (!string.IsNullOrEmpty (customTool))
					projectFile.Generator = customTool;
				
				DotNetProject netProject = project as DotNetProject;
				if (netProject != null) {
					// Add required references
					foreach (string aref in references) {
						string res = netProject.AssemblyContext.GetAssemblyFullName (aref, netProject.TargetFramework);
						res = netProject.AssemblyContext.GetAssemblyNameForVersion (res, netProject.TargetFramework);
						if (!ContainsReference (netProject, res))
							netProject.References.Add (new ProjectReference (ReferenceType.Package, aref));
					}
				}
				
				return projectFile;
			} else
				return null;
		}
		
		public override bool SupportsProject (Project project, string projectPath)
		{
			DotNetProject netProject = project as DotNetProject;
			if (netProject != null) {
				// Ensure that the references are valid inside the project's target framework.
				foreach (string aref in references) {
					string res = netProject.AssemblyContext.GetAssemblyFullName (aref, netProject.TargetFramework);
					if (string.IsNullOrEmpty (res))
						return false;
					res = netProject.AssemblyContext.GetAssemblyNameForVersion (res, netProject.TargetFramework);
					if (string.IsNullOrEmpty (res))
						return false;
				}
			}
			
			return true;
		}
		
		bool ContainsReference (DotNetProject project, string aref)
		{
			if (string.IsNullOrEmpty (aref))
				return false;
			string aname;
			int i = aref.IndexOf (',');
			if (i == -1)
				aname = aref;
			else
				aname = aref.Substring (0, i);
			foreach (ProjectReference pr in project.References) {
				if (pr.ReferenceType == ReferenceType.Package && (pr.Reference == aname || pr.Reference.StartsWith (aname + ",")) || 
					pr.ReferenceType != ReferenceType.Package && pr.Reference.Contains (aname))
					return true;
			}
			return false;
		}
		
		public override void Show ()
		{
			if (!suppressAutoOpen)
				IdeApp.Workbench.OpenDocument (generatedFile);
		}
		
		// Creates a file and saves it to disk. Returns the path to the new file
		// All parameters are optional (can be null)
		public string SaveFile (SolutionItem policyParent, Project project, string language, string baseDirectory, string entryName)
		{
			string file = GetFileName (policyParent, project, language, baseDirectory, entryName);
			
			if (File.Exists (file)) {
				if (!MessageService.Confirm (GettextCatalog.GetString ("File already exists"),
				                                GettextCatalog.GetString ("File {0} already exists. Do you want to overwrite\nthe existing file?", file),
				                                AlertButton.OverwriteFile)) {
					return null;
				}
			}
			
			if (!Directory.Exists (Path.GetDirectoryName (file)))
				Directory.CreateDirectory (Path.GetDirectoryName (file));
					
			Stream stream = CreateFileContent (policyParent, project, language, file, entryName);
			
			byte[] buffer = new byte [2048];
			int nr;
			FileStream fs = null;
			try {
				fs = File.Create (file);
				while ((nr = stream.Read (buffer, 0, 2048)) > 0)
					fs.Write (buffer, 0, nr);
			} finally {
				stream.Close ();
				if (fs != null)
					fs.Close ();
			}
			return file;
		}
		
		// Returns the name of the file that this template generates.
		// All parameters are optional (can be null)
		public virtual string GetFileName (SolutionItem policyParent, Project project, string language, string baseDirectory, string entryName)
		{
			if (string.IsNullOrEmpty (entryName) && !string.IsNullOrEmpty (defaultName))
				entryName = defaultName;
			
			string fileName = entryName;
			
			//substitute tags
			if ((name != null) && (name.Length > 0)) {
				Dictionary<string,string> tags = new Dictionary<string,string> ();
				ModifyTags (policyParent, project, language, entryName ?? name, null, ref tags);
				fileName = StringParserService.Parse (name, tags);
			}
			
			if (fileName == null)
				throw new InvalidOperationException ("File name not provided in template");
			
			//give it an extension if it didn't get one (compatibility with pre-substition behaviour)
			if (Path.GetExtension (fileName).Length == 0) {
				if (!string.IsNullOrEmpty  (defaultExtension)) {
					fileName = fileName + defaultExtension;
				}
				else if (!string.IsNullOrEmpty  (language)) {
					var languageBinding = GetLanguageBinding (language);
					fileName = languageBinding.GetFileName (fileName);
				} 
			}
			
			if (baseDirectory != null)
				fileName = Path.Combine (baseDirectory, fileName);

			return fileName;
		}
		
		// Returns a stream with the content of the file.
		// project and language parameters are optional
		public virtual Stream CreateFileContent (SolutionItem policyParent, Project project, string language, string fileName, string identifier)
		{
			Dictionary<string, string> tags = new Dictionary<string, string> ();
			ModifyTags (policyParent, project, language, identifier, fileName, ref tags);
			
			string content = CreateContent (project, tags, language);
			content = StringParserService.Parse (content, tags);
			string mime = DesktopService.GetMimeTypeForUri (fileName);
			CodeFormatter formatter = !string.IsNullOrEmpty (mime) ? CodeFormatterService.GetFormatter (mime) : null;
			
			if (formatter != null) {
				var formatted = formatter.FormatText (policyParent != null ? policyParent.Policies : null, content);
				if (formatted != null)
					content = formatted;
			}
			
			MemoryStream ms = new MemoryStream ();
			byte[] data;
			if (AddStandardHeader) {
				string header = StandardHeaderService.GetHeader (policyParent, fileName, true);
				data = System.Text.Encoding.UTF8.GetBytes (header);
				ms.Write (data, 0, data.Length);
			}
			
			Mono.TextEditor.TextDocument doc = new Mono.TextEditor.TextDocument ();
			doc.Text = content;
			
			TextStylePolicy textPolicy = policyParent != null ? policyParent.Policies.Get<TextStylePolicy> ("text/plain")
				: MonoDevelop.Projects.Policies.PolicyService.GetDefaultPolicy<TextStylePolicy> ("text/plain");
			string eolMarker = TextStylePolicy.GetEolMarker (textPolicy.EolMarker);
			byte[] eolMarkerBytes = System.Text.Encoding.UTF8.GetBytes (eolMarker);
			
			var tabToSpaces = textPolicy.TabsToSpaces? new string (' ', textPolicy.TabWidth) : null;
			
			foreach (Mono.TextEditor.DocumentLine line in doc.Lines) {
				var lineText = doc.GetTextAt (line.Offset, line.Length);
				if (tabToSpaces != null)
					lineText = lineText.Replace ("\t", tabToSpaces);
				data = System.Text.Encoding.UTF8.GetBytes (lineText);
				ms.Write (data, 0, data.Length);
				ms.Write (eolMarkerBytes, 0, eolMarkerBytes.Length);
			}
			
			ms.Position = 0;
			return ms;
		}
		
		// Creates the text content of the file
		// The Language parameter is optional

		public virtual string CreateContent (Project project, Dictionary<string,string> tags, string language)
		{
			return CreateContent (language);
		}
		
		public virtual string CreateContent (string language)
		{
			return string.Empty;
		}
		
		// Can add tags for substitution based on project, language or filename.
		// If overriding but still want base implementation's tags, should invoke base method.
		// We supply defaults whenever it is possible, to avoid having unsubstituted tags. However,
		// do not substitute blanks when a sensible default cannot be guessed, because they result
		//in less obvious errors.
		public virtual void ModifyTags (SolutionItem policyParent, Project project, string language,
			string identifier, string fileName, ref Dictionary<string,string> tags)
		{
			DotNetProject netProject = project as DotNetProject;
			string languageExtension = "";
			ILanguageBinding binding = null;
			if (!string.IsNullOrEmpty (language)) {
				binding = GetLanguageBinding (language);
				if (binding != null)
					languageExtension = Path.GetExtension (binding.GetFileName ("Default")).Remove (0, 1);
			}
			
			//need a default namespace or if there is no project, substitutions can get very messed up
			string ns = netProject != null ? netProject.GetDefaultNamespace (fileName) : "Application";
			
			//need an 'identifier' for tag substitution, e.g. class name or page name
			//if not given an identifier, use fileName
			if ((identifier == null) && (fileName != null))
				identifier = Path.GetFileName (fileName);
			 
			 if (identifier != null) {
			 	//remove all extensions
				while (Path.GetExtension (identifier).Length > 0)
					identifier = Path.GetFileNameWithoutExtension (identifier);
			 	identifier = CreateIdentifierName (identifier);
				tags ["Name"] = identifier;
				tags ["FullName"] = ns.Length > 0 ? ns + "." + identifier : identifier;
				
				//some .NET languages may be able to use keywords as identifiers if they're escaped
				IDotNetLanguageBinding dnb = binding as IDotNetLanguageBinding;
				if (dnb != null) {
					System.CodeDom.Compiler.CodeDomProvider provider = dnb.GetCodeDomProvider ();
					if (provider != null) {
						tags ["EscapedIdentifier"] = provider.CreateEscapedIdentifier (identifier);
					}
				}
			}
			
			tags ["Namespace"] = ns;
			if (policyParent != null)
				tags ["SolutionName"] = policyParent.Name;
			if (project != null) {
				tags ["ProjectName"] = project.Name;
				tags ["SafeProjectName"] = CreateIdentifierName (project.Name);
				var info = project.AuthorInformation ?? AuthorInformation.Default;
				tags ["AuthorCopyright"] = info.Copyright;
				tags ["AuthorCompany"] = info.Company;
				tags ["AuthorTrademark"] = info.Trademark;
				tags ["AuthorEmail"] = info.Email;
				tags ["AuthorName"] = info.Name;
			}
			if ((language != null) && (language.Length > 0))
				tags ["Language"] = language;
			if (languageExtension.Length > 0)
				tags ["LanguageExtension"] = languageExtension;
			
			if (fileName != FilePath.Null) {
				FilePath fileDirectory = Path.GetDirectoryName (fileName);
				if (project != null && project.BaseDirectory != FilePath.Null && fileDirectory.IsChildPathOf (project.BaseDirectory))
					tags ["ProjectRelativeDirectory"] = fileDirectory.ToRelative (project.BaseDirectory);
				else
					tags ["ProjectRelativeDirectory"] = fileDirectory;
				
				tags ["FileNameWithoutExtension"] = Path.GetFileNameWithoutExtension (fileName); 
				tags ["Directory"] = fileDirectory;
				tags ["FileName"] = fileName;
			}
		}
		
		static string CreateIdentifierName (string identifier)
		{
			StringBuilder result = new StringBuilder ();
			for (int i = 0; i < identifier.Length; i++) {
				char ch = identifier[i];
				if (i != 0 && Char.IsLetterOrDigit (ch) || i == 0 && Char.IsLetter (ch) || ch == '_') {
					result.Append (ch);
				} else {
					result.Append ('_');
				}
			}
			return result.ToString ();
		}

		
		protected ILanguageBinding GetLanguageBinding (string language)
		{
			var binding = LanguageBindingService.GetBindingPerLanguageName (language);
			if (binding == null)
				throw new InvalidOperationException ("Language '" + language + "' not found");
			return binding;
		}
	}
}