Rick Strahl
Rick Strahl

Reputation: 17671

Generation and Compilation of .resources files with MsBuild

I have a process where I need to automate the process of generating the satellite assemblies. Specifically this is for WPF and combining Resx and BAML resources.

I've got a build script that works, but it requires manual adding of the .resources files I want to combine with the BAML resources. IOW, I have to add to the build script each time I add a .Resx resource. Not cool!

Currently I'm running the assembly linker manually and the script looks like this:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- Adds the build action 'LocBamlCsv' -->
  <ItemGroup>
    <AvailableItemName Include="LocBamlCsv" />
  </ItemGroup>


  <Target Name="CreateSatelliteAssemblies"
          DependsOnTargets="$(CreateSatelliteAssembliesDependsOn)">

    <!-- Locbaml needs the runtime assemblies in the intermediate dir -->
    <Copy SourceFiles="$(ProjectDir)..\Tools\LocBaml.exe"
          DestinationFolder="$(OutputPath)" />


    <!-- generate a .resources file for .csv merged output -->
    <Exec Command="LocBaml /generate ..\..\$(IntermediateOutputPath)$(TargetName).g.$(UICulture).resources /trans:%(LocBamlCsv.FullPath) /out:../../$(IntermediateOutputPath) /cul:%(LocBamlCsv.Culture)"
          WorkingDirectory="$(OutputPath)"
          Outputs="$(OutputPath)%(LocBamlCsv.Culture)\$(TargetName).$(UICulture).dll" />

    <!-- Generate the resource assembly by merging all .resources files -->
    <!-- NOTE: Explicitly add any resource files here -->
    <Exec Command="al /template:$(TargetName).exe /culture:%(LocBamlCsv.Culture) /out:%(LocBamlCsv.Culture)\$(TargetName).resources.dll /embed:$(TargetName).g.%(LocBamlCsv.Culture).resources /embed:$(TargetName).Properties.Resources.%(LocBamlCsv.Culture).resources"
          WorkingDirectory="$(InterMediateOutputPath)"
          />


  </Target>
</Project>

As mentioned it works. but the last command that calls al would be much easier to work with if there was some way to use wild cards (ie. $(TargetName).*s.%(LocBamlCsv.Culture).resources.

I've tried a number of things. Using the build process apparently fires at the wrong time and it ends up failing to find files.

Upvotes: 1

Views: 3374

Answers (1)

Sayed Ibrahim Hashimi
Sayed Ibrahim Hashimi

Reputation: 44342

I'm not sure exactly what your problem is but you did say something that makes me wonder. "Using the build process apparently fires at the wrong time and it ends up failing to find files." From this I get the impression that you are trying to create an item which contains files that are generated during the build process. If this is the case then you should declare those as dynamic items, which are items declared inside of a target. Items declared outside of targets (static items) are evaluated before any target begins to execute. See my blog post MSBuild: Property and Item Evaluation.

Sayed Ibrahim Hashimi

My Book: Inside the Microsoft Build Engine : Using MSBuild and Team Foundation Build

Upvotes: 1

Related Questions