<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <_AvaloniaUseExternalMSBuild>$(AvaloniaUseExternalMSBuild)</_AvaloniaUseExternalMSBuild>
    <_AvaloniaUseExternalMSBuild Condition="'$(_AvaloniaForceInternalMSBuild)' == 'true'">false</_AvaloniaUseExternalMSBuild>
  </PropertyGroup>
  
  <UsingTask TaskName="GenerateAvaloniaResourcesTask"
             AssemblyFile="$(AvaloniaBuildTasksLocation)"
             />


  <Target Name="AddAvaloniaResources" BeforeTargets="ResolveReferences">
    <PropertyGroup>
      <AvaloniaResourcesTemporaryFilePath Condition="'$(AvaloniaResourcesTemporaryFilePath)' == ''">$(IntermediateOutputPath)/Avalonia/resources</AvaloniaResourcesTemporaryFilePath>
    </PropertyGroup>
    <ItemGroup>
      <EmbeddedResource Include="$(AvaloniaResourcesTemporaryFilePath)">
        <LogicalName>!AvaloniaResources</LogicalName>
      </EmbeddedResource>
    </ItemGroup>
  </Target>
  
  <Target Name="GenerateAvaloniaResources" 
          BeforeTargets="CoreCompile;CoreResGen"
          Inputs="@(AvaloniaResource);$(MSBuildAllProjects)"
          Outputs="$(AvaloniaResourcesTemporaryFilePath)"
          DependsOnTargets="$(BuildAvaloniaResourcesDependsOn);AddAvaloniaResources;ResolveReferences">
    <GenerateAvaloniaResourcesTask
      Condition="'$(_AvaloniaUseExternalMSBuild)' != 'true'"
      Output="$(AvaloniaResourcesTemporaryFilePath)"
      Root="$(MSBuildProjectDirectory)"
      Resources="@(AvaloniaResource)"
      EmbeddedResources="@(EmbeddedResources)"/>
    <Exec 
      Condition="'$(_AvaloniaUseExternalMSBuild)' == 'true'"
      Command="dotnet msbuild /nodereuse:false $(MSBuildProjectFile) /t:GenerateAvaloniaResources /p:_AvaloniaForceInternalMSBuild=true /p:Configuration=$(Configuration)"/>

  </Target>
  
  <ItemGroup>
    <UpToDateCheckInput Include="@(AvaloniaResource)" />
  </ItemGroup>
</Project>
