In Visual Studio, can I make one file to run another custom tool? (in this case using Xsd2Code)

I am trying to figure out if this is possible when using a custom tool in Visual Studio to change the contents of one file, call a custom tool of another.

My scenario is this:

In a Visual Studio C # project, I have an xml schema "master.xsd" that includes several other xsd files. I am using the Xsd2Code Visual Studio Custom Tool to generate .cs from a schema. This works fine when master.xsd itself changes, but I would like the user tool to run in the master.xsd file when one of the other xsds changes.

Is there a way for one file to run another custom tool?

[EDIT - more on why I am studying using this tool for this]

We currently have a GenerateFiles.bat file that calls Xsd2Code from the command line to generate code from the schemas (as suggested by MattDavey below). It works too slow.

The problem is that with every build Xsd2Code will be executed, but since many other projects depend on this project with schemas, they will all be recompiled, although probably nothing has changed. The bottom line is that even a minor change in unit test involves half the recompilation of projects. That's why we considered the custom tool approach to only generate code files if the schema changes.

+7
source share
1 answer

I use Xsd2Code a lot, but my approach is to add a pre-build event that calls the Xsd2Code command line and regenerates xml for each assembly.

My pre-build event is as follows:

$(ProjectDir)BuildTools\Xsd2Code.exe $(ProjectDir)Api\Schemas\MySchema.xsd MyProject.Api.Schemas $(ProjectDir)Api\Schemas\MySchema.cs /platform Net40 /collection Array /sc+ /ap+ /if- /xa+ 

In your case, you can perform this pre-build step only on the main xsd (which I guess xsd: Imports other schemas), or you can run the command for each of your schema files separately.

The advantage of this is that if I change the XSD schema, I get very useful compile-time errors :)

Hope you get some ideas!

EDIT

I spent some time thinking about the problem you identified regarding the build time, and modified the pre-build script as follows:

 $(ProjectDir)BuildTools\Xsd2Code.exe $(ProjectDir)Api\Schemas\MySchema.xsd MyProject.Api.Schemas $(ProjectDir)Api\Schemas\MySchema.cs.temp /platform Net40 /collection Array /sc+ /ap+ /if- /xa+ fc $(ProjectDir)Api\Schemas\MySchema.cs $(ProjectDir)Api\Schemas\MySchema.cs.temp if errorlevel 1 copy $(ProjectDir)Api\Schemas\MySchema.cs.temp $(ProjectDir)Api\Schemas\MySchema.cs /Y del $(ProjectDir)Api\Schemas\MySchema.cs.temp 

So, Xsd2Code now generates the source code in a temporary file that only overwrites the existing .cs file if it is different. This should mean that if .xsd has not changed at all, then .cs will not be generated :)

You still take a hit at running xsd2code, but you don't take a hit at msbuild, rebuilding a whole chain of projects if the generated source was the same.

+4
source

All Articles