From b74c496ea63c653d06526d679cfe9493b95faa58 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Tue, 2 Jan 2007 21:41:44 +0000 Subject: [PATCH] Try to avoid Project GUID conflicts. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/2.1@2260 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Src/Project/MSBuildBasedProject.cs | 34 ++++++++++++++----- .../Project/Src/Project/MissingProject.cs | 1 - .../Solution/AbstractSolutionFolder.cs | 2 +- .../Src/Project/Solution/SolutionFolder.cs | 4 +++ .../Project/Src/Project/UnknownProject.cs | 1 - .../Services/ProjectService/ProjectService.cs | 8 +++++ src/Main/Base/Project/Src/Util/Linq.cs | 12 +++++++ 7 files changed, 50 insertions(+), 12 deletions(-) diff --git a/src/Main/Base/Project/Src/Project/MSBuildBasedProject.cs b/src/Main/Base/Project/Src/Project/MSBuildBasedProject.cs index 00f6d437f3..dec810b845 100644 --- a/src/Main/Base/Project/Src/Project/MSBuildBasedProject.cs +++ b/src/Main/Base/Project/Src/Project/MSBuildBasedProject.cs @@ -120,7 +120,7 @@ namespace ICSharpCode.SharpDevelop.Project Name = information.ProjectName; FileName = information.OutputProjectFileName; - IdGuid = "{" + Guid.NewGuid().ToString().ToUpperInvariant() + "}"; + base.IdGuid = "{" + Guid.NewGuid().ToString().ToUpperInvariant() + "}"; MSBuild.BuildPropertyGroup group = project.AddNewPropertyGroup(false); group.AddNewProperty(ProjectGuidPropertyName, IdGuid, true); group.AddNewProperty("Configuration", "Debug", true).Condition = " '$(Configuration)' == '' "; @@ -130,6 +130,15 @@ namespace ICSharpCode.SharpDevelop.Project this.ActivePlatform = "AnyCPU"; } + /// + /// The MSBuild property used to store the project's IdGuid. + /// The IdGuid is only stored in the project file to make multiple solutions use the same + /// GUID for the project when the project is added to multiple solutions. However, the actual + /// GUID used for the project in the solution can differ from the GUID in the project file - + /// SharpDevelop does not try to correct mismatches but simply always use the value from the solution. + /// SharpDevelop creates a new GUID for the solution when the project GUID cannot be used because it + /// would conflict with another project. This happens when one project is created by copying another project. + /// public const string ProjectGuidPropertyName = "ProjectGuid"; /// @@ -849,18 +858,25 @@ namespace ICSharpCode.SharpDevelop.Project CreateItemsListFromMSBuild(); LoadConfigurationPlatformNamesFromMSBuild(); - IdGuid = GetEvaluatedProperty(ProjectGuidPropertyName); - if (IdGuid == null) { - // Fix projects that have nb GUID - IdGuid = Guid.NewGuid().ToString(); - SetPropertyInternal(null, null, ProjectGuidPropertyName, IdGuid, PropertyStorageLocations.Base, true); + base.IdGuid = GetEvaluatedProperty(ProjectGuidPropertyName); + } finally { + isLoading = false; + } + } + + [Browsable(false)] + public override string IdGuid { + get { return base.IdGuid; } + set { + if (base.IdGuid == null) { + // Save the GUID in the project if the project does not yet have a GUID. + SetPropertyInternal(null, null, ProjectGuidPropertyName, value, PropertyStorageLocations.Base, true); try { // save fixed project - project.Save(fileName); + project.Save(this.FileName); } catch {} } - } finally { - isLoading = false; + base.IdGuid = value; } } #endregion diff --git a/src/Main/Base/Project/Src/Project/MissingProject.cs b/src/Main/Base/Project/Src/Project/MissingProject.cs index c60eadf007..762da91df7 100644 --- a/src/Main/Base/Project/Src/Project/MissingProject.cs +++ b/src/Main/Base/Project/Src/Project/MissingProject.cs @@ -15,7 +15,6 @@ namespace ICSharpCode.SharpDevelop.Project { Name = title; FileName = fileName; - IdGuid = "{" + Guid.NewGuid().ToString() + "}"; TypeGuid = "{00000000-0000-0000-0000-000000000000}"; } } diff --git a/src/Main/Base/Project/Src/Project/Solution/AbstractSolutionFolder.cs b/src/Main/Base/Project/Src/Project/Solution/AbstractSolutionFolder.cs index b3f794988d..e7c6b89164 100644 --- a/src/Main/Base/Project/Src/Project/Solution/AbstractSolutionFolder.cs +++ b/src/Main/Base/Project/Src/Project/Solution/AbstractSolutionFolder.cs @@ -51,7 +51,7 @@ namespace ICSharpCode.SharpDevelop.Project } [Browsable(false)] - public string IdGuid { + public virtual string IdGuid { get { return idGuid; } diff --git a/src/Main/Base/Project/Src/Project/Solution/SolutionFolder.cs b/src/Main/Base/Project/Src/Project/Solution/SolutionFolder.cs index eff04343e0..a14abd9a08 100644 --- a/src/Main/Base/Project/Src/Project/Solution/SolutionFolder.cs +++ b/src/Main/Base/Project/Src/Project/Solution/SolutionFolder.cs @@ -79,6 +79,10 @@ namespace ICSharpCode.SharpDevelop.Project public virtual void AddFolder(ISolutionFolder folder) { + if (string.IsNullOrEmpty(folder.IdGuid)) { + folder.IdGuid = Guid.NewGuid().ToString().ToUpperInvariant(); + } + if (folder.Parent != null) { folder.Parent.RemoveFolder(folder); } diff --git a/src/Main/Base/Project/Src/Project/UnknownProject.cs b/src/Main/Base/Project/Src/Project/UnknownProject.cs index 4686b9a514..ca2a79ae1a 100644 --- a/src/Main/Base/Project/Src/Project/UnknownProject.cs +++ b/src/Main/Base/Project/Src/Project/UnknownProject.cs @@ -44,7 +44,6 @@ namespace ICSharpCode.SharpDevelop.Project { Name = title; FileName = fileName; - IdGuid = "{" + Guid.NewGuid().ToString() + "}"; TypeGuid = "{00000000-0000-0000-0000-000000000000}"; } } diff --git a/src/Main/Base/Project/Src/Services/ProjectService/ProjectService.cs b/src/Main/Base/Project/Src/Services/ProjectService/ProjectService.cs index 63635b747e..5eb1884545 100644 --- a/src/Main/Base/Project/Src/Services/ProjectService/ProjectService.cs +++ b/src/Main/Base/Project/Src/Services/ProjectService/ProjectService.cs @@ -196,6 +196,14 @@ namespace ICSharpCode.SharpDevelop.Project public static void AddProject(ISolutionFolderNode solutionFolderNode, IProject newProject) { + if (Linq.Exists(solutionFolderNode.Solution.SolutionFolders, + delegate (ISolutionFolder folder) { + return string.Equals(folder.IdGuid, newProject.IdGuid, StringComparison.OrdinalIgnoreCase); + })) + { + LoggingService.Warn("ProjectService.AddProject: Duplicate IdGuid detected"); + newProject.IdGuid = Guid.NewGuid().ToString().ToUpperInvariant(); + } solutionFolderNode.Container.AddFolder(newProject); ParserService.CreateProjectContentForAddedProject(newProject); solutionFolderNode.Solution.FixSolutionConfiguration(new IProject[] { newProject }); diff --git a/src/Main/Base/Project/Src/Util/Linq.cs b/src/Main/Base/Project/Src/Util/Linq.cs index d06bc5ad2f..b29526f93f 100644 --- a/src/Main/Base/Project/Src/Util/Linq.cs +++ b/src/Main/Base/Project/Src/Util/Linq.cs @@ -75,6 +75,18 @@ namespace ICSharpCode.SharpDevelop return default(T); } + /// + /// Gets if any element in the input matches the filter. + /// + public static bool Exists(IEnumerable input, Predicate filter) + { + foreach (T element in input) { + if (filter(element)) + return true; + } + return false; + } + public static List ToList(IEnumerable input) { return new List(input);