<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. -->


<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
  <PropertyGroup>
    <TargetExt Condition=" '$(TargetExt)'=='' ">.chm</TargetExt>
    <TargetPath>$(OutputPath_x86)$(TargetName)$(TargetExt)</TargetPath>

    <IntermediateDocumentsFolder>$(IntermediateOutputPath)documents\</IntermediateDocumentsFolder>
    <IntermediateDocumentsPreprocessedFolder>$(IntermediateOutputPath)intermediate\</IntermediateDocumentsPreprocessedFolder>
    <IntermediateBuildFolder>$(IntermediateOutputPath)out\</IntermediateBuildFolder>
  </PropertyGroup>

  <Import Project="$(MSBuildToolsPath)\Microsoft.Common.targets" />

  <!-- This target exists solely to make Microsoft.Common.targets happy. -->
  <Target Name="CreateManifestResourceNames" />

  <!--
  ================================================================================================
  Compile

    Compile step for help content.

  ================================================================================================
  -->
  <PropertyGroup>
    <CompileDependsOn>
      CompileHelpDocuments;
      CompileHelpProjectFiles;
    </CompileDependsOn>
  </PropertyGroup>
  <Target Name="Compile"
          DependsOnTargets="$(CompileDependsOn)" />

  <Target Name="_GetSchemaDocumentsToProcess">
    <ItemGroup>
      <DocFromXsdInput Include="@(SchemaDocument)" />
      <DocFromXsdOutput Include="@(SchemaDocument->'$(IntermediateDocumentsFolder)xsd\%(FileName)\index.html')" />
    </ItemGroup>
  </Target>

  <Target Name="_ProcessXsdToDocuments"
          DependsOnTargets="_GetSchemaDocumentsToProcess"
          Inputs="@(DocFromXsdInput)"
          Outputs="@(DocFromXsdOutput)">
    <Exec Command="&quot;$(OutputPath_x86)docfromxsd.exe&quot; -out &quot;$(IntermediateDocumentsFolder)xsd&quot; @(SchemaDocument->'&quot;%(Identity)&quot;', ' ')" />
  </Target>

  <Target Name="_ProcessDocuments"
          DependsOnTargets="_ProcessXsdToDocuments"
          Inputs="@(SchemaContent);@(DocumentContent);@(LayoutContent)"
          Outputs="$(IntermediateOutputPath)$(TargetName).hhp">

    <!-- ignore static content for markup since content types in repo aren't always correct -->
    <ItemGroup>
      <_Ignore Include="@(FileContent->'$(IntermediateDocumentsPreProcessedFolder)%(RecursiveDir)')" />
      <_Ignore Include="@(FileContent->'$(IntermediateBuildFolder)%(RecursiveDir)')" />
    </ItemGroup>

    <RemoveDuplicates Inputs="@(_Ignore)">
      <Output TaskParameter="Filtered" ItemName="_UniqueIgnore" />
    </RemoveDuplicates>

    <!-- copy the documents to the intermediate folder to be interleaved with the generated XSD documents -->
    <Copy SourceFiles="@(DocumentContent)" DestinationFiles="@(DocumentContent->'$(IntermediateDocumentsFolder)%(RecursiveDir)%(Filename)%(Extension)')"
          SkipUnchangedFiles="true" UseHardlinksIfPossible="false" />

    <!-- replace all uri's starting with ~/ by a relative uri into the IntermediateDocumentsPreprocessedFolder folder -->
    <Exec Command="&quot;$(OutputPath_x86)tildetorelative.exe&quot; &quot;$(IntermediateDocumentsFolder)\&quot; &quot;$(IntermediateDocumentsPreprocessedFolder)\&quot;" />

    <!-- Convert the Markdown and Html files and generate the HtmlHelpProject. -->
    <Exec Command="&quot;$(OutputPath_x86)doccompiler.exe&quot; &quot;$(IntermediateDocumentsPreprocessedFolder)\&quot; &quot;$(IntermediateBuildFolder)\&quot; &quot;$(LayoutsFolder)&quot; -appendmdtoc &quot;$(IntermediateDocumentsPreprocessedFolder)\index.html.md&quot; @(_UniqueIgnore->'-ignore &quot;%(RootDir)%(Directory)\&quot;', ' ') -ignorexsdsimpletypeintoc -htmlhelp &quot;$(IntermediateOutputPath)$(TargetName).hhp&quot;" />
  </Target>

  <Target Name="_ProcessFileContent"
          Inputs="@(FileContent)"
          Outputs="@(FileContent->'$(IntermediateBuildFolder)%(RecursiveDir)%(Filename)%(Extension)')">
    <!-- "processing" file content is easy, it just gets duplicated into the intermediate build locations. -->
    <Copy SourceFiles="@(FileContent)" DestinationFiles="@(FileContent->'$(IntermediateDocumentsPreProcessedFolder)%(RecursiveDir)%(Filename)%(Extension)')"
          SkipUnchangedFiles="true" UseHardlinksIfPossible="true" />
    <Copy SourceFiles="@(FileContent)" DestinationFiles="@(FileContent->'$(IntermediateBuildFolder)%(RecursiveDir)%(Filename)%(Extension)')"
          SkipUnchangedFiles="true" UseHardlinksIfPossible="true" />
  </Target>

  <!--
  ================================================================================================
  CompileHelpDocuments

    Compiles when schemas and documents are collected and compiled into an HTML Help file.

  ================================================================================================
  -->
  <Target Name="CompileHelpDocuments"
          DependsOnTargets="_ProcessDocuments;_ProcessFileContent"
          Inputs="@(SchemaContent);@(DocumentContent);@(LayoutContent);@(FileContent);$(IntermediateOutputPath)$(TargetName).hhp"
          Outputs="$(IntermediateOutputPath)$(TargetName).chm"
          Condition=" '@(SchemaContent)@(DocumentContent)'!='' ">
    <Exec Command="&quot;$(HelpCompiler)&quot; &quot;$(IntermediateOutputPath)$(TargetName).hhp&quot;" WorkingDirectory="$(IntermediateOutputPath)" IgnoreExitCode="true">
      <Output TaskParameter="ExitCode" PropertyName="ErrorCode"/>
    </Exec>

    <Delete Files="$(IntermediateOutputPath)$(TargetName).chm" Condition=" '$(ErrorCode)'!='1' " />
    <Error Text="Help compiler failed while compiling: @(HelpProjectFile). For more information, see: $(IntermediateOutputPath)$(TargetName).log" Condition=" '$(ErrorCode)'!='1' " />
  </Target>

  <Target Name="_CopyHelpProjectContent"
          Condition=" '@(HelpProjectContent)'!='' ">
    <ItemGroup>
      <_HelpProjectContent Include="@(HelpProjectFile)">
        <SourcePath>$(MSBuildProjectFilePath)%(HelpProjectFile.Identity)</SourcePath>
      </_HelpProjectContent>
      <_HelpProjectContent Include="@(HelpProjectContent)" Condition=" '%(HelpProjectContent.SourcePath)'=='' ">
        <SourcePath>$(MSBuildProjectFilePath)%(HelpProjectContent.Identity)</SourcePath>
      </_HelpProjectContent>
      <_HelpProjectContent Include="@(HelpProjectContent)" Condition=" '%(HelpProjectContent.SourcePath)'!='' ">
        <SourcePath>%(HelpProjectContent.SourcePath)</SourcePath>
      </_HelpProjectContent>
    </ItemGroup>

    <Copy SourceFiles="%(_HelpProjectContent.SourcePath)" DestinationFiles="$(IntermediateOutputPath)%(_HelpProjectContent.Identity)"
          SkipUnchangedFiles="true">
    </Copy>
  </Target>

  <!--
  ================================================================================================
  CompileHelpProjectFiles

    Compiles when the HTML Help project is handcrafted.

  ================================================================================================
  -->
  <Target Name="CompileHelpProjectFiles"
          Condition=" '@(HelpProjectFile)'!='' "
          DependsOnTargets="_CopyHelpProjectContent"
          Inputs="@(HelpProjectFile);@(HelpProjectContent);$(MSBuildProjectFile)"
          Outputs="$(TargetPath)">

    <Exec Command="&quot;$(HelpCompiler)&quot; &quot;$(IntermediateOutputPath)@(HelpProjectFile)&quot;" WorkingDirectory="$(IntermediateOutputPath)" IgnoreExitCode="true">
      <Output TaskParameter="ExitCode" PropertyName="ErrorCode"/>
    </Exec>

    <Error Text="Help compiler failed while compiling: @(HelpProjectFile)" Condition=" '$(ErrorCode)'!='1'" />
  </Target>

  <!--
  ================================================================================================
  AfterClean

    Overrides AfterClean to clean up intermediate files missed by CoreClean.

  ================================================================================================
  -->
  <Target Name="AfterClean">
    <RemoveDir Directories="$(IntermediateOutputPath)" />
  </Target>
</Project>
