forked from icsharpcode/SharpDevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
576 lines (503 loc) · 18.5 KB
/
Copy pathSolution.cs
File metadata and controls
576 lines (503 loc) · 18.5 KB
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// 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.ComponentModel;
using System.Drawing.Design;
using System.IO;
using System.Linq;
using System.Windows.Threading;
using System.Xml;
using ICSharpCode.Core;
using ICSharpCode.NRefactory;
using ICSharpCode.SharpDevelop.Dom;
using ICSharpCode.SharpDevelop.Gui;
using ICSharpCode.SharpDevelop.Workbench;
namespace ICSharpCode.SharpDevelop.Project
{
class Solution : SolutionFolder, ISolution
{
FileName fileName;
FileName sdSettingsFileName;
DirectoryName directory;
readonly IProjectChangeWatcher changeWatcher;
readonly IFileService fileService;
internal Version currVSVersion, minVSVersion;
static readonly Version DefaultVSVersion = new Version(12, 0, 20827, 3);
static readonly Version DefaultMinVSVersion = new Version(10, 0, 40219, 1);
public Solution(FileName fileName, IProjectChangeWatcher changeWatcher, IFileService fileService)
{
this.changeWatcher = changeWatcher;
this.fileService = fileService;
this.ConfigurationNames = new SolutionConfigurationOrPlatformNameCollection(this, false);
this.PlatformNames = new SolutionConfigurationOrPlatformNameCollection(this, true);
this.projects = new SynchronizedModelCollection<IProject>(new ProjectModelCollection(this));
this.FileName = fileName;
this.sdSettingsFileName = new FileName(fileName + ".sdsettings");
base.Name = fileName.GetFileNameWithoutExtension();
this.globalSections = new SynchronizedModelCollection<SolutionSection>(new NullSafeSimpleModelCollection<SolutionSection>());
globalSections.CollectionChanged += OnSolutionSectionCollectionChanged;
fileService.FileRenamed += FileServiceFileRenamed;
fileService.FileRemoved += FileServiceFileRemoved;
changeWatcher.Enable();
}
public void Dispose()
{
fileService.FileRenamed -= FileServiceFileRenamed;
fileService.FileRemoved -= FileServiceFileRemoved;
changeWatcher.Dispose();
foreach (var project in this.Projects) {
project.Dispose();
}
}
/// <summary>
/// Gets whether the solution is open in the IDE.
/// </summary>
internal bool IsLoaded {
get { return SD.ProjectService.CurrentSolution == this; }
}
#region FileName
public event EventHandler FileNameChanged = delegate { };
public FileName FileName {
get { return fileName; }
private set {
if (fileName != value) {
this.fileName = value;
this.directory = value.GetParentDirectory();
UpdateMSBuildProperties();
FileNameChanged(this, EventArgs.Empty);
}
}
}
public DirectoryName Directory {
get { return directory; }
}
public override string Name {
get {
return base.Name;
}
set {
var newFileName = directory.CombineFile(value + ".sln");
changeWatcher.Disable();
try {
if (!FileService.RenameFile(fileName, newFileName, false)) {
return;
}
base.Name = value;
this.FileName = newFileName;
changeWatcher.Rename(newFileName);
} finally {
changeWatcher.Enable();
}
}
}
#endregion
#region StartupProject
public event EventHandler StartupProjectChanged = delegate { };
IProject startupProject;
[Browsable(false)]
public IProject StartupProject {
get {
if (startupProject == null) {
startupProject = AutoDetectStartupProject();
}
return startupProject;
}
set {
if (value == null || value.ParentSolution != this)
throw new ArgumentException();
if (startupProject != value) {
startupProject = value;
preferences.Set("StartupProject", value.IdGuid.ToString());
StartupProjectChanged(this, EventArgs.Empty);
}
}
}
IProject AutoDetectStartupProject()
{
string startupProjectGuidText = preferences.Get("StartupProject", string.Empty);
Guid startupProjectGuid;
if (Guid.TryParse(startupProjectGuidText, out startupProjectGuid)) {
var project = projects.FirstOrDefault(p => p.IdGuid == startupProjectGuid);
if (project != null)
return project;
}
return projects.FirstOrDefault(p => p.IsStartable);
}
#endregion
#region Project list
readonly IMutableModelCollection<IProject> projects; // = new SynchronizedModelCollection<IProject>(new ProjectModelCollection(this));
sealed class ProjectModelCollection : SimpleModelCollection<IProject>
{
Solution solution;
public ProjectModelCollection(Solution solution)
{
this.solution = solution;
}
protected override void OnCollectionChanged(IReadOnlyCollection<IProject> removedItems, IReadOnlyCollection<IProject> addedItems)
{
foreach (var project in addedItems) {
project.ProjectSections.CollectionChanged += solution.OnSolutionSectionCollectionChanged;
project.ConfigurationMapping.Changed += solution.OnProjectConfigurationMappingChanged;
solution.OnSolutionSectionCollectionChanged(EmptyList<SolutionSection>.Instance, project.ProjectSections);
}
foreach (var project in removedItems) {
project.ProjectSections.CollectionChanged -= solution.OnSolutionSectionCollectionChanged;
project.ConfigurationMapping.Changed -= solution.OnProjectConfigurationMappingChanged;
solution.OnSolutionSectionCollectionChanged(project.ProjectSections, EmptyList<SolutionSection>.Instance);
}
// If the startup project was removed, reset that property
bool startupProjectWasRemoved = removedItems.Contains(solution.startupProject);
if (startupProjectWasRemoved)
solution.startupProject = null; // this will force auto-detection on the next property access
base.OnCollectionChanged(removedItems, addedItems);
// After the event is raised; dispose any removed projects.
// Note that this method is only called at the end of a batch update.
// When moving a project from one folder to another, a batch update
// must be used to prevent the project from being disposed.
foreach (var project in removedItems)
project.Dispose();
if (startupProjectWasRemoved || (solution.startupProject == null && addedItems.Contains(solution.AutoDetectStartupProject())))
solution.StartupProjectChanged(solution, EventArgs.Empty);
}
}
public IModelCollection<IProject> Projects {
get { return projects; }
}
internal IDisposable ReportBatch()
{
return projects.BatchUpdate();
}
internal void ReportRemovedItem(ISolutionItem oldItem)
{
if (oldItem is ISolutionFolder) {
// recurse into removed folders
foreach (var childItem in ((ISolutionFolder)oldItem).Items) {
ReportRemovedItem(childItem);
}
} else if (oldItem is IProject) {
projects.Remove((IProject)oldItem);
}
}
internal void ReportAddedItem(ISolutionItem newItem)
{
if (newItem is ISolutionFolder) {
// recurse into added folders
foreach (var childItem in ((ISolutionFolder)newItem).Items) {
ReportAddedItem(childItem);
}
} else if (newItem is IProject) {
projects.Add((IProject)newItem);
}
}
#endregion
[Browsable(false)]
public IEnumerable<ISolutionItem> AllItems {
get {
return this.Items.Flatten(i => i is ISolutionFolder ? ((ISolutionFolder)i).Items : null);
}
}
readonly IMutableModelCollection<SolutionSection> globalSections;
public IMutableModelCollection<SolutionSection> GlobalSections {
get { return globalSections; }
}
void OnProjectConfigurationMappingChanged(object sender, EventArgs e)
{
this.IsDirty = true;
}
void OnSolutionSectionCollectionChanged(IReadOnlyCollection<SolutionSection> oldItems, IReadOnlyCollection<SolutionSection> newItems)
{
this.IsDirty = true;
foreach (var section in oldItems) {
section.Changed -= OnSolutionSectionChanged;
}
foreach (var section in newItems) {
section.Changed += OnSolutionSectionChanged;
}
}
void OnSolutionSectionChanged(object sender, EventArgs e)
{
this.IsDirty = true;
}
public ISolutionItem GetItemByGuid(Guid guid)
{
// Maybe we should maintain a dictionary to make these lookups faster?
// But I don't think lookups by GUID are commonly used...
return this.AllItems.FirstOrDefault(i => i.IdGuid == guid);
}
#region Preferences
Properties preferences = new Properties();
[Browsable(false)]
public Properties Preferences {
get { return preferences; }
}
Properties sdSettings = new Properties();
[Browsable(false)]
public Properties SDSettings {
get { return sdSettings; }
}
string GetPreferencesKey()
{
return "solution:" + fileName.ToString().ToUpperInvariant();
}
internal void LoadPreferences()
{
try {
preferences = SD.PropertyService.LoadExtraProperties(GetPreferencesKey());
sdSettings = Properties.Load(sdSettingsFileName);
} catch (IOException) {
} catch (XmlException) {
// ignore errors about inaccessible or malformed files
}
// Load active configuration from preferences
CreateDefaultConfigurationsIfMissing();
this.ActiveConfiguration = ConfigurationAndPlatform.FromKey(preferences.Get("ActiveConfiguration", "Debug|Any CPU"));
ValidateConfiguration();
// We can't set the startup project property yet; LoadPreferences() is called before
// the projects are loaded into the solution.
// This is necessary so that the projects can be loaded in the correct configuration
// - we avoid an expensive configuration switch during solution load.
}
public event EventHandler PreferencesSaving = delegate { };
public void SavePreferences()
{
preferences.Set("ActiveConfiguration", activeConfiguration.ToString());
PreferencesSaving(this, EventArgs.Empty);
try {
SD.PropertyService.SaveExtraProperties(GetPreferencesKey(), preferences);
if (sdSettings.IsDirty) {
sdSettings.Save(sdSettingsFileName);
}
} catch (IOException) {
} catch (UnauthorizedAccessException) {
// ignore errors writing to extra properties
}
}
#endregion
#region Save
public void Save()
{
try {
changeWatcher.Disable();
using (var solutionWriter = new SolutionWriter(fileName)) {
var version = ComputeSolutionVersion();
solutionWriter.WriteFormatHeader(version);
solutionWriter.WriteSolutionVersionProperties(version, currVSVersion ?? DefaultVSVersion, minVSVersion ?? DefaultMinVSVersion);
solutionWriter.WriteSolutionItems(this);
solutionWriter.WriteGlobalSections(this);
}
changeWatcher.Enable();
} catch (IOException ex) {
MessageService.ShowErrorFormatted("${res:SharpDevelop.Solution.CannotSave.IOException}", fileName, ex.Message);
} catch (UnauthorizedAccessException ex) {
FileAttributes attributes = File.GetAttributes(fileName);
if ((FileAttributes.ReadOnly & attributes) == FileAttributes.ReadOnly) {
MessageService.ShowErrorFormatted("${res:SharpDevelop.Solution.CannotSave.ReadOnly}", fileName);
}
else
{
MessageService.ShowErrorFormatted
("${res:SharpDevelop.Solution.CannotSave.UnauthorizedAccessException}", fileName, ex.Message);
}
}
}
SolutionFormatVersion ComputeSolutionVersion()
{
SolutionFormatVersion version = SolutionFormatVersion.VS2005;
foreach (var project in this.Projects) {
if (project.MinimumSolutionVersion > version)
version = project.MinimumSolutionVersion;
}
if ((minVSVersion != null || currVSVersion != null) && version < SolutionFormatVersion.VS2012)
version = SolutionFormatVersion.VS2012;
return version;
}
#endregion
#region MSBuildProjectCollection
readonly Microsoft.Build.Evaluation.ProjectCollection msBuildProjectCollection = new Microsoft.Build.Evaluation.ProjectCollection();
[Browsable(false)]
public Microsoft.Build.Evaluation.ProjectCollection MSBuildProjectCollection {
get { return msBuildProjectCollection; }
}
void UpdateMSBuildProperties()
{
var dict = new Dictionary<string, string>();
MSBuildInternals.AddMSBuildSolutionProperties(this, dict);
foreach (var pair in dict) {
msBuildProjectCollection.SetGlobalProperty(pair.Key, pair.Value);
}
}
#endregion
#region Handle FileService.FileRenamed / FileRemoved
void FileServiceFileRenamed(object sender, FileRenameEventArgs e)
{
string oldName = e.SourceFile;
string newName = e.TargetFile;
foreach (ISolutionFileItem fileItem in this.AllItems.OfType<ISolutionFileItem>()) {
if (FileUtility.IsBaseDirectory(oldName, fileItem.FileName)) {
string newFullName = FileUtility.RenameBaseDirectory(fileItem.FileName, oldName, newName);
fileItem.FileName = FileName.Create(newFullName);
}
}
foreach (IProject project in this.Projects) {
if (FileUtility.IsBaseDirectory(project.Directory, oldName)) {
foreach (ProjectItem item in project.Items) {
if (FileUtility.IsBaseDirectory(oldName, item.FileName)) {
SD.GetRequiredService<IProjectServiceRaiseEvents>().RaiseProjectItemRemoved(new ProjectItemEventArgs(project, item));
item.FileName = FileName.Create(FileUtility.RenameBaseDirectory(item.FileName, oldName, newName));
SD.GetRequiredService<IProjectServiceRaiseEvents>().RaiseProjectItemAdded(new ProjectItemEventArgs(project, item));
}
}
}
}
}
void FileServiceFileRemoved(object sender, FileEventArgs e)
{
string fileName = e.FileName;
foreach (ISolutionFileItem fileItem in this.AllItems.OfType<ISolutionFileItem>().ToArray()) {
if (FileUtility.IsBaseDirectory(fileName, fileItem.FileName)) {
fileItem.ParentFolder.Items.Remove(fileItem);
}
}
foreach (IProject project in this.Projects) {
if (FileUtility.IsBaseDirectory(project.Directory, fileName)) {
foreach (ProjectItem item in project.Items.ToArray()) {
if (FileUtility.IsBaseDirectory(fileName, item.FileName)) {
project.Items.Remove(item);
}
}
}
}
}
#endregion
public bool IsReadOnly {
get {
try {
FileAttributes attributes = File.GetAttributes(fileName);
return ((FileAttributes.ReadOnly & attributes) == FileAttributes.ReadOnly);
} catch (FileNotFoundException) {
return false;
} catch (DirectoryNotFoundException) {
return true;
}
}
}
#region IConfigurable implementation
ConfigurationAndPlatform activeConfiguration = new ConfigurationAndPlatform("Debug", "AnyCPU");
public ConfigurationAndPlatform ActiveConfiguration {
get { return activeConfiguration; }
set {
if (value.Configuration == null || value.Platform == null)
throw new ArgumentNullException();
if (activeConfiguration != value) {
activeConfiguration = value;
// Apply changed configuration to all projects:
foreach (var project in this.Projects) {
project.ActiveConfiguration = project.ConfigurationMapping.GetProjectConfiguration(value);
}
ActiveConfigurationChanged(this, EventArgs.Empty);
}
}
}
public event EventHandler ActiveConfigurationChanged = delegate { };
[Browsable(false)]
public IConfigurationOrPlatformNameCollection ConfigurationNames { get; private set; }
[Browsable(false)]
public IConfigurationOrPlatformNameCollection PlatformNames { get; private set; }
void CreateDefaultConfigurationsIfMissing()
{
if (this.ConfigurationNames.Count == 0) {
this.ConfigurationNames.Add("Debug");
this.ConfigurationNames.Add("Release");
}
if (this.PlatformNames.Count == 0) {
this.PlatformNames.Add("Any CPU");
}
}
void ValidateConfiguration()
{
string config = this.ActiveConfiguration.Configuration;
string platform = this.ActiveConfiguration.Platform;
if (!this.ConfigurationNames.Contains(config))
config = this.ConfigurationNames.First();
if (!this.PlatformNames.Contains(platform))
platform = this.PlatformNames.First();
this.ActiveConfiguration = new ConfigurationAndPlatform(config, platform);
}
#endregion
#region ICanBeDirty implementation
public event EventHandler IsDirtyChanged;
bool isDirty;
[Browsable(false)]
public bool IsDirty {
get { return isDirty; }
set {
if (isDirty != value) {
isDirty = value;
if (IsDirtyChanged != null) {
IsDirtyChanged(this, EventArgs.Empty);
}
}
}
}
#endregion
[EditorAttribute(typeof(FormatterSettingsEditor), typeof(UITypeEditor))]
public object FormatterSettings
{
get {
// We don't need any return value etc.
return null;
}
}
/// <summary>
/// Pseudo-editor showing a "..." for FormattingSettings option and opening the formatting editor for solution
/// </summary>
class FormatterSettingsEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
var treeNode = AddInTree.GetTreeNode("/SharpDevelop/Dialogs/SolutionFormattingOptionsDialog", false);
bool? result = ICSharpCode.SharpDevelop.Commands.OptionsCommand.ShowTreeOptions("SolutionFormattingOptionsDialog",
StringParser.Parse("${res:ICSharpCode.SharpDevelop.Project.SolutionFormattingOptions.Title}"),
treeNode);
if ((bool) result) {
// Formatting options have been changed, make solution dirty
var solution = context.Instance as Solution;
if (solution != null) {
solution.IsDirty = true;
}
}
return null;
}
}
public override string ToString()
{
return "[Solution " + fileName + " with " + projects.Count + " projects]";
}
public Version VSVersion {
get { return currVSVersion; }
}
public Version MinVSVersion {
get { return minVSVersion; }
}
}
}