Automated generation and configuration in Visual Studio .NET
Kemp Brownvisual Studio Teammicrosoft Corporation 2002 Summary: This article describes how to use Microsoft® Visual Studio® .NET Automation Model to generate and generate and generate configuration. table of Contents
Solutions and Project Automation Generation Models Object Control Generation Configuration
Solution Generate Configuration Project Generate Configuration Startup Solutions and Project Generation Project Generate Definition Startup Project Summary Introduction Generates a project or solution in Visual Studio .NET, you need to generate according to the generated configuration associated with it. Typically, you can generate solutions or items by using the Integrated Development Environment (IDE); but the object included in the Visual Studio .NET Automation model allows you to change the generation configuration and complete the generation operations by programming how to change the generation. Using the generated model, you can select the items you want to generate and eliminate items that do not need to be generated. Alternatively, you can also use the generated configuration to determine how to generate the selected item. Two types of build configurations can be defined in Visual Studio: Solutions and projects. Using the SolutionBuild object, you can run, debug and deploy solutions or selected items by programming. For the creation of unattended generation, automated test suite, and batch jobs, it is useful to control the generation of generated configurations and the generation of projects and solutions. In addition, you can use the generated model to complete the following work:
Create, activate, and delete solution configurations. Generate, run or deploy any items or all items in the Solution Configuration. Get information about objects within the project or solution configuration. Add, delete or get information on the project generation dependencies. Most members of the Visual Studio .NET automation model and most members of the project generation object can correspond to the commands in the Visual Studio .NET IDE. For example, the SolutionBuild object is equivalent to a command in the build (generated) menu: Build Solution, Rebuild Solution, Build Project, REBUILD Project, and many more. For more information on how solutions and project build configurations, and how to set up for settings in IDE, see Builds During Application Developments in the Visual Studio product documentation (English) and Build Configurations. Solutions and Project Automation Generation Model Objects Visual Studio .NET Automation Generation Model Some objects and collections allow you to modify solutions and project build configurations and dependencies, can also start generation by programming, these objects and collections include :
Object Name Description The BuildDependency object represents a project that must be generated before the master project. The BuildDependenCIES collection contains all items that must be generated before generating the primary project. The Configuration object represents an item configuration or generate settings. For example, the Debug (debug) project configuration for the .NET platform. The Configurations collection contains all project configuration objects. The ConfigurationManager object represents the generated configuration and platform. OutputGroup objects contain files generated by the project. OutputGroups collection contains all OutputGroup objects. SolutionBuild objects are used to generate, clear, and deploy the currently active solution configuration. The SolutionConfiguration object represents the list of items to be generated and their configurations. The SolutionConfigurations collection contains all defined SolutionConfiguration objects. The SolutionContext object represents the generation context of each item in the SolutionConfiguration object. The SolutionContexts collection contains all the SolutionContext objects in the SolutionConfiguration object (one for each item). In Visual Studio .NET automation model, the hierarchical relationships between objects and collections are as follows: Detailing Visual Studio .NET automation model for a complete DTE SolutionBuild BuildDependencies BuildDependency SolutionConfigurations SolutionConfiguration SolutionContexts SolutionContext Project ConfigurationManager Configurations Configuration OutputGroups OutputGroup, please See the Automation Object Model Chart in the Visual Studio product documentation. For information on available configuration and project context, see Configuration, Configuration Properties, Solution Property Pages Dialog Box. Control generating configuration generation configuration is divided into two categories: solutions and projects. The following sections describe how to create and control each type of configuration by programming. Solution Generate Configuration Solution Generation Configuration Specifies how to generate specific items in your solution and how to deploy them (if enabled). The solution contains two default build configurations: Debug and Release. Using the Configuration Manager dialog box, you can create a new solution configuration, delete configuration, or edit an existing configuration. You can also use the SolutionConfiguration object to perform the above operations by programming. The following VSMACRO example illustrates the method of implementing the above operation. Sub SolutionConfigurationExample () 'Sets the Solution Configuration to Release.
Dim Solncfg As SolutionConfiguration = DTE.Solution. _SolutionBuild.SolutionConfigurations.Item ("release")
'Remove the comment mark in the next row will delete the currently selected configuration.
'Note: You cannot delete the last solution configuration.
'Solncfg.delete ()
'Creating a new existing debug-based generation configuration
'Solution configuration. The new configuration must be based on an existing configuration.
DTE.Solution.SolutionBuild.SolutionConfigurations.Add ("newsolncfg", _
"Debug", False)
'Activate the specified solution configuration, in this example
'"Newsolncfg".
Solncfg = DTE.Solution.SolutionBuild.SolutionConfigurations ._ s
Item ("newsolncfg")
Solncfg.activate ()
End Sub Solution Context is a row setting in the "Configuration Manager" dialog box displayed in the solution, namely: Project, Project Configuration (project configuration), Platform (platform), and whether it is generated. SolutionContext object represents this line attribute value, the SolutionContexts collection contains all rows configured for this solution. The following example lists all solutions for each item in the Debug (Debug "solution configuration. Sub SolnctX ()
'Choose the Debug (Debug) "solution configuration.
Dim Solncfg As SolutionConfiguration = DTE.Solution. _
SolutionBuild.SolutionConfigurations.Item ("debug")
Dim Solnctx as SolutionContext
Dim Solnctxs as SolutionContexts = SOLNCFG.SOLUTIONCONTEXTS
DIM MSG AS String
'Lists the solution to each item in the solution.
For Each SolnctX in Solnctxs
MSG = "project name:" & SolnctX.ProjectName & VBCR
Msg = MSG & "Configuration Name:" & Solnctx.configurationName & _
VBCR
Msg = MSG & "Platform Name:" & Solnctx.PlatformName & VBCR
MSG = MSG & "Is it generated:" & Solnctx.Shousebuild & VBCR
Msg = MSG & "Whether to deploy:" & Solnctx.SholddePloy & VBCR
MSGBOX (MSG)
NEXT
End Sub Project Generation Configuration Project Generation Configuration includes multiple sets of settings for debugging, generating, deployment, etc. These configuration settings are listed in the Project Property Pages dialog box. You can check these settings by using the right-click a project in the Solution Explorer and select Properties. Available project configuration names and platform names are displayed in the drop-down box at the top of the dialog. The "CommON Properties" and "Configuration Properties" nodes in the dialog box list the properties that you can change, all available items generated. For example, to view or change the generated attribute, click the "Build" node under "Configuration Properties". After changing the settings and select OK, the setting of the specific configuration name and the platform name is saved. Using the object in the Visual Studio .NET Automation model, you can control the settings in the project generation configuration by programming. For more information on generating a configuration, see Default and Custom Builds (English). The following VSMACRO example demonstrates how to change the value of the settings in the generation configuration of the project. Sub Projconfig () 'This example shows how to change the generated configuration of the specified item.
'"Defines the Track Constant" to set the Boolean value.
'This example requires an already loaded item.
Dim Proj as Project = DTE.vbProjects.Item (1)
Msgbox ("Project Name:" & Proj.Name)
Msgbox ("Item Configuration Properties to Change:" & Proj. _
ConfigurationManager.ActiveConfiguration.properties.Item (3) .name)
Msgbox ("Current Value:" & Proj.configurationManager. _
ActiveConfiguration.properties.Item ("Definetrace"). Value)
Proj.configurationManager.ActiveConfiguration.properties.Item_Properties.Item_
("Definetrace"). Value = TRUE
Msgbox ("New Value:" & Proj.configurationManager. _
ActiveConfiguration.properties.Item ("Definetrace"). Value)
Another way for the End Sub Control Project Configuration Properties is to use the ConfigurationManager object. The following example demonstrates how to do this. Sub cfgmgrexample ()
'This example shows how to use ConfigurationManager
'Objects to set the project configuration properties.
Dim Proj as Project = DTE.vbProjects.Item (1)
DIM MSG AS String
MSG = "Project Name:" & Proj.Name & VBCR
MSG = MSG & "Project Properties Name:" & Proj.configurationManager. _
Item (1) .properties.Item (1) .Name & VBCR
Msg = msg & "Project Property:" & Proj.configurationManager. _Item (1) .properties.Item (1) .value & VBCR
MSGBOX (MSG)
Msg = ""
Proj.configurationManager.Item (1) .properties.Item (1) .value = false
MSG = "Project Name:" & Proj.Name & VBCR
MSG = MSG & "Project Properties Name:" & Proj.configurationManager. _
Item (1) .properties.Item (1) .Name & VBCR
MSG = MSG & "Project Properties:" & Proj.configurationManager. _
Item (1) .properties.Item (1) .value & vbcr
MSGBOX (MSG)
End Sub Startup Solutions and Projects Generate Use Visual Studio .NET Automation Model, you can perform solutions and project builds by programming. The main object that implements this feature is the SolutionBuild object. Use this object:
Generate a solution by calling the Build method of the SolutionBuild object. Generate a specific item in the solution by calling the BuildProject method. Start the "debug" generation of the solution by calling the Debug method. Deploy certain items in the solution by calling the deploy method. Perform the specified startup item by calling the RUN method. Various properties of the SolutionBuild object also allow you to access:
Activity configuration (ie, the current solution configuration) will be used when generating a solution. Generate dependencies (ie, which projects rely on other projects to be generated correctly). Generate status (ie, generates whether it has been started in the current environment session, generates whether it is being processed, or whether it has been completed). The SolutionBuild object also contains a Clean method that calls this method to delete the compiler generated from the project (marked as generated in the active solution configuration). In addition, the SolutionBuild object also has other properties. To get a complete list, see SolutionBuild Object Properties, Methods, And Events. The following example demonstrates how to start the generation of the current solution configuration. This example assumes that your generated configuration is "MyBuildConfig". SUB SOLUTIONBUILDEXAMPLE ()
'Generate Activity Solution Configuration.
DIM SB As SolutionBuild = DTE.Solution.SolutionBuild
Sb.SolutionConfigurations.Item ("MyBuildConfig"). ActiVate
Sb.build
End Sub's example demonstrates how to initiate a specific project and its dependencies in the solution. This example assumes that your generated configuration name "MyBuildConfig" and the current solution contains items called "consoleApplication1". Sub ProjectBuildexample ()
'Generate the project specified in the solution.
DIM SB As SolutionBuild = DTE.Solution.SolutionBuild
Sb.buildProject ("MyBuildconfig", "consoleapplication1", false) End Sub
Note: The WaitForBuildTofinish flag is used to determine if the generated operation is reserved until the generation operation is completed. The default is false. If WaitForBuildTofinish is set to false (ie, the control is returned immediately after the start generation operation), you can use the builddone event to determine the completion of the generation operation.
Project Generation Dependency Visual Studio.net Generates a Configuration Model Allows you to use buildDependenCIES properties to define two dependencies between the two projects. The following example shows how to make the Project1 depend on Project2, which must be monitored to Project2 when generating a solution. Sub builddependenciesexample ()
'Let Project 1 depend on Project 2.
Dim p1 as project = dte.solution.Item (1) 'Project 1
Dim P2 As Project = DTE.SOLUTION.ITEM (2) 'Project 2
DIM SB As SolutionBuild = DTE.Solution.SolutionBuild
Sb.builddependencies.Item (p1.uniquename) .addproject (p2.uniquename)
End Sub Definition Start Item Startup Project Defines the project to run when starting the Visual Studio debugger. You can use the STARTUPPROJECTS properties of the SolutionBuild object to define this setting.
Note: The StartupProjects collection in the current Visual Studio .NET version only allows a startup project, but multiple startup items are allowed in future versions.
The following example demonstrates how to view and change the current startup project. Sub Startupproj ()
'This example requires two items in the solution.
DIM SB As SolutionBuild = DTE.Solution.SolutionBuild
Dim p1 as project = dte.solution.Item (1) 'Project 1
Dim P2 As Project = DTE.SOLUTION.ITEM (2) 'Project 2
Msgbox ("Current Start Project:" & Sb.StartupProjects (0))
'Change the startup item to another.
Sb.StartupProjects = p1.uniquename
Msgbox ("New Start Project:" & Sb.StartupProjects (0))