从向导访问 Visual Studio 项目模板 zip 存档的内容 [英] Access contents of Visual Studio project template zip archive from Wizard

查看:25
本文介绍了从向导访问 Visual Studio 项目模板 zip 存档的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 Visual Studio 构建自定义的多项目模板,但我有一些文件资产不属于多项目模板中的任何项目.我想将这些静态资产从模板的 .zip 存档中提取到新项目的目标目录中.

I am trying to build a customized multi-project template for Visual Studio, and I have some file assets that do not belong to any of the projects within the multi-project template. I would like to extract these static assets from the template's .zip archive into the destination directory of the new project.

我已将模板设置为使用 .vstemplate 文件中的向导:

I have the template set up to use a wizard in the .vstemplate file:

<WizardExtension>
    <Assembly>TestWizard, Version=1.0.0.0, Culture=neutral, PublicKeyToken=...</Assembly>
    <FullClassName>TestWizard.Wizard</FullClassName>
</WizardExtension>

我还可以确认向导正在执行IWizard界面中的RunStarted方法:

I can also confirm that the wizard is executing the RunStarted method in the IWizard interface:

public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
{
    MessageBox.Show("This works just fine");
}

如何访问包含所有模板资产的 .zip 文件并将这些资产提取到目标目录?我没有看到任何可以让我访问文件系统中任一位置的属性.

How can I access the .zip file that contains all the template assets and extract those assets to the destination directory? I'm not seeing any properties that would give me access to either location in the filesystem.

推荐答案

TemplateWizard 来帮助我.我想我们想要实现的更多是与 VS 自动化有关,而不是向导.

Could not find anything in MSDN doco on TemplateWizard to help me out. I guess what we're trying to achieve is more to do with VS automation rather then the wizard.

无论如何,单步执行代码并查看您在 IWizard.RunStarted 我发现 customParams[0] 的第一项指向正在创建的项目模板.doco 中没有任何内容可以为您提供有关它的线索.(选项 1)

Anyway, stepping through the code and looking at what context you get in IWizard.RunStarted I found that the first item of customParams[0] points at the project template being crated. Nothing in the doco to give you a clue about it. (Option 1)

获得与上述相同路径的另一种方法是通过 VS 解决方案获得它.在 MSDN 论坛.(选项 2)

Another way to get the same path as described above is to get it via the VS solution. Found the code for this option on MSDN forum. (Option 2)

这应该为您提供了一种获取打包到项目模板中的额外项目的方法.现在由您决定将它们纳入解决方案并将它们添加为项目或其他任何内容.您可以通过传入的上下文 (DTE)automationObject.Solution 访问解决方案.

This should give you a way to get to the extra items you have packaged into your project template. Now its up to you to get them into the solution and add them as items or whatever. You have access to the solution via passed in context (DTE)automationObject.Solution.

下面的代码没有错误检查这是我的 POC.此外,由于您正在获取物品,因此您需要替换/扩展可能位于这些物品内的令牌.代码是使用VS2013开发和测试的,不知道为什么它在早期版本中不起作用.

The code below has no error checking this was my POC. Also since you're getting the items across it's on you to replace/expand tokens that may be inside those items. Code was developed and tested using VS2013, can't see why it would not work in the earlier versions.

public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
{
    // Pick one of the options below, error checking skipped for brevity.
    // Option 1
    var tempaltePath = customParams[0] as string;

    // Option 2
    var templatePath = ((DTE)automationObject.Solution as Solution2).GetProjectTemplate("Sc.Accelerator.zip", "CSharp");

    // Get the source and destination folders, error checking skipped for brevity.
    var packagePath = Path.GetDirectoryName(templatePath as string);

    // Found this by looking at the dictionary, undocumented token.
    var solutionPath = replacementsDictionary["$solutiondirectory$"]; 

    // Alternative, did not work for me, may be it should to be called from IWizard.RunFinished
    // var solutionPath = Path.GetDirectoryName((DTE)automationObject.Solution.FullName);

    // Copy a file from project package into solution folder, error checking skipped for brevity.
    File.Copy(Path.Combine(packagePath, ".gitignore"), Path.Combine(solutionPath, ".gitignore"));

    // ... 
}

这篇关于从向导访问 Visual Studio 项目模板 zip 存档的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆