0

For the gurus out there in batch file writing and Visual Studio (most significantly Visual Studio 2013), I would like to know if it is possible to do the following:

I have a series of projects part of a solution, many of which are dependent on the dll's generated by other projects (in a waterfall fashion really). I would like to know if it is possible to write a batch file to build all projects to generate the corresponding dll's, and then update their references to the newly assembled dll's (also with the batch file).

Is this even possible? I would like avoid using extensions like NuGet or any other software.

1 Answer 1

2

You pick the right language/tool to do builds. msbuild is the tool (and kind of the language) that you write this kind of stuff. they are called build-scripts. they are NOT bat scripts. do not use .bat scripts. that's what people did 20 years ago.

Below is a basic msbuild script file. It will

  1. Build the .sln
  2. copy the files of one of the csproj's (usually the GUI csproj) to a folder
  3. zip the files into a zip file

You would put this in a file called "MyBuildScript.proj" (or MyBuildScript.msbuild)

After you do that, you will execute the file with msbuild.exe

"%WINDIR%\Microsoft.NET\Framework\v4.0.30319\msbuild.exe" /target:AllTargetsWrapped "MyBuildScript.proj" /p:Configuration=Debug;FavoriteFood=Popeyes /l:FileLogger,Microsoft.Build.Engine;logfile=MyBuildScript.proj.Release.log

(MyBuildScript.proj)

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="AllTargetsWrapped">

    <PropertyGroup>
        <!-- Get this project from http://msbuildextensionpack.codeplex.com/releases/view/105659 -->
        <MSBuildExtensionPackFoundPath Condition="Exists('$(MSBuildExtensionsPath64)\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks')">$(MSBuildExtensionsPath64)\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks</MSBuildExtensionPackFoundPath>
        <MSBuildExtensionPackFoundPath Condition="Exists('$(MSBuildExtensionsPath32)\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks')">$(MSBuildExtensionsPath32)\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks</MSBuildExtensionPackFoundPath>
        <MSBuildExtensionPackFoundPath Condition="Exists('$(MSBuildExtensionsPath)\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks')">$(MSBuildExtensionsPath)\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks</MSBuildExtensionPackFoundPath>
        <MSBuildExtensionPackFoundPath Condition="$(MSBuildExtensionPackFoundPath)==''">CouldNotFindBaseDirectoryCheckForInstalledProduct\MSBuild.ExtensionPack.tasks</MSBuildExtensionPackFoundPath>       
    </PropertyGroup>
    <Import Project="$(MSBuildExtensionPackFoundPath)"/>

    <PropertyGroup>
        <!-- Always declare some kind of "base directory" and then work off of that in the majority of cases  -->
        <WorkingCheckout>.</WorkingCheckout>
        <WorkingDir>.</WorkingDir>
        <ArtifactDestinationFolder>$(WorkingCheckout)\ZZZArtifacts</ArtifactDestinationFolder>
        <ZipArtifactDestinationFolder>$(WorkingDir)\ZZZZipArtifacts</ZipArtifactDestinationFolder>      
    </PropertyGroup>

    <Target Name="AllTargetsWrapped">

        <CallTarget Targets="CleanArtifactFolder" />
        <CallTarget Targets="BuildItUp" />
        <CallTarget Targets="CopyFilesToArtifactFolder" />
        <CallTarget Targets="ZipItUp" />
    </Target>

    <Target Name="BuildItUp" >
        <MSBuild Projects="$(WorkingCheckout)\Solution1.sln" Targets="Build" Properties="Configuration=$(Configuration)">
            <Output TaskParameter="TargetOutputs" ItemName="TargetOutputsItemName"/>
        </MSBuild>
        <Message Text="BuildItUp completed" />
    </Target>

    <Target Name="CleanArtifactFolder">

        <RemoveDir Directories="$(ArtifactDestinationFolder)" Condition="Exists($(ArtifactDestinationFolder))"/>
        <MakeDir Directories="$(ArtifactDestinationFolder)" Condition="!Exists($(ArtifactDestinationFolder))"/>

        <RemoveDir Directories="$(ZipArtifactDestinationFolder)" Condition="Exists($(ZipArtifactDestinationFolder))"/>
        <MakeDir Directories="$(ZipArtifactDestinationFolder)" Condition="!Exists($(ZipArtifactDestinationFolder))"/>               

        <Message Text="Cleaning done" />

    </Target>

    <Target Name="CopyFilesToArtifactFolder">

        <ItemGroup>
            <MyExcludeFiles Include="$(WorkingDir)\**\*.doesnotexist" />
        </ItemGroup>

        <ItemGroup>
            <MyIncludeFiles Include="$(WorkingDir)\CsProjectOne\bin\$(Configuration)\**\*.*" Exclude="@(MyExcludeFiles)"/>
        </ItemGroup>        

        <Copy
                SourceFiles="@(MyIncludeFiles)"
                DestinationFiles="@(MyIncludeFiles->'$(ArtifactDestinationFolder)\%(Filename)%(Extension)')"
        />  <!-- %(RecursiveDir)  -->

        </Target>

        <Target Name="ZipItUp">

            <ItemGroup>
                <NonConfigFilesExcludeFiles Include="$(ArtifactDestinationFolder)\**\*.doesnotexist" />
            </ItemGroup>

            <ItemGroup>
                <NonConfigFilesIncludeFiles Include="$(ArtifactDestinationFolder)\**\*" Exclude="@(NonConfigFilesExcludeFiles)"/>   
            </ItemGroup>

            <!-- Create a zip file based on the FilesToZip collection -->
            <MSBuild.ExtensionPack.Compression.Zip TaskAction="Create" CompressFiles="@(NonConfigFilesIncludeFiles)" RemoveRoot="$(ArtifactDestinationFolder)" ZipFileName="$(ZipArtifactDestinationFolder)\MyOutputFile.zip"/> 
            <!-- -->

        </Target>       

    </Project>

That is how you write build-logic. There are other build tools out there, but this is the default dotNet one.

Do NOT write crappy, hard to maintain .bat files.

Use the correct tool for the job.

PS "msbuildextensionpack" is an example of extensions for msbuild. there are MANY MANY helpful extensions for msbuild. 99% of the time, somebody has written a msbuild extension to help do what you need to do.

Need to send a file to an ftp client destination? Somebody already wrote a task.

Need to manipulate some .xml file? Somebody already wrote a task.

Need to zip a file (like in this example). Somebody already wrote a task.

Need to do........anything mainstream........ Somebody already wrote a task.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.