Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a command line code generator to Visual Studio?

I'm working on a project that uses code generation to generate C# classes using a command line tool from a text-based description. We are going to start using these descriptions for javascript too.

Currently these classes are generated and then checked in, however, I would like to be able to make the code generate automatically so that any changes are propagated to both builds.

The step that is run manually is:

servicegen.exe -i:MyService.txt -o:MyService.cs

When I build I want MSBuild/VS to first generate the CS file then compile it. It is possible to do this using, by modifying the csproj, perhaps using a MSBuild Task with Exec, DependentUpon & AutoGen?

like image 254
Dave Hillier Avatar asked Mar 27 '13 22:03

Dave Hillier


People also ask

How do I add command line options in Visual Studio?

To set command-line arguments in Visual Studio, right click on the project name, then go to Properties. In the Properties Pane, go to "Debugging", and in this pane is a line for "Command-line arguments." Add the values you would like to use on this line. They will be passed to the program via the argv array.

How do I get the Visual Studio command line code?

To get an overview of the VS Code command-line interface, open a terminal or command prompt and type code --help . You will see the version, usage example, and list of command line options.

How do you add a command palette in VS Code?

VS Code is equally accessible from the keyboard. The most important key combination to know is Ctrl+Shift+P, which brings up the Command Palette.

How do I add an extension to VS Code?

You can browse and install extensions from within VS Code. Bring up the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of VS Code or the View: Extensions command (Ctrl+Shift+X). This will show you a list of the most popular VS Code extensions on the VS Code Marketplace.


1 Answers

Normally I would recommend a pre-build command be placed in a pre-build event, but since your command line tool will be creating C# classes needed for compiling, this should be done in the BeforeBuild target in the .csproj file. The reason for this is because MSBuild looks for the files it needs to compile between the time BeforeBuild is called and the time when PreBuildEvent is called in the overall process (you can see this flow in the Microsoft.Common.targets file used by MSBuild).

Call the Exec task from within the BeforeBuild target to generate the files:

<Target Name="BeforeBuild">
  <Exec Command="servicegen.exe -i:MyService.txt -o:MyService.cs" />
</Target>

See the Exec task MSDN documentation for more details about specifying different options for the Exec task.

like image 123
Michael Avatar answered Oct 26 '22 08:10

Michael