VS 2008 Package 备忘

  • 2019 年 10 月 5 日
  • 笔记

今天研究了一下vs 2008 package的部署,即怎样放置package中的dll、ProjectTemplate和ItemTemplate,趁着热乎劲先记录下来,免得以后忘记。

首先,创建一个简单的package,供测试之用。

打开visual studio 2008,新建项目,选择其他项目类型->扩展性->Visual Studio Integration Package,输入项目名称为DeployPackage,如下图:

点击确定后,弹出创建package的向导,选择C#作为开发语言,选中Menu Command。如下两图:

完成向导后,vs帮我们创建了空的package项目,如下图:

双击Guids.cs文件,添加一个guid,作为接下来要创建的ProjectFactory的guid,修改后的Guids.cs文件如下:

1: using System;   2:     3: namespace Company.DeployPackage   4: {   5:     static class GuidList   6:     {   7:         public const string guidDeployPackagePkgString = "bad3390c-b2a2-4bfc-a3ad-87e8119df413";   8:         public const string guidDeployPackageCmdSetString = "b09083be-4856-4364-8bb1-243d133c7177";   9:    10:    11:         //该Guid为接下来要创建的ProjectFactory的guid  12:         public const string guidDixProjectFactoryString = "9819EC75-61CC-468c-9823-E853AEE0FE8C";  13:    14:         public static readonly Guid guidDeployPackageCmdSet = new Guid(guidDeployPackageCmdSetString);  15:     };  16: }

新增一个ProjectFactory,名为DixProjectFactory,如下:

1: using System;   2: using System.Runtime.InteropServices;   3: using Microsoft.VisualStudio.Project;   4: using IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;   5:     6: namespace Company.DeployPackage   7: {   8:     //注意这个guid   9:     [Guid(GuidList.guidDixProjectFactoryString)]  10:     public class DixProjectFactory : ProjectFactory  11:     {          12:         public DixProjectFactory(DeployPackagePackage package)  13:             : base(package)  14:         {  15:         }  16:    17:         protected override ProjectNode CreateProject()  18:         {  19:             DixProjectNode node = new DixProjectNode();  20:             node.SetSite((IOleServiceProvider)((IServiceProvider)Package).GetService(typeof(IOleServiceProvider)));  21:             return node;  22:         }  23:     }  24: }

注意DixProjectFactory的Guid就是刚刚在Guids.cs文件中的那个Guid。

注意:需要下载MPFProjectBase才能使用ProjectFactory,以及下面的ProjectNode、ProjectPackage

新增一个ProjectNode类,名为DixProjectNode,如下:

1: using System;   2: using Microsoft.VisualStudio.Project;   3:     4: namespace Company.DeployPackage   5: {   6:     public class DixProjectNode : ProjectNode   7:     {   8:         /// <summary>   9:         /// 返回DixProjectFactory的Guid  10:         /// </summary>  11:         public override Guid ProjectGuid  12:         {  13:             get { return typeof(DixProjectFactory).GUID; }  14:         }  15:    16:         public override string ProjectType  17:         {  18:             get { return "Dix"; }  19:         }  20:     }  21: }

修改DeployPackagePackage类,使它继承于ProjectPackage(原来是继承与Package的),并添加一个ProvideProjectFactoryAttribute,如下:

1: ...   2: [ProvideProjectFactory(typeof(DixProjectFactory), "我的dix项目", "Dix Project Files (*.dixproj);*.dixproj", "dixproj", "dixproj", "", LanguageVsTemplate = "DiX")]   3: public sealed class DeployPackagePackage : ProjectPackage   4: {   5:     ...   6: }

修改DeployPackagePackage类的Initialize方法,添加RegisterProjectFactory(new DixProjectFactory(this))语句,修改后的Initialize方法如下:

1: protected override void Initialize()   2: {   3:     Trace.WriteLine (string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));   4:     base.Initialize();   5:     6:     // Add our command handlers for menu (commands must exist in the .vsct file)   7:     OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;   8:     if ( null != mcs )   9:     {  10:         // Create the command for the menu item.  11:         CommandID menuCommandID = new CommandID(GuidList.guidDeployPackageCmdSet, (int)PkgCmdIDList.cmdidMyCommand);  12:         MenuCommand menuItem = new MenuCommand(MenuItemCallback, menuCommandID );  13:         mcs.AddCommand( menuItem );  14:     }  15:    16:     //注册ProjectFactory  17:     RegisterProjectFactory(new DixProjectFactory(this));  18: }

下面为该package添加ProjectTemplate和ItemTemplate。

添加Templates、Projects、Items等文件夹和文件,添加后如下图:

注意这里面的cs文件需要改一下Build Action,否则编译不过。也可以把这堆文件的Build Action设置成ZipProject和ZipItem,但为了更加清楚模板的部署方式,我只是把他们的Build Action设置成了None。

Projects文件夹中放置Project模板,Items文件夹中放置ProjectItem模板。

Project模板以WinForm为例,要注意WinForm.vstemplate文件,该文件的定义如下:

<VSTemplate Version="2.0.0" Type="Project" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">      <TemplateData>          <Name>DiX WinForm Application</Name>          <Description>Create Dix WinForm Application</Description>          <Icon>WinForm.ico</Icon>          <!--ProjectType要和Package里的LanguageVsTemplate相同-->          <ProjectType>DiX</ProjectType>          <SortOrder>20</SortOrder>          <NumberOfParentCategoriesToRollUp>1</NumberOfParentCategoriesToRollUp>          <CreateNewFolder>true</CreateNewFolder>          <DefaultName>WinFormApplication</DefaultName>          <ProvideDefaultName>true</ProvideDefaultName>          <PromptForSaveOnCreation>false</PromptForSaveOnCreation>      </TemplateData>      <TemplateContent>          <Project File="WinForm.dixproj" TargetFileName="$safeprojectname$.dixproj" ReplaceParameters="true">              <ProjectItem ReplaceParameters="true" OpenInEditor="true" TargetFileName="Form1.dix">Form1.dix</ProjectItem>          </Project>      </TemplateContent>  </VSTemplate>

ProjectItem模板以Class为例,要注意Class.vstemplate文件,该文件的定义如下:

<?xml version="1.0" encoding="utf-8"?>  <VSTemplate Version="3.0.0" Type="Item" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005">    <TemplateData>      <Name Package="{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}" ID="2245" />      <Description Package="{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}" ID="2262" />      <Icon Package="{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}" ID="4515" />      <TemplateID>Dix.Class</TemplateID>      <ProjectType>Dix</ProjectType>      <RequiredFrameworkVersion>2.0</RequiredFrameworkVersion>      <NumberOfParentCategoriesToRollUp>1</NumberOfParentCategoriesToRollUp>      <DefaultName>Class.cs</DefaultName>    </TemplateData>    <TemplateContent>      <ProjectItem ReplaceParameters="true">Class.cs</ProjectItem>    </TemplateContent>  </VSTemplate>

在这两个vstemplate文件里,ProjectType要和DeployPackagePackage类声明中的ProvideProjectFactoryAttribute标记的LanguageVsTemplate相同,即都要是“Dix”。其他的文件是随便写的,里面的内容对于本次测试并不重要。

好了,用于测试的Package已经做完了,编译成功后,下面开始测试如何在vs 2008里应用它。

1.在visual studio Experimental hive中测试

在visual studio Experimental hive中测试比较简单,不需要再手动注册package了,因为我们的Package项目在编译成功后已经被自动注册到visual studio Experimental hive中了。编译时的输出片段如下:

1: f:Microsoft Visual Studio 2008 SDKVisualStudioIntegrationToolsbinRegPkg.exe /root:SoftwareMicrosoftVisualStudio9.0Exp "/pkgdeffile:objDebugDeployPackage.pkgdef" "D:DeployPackageDeployPackagebinDebugDeployPackage.dll"    2: f:Microsoft Visual Studio 2008 SDKVisualStudioIntegrationToolsbinRegPkg.exe /root:SoftwareMicrosoftVisualStudio9.0Exp /ranu /codebase "D:DeployPackageDeployPackagebinDebugDeployPackage.dll"    3: C:Program FilesMicrosoft Visual Studio 9.0Common7IDEdevenv.com /rt "SoftwareMicrosoftVisualStudio9.0Exp" /RANU /setup /nosetupvstemplates

前两句执行完之后,在注册表的HKEY_CURRENT_USERSoftwareMicrosoftVisualStudio9.0ExpConfigurationInstalledProducts下可以看到该package的名称,在HKEY_CURRENT_USERSoftwareMicrosoftVisualStudio9.0ExpConfigurationPackages可以看到以该package的GUID为名的子项,在Projects,Menus等目录下也可以找到该package的信息。

编译成功后,点击开始菜单,依次选择所有程序,Microsoft Visual Studio 2008 SDK,Tools,Start Microsoft Visual Studio 2008 SP1 under Experimental hive打开visual studio Experimental hive,可以看到在工具菜单下已经有了我们的package中的Menu Command,但是新建一个项目的时候,并没有出现我们的项目模板。这是因为我们并没有把模板部署到vs。

注意上面的编译时输出的第3句C:Program FilesMicrosoft Visual Studio 9.0Common7IDEdevenv.com /rt "SoftwareMicrosoftVisualStudio9.0Exp" /RANU /setup /nosetupvstemplates,这句命令最后有个/nosetupvstemplates的开关,意思是不需要安装vstemplate。编译package自动加上这个开关的原因是vs并不知道我们的package中存在template,如果我们把上面那堆模板文件设置成ZipProject或者ZipItem的话,vs在编译这个package的时候,就不会自动加这个开关了,这样编译后,模板就会自动拷贝到vs识别的目录里。

由于我并没有设置Build Action为ZipProject或者ZipItem,所以现在要手动的去压缩模板:

将Project模板分别压缩成WinForm.zip和ConsoleApp.zip,在C:Documents and SettingsUser NameApplication DataMicrosoftVisualStudio9.0ExpProjectTemplates下建立Dix目录,并在Dix目录下建立Windows和控制台两个子目录,把WinForm.zip和ConsoleApp.zip分别放到这两个目录下。

将ProjectItem模板分别压缩成WinForm.zip和Class.zip,在C:Documents and SettingskongxiangxinApplication DataMicrosoftVisualStudio9.0ExpItemTemplates下建立Dix目录,并在Dix目录下建立Windows Form和代码两个子目录,把WinForm.zip和Class.zip分别放到这两个目录下。

注意,在压缩成zip文件的时候,只压缩文件,不要把文件夹整个压缩。

运行"C:Program FilesMicrosoft Visual Studio 9.0Common7IDEdevenv.com" /rt "SoftwareMicrosoftVisualStudio9.0Exp" /RANU /installvstemplates,等这条命令运行成功后,启动visual studio Experimental hive,新建项目,可以看到项目模板已经出现在新建项目对话框里了。

新建一个Dix WinForm Application的项目,然后添加新项,可以看到项模板出现在添加新项对话框里:

2.在visual studio中测试

在visual studio里测试需要自己运行命令去注册package,可以参考package编译时执行的命令,把root参数和ranu参数去掉就可以了:

1: f:Microsoft Visual Studio 2008 SDKVisualStudioIntegrationToolsbinRegPkg.exe /codebase "D:DeployPackageDeployPackagebinDebugDeployPackage.dll"    2:     3: C:Program FilesMicrosoft Visual Studio 9.0Common7IDEdevenv.com /setup

后记:完成这篇文章的时候,已经离开始写这篇文章有两个星期的时间了。一开始是没有时间写,拖的久了也就不想写了。所以后面的内容写的不够详细。