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

ProjectFile.cs « MonoDevelop.Projects « MonoDevelop.Core « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ad7d99b972e18da956bf6fdaaf1d280f6077cc4c (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
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544

//  ProjectFile.cs
//
// Author:
//   Lluis Sanchez Gual <lluis@novell.com>
//   Viktoria Dudka  <viktoriad@remobjects.com>
// 
// Copyright (c) 2009 Novell, Inc (http://www.novell.com)
// Copyright (c) 2009 RemObjects Software
//
// 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.Diagnostics;
using System.IO;
using MonoDevelop;
using MonoDevelop.Core;
using MonoDevelop.Core.Serialization;
using MonoDevelop.Projects;
using MonoDevelop.Projects.Extensions;
using MonoDevelop.Projects.Formats.MSBuild;

namespace MonoDevelop.Projects
{
	public enum Subtype
	{
		Code,
		Directory
	}

	/// <summary>
	/// This class represent a file information in an IProject object.
	/// </summary>
	public class ProjectFile : ProjectItem, ICloneable, IFileItem, IDisposable
	{
		public ProjectFile ()
		{
		}

		public ProjectFile (string filename): this (filename, MonoDevelop.Projects.BuildAction.Compile)
		{
		}

		public ProjectFile (string filename, string buildAction)
		{
			this.filename = FileService.GetFullPath (filename);
			subtype = Subtype.Code;
			ItemName = buildaction = buildAction;
		}

		public override string Include {
			get {
				if (project != null) {
					string path = MSBuildProjectService.ToMSBuildPath (project.ItemDirectory, FilePath);
					if (path.Length > 0) {
						//directory paths must end with '/'
						if ((Subtype == Subtype.Directory) && path [path.Length - 1] != '\\')
							path = path + "\\";
						return path;
					}
				}
				return base.Include;
			}
			protected set {
				base.Include = value;
			}
		}

		internal protected override void Read (Project project, IMSBuildItemEvaluated buildItem)
		{
			base.Read (project, buildItem);

			if (buildItem.Name == "Folder") {
				// Read folders
				string path = MSBuildProjectService.FromMSBuildPath (project.ItemDirectory, buildItem.Include);
				Name = Path.GetDirectoryName (path);
				Subtype = Subtype.Directory;
				return;
			}

			Name = MSBuildProjectService.FromMSBuildPath (project.ItemDirectory, buildItem.Include);
			BuildAction = buildItem.Name;

			DependsOn = buildItem.Metadata.GetPathValue ("DependentUpon", relativeToPath:FilePath.ParentDirectory);

			string copy = buildItem.Metadata.GetValue ("CopyToOutputDirectory");
			if (!string.IsNullOrEmpty (copy)) {
				switch (copy) {
				case "None": break;
				case "Always": CopyToOutputDirectory = FileCopyMode.Always; break;
				case "PreserveNewest": CopyToOutputDirectory = FileCopyMode.PreserveNewest; break;
				default:
					LoggingService.LogWarning (
						"Unrecognised value {0} for CopyToOutputDirectory MSBuild property",
						copy);
					break;
				}
			}

			Visible = buildItem.Metadata.GetValue ("Visible", true);
			resourceId = buildItem.Metadata.GetValue ("LogicalName");
			contentType = buildItem.Metadata.GetValue ("SubType");
			generator = buildItem.Metadata.GetValue ("Generator");
			customToolNamespace = buildItem.Metadata.GetValue ("CustomToolNamespace");
			lastGenOutput = buildItem.Metadata.GetValue ("LastGenOutput");
			Link = buildItem.Metadata.GetPathValue ("Link", relativeToProject:false);
		}

		internal protected override void Write (MSBuildFileFormat fmt, MSBuildItem buildItem)
		{
			base.Write (fmt, buildItem);

			buildItem.Metadata.SetValue ("DependentUpon", DependsOn, FilePath.Empty, relativeToPath:FilePath.ParentDirectory);
			buildItem.Metadata.SetValue ("SubType", ContentType, "");
			buildItem.Metadata.SetValue ("Generator", Generator, "");
			buildItem.Metadata.SetValue ("CustomToolNamespace", CustomToolNamespace, "");
			buildItem.Metadata.SetValue ("LastGenOutput", LastGenOutput, "");
			buildItem.Metadata.SetValue ("Link", Link, FilePath.Empty, relativeToProject:false);
			buildItem.Metadata.SetValue ("CopyToOutputDirectory", CopyToOutputDirectory.ToString (), "None");
			buildItem.Metadata.SetValue ("Visible", Visible, true);

			var resId = ResourceId;

			//For EmbeddedResource, emit LogicalName only when it does not match the default Id
			if (BuildAction == MonoDevelop.Projects.BuildAction.EmbeddedResource && project.GetDefaultResourceId (this) == resId)
				resId = "";

			buildItem.Metadata.SetValue ("LogicalName", resId, "");
		}


		Subtype subtype;
		public Subtype Subtype {
			get { return subtype; }
			set {
				subtype = value;
				OnChanged ("Subtype");
			}
		}

		public string Name {
			get { return filename; }

			set {
				Debug.Assert (!String.IsNullOrEmpty (value));

				FilePath oldVirtualPath = ProjectVirtualPath;
				FilePath oldPath = filename;

				filename = FileService.GetFullPath (value);

				if (HasChildren) {
					foreach (ProjectFile projectFile in DependentChildren)
						projectFile.dependsOn = Path.GetFileName (FilePath);
				}

				// If the file is a link, rename the link too
				if (IsLink && Link.FileName == oldPath.FileName)
					link = Path.Combine (Path.GetDirectoryName (link), filename.FileName);

				OnPathChanged (oldPath, filename, oldVirtualPath, ProjectVirtualPath);

				if (project != null)
					project.NotifyFileRenamedInProject (new ProjectFileRenamedEventArgs (project, this, oldPath));
			}
		}

		string buildaction = MonoDevelop.Projects.BuildAction.None;
		public string BuildAction {
			get { return buildaction; }
			set {
				buildaction = string.IsNullOrEmpty (value) ? MonoDevelop.Projects.BuildAction.None : value;
				OnChanged ("BuildAction");
			}
		}

		string resourceId = String.Empty;

		internal string GetResourceId (IResourceHandler resourceHandler)
		{
			if (string.IsNullOrEmpty (resourceId))
				return resourceHandler.GetDefaultResourceId (this);
			return resourceId;
		}

		FilePath filename;
		public FilePath FilePath {
			get { return filename; }
		}

		FilePath IFileItem.FileName {
			get { return FilePath; }
		}

		/// <summary>
		/// The file should be treated as effectively having this relative path within the project. If the file is
		/// a link or outside the project root, this will not be the same as the physical file.
		/// </summary>
		public FilePath ProjectVirtualPath {
			get {
				if (!Link.IsNullOrEmpty)
					return Link;
				if (project != null) {
					var rel = project.GetRelativeChildPath (FilePath);
					if (!rel.ToString ().StartsWith ("..", StringComparison.Ordinal))
						return rel;
				}
				return FilePath.FileName;
			}
		}


		Project project;
		public Project Project {
			get { return project; }
		}

		string contentType;
		public string ContentType {
			get { return contentType ?? ""; }
			set {
				contentType = value;
				OnChanged ("ContentType");
			}
		}

		bool visible = true;
		
		/// <summary>
		/// Whether the file should be shown to the user.
		/// </summary>
		public bool Visible {
			get { return visible; }
			set {
				if (visible != value) {
					visible = value;
					OnChanged ("Visible");
				}
			}
		}

		string generator;
		
		/// <summary>
		/// The ID of a custom code generator.
		/// </summary>
		public string Generator {
			get { return generator ?? ""; }
			set {
				if (generator != value) {
					generator = value;
					OnChanged ("Generator");
				}
			}
		}
		
		string customToolNamespace;
		
		/// <summary>
		/// Overrides the namespace in which the custom code generator should generate code.
		/// </summary>
		public string CustomToolNamespace {
			get { return customToolNamespace ?? ""; }
			set {
				if (customToolNamespace != value) {
					customToolNamespace = value;
					OnChanged ("CustomToolNamespace");
				}
			}
		}
		
		
		string lastGenOutput;
		
		/// <summary>
		/// The file most recently generated by the custom tool. Relative to this file's parent directory.
		/// </summary>
		public string LastGenOutput {
			get { return lastGenOutput ?? ""; }
			set {
				if (lastGenOutput != value) {
					lastGenOutput = value;
					OnChanged ("LastGenOutput");
				}
			}
		}
		
		string link;
		
		/// <summary>
		/// If the file's real path is outside the project root, this value can be used to set its virtual path
		/// within the project root. Use ProjectVirtualPath to read the effective virtual path for any file.
		/// </summary>
		public FilePath Link {
			get { return link ?? ""; }
			set {
				if (link != value) {
					if (value.IsAbsolute || value.ToString ().StartsWith ("..", StringComparison.Ordinal))
						throw new ArgumentException ("value");

					var oldLink = link;
					link = value;

					OnVirtualPathChanged (oldLink, link);
					OnChanged ("Link");
				}
			}
		}
		
		/// <summary>
		/// Whether the file is a link.
		/// </summary>
		public bool IsLink {
			get {
				return !Link.IsNullOrEmpty || (project != null && !FilePath.IsChildPathOf (project.BaseDirectory));
			}
		}
		
		/// <summary>
		/// Whether the file is outside the project base directory.
		/// </summary>
		public bool IsExternalToProject {
			get {
				return !FilePath.IsChildPathOf (project.BaseDirectory);
			}
		}

		FileCopyMode copyToOutputDirectory = FileCopyMode.None;
		public FileCopyMode CopyToOutputDirectory {
			get { return copyToOutputDirectory; }
			set {
				if (copyToOutputDirectory != value) {
					copyToOutputDirectory = value;
					OnChanged ("CopyToOutputDirectory");
				}
			}
		}

		#region File grouping
		string dependsOn;
		public string DependsOn {
			get { return dependsOn ?? ""; }

			set {
				if (dependsOn != value) {
					var oldPath = !string.IsNullOrEmpty (dependsOn)
						? FilePath.ParentDirectory.Combine (Path.GetFileName (dependsOn))
						: FilePath.Empty;
					dependsOn = value;
	
					if (dependsOnFile != null) {
						dependsOnFile.dependentChildren.Remove (this);
						dependsOnFile = null;
					}
	
					if (project != null && value != null)
						project.UpdateDependency (this, oldPath);
	
					OnChanged ("DependsOn");
				}
			}
		}

		ProjectFile dependsOnFile;
		public ProjectFile DependsOnFile {
			get { return dependsOnFile; }
			internal set { dependsOnFile = value; }
		}

		List<ProjectFile> dependentChildren;
		public bool HasChildren {
			get { return dependentChildren != null && dependentChildren.Count > 0; }
		}

		public IList<ProjectFile> DependentChildren {
			get { return ((IList<ProjectFile>)dependentChildren) ?? new ProjectFile[0]; }
		}

		internal FilePath DependencyPath {
			get {
				return FilePath.ParentDirectory.Combine (Path.GetFileName (DependsOn));
			}
		}

		internal bool ResolveParent (ProjectFile potentialParentFile)
		{
			if (potentialParentFile.FilePath == DependencyPath) {
				dependsOnFile = potentialParentFile;
				if (dependsOnFile.dependentChildren == null)
					dependsOnFile.dependentChildren = new List<ProjectFile> ();
				dependsOnFile.dependentChildren.Add (this);
				return true;
			}
			return false;
		}

		internal bool ResolveParent ()
		{
			if (dependsOnFile == null && (!string.IsNullOrEmpty (dependsOn) && project != null)) {
				//NOTE also that the dependent files are always assumed to be in the same directory
				//This matches VS behaviour
				var parentPath = DependencyPath;

				//don't allow cyclic references
				if (parentPath == FilePath) {
					LoggingService.LogWarning (
						"Cyclic dependency in project '{0}': file '{1}' depends on '{2}'",
						project == null ? "(none)" : project.Name, FilePath, parentPath
					);
					return true;
				}

				dependsOnFile = project.Files.GetFile (parentPath);
				if (dependsOnFile != null) {
					if (dependsOnFile.dependentChildren == null)
						dependsOnFile.dependentChildren = new List<ProjectFile> ();
					dependsOnFile.dependentChildren.Add (this);
					return true;
				} else {
					return false;
				}
			} else {
				return true;
			}
		}
		#endregion

		// FIXME: rename this to LogicalName for a better mapping to the MSBuild property
		public string ResourceId {
			get {
				if (BuildAction == MonoDevelop.Projects.BuildAction.EmbeddedResource && string.IsNullOrEmpty (resourceId) && project is DotNetProject)
					return ((DotNetProject)project).ResourceHandler.GetDefaultResourceId (this);

				return resourceId;
			}
			set {
				if (resourceId != value) {
					var oldVal = ResourceId;
					resourceId = value;
					if (ResourceId != oldVal)
						OnChanged ("ResourceId");
				}
			}
		}

		internal void SetProject (Project project)
		{
			this.project = project;
			if (project != null)
				OnVirtualPathChanged (FilePath.Null, ProjectVirtualPath);
		}

		public override string ToString ()
		{
			return "[ProjectFile: FileName=" + filename + "]";
		}

		public object Clone ()
		{
			ProjectFile pf = (ProjectFile)MemberwiseClone ();
			pf.dependsOnFile = null;
			pf.dependentChildren = null;
			pf.project = null;
			pf.VirtualPathChanged = null;
			pf.PathChanged = null;
			return pf;
		}

		public virtual void Dispose ()
		{
		}

		internal event EventHandler<ProjectFileVirtualPathChangedEventArgs> VirtualPathChanged;

		void OnVirtualPathChanged (FilePath oldVirtualPath, FilePath newVirtualPath)
		{
			var handler = VirtualPathChanged;

			if (handler != null)
				handler (this, new ProjectFileVirtualPathChangedEventArgs (this, oldVirtualPath, newVirtualPath));
		}

		internal event EventHandler<ProjectFilePathChangedEventArgs> PathChanged;

		void OnPathChanged (FilePath oldPath, FilePath newPath, FilePath oldVirtualPath, FilePath newVirtualPath)
		{
			var handler = PathChanged;

			if (handler != null)
				handler (this, new ProjectFilePathChangedEventArgs (this, oldPath, newPath, oldVirtualPath, newVirtualPath));
		}

		protected virtual void OnChanged (string property)
		{
			if (project != null)
				project.NotifyFilePropertyChangedInProject (this, property);
		}
	}

	internal class ProjectFileVirtualPathChangedEventArgs : EventArgs
	{
		public ProjectFileVirtualPathChangedEventArgs (ProjectFile projectFile, FilePath oldPath, FilePath newPath)
		{
			ProjectFile = projectFile;
			OldVirtualPath = oldPath;
			NewVirtualPath = newPath;
		}

		public ProjectFile ProjectFile { get; private set; }
		public FilePath OldVirtualPath { get; private set; }
		public FilePath NewVirtualPath { get; private set; }
	}

	internal class ProjectFilePathChangedEventArgs : ProjectFileVirtualPathChangedEventArgs
	{
		public ProjectFilePathChangedEventArgs (ProjectFile projectFile, FilePath oldPath, FilePath newPath, FilePath oldVirtualPath, FilePath newVirtualPath) : base (projectFile, oldVirtualPath, newVirtualPath)
		{
			OldPath = oldPath;
			NewPath = newPath;
		}

		public FilePath OldPath { get; private set; }
		public FilePath NewPath { get; private set; }
	}
}