views:

129

answers:

1

I'm having a weird issue with my Silverlight 4 project, although it's one I've seen before. Basically, embedding fonts forces my Silverlight app to always rebuild even if everything is up to date. This kind of sucks as the font embed task takes a lot of memory and will eventually crash VS. I'd like to be able to build the project from the command line, but no matter what my local projects are out of date so the "Run" command forces another rebuild. I've tried to snip out some of the relevant log information out of my msbuild log.

Project "D:\Projects\Test\Test.Web\Test.Web.csproj" (10) is building "D:\Projects\Test\Test.SL\Test.SL.csproj" (2:4) on node 1 (default targets).
Building with tools version "4.0".
// Build operation starts normally (well, the dependency set on the server project is forcing the SL application to build). 
...
Target "ResolveReferences" skipped. Previously built successfully.
// A bunch of tasks are skipped (like this one)
...
Target "SubsetFontsSilverlight" in file "C:\Program Files\MSBuild\Microsoft\Expression\Blend\Silverlight\v4.0\SubsetFontSilverlight.targets" from project "D:\Projects\Test\Test.SL\Test.SL.csproj" (target "PrepareResources" depends on it):
Using "SubsetFontsSilverlight" task from assembly "C:\Program Files\MSBuild\Microsoft\Expression\Blend\Silverlight\v4.0\SubsetFontTask.dll".
Task "SubsetFontsSilverlight"
Done executing task "SubsetFontsSilverlight".
Done building target "SubsetFontsSilverlight" in project "Test.SL.csproj".
// this task never gets skipped
...
Target "MainResourcesGeneration" in file "C:\Program Files\MSBuild\Microsoft\Silverlight\v4.0\Microsoft.Silverlight.Common.targets" from project "D:\Projects\Test\Test.SL\Test.SL.csproj" (target "PrepareResources" depends on it):
Building target "MainResourcesGeneration" completely.
Input file "obj\Debug\Fonts\Fonts.zip" is newer than output file "obj\Debug\Test.SL.g.resources".
// note that the Fonts.zip file now makes the resources file out of date
...
Target "CoreCompile" in file "C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Microsoft.CSharp.Targets" from project "D:\Projects\Test\Test.SL\Test.SL.csproj" (target "Compile" depends on it):
Building target "CoreCompile" completely.
Input file "obj\Debug\Test.SL.g.resources" is newer than output file "obj\Debug\Test.SL.pdb".
// and the full recompile begins...

Anyhow, is there a way to stop the font task from running if the font files have not changed? Because constant rebuilds are really annoying.

Update: a sample font from the project file.

<BlendEmbeddedFont Include="Fonts\MyriadPro-BoldIt.otf">
  <IsSystemFont>True</IsSystemFont>
  <All>True</All>
  <AutoFill>True</AutoFill>
  <Characters>
  </Characters>
  <Uppercase>True</Uppercase>
  <Lowercase>True</Lowercase>
  <Numbers>True</Numbers>
  <Punctuation>True</Punctuation>
</BlendEmbeddedFont>

Update 2:

I'd like to post a little repro project, but I'm not sure where to toss the zip file. In any case, one can be whipped up in a few seconds flat. Here are the steps:

  1. Create Sillverlight Application (Blend or VS, though you need Blend for this to work anyhow)

  2. Use the Font Manager (Tools -> Font Manager) in Blend, you may have to open MainPage.xaml for it to become enabled. Embed the "Tahoma" font.

Now, every time you hit "Rebuild" you can verify that csc.exe runs every time, regardless of whether anything was changed or not.

A: 

I got around this issue by hacking up my SubsetFontSilverlight.target file a little. It's not a complete fix and it won't necessarily work for everyone, but it works for me.

The target file can be found in C:\Program Files\MSBuild\Microsoft\Expression\Blend\Silverlight\v4.0, at least on my machine.

The main idea here is to use the MSBUILD target Input/Ouput properties to see if the intermediate Font.zip file(s) need to be updated. I simply hardcoded the output file path, because I always use default Blend font locations - but it's possible to construct it out of the individual font files.

The condition I set was that the task should be skipped if the Font.zip file is newer than the embedded font files and the project file. That way if you add/subtract a font or replace one of them the fonts will get re-embedded, but if there are no changes (or just code changes) the font.zip will be left alone.

It's important to add the fonts.zip to the resources collection manually if the fonts are not embedded - that way if the code needs to be recompiled but the fonts may be left alone, msbuild remembers to include the fonts file into the resources. This is why I added a new task to run after the main embed task.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;
    <UsingTask AssemblyFile="SubsetFontTask.dll" TaskName="Microsoft.Expression.SubsetFontTask.SubsetFontsSilverlight" />
    <UsingTask AssemblyFile="SubsetFontTask.dll" TaskName="Microsoft.Expression.SubsetFontTask.CleanSubsetFonts" />

    <ItemGroup Condition="'$(BuildingInsideVisualStudio)'=='true'">
        <AvailableItemName Include="BlendEmbeddedFont"/>
    </ItemGroup>

  <ItemGroup>
    <!-- Files that we specify as target inputs for msbuild target timestamp check -->
    <FontEmbedRelatedFile Include="$(MSBuildProjectFile)" />
    <FontEmbedRelatedFile Include="@(BlendEmbeddedFont)" />

    <!-- Anticipated font file locations -->
    <FontOutputFile Include="$(IntermediateOutputPath)\Fonts\Fonts.zip" />
  </ItemGroup>

  <!-- 
    this task runs after the main subset task - basically it checks if the task got skipped, and if so
    it adds the projected font output files to the resources collection
   -->
  <Target Name='AfterFontEmbed' AfterTargets="SubsetFontsSilverlight"
    Condition="'@(BlendEmbeddedFont)' != '' AND '@(BlendSubsettedFont)' == ''">
    <Message Text="Adding font files to the resource collection because Font embed was skipped" Importance="normal" />
    <ItemGroup>
      <Resource Include="@(FontOutputFile)" />
    </ItemGroup>
  </Target>

    <Target Name='SubsetFontsSilverlight'  
    Inputs="@(FontEmbedRelatedFile)" Outputs="@(FontOutputFile)"
    DependsOnTargets="$(SubsetFontsDependsOn)"  
    Condition="'@(BlendEmbeddedFont)' != ''">
    <Message Text="Embedding font subsets" Importance="normal" />
        <SubsetFontsSilverlight 
            Fonts="@(BlendEmbeddedFont)" XamlFiles="@(Page)" Resources="@(Resources)"
            IntermediateFilesDirectory="$(IntermediateOutputPath)"
            >
            <Output TaskParameter="SubsettedFonts" ItemName="Resource"/>
            <!-- save our list of embedded font files for later use -->
            <Output TaskParameter="SubsettedFonts" ItemName="BlendSubsettedFont"/>
        </SubsetFontsSilverlight>
    </Target>

    <Target Name='CleanSubsetFonts' DependsOnTargets="$(CleanSubsetFontsDependsOn)" Condition="'@(BlendEmbeddedFont)' != ''">
        <CleanSubsetFonts 
            Fonts="@(BlendEmbeddedFont)"
            IntermediateFilesDirectory="$(IntermediateOutputPath)"
            />
    </Target>

    <PropertyGroup>
        <PrepareResourcesDependsOn>
            SubsetFontsSilverlight;
            $(PrepareResourcesDependsOn);
        </PrepareResourcesDependsOn>
    </PropertyGroup>

    <PropertyGroup>
        <CleanDependsOn>
            $(CleanDependsOn);
            CleanSubsetFonts;
        </CleanDependsOn>
    </PropertyGroup>

</Project>

Visual Studio is still not great at noticing that the projects are up to date, but when it builds them the corecompile task won't run every time and the dll file won't be overwritten needlessly. This means that you can run the compiler on the command line, set VS to "Never Build" on debug, and still have the correct debug symbols loaded.

I tested this with most of the situations I could think of (before/after clean, missing files, code changes only, project changes, etc) and it seems to do fine - meaning that the fonts are there, and everything isn't always getting recompiled.

Egor