I have opted for a solution using Wizards with Project Templates mainly because some of my templates already required wizards.
I created a base class that all my other wizards are supposed to extend or that can be used by itself for just basic functionality:
public class AddTargetsWizard : IWizard
{
private const string RELATIVE_PATH_TO_TARGETS = @"..\..\..\..\PATH\TO\Custom.Tasks.Targets";
private const string TASK_NOT_FOUND_MESSAGE = @"A project of this type should be created under a specific path in order for the custom build task to be properly executed.
The build task could not be found at the following location:
{0}
Including the build task would result in unexpected behavior in Visual Studio.
The project was created, but the build task WILL NOT BE INCLUDED.
This project's builds WILL NOT benefit from the custom build task.";
private string _newProjectFileName;
private bool _addTaskToProject;
private Window _mainWindow;
public AddTargetsWizard()
{
this._addTaskToProject = true;
}
protected Window MainWindow
{
get
{
return this._mainWindow;
}
}
public virtual void BeforeOpeningFile(EnvDTE.ProjectItem projectItem)
{
}
public virtual void ProjectFinishedGenerating(EnvDTE.Project project)
{
this._newProjectFileName = project.FullName;
var projectDirectory = Path.GetDirectoryName(this._newProjectFileName);
var taskPath = Path.GetFullPath(Path.Combine(projectDirectory, RELATIVE_PATH_TO_TARGETS));
if (!File.Exists(taskPath))
{
MessageBox.Show(
this.MainWindow,
string.Format(TASK_NOT_FOUND_MESSAGE, taskPath),
"Project Creation Error",
MessageBoxButton.OK,
MessageBoxImage.Error,
MessageBoxResult.OK,
MessageBoxOptions.None);
this._addTaskToProject = false;
}
}
public virtual void ProjectItemFinishedGenerating(EnvDTE.ProjectItem projectItem)
{
}
public virtual void RunFinished()
{
if (this._addTaskToProject)
{
var project = new Microsoft.Build.Evaluation.Project(this._newProjectFileName);
project.Xml.AddImport(RELATIVE_PATH_TO_TARGETS);
project.Save();
}
}
public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
{
var dte = (EnvDTE80.DTE2)automationObject;
var mainWindow = dte.MainWindow;
foreach (var proc in System.Diagnostics.Process.GetProcesses())
{
if (proc.MainWindowTitle.Equals(mainWindow.Caption))
{
var source = HwndSource.FromHwnd(proc.MainWindowHandle);
this._mainWindow = source.RootVisual as System.Windows.Window;
break;
}
}
this.OnRunStarted(automationObject, replacementsDictionary, runKind, customParams);
}
protected virtual void OnRunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
{
}
public virtual bool ShouldAddProjectItem(string filePath)
{
return true;
}
}
This walkthrough provides a pretty good explanation on how to associate the wizard to a project (or item) template.
You'll notice that I am providing a virtual OnRunStarted
method for child wizards that would need to provide additional functionality, such as displaying wizard windows, populating the replacements dictionary, etc.
The things I don't like about this approach and/or my implementation:
- It is way more complicated than a plain project template.
- In order for my wizard windows—all WPF—to be real modal windows with Visual Studio as their owner, I found no better way than to use the current instance's title to determine the HWND and associated
Window
.
- All's well when the projects are created in the expected folder hierarchy, but Visual Studio behaves strangely (i.e. unhelpful dialogs pop up) otherwise. That's why I chose to display an error message and avoid inserting the
Import
if the current project's location does not work with my relative path.
If anyone has other ideas, I'm still all ears.