From 9d9c3dcfc0d033566d1a6eddc5fa064439abe1ca Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Fri, 7 Jan 2011 14:39:57 +0100 Subject: [PATCH] CustomTool.GetOutputFileName: don't use file names that already exist in the project but don't depend on the base item. --- .../Base/Project/Src/Project/CustomTool.cs | 51 +++++++++++++++++-- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/src/Main/Base/Project/Src/Project/CustomTool.cs b/src/Main/Base/Project/Src/Project/CustomTool.cs index b28562f90a..30de47b38b 100644 --- a/src/Main/Base/Project/Src/Project/CustomTool.cs +++ b/src/Main/Base/Project/Src/Project/CustomTool.cs @@ -98,12 +98,24 @@ namespace ICSharpCode.SharpDevelop.Project } public string GetOutputFileName(FileProjectItem baseItem, string additionalExtension) + { + return GetOutputFileName(baseItem, additionalExtension, true); + } + + public string GetOutputFileName(FileProjectItem baseItem, string additionalExtension, bool isPrimaryOutput) { if (baseItem == null) throw new ArgumentNullException("baseItem"); if (baseItem.Project != project) throw new ArgumentException("baseItem is not from project this CustomToolContext belongs to"); + if (isPrimaryOutput) { + string lastGenOutput = baseItem.GetEvaluatedMetadata("LastGenOutput"); + if (!string.IsNullOrEmpty(lastGenOutput)) { + return Path.Combine(Path.GetDirectoryName(baseItem.FileName), lastGenOutput); + } + } + string newExtension = null; if (project.LanguageProperties.CodeDomProvider != null) { newExtension = project.LanguageProperties.CodeDomProvider.FileExtension; @@ -120,22 +132,55 @@ namespace ICSharpCode.SharpDevelop.Project newExtension = "." + newExtension; } - return Path.ChangeExtension(baseItem.FileName, additionalExtension + newExtension); + string newFileName = Path.ChangeExtension(baseItem.FileName, additionalExtension + newExtension); + int retryIndex = 0; + while (true) { + FileProjectItem item = project.FindFile(newFileName); + // If the file does not exist in the project, we can use that name. + if (item == null) + return newFileName; + // If the file already exists in the project, use it only if it belongs to our base item + if (string.Equals(item.DependentUpon, Path.GetFileName(baseItem.FileName), StringComparison.OrdinalIgnoreCase)) + return newFileName; + // Otherwise, find another free file name + retryIndex++; + newFileName = Path.ChangeExtension(baseItem.FileName, additionalExtension + retryIndex + newExtension); + } } public FileProjectItem EnsureOutputFileIsInProject(FileProjectItem baseItem, string outputFileName) { + return EnsureOutputFileIsInProject(baseItem, outputFileName, true); + } + + public FileProjectItem EnsureOutputFileIsInProject(FileProjectItem baseItem, string outputFileName, bool isPrimaryOutput) + { + if (baseItem == null) + throw new ArgumentNullException("baseItem"); + if (baseItem.Project != project) + throw new ArgumentException("baseItem is not from project this CustomToolContext belongs to"); + WorkbenchSingleton.AssertMainThread(); + bool saveProject = false; + if (isPrimaryOutput) { + if (baseItem.GetEvaluatedMetadata("LastGenOutput") != Path.GetFileName(outputFileName)) { + saveProject = true; + baseItem.SetEvaluatedMetadata("LastGenOutput", Path.GetFileName(outputFileName)); + } + } FileProjectItem outputItem = project.FindFile(outputFileName); if (outputItem == null) { outputItem = new FileProjectItem(project, ItemType.Compile); outputItem.FileName = outputFileName; outputItem.DependentUpon = Path.GetFileName(baseItem.FileName); + outputItem.SetEvaluatedMetadata("AutoGen", "True"); ProjectService.AddProjectItem(project, outputItem); FileService.FireFileCreated(outputFileName, false); - project.Save(); + saveProject = true; ProjectBrowserPad.RefreshViewAsync(); } + if (saveProject) + project.Save(); return outputItem; } @@ -463,7 +508,7 @@ namespace ICSharpCode.SharpDevelop.Project activeToolRun = null; if(toolRuns.Count > 0) { CustomToolRun nextRun = toolRuns.Dequeue(); - if(nextRun != null) { + if(nextRun != null) { RunCustomTool(nextRun); } }