[SOLVED] Conditional compilation of pipeline Content based on Visual Studio project / platform

I came across this issue recently and I agree that the problem is that any “/platform:” in the .mgcb file will take priority over any argument passed from the command line. This problem is made worse because of the fact that using the pipeline tool will add the “/platform:” line to the file when you save, even if it was removed. I found a solution to this by setting up a target to run before the content build that will comment out the problematic line.

To fix it just append this to the end of your .csproj file.

<UsingTask TaskName="ReplaceFileText" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
        <ParameterGroup>
            <InputFilename ParameterType="System.String" Required="true" />
            <OutputFilename ParameterType="System.String" Required="true" />
            <MatchExpression ParameterType="System.String" Required="true" />
            <ReplacementText ParameterType="System.String" Required="true" />
        </ParameterGroup>
        <Task>
            <Reference Include="System.Core" />
            <Using Namespace="System" />
            <Using Namespace="System.IO" />
            <Using Namespace="System.Text.RegularExpressions" />
            <Code Type="Fragment" Language="cs">
                <![CDATA[
                            File.WriteAllText(
                                    OutputFilename,
                                    Regex.Replace(File.ReadAllText(InputFilename), MatchExpression, ReplacementText)
                                    );
                        ]]>
            </Code>
        </Task>
    </UsingTask>
    <Target Name="RemovePlatformBeforeContent" BeforeTargets="BuildContent">
    <ReplaceFileText 
    InputFilename="Content\Content.mgcb" 
    OutputFilename="Content\Content.mgcb" 
    MatchExpression="/platform:" 
    ReplacementText="#" />
  </Target>
1 Like