i've got a similiar Problem like the question here: Create MSBuild custom task to modify C# code *before* compile
I need to change .cs files before compilation. Of course i don't want them to get changed in place, because of the version control. I already figured out, that a Custom MSBuild Task might be the right choice to do the job.
Quote of the answer from the above question:
Create custom task that accepts the list of cs files to adapt prior to compilation
The custom task adapts the list of files received and creates them on disk
The custom task sets the list of changed files on the output parameter
The output of the task would replace the original cs files list
The compilation is done against the changed files.
The solution seems clear and fine, but what i can't find out (all for Visual Studio 2012):
What target should that task get? BeforeCompile or is this too late?
How exactly can i give the "files for compilation" as parameter into the task? How is it defined?
How is the output defined exactly so that the compilation uses the modified files?
Thanks, it would be nice if somebody can help me with an example :-)
A customer of mine asked me to replace some version and copyright information in some AssemblyInfo.cs files prior to the compile. I'm doing this via FileUpdate task with a RegEx in place. This of course leads to modified files. But that's no problem at all, just use the Exec task and run "svn revert" at the end of the build job:
<Target Name="RevertModifications">
<Message Text="reverting modifications..." />
<Exec Command="svn revert -R $(RootDir)"/>
</Target>
Perhaps that might help?
Related
I'm trying to create post build events to copy the final .js and .debug.js files for my script# projects into the proper directories. I can't use the regular output folder, since I have more than one project that references another project, and that always results in a build error (Unable to copy referenced script because it is being used by another process).
The problem is that the C# compiler appears to run the post build events BEFORE it writes the actual .js files, so they don't exist when the post build event happens.
Is there any other solution to make this work?
You can set up a DeploymentPath property in your csproj and the generated scripts will be copied there.
All of the logic is in here: https://github.com/nikhilk/scriptsharp/blob/cc/src/Core/Build/Tasks/ScriptCompilerTask.cs ... so another option is to customize the build task to exactly your requirements.
The latest work if you check out the github repo, also has the script# part of the build process done during the build step of an msbuild project, so that should free up the post-build step for you to do what you'd like with the generated scripts. See https://github.com/nikhilk/scriptsharp/blob/cc/src/Core/Build/ScriptSharp.targets. Again, its just msbuild stuff, so you could potentially customize the .targets file to your liking as well if it doesn't fit your needs.
I got around this by adding the "copy" command as a pre build step on the projects that were using the script# project output, then adding a dependency so that the script# project would be built first.
I'm trying to use the TransformXml task to get all the config transformed at one shot irrespective of the build configuration that is selected in visual studio. I'm able to accomplish it by editing the .csproj file and here is it.
<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>
<Target Name="TransformWebConfig" AfterTargets="AfterPublish">
<TransformXml Source="$(SolutionDir)WCFServices\Web.config"
Transform="$(SolutionDir)WCFServices\Web.Release.config"
Destination="$(OutDir)WebRelease.config"
StackTrace="true" />
</Target>
The problem here is while I do a publish, the transformed config files gets placed in the output dir but this happens before the delete operation. I would need the configs to be transformed after the delete operation.
Please advise?
Beware MSBuild and VSBuild DO differ. Afterpublish is VERY limited
Try having msbuild save the emitted sln Also here.
Have you verified the file is getting transformed at the expected time? Add in a <message importance="high" text="Doing Custom Transform" /> before your transformxml call, then another message with different text Finished Custom Transform after this call.
This post speaks directly to your desire to do things involving the specialized publishing targets.
General msbuild debugging information:
Hopefully this behavior IS msbuild dependant instead of VSBuild, since the two build processes differ. If you are in luck and this is following the same behavior as MSBuild would follow the msbuild debugging process:
As with any magical behavior in msbuild, in your CSProj file look for any import target= code and go find those files. Following these through will lead you to the part that is making the decision about any of this.
Also you can attach VS's debugger to your build process - http://blogs.msdn.com/b/visualstudio/archive/2010/07/06/debugging-msbuild-script-with-visual-studio.aspx + http://blogs.msdn.com/b/visualstudio/archive/2010/07/09/debugging-msbuild-script-with-visual-studio-2.aspx
Determining build order
Also in crank up the build verbosity temporarily http://blogs.msdn.com/b/msbuild/archive/2005/09/29/475157.aspx
Is it possible to update the version number(AssemblyVersion("1.0.2.1")) insdie AssemblyInfo.cs outside visual studio???(Maybe using a script or a batch file)
I have used UpdateVersion in the past to update the version number as part of a build script before. If you use subversion you can also use SubWCRev.exe from TortoiseSVN to update the version number to include the SVN revision number.
It can also be done from MSBuild..
Check out the AssemblyInfo task here:
MSBuild tasks
We use it from CruiseControl.net it's very straightforward to use.
I used to have a PowerShell script that would write the version number out to "AssemblyVersion.cs", like this:
// DO NOT EDIT
// Generated by UpdateVersion.ps1.
[AssemblyVersion("1.0.2.1")]
The version number was updated in all projects/assemblies at once this way. The canonical number was stored elsewhere.
It's trivial to write, but varies depending on your exact needs, so I won't try to post it here.
You can simply use any search and replace tool or write one yourself.
For the current porject (that is built with msbuild) I'm using a custom build step (self written as it is small and simple) that searches for AssemblyVersion and replaces that in the AssemblyInfo.cs files.
In C++ projects there is the possibility to set a custom build step for files. Is there a similar functionality in C# projects? I couldn't really find anything.
One idea would be to create a second project (makefile or c++) and move the files there.
MsBuild should work for you although it might take some time to figure out how it works. It appears that you can setup a step that runs prior to building each .cs file by separating each .cs file into its own build group.
In MSBuild script for compiling each .cs file into an EXE, Dino Chiesa comments:
By using the %(CSFile.identity)
scalar, we run this task once for each
file. The converse would be
#(CSFile.identity). That would run
the compile once, for all files,
compiling them all together into a
single assembly.
Also, these links might help:
Custom build step for C# files
Master Complex Builds with MSBuild
No custom build step for individual files with C# projects. You could probably hack something together with MSBuild...
Look at the BeforeBuild and AfterBuild targets in your csproj file.
I think you are on the right track with your comment about multiple projects. Combine this with the fact that you can include multiple projects within a single Solution and you may have your answer. I use this functionality to build several components at a time and it works quite well.
I have a custom program which preprocesses a C# file and generates a new C# file as output. I would like to invoke this from msbuild on each of the C# files in the project, then compile the output files instead of the original C# files. How would I go about this?
You might want to look into using the "custom tool" code generation techniques in Visual Studio; there's an article about it on CodeProject
I'm not sure if there is an easy way to perform it with default msbuild tasks. But you can create your own task to do whatever you want:
How To: Implementing Custom Tasks - Part I
Also you can search for suitable tasks at "MSBuild community tasks" site.
If your custom program is an executable the easiest way would be to call the executable with Exec:
<Exec Command="your executable" />
I would recommend writing a custom MSBuild task though. You can look at the SDC Tasks for samples on how to do it.