从自定义项目模板执行PowerShell脚本

时间:2013-01-06 01:18:46

标签: visual-studio powershell visual-studio-2012 nuget project-template

我目前正在创建一个项目模板,我想知道在项目创建后是否可以执行PowerShell脚本(类似于NuGet包中的 install.ps1 )。 / p>

如果没有实现我自己的IWizard

,有机会这样做吗?

1 个答案:

答案 0 :(得分:2)

好的......我尝试了很多东西,但最后我最终得到了自定义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()
    { }
}