3

I am currently creating a project template and I was wondering if it would be possible to execute a PowerShell script after the project has been created (similar to the install.ps1 in NuGet packages).

Any chance to do this without implementing my own IWizard?

philipproplesch
  • 2,127
  • 17
  • 20

1 Answers1

2

OK... I have tried many things, but finally I ended up with the custom IWizard.

public class InitScriptWizard : IWizard
{
    public void RunStarted(
        object automationObject,
        Dictionary<string, string> replacementsDictionary,
        WizardRunKind runKind,
        object[] customParams)
    { }

    public void ProjectFinishedGenerating(Project project)
    {
        var script =
            project.ProjectItems.FindProjectItem(
                item => item.Name.Equals("init.ps1"));

        if (script == null)
        {
            return;
        }

        var process =
            System.Diagnostics.Process.Start(
                "powershell",
                string.Concat(
                    "-NoProfile -ExecutionPolicy Unrestricted -File \"",
                    script.FileNames[0],
                    "\""));

        //if (process != null)
        //{
        //    process.WaitForExit();
        //}

        //script.Delete();
    }

    public void ProjectItemFinishedGenerating(ProjectItem projectItem)
    { }

    public bool ShouldAddProjectItem(string filePath)
    {
        return true;
    }

    public void BeforeOpeningFile(ProjectItem projectItem)
    { }

    public void RunFinished()
    { }
}
philipproplesch
  • 2,127
  • 17
  • 20
  • This works. And don't believe [MSDN](http://msdn.microsoft.com/en-us/library/ms185301.aspx) that .dll with your wizard could only be deployed to GAC. Assembly can be unsigned and deployed with VSIX, check [this article](http://oncoding.blogspot.com/2012/04/visual-studio-template-wizards-without.html) (works great for me). – whyleee Oct 25 '13 at 17:13
  • I had an issue with var script = project.ProjectItems.FindProjectItem( item => item.Name.Equals("init.ps1")); The error was ProjectItems does not contain a definition for FindProjectItem and no extension method 'FindProjectItem' accepting a first argument of type "ProjectItems" etc. ...(are you missing a user directive or an assembly reference) Any help is much appreciated. – David Jul 29 '15 at 18:07
  • It's just a simple extension method. You can see it here: https://gist.github.com/philipproplesch/88540038e0d446c93769 – philipproplesch Jul 29 '15 at 18:12
  • Super..found that... odd right now getting project null thing exception for some reason in the ProjectFinishedGenerating line public void ProjectFinishedGenerating(Project project) – David Jul 29 '15 at 21:30
  • Do you know how to keep powershell/cmd window open when using process.start? – David Aug 03 '15 at 14:41