Melody's VSTA Blog

Get the latest and greatest information on VSTA here! Find new samples, workarounds, and inside tips in our downloads and blogs. Get your questions answered quickly in our forums or search our site for a FAQ. We're here to help- just ask.

Controlling Configurations

It is possible control the configurations of a VSTA project both by editing the proj file in the template and programatically.

By default, ProjectGen creates templates with two configuration settings: Release and Debug.  The user may choose to change the Active Configuration for a project to any available configuration through the configuration manager and there by change the build path of the project.  The available configurations can be easily changed by editing the csproj and vbproj files to add a configuration specific to your host, remove an unwanted configuration, or change an attribute of a configuration.  Below is the configurations taken from the application level add-in included with the ShapeAppBasicSample with a ShapeApp configuration added to them.

Alternately, the configurations can be controlled programatically.   This gets a little tricky, but here's the rules as I understand them (please add comments or email me at melody@summsoft.com if you have anything to add fix to this list):
1)  You can read in the Active Configuration (not to be confused with the current configuration) with this line:
string configName = this.macroProject.ConfigurationManager.ActiveConfiguration.ConfigurationName;

2)  You can delete a configuration, even the active configuration but not the last configuration with this line:
this.macroProject.ConfigurationManager.DeleteConfigurationRow("ConfigName");

3)  If you delete the active configuration, the next available configuration becomes the active configuration.

4)  You can read and write the build path of the active configuration with these lines:
string
buildPath = this.macroProject.ConfigurationManager.ActiveConfiguration.Properties.Item("OutputPath").Value;

this.macroProject.ConfigurationManager.ActiveConfiguration.Properties.Item("OutputPath").Value = "NewBuildPath";

5)  The project will build to the build path of the active configuration (even if it is not the selected configuration).

By using these rules together, you con programatically control the project's configuration and build path.  See the example below.


ShapeApp configurations from the csproj file included with the sample add-ins/template- this configuration builds the assembly to %MyDocs%\ShapeAppCSharp\AppAddIn\AddInName, which is the same location that the post build event included with the template and samples moves the assembly to.

<!-- This section defines properties that are set when the "ShapeApp" configuration is selected.  -->

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ShapeApp|AnyCPU' ">

      <DebugType>pdbonly</DebugType>

      <Optimize>true</Optimize>

      <OutputPath>..\..\..\..\..\Documents and Settings\User\My Documents\ShapeAppCSharp\AppAddins\$safeprojectname$</OutputPath>

      <DefineConstants>TRACE</DefineConstants>

      <WarningLevel>4</WarningLevel>

</PropertyGroup>

<!-- This section defines properties that are set when the "Debug" configuration is

       selected.

       DebugSymbols - If true, create symbols (.pdb). If false, do not create symbols.

       DebugType - If full enables a debugger to be attached to a running program.

                   If pdbonly allows source code debugging when the program is started in the debugger but will only

                   display assembler when the running program is attached to the debugger.

                   If none debugging is disabled.

       Optimize - If true, optimize the build output. If false, do not optimize.

       OutputPath - Output path of the project relative to the project file.

       DefineConstants - Constants defined for the preprocessor.

       Warning Level - Warning level for the compiler.

  -->

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">

    <DebugSymbols>true</DebugSymbols>

    <DebugType>full</DebugType>

    <Optimize>false</Optimize>

    <OutputPath>bin\Debug\</OutputPath>

    <DefineConstants>DEBUG;TRACE</DefineConstants>

    <WarningLevel>4</WarningLevel>

  </PropertyGroup>

  <!-- This section defines properties that are set when the "Release" configuration is

       selected.

       DebugType - If full enables a debugger to be attached to a running program.

                   If pdbonly allows source code debugging when the program is started in the debugger but will only

                   display assembler when the running program is attached to the debugger.

                   If none debugging is disabled.

       DebugSymbols - If true, create symbols (.pdb). If false, do not create symbols.

       Optimize - If true, optimize the build output. If false, do not optimize.

       OutputPath - Output path of the project relative to the project file.

       EnableUnmanagedDebugging - If true, starting the debugger will attach both managed and unmanaged debuggers.

       DefineConstants - Constants defined for the preprocessor.

       Warning Level - Warning level for the compiler.

  -->

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">

    <DebugType>pdbonly</DebugType>

    <Optimize>true</Optimize>

    <OutputPath>bin\Release\</OutputPath>

    <DefineConstants>TRACE</DefineConstants>

    <WarningLevel>4</WarningLevel>

  </PropertyGroup>



This code controls the configuration and build path by deleting all configurations except the ShapeApp configuration and correcting any changes that were made to the build path:

void buildEvents_OnBuildBegin(EnvDTE.vsBuildScope Scope, EnvDTE.vsBuildAction Action)
{
   bool configOK = false;

   while (!configOK)
   {
      //Ensure the configuration is ShapeApp
     
if (this.macroProject.ConfigurationManager.ActiveConfiguration.ConfigurationName != "ShapeApp")
      {
         this.macroProject.ConfigurationManager.DeleteConfigurationRow(

this.macroProject.ConfigurationManager.ActiveConfiguration.ConfigurationName);
      }
      else
     
{
      //Ensure the build path has not been modified

this.macroProject.ConfigurationManager.ActiveConfiguration.Properties.Item("OutputPath").Value = "Revit\\";

       configOK = true;
      }
   }
}




*This applies to both VSTA v 1 and v 2.
Published Wednesday, April 30, 2008 3:07 PM by Melody

Comments

  • No Comments
Anonymous comments are disabled