<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <ProjectFiles Include="Microsoft.VisualStudio.Services.Agent/Microsoft.VisualStudio.Services.Agent.csproj" />
    <ProjectFiles Include="Agent.Listener/Agent.Listener.csproj" />
    <ProjectFiles Include="Agent.Worker/Agent.Worker.csproj" />
    <ProjectFiles Include="Agent.PluginHost/Agent.PluginHost.csproj" />
    <ProjectFiles Include="Agent.Sdk/Agent.Sdk.csproj" />
    <ProjectFiles Include="Agent.Plugins/Agent.Plugins.csproj" />
  </ItemGroup>

  <PropertyGroup Condition="'$(LayoutRoot)'==''">
    <LayoutRoot>$(MSBuildProjectDirectory)/../_layout/unknown</LayoutRoot>
  </PropertyGroup>

  <PropertyGroup>
    <L1Root>$(MSBuildProjectDirectory)/../_l1</L1Root>
    <TreatWarningsAsErrors Condition="'$(BUILDCONFIG)'=='Release'">true</TreatWarningsAsErrors>
  </PropertyGroup>

  <ItemGroup>
    <LayoutRootFiles Include="$(MSBuildProjectDirectory)/Misc/layoutroot/**"/>
    <LayoutBinFiles Include="$(MSBuildProjectDirectory)/Misc/layoutbin/**"/>
  </ItemGroup>

  <ItemGroup>
    <L1Tasks Include="$(L1Root)/externals/Tasks/**"/>
  </ItemGroup>

  <Target Name="GenerateConstant">
    <Exec Command="git rev-parse HEAD" ConsoleToMSBuild="true" IgnoreExitCode="true">
      <Output TaskParameter="ConsoleOutput" PropertyName="GitInfoCommitHash" />
    </Exec>
    <Message Text="Building $(GitInfoCommitHash) --- $(PackageRuntime)" Importance="high" Condition=" !$(GitInfoCommitHash.Contains('fatal')) "/>
    <Message Text="Building from non-Git sources --- $(PackageRuntime)" Importance="high" Condition=" $(GitInfoCommitHash.Contains('fatal')) "/>

    <ItemGroup>
        <BuildConstants Include="namespace Microsoft.VisualStudio.Services.Agent"/>
        <BuildConstants Include="{"/>
        <BuildConstants Include="%20%20%20%20public static class BuildConstants"/>
        <BuildConstants Include="%20%20%20%20{"/>
        <BuildConstants Include="%20%20%20%20%20%20%20%20public static class Source"/>
        <BuildConstants Include="%20%20%20%20%20%20%20%20{"/>
        <BuildConstants Include="%20%20%20%20%20%20%20%20%20%20%20%20public static readonly string CommitHash = %22$(GitInfoCommitHash)%22%3B" Condition=" !$(GitInfoCommitHash.Contains('fatal')) "/>
        <BuildConstants Include="%20%20%20%20%20%20%20%20%20%20%20%20public static readonly string CommitHash = %220000000000000000000000000000000000000000%22%3B" Condition=" $(GitInfoCommitHash.Contains('fatal')) "/>
        <BuildConstants Include="%20%20%20%20%20%20%20%20}%0A"/>
        <BuildConstants Include="%20%20%20%20%20%20%20%20public static class AgentPackage"/>
        <BuildConstants Include="%20%20%20%20%20%20%20%20{"/>
        <BuildConstants Include="%20%20%20%20%20%20%20%20%20%20%20%20public static readonly string PackageType = %22$(PackageType)%22%3B"/>
        <BuildConstants Include="%20%20%20%20%20%20%20%20%20%20%20%20public static readonly string PackageName = %22$(PackageRuntime)%22%3B"/>
        <BuildConstants Include="%20%20%20%20%20%20%20%20%20%20%20%20public static readonly string Version = %22$(AgentVersion)%22%3B"/>
        <BuildConstants Include="%20%20%20%20%20%20%20%20}"/>
        <BuildConstants Include="%20%20%20%20}"/>
        <BuildConstants Include="}"/>
    </ItemGroup>

    <WriteLinesToFile
            File="Microsoft.VisualStudio.Services.Agent/BuildConstants.cs"
            Lines="@(BuildConstants)"
            Overwrite="true"
            Encoding="Unicode"/>
  </Target>

  <Target Name="Build" DependsOnTargets="GenerateConstant">
    <MSBuild Targets="Restore"
             BuildInParallel="false"
             Projects="@(ProjectFiles)"
             SkipNonExistentProjects="false"
             StopOnFirstFailure="true" />

    <MSBuild Targets="Publish"
             BuildInParallel="false"
             Projects="@(ProjectFiles)"
             SkipNonExistentProjects="false"
             StopOnFirstFailure="true"
             Properties="Configuration=$(BUILDCONFIG);PackageRuntime=$(PackageRuntime);Version=$(AgentVersion);RuntimeIdentifier=$(PackageRuntime);PublishDir=$(LayoutRoot)/bin;TreatWarningsAsErrors=$(TreatWarningsAsErrors)" />
    <Exec Command="%22$(DesktopMSBuild)%22 Agent.Service/Windows/AgentService.csproj /p:Configuration=$(BUILDCONFIG) /p:OutputPath=%22$(LayoutRoot)/bin%22 /p:TreatWarningsAsErrors=$(TreatWarningsAsErrors)" ConsoleToMSBuild="true" Condition="'$(PackageRuntime)' == 'win-x64' Or '$(PackageRuntime)' == 'win-x86'" />
  </Target>

  <Target Name="TestL0" DependsOnTargets="GenerateConstant">
    <Exec Command="dotnet build Test/Test.csproj -c $(BUILDCONFIG) /p:PackageRuntime=$(PackageRuntime) /p:SelfContained=false" ConsoleToMSBuild="true" />
    <Exec Command="dotnet build Test/Test.csproj /t:CopyFilesL0" Condition="'$(PackageRuntime)' == 'win-x86'"/>
    <Exec Command="dotnet test Test/Test.csproj --settings $(MSBuildProjectDirectory)/Test/CodeCoverage.runsettings --no-build --logger:trx --collect:&quot;Code Coverage&quot; --filter &quot;$(TestFilters)&quot; /p:PackageRuntime=$(PackageRuntime)" ConsoleToMSBuild="true" />
  </Target>

  <Target Name="TestL1" DependsOnTargets="GenerateConstant">
    <Copy SourceFiles="@(LayoutBinFiles)"  DestinationFolder="$(L1Root)/bin/%(RecursiveDir)"/>
    <Copy SourceFiles="@(L1Tasks)" DestinationFolder="$(L1Root)/bin/L1/Tasks/%(RecursiveDir)"/>

    <MSBuild Targets="Restore"
             BuildInParallel="false"
             Projects="@(ProjectFiles)"
             SkipNonExistentProjects="false"
             StopOnFirstFailure="true" />

    <MSBuild Targets="Publish"
             BuildInParallel="false"
             Projects="@(ProjectFiles)"
             SkipNonExistentProjects="false"
             StopOnFirstFailure="true"
             Properties="Configuration=$(BUILDCONFIG);PackageRuntime=$(PackageRuntime);Version=1.0.0.0;RuntimeIdentifier=$(PackageRuntime);PublishDir=$(L1Root)/bin" />

    <Exec Command="dotnet build Test/Test.csproj -c $(BUILDCONFIG) /p:PackageRuntime=$(PackageRuntime) /p:SelfContained=false -o $(L1Root)/bin" ConsoleToMSBuild="true" />
    <Exec Command="dotnet build Test/Test.csproj /t:CopyFilesL1" Condition="'$(PackageRuntime)' == 'win-x86'"/>
    <Exec Command="dotnet test Test/Test.csproj -o $(L1Root)/bin --no-build --logger:trx --verbosity=detailed --filter &quot;$(TestFilters)&quot;" ConsoleToMSBuild="true" />
  </Target>

  <Target Name="Layout" DependsOnTargets="Clean;Build">
    <Copy SourceFiles="@(LayoutRootFiles)"  DestinationFolder="$(LayoutRoot)/%(RecursiveDir)"/>
    <Copy SourceFiles="@(LayoutBinFiles)"  DestinationFolder="$(LayoutRoot)/bin/%(RecursiveDir)"/>

    <ItemGroup>
        <LayoutRootFilesToDelete Include="$(LayoutRoot)/*.cmd" Condition="'$(PackageRuntime)' != 'win-x64' And '$(PackageRuntime)' != 'win-x86'"/>
        <LayoutRootFilesToDelete Include="$(LayoutRoot)/*.sh" Condition="'$(PackageRuntime)' == 'win-x64' Or '$(PackageRuntime)' == 'win-x86'"/>
        <LayoutRootFilesToDelete Include="$(LayoutRoot)/license.html" Condition="'$(PackageRuntime)' == 'win-x64' Or '$(PackageRuntime)' == 'win-x86'"/>
        <LayoutRootFilesToDelete Include="$(LayoutRoot)/bin/AgentService.js" Condition="'$(PackageRuntime)' == 'win-x64' Or '$(PackageRuntime)' == 'win-x86'"/>
    </ItemGroup>

    <Delete Files="@(LayoutRootFilesToDelete)" />
  </Target>

  <Target Name="CleanL1">
    <RemoveDir  Directories="$(L1Root)" ContinueOnError="WarnAndContinue" />
  </Target>

  <Target Name="Clean">
    <RemoveDir  Directories="$(LayoutRoot)" ContinueOnError="WarnAndContinue" />
    <RemoveDir  Directories="Microsoft.VisualStudio.Services.Agent/bin" />
    <RemoveDir  Directories="Microsoft.VisualStudio.Services.Agent/obj" />
    <RemoveDir  Directories="Agent.Listener/bin" />
    <RemoveDir  Directories="Agent.Listener/obj" />
    <RemoveDir  Directories="Agent.Worker/bin" />
    <RemoveDir  Directories="Agent.Worker/obj" />
    <RemoveDir  Directories="Agent.PluginHost/bin" />
    <RemoveDir  Directories="Agent.PluginHost/obj" />
    <RemoveDir  Directories="Agent.Sdk/bin" />
    <RemoveDir  Directories="Agent.Sdk/obj" />
    <RemoveDir  Directories="Agent.Plugins/bin" />
    <RemoveDir  Directories="Agent.Plugins/obj" />
    <RemoveDir  Directories="Test/bin" />
    <RemoveDir  Directories="Test/obj" />
  </Target>
</Project>
