Browse Source

Support running custom tools on build.

Add Custom Tools project options panel that can be used to enable running custom tools when a build is started just before the build executes.
Project options panel allows enabling/disabling this feature and specifying which files will have their custom tools run pre-build.
Configuration is stored in IProject.ProjectSpecificProperties.
pull/28/head
Matt Ward 13 years ago
parent
commit
ec75e495d4
  1. 3
      src/Main/Base/Project/ICSharpCode.SharpDevelop.addin
  2. 9
      src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj
  3. 33
      src/Main/Base/Project/Src/Gui/Dialogs/OptionPanels/ProjectOptions/ProjectCustomToolOptionsPanel.xaml
  4. 107
      src/Main/Base/Project/Src/Gui/Dialogs/OptionPanels/ProjectOptions/ProjectCustomToolOptionsPanel.xaml.cs
  5. 36
      src/Main/Base/Project/Src/Project/BeforeBuildCustomToolFileNameFilter.cs
  6. 51
      src/Main/Base/Project/Src/Project/BeforeBuildCustomToolProjectItems.cs
  7. 30
      src/Main/Base/Project/Src/Project/BeforeBuildCustomToolRunner.cs
  8. 3
      src/Main/Base/Project/Src/Project/CustomTool.cs
  9. 46
      src/Main/Base/Project/Src/Project/ProjectCustomToolOptions.cs
  10. 4
      src/Main/Base/Test/ICSharpCode.SharpDevelop.Tests.csproj
  11. 267
      src/Main/Base/Test/Project/BeforeBuildCustomToolProjectItemsTests.cs
  12. 212
      src/Main/Base/Test/Project/ProjectCustomToolOptionsTests.cs
  13. 37
      src/Main/Base/Test/Utils/ProjectHelper.cs

3
src/Main/Base/Project/ICSharpCode.SharpDevelop.addin

@ -2273,6 +2273,9 @@ @@ -2273,6 +2273,9 @@
<Path name="/SharpDevelop/BackendBindings/ProjectOptions/AllManaged">
<!-- put project option panels valid for all .NET projects here -->
<OptionPanel id = "CustomTool"
label = "${res:ICSharpCode.SharpDevelop.Internal.Project.ProjectFile.CustomTool}"
class = "ICSharpCode.SharpDevelop.Gui.OptionPanels.ProjectCustomToolOptionsPanel"/>
</Path>
<Path name="/SharpDevelop/Pads/ErrorList/TaskContextMenu">

9
src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj

@ -257,6 +257,10 @@ @@ -257,6 +257,10 @@
<DependentUpon>DebugOptions.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="Src\Gui\Dialogs\OptionPanels\ProjectOptions\ProjectCustomToolOptionsPanel.xaml.cs">
<DependentUpon>ProjectCustomToolOptionsPanel.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="Src\Gui\Dialogs\OptionPanels\ProjectOptions\ProjectOptionPanel.cs" />
<Compile Include="Src\Gui\Dialogs\OptionPanels\ProjectOptions\StorageLocationConverter.cs" />
<Compile Include="Src\Gui\Dialogs\OptionPanels\ProjectOptions\StorageLocationPicker.cs" />
@ -350,6 +354,9 @@ @@ -350,6 +354,9 @@
<Compile Include="Src\Internal\Templates\File\FileTemplate.cs" />
<Compile Include="Src\Internal\Templates\TextTemplate.cs" />
<Compile Include="Src\Internal\Templates\File\INewFileCreator.cs" />
<Compile Include="Src\Project\BeforeBuildCustomToolFileNameFilter.cs" />
<Compile Include="Src\Project\BeforeBuildCustomToolProjectItems.cs" />
<Compile Include="Src\Project\BeforeBuildCustomToolRunner.cs" />
<Compile Include="Src\Project\Behaviors\DefaultProjectBehavior.cs" />
<Compile Include="Src\Project\Behaviors\DotNetStartBehavior.cs" />
<Compile Include="Src\Project\Behaviors\ProjectBehavior.cs" />
@ -391,6 +398,7 @@ @@ -391,6 +398,7 @@
</Compile>
<Compile Include="Src\Project\PortableLibrary\SupportedFramework.cs" />
<Compile Include="Src\Project\ProjectChangeWatcher.cs" />
<Compile Include="Src\Project\ProjectCustomToolOptions.cs" />
<Compile Include="Src\Project\ProjectLoadInformation.cs" />
<Compile Include="Src\Project\ProjectStartException.cs" />
<Compile Include="Src\Project\ProjectLoadException.cs" />
@ -905,6 +913,7 @@ @@ -905,6 +913,7 @@
<Page Include="Src\Gui\Dialogs\OptionPanels\IDEOptions\TaskListOptionsl.xaml" />
<Page Include="Src\Gui\Dialogs\OptionPanels\ProjectOptions\BuildEvents.xaml" />
<Page Include="Src\Gui\Dialogs\OptionPanels\ProjectOptions\DebugOptions.xaml" />
<Page Include="Src\Gui\Dialogs\OptionPanels\ProjectOptions\ProjectCustomToolOptionsPanel.xaml" />
<Page Include="Src\Gui\Dialogs\OptionPanels\ProjectOptions\ProjectOptionPanel.xaml">
<DependentUpon>ProjectOptionPanel.cs</DependentUpon>
</Page>

33
src/Main/Base/Project/Src/Gui/Dialogs/OptionPanels/ProjectOptions/ProjectCustomToolOptionsPanel.xaml

@ -0,0 +1,33 @@ @@ -0,0 +1,33 @@
<gui:OptionPanel
x:Class="ICSharpCode.SharpDevelop.Gui.OptionPanels.ProjectCustomToolOptionsPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:core="http://icsharpcode.net/sharpdevelop/core"
xmlns:widgets="http://icsharpcode.net/sharpdevelop/widgets"
xmlns:project="clr-namespace:ICSharpCode.SharpDevelop.Project"
xmlns:gui="clr-namespace:ICSharpCode.SharpDevelop.Gui"
xmlns:optionpanels="clr-namespace:ICSharpCode.SharpDevelop.Gui">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<CheckBox
Margin="10, 20, 10, 5"
IsChecked="{Binding RunCustomToolOnBuild}"
Content="Run on Build" />
<Label
Margin="5, 5, 5, 0"
Grid.Row="1"
Content="Filenames:" />
<TextBox
Margin="10, 0"
Grid.Row="2"
AcceptsReturn="True"
Text="{Binding Path=FileNames, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
</gui:OptionPanel>

107
src/Main/Base/Project/Src/Gui/Dialogs/OptionPanels/ProjectOptions/ProjectCustomToolOptionsPanel.xaml.cs

@ -0,0 +1,107 @@ @@ -0,0 +1,107 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.ComponentModel;
using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Project;
namespace ICSharpCode.SharpDevelop.Gui.OptionPanels
{
public partial class ProjectCustomToolOptionsPanel : INotifyPropertyChanged, ICanBeDirty
{
ProjectCustomToolOptions customToolsOptions;
bool runCustomToolOnBuild;
string fileNames;
bool isDirty;
public ProjectCustomToolOptionsPanel()
{
this.DataContext = this;
InitializeComponent();
}
public bool RunCustomToolOnBuild {
get { return runCustomToolOnBuild; }
set {
runCustomToolOnBuild = value;
OnChanged();
}
}
public string FileNames {
get { return fileNames; }
set {
fileNames = value;
OnChanged();
}
}
void OnChanged()
{
IsDirty = OptionsHaveChanged();
}
bool OptionsHaveChanged()
{
return
(runCustomToolOnBuild != customToolsOptions.RunCustomToolOnBuild) ||
(fileNames != customToolsOptions.FileNames);
}
public override void LoadOptions()
{
var project = Owner as IProject;
customToolsOptions = new ProjectCustomToolOptions(project);
RunCustomToolOnBuild = customToolsOptions.RunCustomToolOnBuild;
FileNames = customToolsOptions.FileNames;
OnPropertyChanged();
}
public override bool SaveOptions()
{
if (OptionsHaveChanged()) {
customToolsOptions.RunCustomToolOnBuild = runCustomToolOnBuild;
customToolsOptions.FileNames = fileNames;
}
IsDirty = false;
return true;
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged()
{
OnPropertyChanged(null);
}
void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event EventHandler IsDirtyChanged;
void OnIsDirtyChanged()
{
if (IsDirtyChanged != null) {
IsDirtyChanged(this, new EventArgs());
}
}
public bool IsDirty {
get { return isDirty; }
private set {
if (isDirty != value) {
isDirty = value;
OnIsDirtyChanged();
}
}
}
}
}

36
src/Main/Base/Project/Src/Project/BeforeBuildCustomToolFileNameFilter.cs

@ -0,0 +1,36 @@ @@ -0,0 +1,36 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace ICSharpCode.SharpDevelop.Project
{
public class BeforeBuildCustomToolFileNameFilter
{
List<string> fileNames;
public BeforeBuildCustomToolFileNameFilter(IProject project)
{
var customToolOptions = new ProjectCustomToolOptions(project);
if (customToolOptions.RunCustomToolOnBuild) {
fileNames = customToolOptions.SplitFileNames().ToList();
} else {
fileNames = new List<string>();
}
}
public bool IsMatch(string fullPath)
{
string fileNameToMatch = Path.GetFileName(fullPath);
return fileNames.Any(fileName => String.Equals(fileName, fileNameToMatch, StringComparison.OrdinalIgnoreCase));
}
public bool Any()
{
return fileNames.Any();
}
}
}

51
src/Main/Base/Project/Src/Project/BeforeBuildCustomToolProjectItems.cs

@ -0,0 +1,51 @@ @@ -0,0 +1,51 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Linq;
using ICSharpCode.SharpDevelop.Project;
namespace ICSharpCode.SharpDevelop.Project
{
public class BeforeBuildCustomToolProjectItems
{
IBuildable buildable;
public BeforeBuildCustomToolProjectItems(IBuildable buildable)
{
this.buildable = buildable;
}
public IEnumerable<FileProjectItem> GetProjectItems()
{
return GetProjects()
.SelectMany(p => GetConfiguredCustomToolProjectItems(p))
.ToList();
}
IEnumerable<IProject> GetProjects()
{
IProject project = buildable as IProject;
if (project != null) {
return new IProject[] { project };
}
var solution = buildable as Solution;
return solution.Projects;
}
IEnumerable<FileProjectItem> GetConfiguredCustomToolProjectItems(IProject project)
{
var fileNameFilter = new BeforeBuildCustomToolFileNameFilter(project);
if (!fileNameFilter.Any()) {
return new FileProjectItem[0];
}
return project
.Items
.OfType<FileProjectItem>()
.Where(item => fileNameFilter.IsMatch(item.Include));
}
}
}

30
src/Main/Base/Project/Src/Project/BeforeBuildCustomToolRunner.cs

@ -0,0 +1,30 @@ @@ -0,0 +1,30 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Linq;
namespace ICSharpCode.SharpDevelop.Project
{
public class BeforeBuildCustomToolRunner
{
public BeforeBuildCustomToolRunner()
{
ProjectService.BuildStarted += ProjectBuildStarted;
}
void ProjectBuildStarted(object sender, BuildEventArgs e)
{
var projectItems = new BeforeBuildCustomToolProjectItems(e.Buildable);
RunCustomTool(projectItems.GetProjectItems());
}
void RunCustomTool(IEnumerable<FileProjectItem> projectItems)
{
foreach (FileProjectItem projectItem in projectItems) {
CustomToolsService.RunCustomTool(projectItem, false);
}
}
}
}

3
src/Main/Base/Project/Src/Project/CustomTool.cs

@ -356,6 +356,7 @@ namespace ICSharpCode.SharpDevelop.Project @@ -356,6 +356,7 @@ namespace ICSharpCode.SharpDevelop.Project
static Dictionary<string, CustomToolDescriptor> toolDict;
static List<CustomToolDescriptor> customToolList;
static CustomToolRun activeToolRun;
static BeforeBuildCustomToolRunner beforeBuildCustomToolRunner;
internal static void Initialize()
{
@ -369,6 +370,8 @@ namespace ICSharpCode.SharpDevelop.Project @@ -369,6 +370,8 @@ namespace ICSharpCode.SharpDevelop.Project
initialized = true;
FileUtility.FileSaved += OnFileSaved;
}
beforeBuildCustomToolRunner = new BeforeBuildCustomToolRunner();
}
static void OnFileSaved(object sender, FileNameEventArgs e)

46
src/Main/Base/Project/Src/Project/ProjectCustomToolOptions.cs

@ -0,0 +1,46 @@ @@ -0,0 +1,46 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Linq;
using ICSharpCode.Core;
namespace ICSharpCode.SharpDevelop.Project
{
public class ProjectCustomToolOptions
{
Properties properties;
public ProjectCustomToolOptions(IProject project)
{
GetCustomToolProperties(project);
}
void GetCustomToolProperties(IProject project)
{
properties = project.ProjectSpecificProperties.Get("customTool", new Properties());
}
public bool RunCustomToolOnBuild {
get { return properties.Get("runOnBuild", false); }
set { properties.Set("runOnBuild", value); }
}
public string FileNames {
get { return properties.Get("fileNames", String.Empty); }
set { properties.Set("fileNames", value); }
}
public IList<string> SplitFileNames()
{
return
FileNames
.Replace("\r\n", ";")
.Split(';', ',')
.Select(fileName => fileName.Trim())
.Where(fileName => !String.IsNullOrEmpty(fileName))
.ToList();
}
}
}

4
src/Main/Base/Test/ICSharpCode.SharpDevelop.Tests.csproj

@ -87,6 +87,8 @@ @@ -87,6 +87,8 @@
<Compile Include="OutputTextLineParserTests.cs" />
<Compile Include="OverridableMethodsTestFixture.cs" />
<Compile Include="OverridablePropertiesTestFixture.cs" />
<Compile Include="Project\BeforeBuildCustomToolProjectItemsTests.cs" />
<Compile Include="Project\ProjectCustomToolOptionsTests.cs" />
<Compile Include="PropertyPadSortingTests.cs" />
<Compile Include="ReadOnlyDocumentTests.cs" />
<Compile Include="ReflectionLayerTests.cs" />
@ -126,6 +128,7 @@ @@ -126,6 +128,7 @@
<Compile Include="Utils\MockSite.cs" />
<Compile Include="Utils\MockTextMarker.cs" />
<Compile Include="Utils\MockTextMarkerService.cs" />
<Compile Include="Utils\ProjectHelper.cs" />
<Compile Include="Utils\Tests\MockAssemblyTests.cs" />
<Compile Include="VBExpressionFinderTests.cs" />
<Compile Include="WebReferences\ValidReferenceNameTests.cs" />
@ -183,6 +186,7 @@ @@ -183,6 +186,7 @@
<Name>ICSharpCode.Core</Name>
</ProjectReference>
<Folder Include="Highlighting" />
<Folder Include="Project" />
<Folder Include="ServiceReferences" />
<Folder Include="StringTagProvider" />
<Folder Include="Utils" />

267
src/Main/Base/Test/Project/BeforeBuildCustomToolProjectItemsTests.cs

@ -0,0 +1,267 @@ @@ -0,0 +1,267 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop.Project;
using ICSharpCode.SharpDevelop.Tests.Utils;
using NUnit.Framework;
using Rhino.Mocks;
namespace ICSharpCode.SharpDevelop.Tests.Project
{
[TestFixture]
public class BeforeBuildCustomToolProjectItemsTests
{
ProjectHelper projectHelper;
BeforeBuildCustomToolProjectItems beforeBuildCustomToolProjectItems;
Solution solution;
IProject CreateProject(string fileName = @"d:\MyProject\MyProject.csproj")
{
projectHelper = new ProjectHelper(fileName);
return projectHelper.Project;
}
void CreateSolution(params IProject[] projects)
{
IProjectChangeWatcher watcher = MockRepository.GenerateStub<IProjectChangeWatcher>();
solution = new Solution(watcher);
projects.ForEach(p => solution.Folders.Add(p));
}
void ConfigureCustomToolFileNamesForProject(string fileNames)
{
var customToolOptions = new ProjectCustomToolOptions(projectHelper.Project);
customToolOptions.FileNames = fileNames;
}
void EnableCustomToolRunForProject()
{
SetCustomToolRunForProject(true);
}
void SetCustomToolRunForProject(bool enabled)
{
var customToolOptions = new ProjectCustomToolOptions(projectHelper.Project);
customToolOptions.RunCustomToolOnBuild = enabled;
}
void DisableCustomToolRunForProject()
{
SetCustomToolRunForProject(false);
}
List<FileProjectItem> GetProjectItems()
{
return beforeBuildCustomToolProjectItems.GetProjectItems().ToList();
}
void CreateBeforeBuildCustomToolProjectItems()
{
CreateBeforeBuildCustomToolProjectItems(projectHelper.Project as IBuildable);
}
void CreateBeforeBuildCustomToolProjectItems(IBuildable buildable)
{
beforeBuildCustomToolProjectItems = new BeforeBuildCustomToolProjectItems(buildable);
}
void CreateBeforeBuildCustomToolProjectItemsUsingSolution()
{
CreateBeforeBuildCustomToolProjectItems(solution as IBuildable);
}
FileProjectItem AddFileToProject(string include)
{
var projectItem = new FileProjectItem(projectHelper.Project, ItemType.Compile, include);
projectHelper.AddProjectItem(projectItem);
return projectItem;
}
[Test]
public void GetProjectItems_BuildSingleProjectNotConfiguredToRunCustomToolsOnBuild_ReturnsNoItems()
{
CreateProject();
CreateBeforeBuildCustomToolProjectItems();
List<FileProjectItem> projectItems = GetProjectItems();
Assert.AreEqual(0, projectItems.Count);
}
[Test]
public void GetProjectItems_BuildSingleProjectWithOneFileMatchingCustomToolRunConfiguration_OneProjectItemReturned()
{
CreateProject(@"d:\MyProject\MyProject.csproj");
FileProjectItem projectItem = AddFileToProject("template.tt");
EnableCustomToolRunForProject();
ConfigureCustomToolFileNamesForProject("template.tt");
CreateBeforeBuildCustomToolProjectItems();
List<FileProjectItem> projectItems = GetProjectItems();
FileProjectItem[] expectedProjectItems = new FileProjectItem[] {
projectItem
};
CollectionAssert.AreEqual(expectedProjectItems, projectItems);
}
[Test]
public void GetProjectItems_BuildSingleProjectWithOneFileMatchingCustomToolFileNamesConfigured_NoProjectItemsReturnedWhenRunCustomToolIsDisabledForProject()
{
CreateProject(@"d:\MyProject\MyProject.csproj");
AddFileToProject("template.tt");
DisableCustomToolRunForProject();
ConfigureCustomToolFileNamesForProject("template.tt");
CreateBeforeBuildCustomToolProjectItems();
List<FileProjectItem> projectItems = GetProjectItems();
Assert.AreEqual(0, projectItems.Count);
}
[Test]
public void GetProjectItems_BuildSingleProjectWithOneFileMatchingCustomToolRunConfiguration_OtherNonMatchingProjectItemsNotReturned()
{
CreateProject(@"d:\MyProject\MyProject.csproj");
FileProjectItem projectItem = AddFileToProject("template.t4");
AddFileToProject("test.cs");
EnableCustomToolRunForProject();
ConfigureCustomToolFileNamesForProject("template.t4");
CreateBeforeBuildCustomToolProjectItems();
List<FileProjectItem> projectItems = GetProjectItems();
FileProjectItem[] expectedProjectItems = new FileProjectItem[] {
projectItem
};
CollectionAssert.AreEqual(expectedProjectItems, projectItems);
}
[Test]
public void GetProjectItems_BuildSingleProjectWithOneFileMatchingCustomToolRunConfiguration_ProjectItemInSubdirectoryReturned()
{
CreateProject(@"d:\MyProject\MyProject.csproj");
FileProjectItem projectItem = AddFileToProject(@"Model\template.tt");
EnableCustomToolRunForProject();
ConfigureCustomToolFileNamesForProject("template.tt");
CreateBeforeBuildCustomToolProjectItems();
List<FileProjectItem> projectItems = GetProjectItems();
FileProjectItem[] expectedProjectItems = new FileProjectItem[] {
projectItem
};
CollectionAssert.AreEqual(expectedProjectItems, projectItems);
}
[Test]
public void GetProjectItems_BuildSingleProjectWithOneFileMatchingCustomToolRunConfiguration_ProjectItemReturnedWhenFileNameCaseIsDifferent()
{
CreateProject(@"d:\MyProject\MyProject.csproj");
FileProjectItem projectItem = AddFileToProject("template.tt");
EnableCustomToolRunForProject();
ConfigureCustomToolFileNamesForProject("TEMPLATE.TT");
CreateBeforeBuildCustomToolProjectItems();
List<FileProjectItem> projectItems = GetProjectItems();
FileProjectItem[] expectedProjectItems = new FileProjectItem[] {
projectItem
};
CollectionAssert.AreEqual(expectedProjectItems, projectItems);
}
[Test]
public void GetProjectItems_SolutionContainingOneProjectWithMatchingCustomToolFileName_ReturnsOneProjectItem()
{
IProject project = CreateProject(@"d:\MyProject\MyProject.csproj");
FileProjectItem projectItem = AddFileToProject("template.tt");
EnableCustomToolRunForProject();
ConfigureCustomToolFileNamesForProject("TEMPLATE.TT");
CreateSolution(project);
CreateBeforeBuildCustomToolProjectItemsUsingSolution();
List<FileProjectItem> projectItems = GetProjectItems();
FileProjectItem[] expectedProjectItems = new FileProjectItem[] {
projectItem
};
CollectionAssert.AreEqual(expectedProjectItems, projectItems);
}
[Test]
public void GetProjectItems_SolutionWithNoProjects_ReturnsNoProjectItems()
{
CreateSolution();
CreateBeforeBuildCustomToolProjectItemsUsingSolution();
List<FileProjectItem> projectItems = GetProjectItems();
Assert.AreEqual(0, projectItems.Count);
}
[Test]
public void GetProjectItems_SolutionContainingTwoProjectsWithMatchingCustomToolFileNameInSecondProject_ReturnsOneProjectItem()
{
IProject project1 = CreateProject(@"d:\MyProject\FirstProject.csproj");
IProject project2 = CreateProject(@"d:\MyProject\SecondProject.csproj");
FileProjectItem projectItem = AddFileToProject("template.tt");
EnableCustomToolRunForProject();
ConfigureCustomToolFileNamesForProject("TEMPLATE.TT");
CreateSolution(project1, project2);
CreateBeforeBuildCustomToolProjectItemsUsingSolution();
List<FileProjectItem> projectItems = GetProjectItems();
FileProjectItem[] expectedProjectItems = new FileProjectItem[] {
projectItem
};
CollectionAssert.AreEqual(expectedProjectItems, projectItems);
}
[Test]
public void GetProjectItems_SolutionContainingTwoProjectsWithMatchingCustomToolFileNameInFirstProject_ReturnsOneProjectItem()
{
IProject project1 = CreateProject(@"d:\MyProject\FirstProject.csproj");
FileProjectItem projectItem = AddFileToProject("template.tt");
EnableCustomToolRunForProject();
ConfigureCustomToolFileNamesForProject("TEMPLATE.TT");
IProject project2 = CreateProject(@"d:\MyProject\SecondProject.csproj");
CreateSolution(project1, project2);
CreateBeforeBuildCustomToolProjectItemsUsingSolution();
List<FileProjectItem> projectItems = GetProjectItems();
FileProjectItem[] expectedProjectItems = new FileProjectItem[] {
projectItem
};
CollectionAssert.AreEqual(expectedProjectItems, projectItems);
}
[Test]
public void GetProjectItems_SolutionContainingTwoProjectsBothWithFilesAndMatchingCustomToolFileNameInFirstProject_ReturnsOneProjectItem()
{
IProject project1 = CreateProject(@"d:\MyProject\FirstProject.csproj");
FileProjectItem projectItem = AddFileToProject("template.tt");
EnableCustomToolRunForProject();
ConfigureCustomToolFileNamesForProject("TEMPLATE.TT");
IProject project2 = CreateProject(@"d:\MyProject\SecondProject.csproj");
AddFileToProject("test.cs");
CreateSolution(project1, project2);
CreateBeforeBuildCustomToolProjectItemsUsingSolution();
List<FileProjectItem> projectItems = GetProjectItems();
FileProjectItem[] expectedProjectItems = new FileProjectItem[] {
projectItem
};
CollectionAssert.AreEqual(expectedProjectItems, projectItems);
}
}
}

212
src/Main/Base/Test/Project/ProjectCustomToolOptionsTests.cs

@ -0,0 +1,212 @@ @@ -0,0 +1,212 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop.Project;
using NUnit.Framework;
using Rhino.Mocks;
namespace ICSharpCode.SharpDevelop.Tests.Project
{
[TestFixture]
public class ProjectCustomToolOptionsTests
{
IProject project;
Properties projectSpecificProperties;
ProjectCustomToolOptions projectCustomToolOptions;
Properties properties;
void CreateProject()
{
projectSpecificProperties = new Properties();
project = MockRepository.GenerateStub<IProject>();
project.Stub(p => p.ProjectSpecificProperties).Return(projectSpecificProperties);
}
void CreateProjectWithExistingCustomToolProperties(string fileNames)
{
CreateProjectWithExistingCustomToolProperties(false, fileNames);
}
void CreateProjectWithExistingCustomToolProperties(bool runOnBuild, string fileNames = "")
{
CreateProject();
properties = new Properties();
properties.Set("runOnBuild", runOnBuild);
properties.Set("fileNames", fileNames);
projectSpecificProperties.Set("customTool", properties);
}
void CreateProjectCustomToolsOptions()
{
projectCustomToolOptions = new ProjectCustomToolOptions(project);
}
[Test]
public void RunCustomToolOnBuild_ProjectHasNoExistingProjectCustomToolProperties_ReturnsFalse()
{
CreateProject();
CreateProjectCustomToolsOptions();
bool run = projectCustomToolOptions.RunCustomToolOnBuild;
Assert.IsFalse(run);
}
[Test]
public void FileNames_ProjectHasNoExistingProjectCustomToolProperties_ReturnsEmptyString()
{
CreateProject();
CreateProjectCustomToolsOptions();
string fileNames = projectCustomToolOptions.FileNames;
Assert.AreEqual(String.Empty, fileNames);
}
[Test]
public void RunCustomToolOnBuild_ProjectPropertyRunCustomToolOnBuildIsTrue_ReturnsTrue()
{
CreateProjectWithExistingCustomToolProperties(runOnBuild: true);
CreateProjectCustomToolsOptions();
bool run = projectCustomToolOptions.RunCustomToolOnBuild;
Assert.IsTrue(run);
}
[Test]
public void FileNames_ProjectPropertyFileNamesIsNotEmptyString_ReturnsFileName()
{
CreateProjectWithExistingCustomToolProperties(fileNames: "T4MVC.tt");
CreateProjectCustomToolsOptions();
string fileNames = projectCustomToolOptions.FileNames;
Assert.AreEqual("T4MVC.tt", fileNames);
}
[Test]
public void RunCustomToolOnBuild_ChangeRunCustomToolOnBuildToTrue_StoredInProjectProperties()
{
CreateProjectWithExistingCustomToolProperties(runOnBuild: false);
CreateProjectCustomToolsOptions();
projectCustomToolOptions.RunCustomToolOnBuild = true;
CreateProjectCustomToolsOptions();
bool run = projectCustomToolOptions.RunCustomToolOnBuild;
Assert.IsTrue(run);
}
[Test]
public void RunCustomToolOnBuild_ChangeRunCustomToolOnBuildToFalse_StoredInProjectProperties()
{
CreateProjectWithExistingCustomToolProperties(runOnBuild: true);
CreateProjectCustomToolsOptions();
projectCustomToolOptions.RunCustomToolOnBuild = false;
CreateProjectCustomToolsOptions();
bool run = projectCustomToolOptions.RunCustomToolOnBuild;
Assert.IsFalse(run);
}
[Test]
public void FileNames_ChangeFileNamesFromEmptyStringToFileName_StoredInProjectProperties()
{
CreateProjectWithExistingCustomToolProperties(fileNames: String.Empty);
CreateProjectCustomToolsOptions();
projectCustomToolOptions.FileNames = "abc.tt";
CreateProjectCustomToolsOptions();
string fileNames = projectCustomToolOptions.FileNames;
Assert.AreEqual("abc.tt", fileNames);
}
[Test]
public void SplitFileNames_FileNamesIsSemiColonSeparatedOfTwoFiles_ReturnsTwoFiles()
{
CreateProjectWithExistingCustomToolProperties("a.t4;b.t4");
CreateProjectCustomToolsOptions();
IList<string> fileNames = projectCustomToolOptions.SplitFileNames();
string[] expectedFileNames = new string[] {
"a.t4",
"b.t4"
};
CollectionAssert.AreEqual(expectedFileNames, fileNames);
}
[Test]
public void SplitFileNames_FileNamesIsCommaSeparatedOfTwoFiles_ReturnsTwoFiles()
{
CreateProjectWithExistingCustomToolProperties("a.t4,b.t4");
CreateProjectCustomToolsOptions();
IList<string> fileNames = projectCustomToolOptions.SplitFileNames();
string[] expectedFileNames = new string[] {
"a.t4",
"b.t4"
};
CollectionAssert.AreEqual(expectedFileNames, fileNames);
}
[Test]
public void SplitFileNames_FileNamesIsSemiColonSeparatedOfTwoFilesWithWhitespace_ReturnsTwoFilesWithWhitespaceRemoved()
{
CreateProjectWithExistingCustomToolProperties(" a.t4 ; b.t4 ");
CreateProjectCustomToolsOptions();
IList<string> fileNames = projectCustomToolOptions.SplitFileNames();
string[] expectedFileNames = new string[] {
"a.t4",
"b.t4"
};
CollectionAssert.AreEqual(expectedFileNames, fileNames);
}
[Test]
public void SplitFileNames_FileNamesIsTwoFilesEachOnSeparateLine_ReturnsTwoFiles()
{
string text =
"a.t4\r\n" +
"b.t4";
CreateProjectWithExistingCustomToolProperties(text);
CreateProjectCustomToolsOptions();
IList<string> fileNames = projectCustomToolOptions.SplitFileNames();
string[] expectedFileNames = new string[] {
"a.t4",
"b.t4"
};
CollectionAssert.AreEqual(expectedFileNames, fileNames);
}
[Test]
public void SplitFileNames_FileNamesIsTwoFilesEachOnSeparateLineWithEmptyLineBetweenThemAndOneAtEnd_ReturnsTwoFiles()
{
string text =
"a.t4\r\n" +
"\r\n" +
"b.t4\r\n";
CreateProjectWithExistingCustomToolProperties(text);
CreateProjectCustomToolsOptions();
IList<string> fileNames = projectCustomToolOptions.SplitFileNames();
string[] expectedFileNames = new string[] {
"a.t4",
"b.t4"
};
CollectionAssert.AreEqual(expectedFileNames, fileNames);
}
}
}

37
src/Main/Base/Test/Utils/ProjectHelper.cs

@ -0,0 +1,37 @@ @@ -0,0 +1,37 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop.Project;
using Rhino.Mocks;
namespace ICSharpCode.SharpDevelop.Tests.Utils
{
public class ProjectHelper
{
public IProject Project = MockRepository.GenerateMock<IProject, IBuildable>();
public List<ProjectItem> ProjectItems = new List<ProjectItem>();
public Properties ProjectSpecificProperties = new Properties();
public ProjectHelper(string fileName)
{
Project.Stub(p => p.FileName).Return(fileName);
Project
.Stub(p => p.Items)
.Return(null)
.WhenCalled(mi => mi.ReturnValue = new ReadOnlyCollection<ProjectItem>(ProjectItems));
Project.Stub(p => p.ProjectSpecificProperties).Return(ProjectSpecificProperties);
Project.Stub(p => p.SyncRoot).Return(new Object());
}
public void AddProjectItem(ProjectItem projectItem)
{
ProjectItems.Add(projectItem);
}
}
}
Loading…
Cancel
Save