<?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 InitialTargets="InitializeVariables" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">

  <Import Project="..\WixBuild.Version.targets" />
  <Import Project="..\Nuget.targets" />

  <UsingTask AssemblyFile="$(AzureStorageBuildTasksPath)FireGiant.BuildTasks.AzureStorage.dll" TaskName="UploadBlobs" />

  <!--
  ================================================================================================
  Default

    The default target that indicates you must specify which target to build.

  ================================================================================================
  -->
  <Target Name="Default">
    <Warning Text="Specify a target to execute -t:BuildRelease or -t:Release"/>
  </Target>

  <!--
  ================================================================================================
  InitializeVariables

    Verifies input and initializes variables.

  ================================================================================================
  -->
  <Target Name="InitializeVariables">
    <PropertyGroup>
      <ProjectFolder>$([System.IO.Path]::GetFullPath($(MSBuildThisFileDirectory)..\..\))</ProjectFolder>
      <ProjectHistoryFile>$(ProjectFolder)History.md</ProjectHistoryFile>
    </PropertyGroup>

    <Error Text="Cannot locate project's private/public keypair at: $(OfficialBuildPrivateKeyPair). Ensure that the OfficialBuildPrivateKeyPair arguments are correct." Condition=" !Exists('$(OfficialBuildPrivateKeyPair)') " />

    <Error Text="Cannot locate project's root folder at: $(ProjectFolder)." Condition=" !Exists('$(ProjectFolder)') " />
    <Error Text="Cannot locate project's History.md file at: $(ProjectHistoryFile). Ensure that the Major and Minor arguments are correct." Condition=" !Exists('$(ProjectHistoryFile)') " />
  </Target>

  <!--
  ================================================================================================
  BuildProject

    Builds a project.

  ================================================================================================
  -->
  <Target Name="BuildProject">
    <MSBuild Projects="$(ProjectFolder)\tools\WixRelease.relproj"
             Properties="OFFICIAL_WIX_BUILD=$(OfficialBuildPrivateKeyPair)"
             Targets="Build" />
  </Target>

  <!--
  ================================================================================================
  GetVersionNumber

    Gets the build version.

  ================================================================================================
  -->
  <Target Name="GetVersionNumber">
    <MSBuild Projects="$(ProjectFolder)\tools\WixRelease.relproj"
             Properties="OFFICIAL_WIX_BUILD=$(OfficialBuildPrivateKeyPair)"
             Targets="GetVersion">
      <Output TaskParameter="TargetOutputs" PropertyName="Version" />
    </MSBuild>
  </Target>

  <!--
  ================================================================================================
  BuildRelease

    Builds everything required to release.

  ================================================================================================
  -->
  <PropertyGroup>
    <BuildReleaseDependsOn>
      BuildProject
    </BuildReleaseDependsOn>
  </PropertyGroup>
  <Target Name="BuildRelease"
          DependsOnTargets="$(BuildReleaseDependsOn)" />


  <!--
  ================================================================================================
  GetReleasedFiles

    Gets the released files from a project's build.

  ================================================================================================
  -->
  <Target Name="GetReleasedFiles">
    <MSBuild Projects="$(ProjectFolder)\tools\WixRelease.relproj"
             Properties="OFFICIAL_WIX_BUILD=$(OfficialBuildPrivateKeyPair)"
             Targets="GetReleasedFiles">
      <Output TaskParameter="TargetOutputs" ItemName="ReleasedFiles" />
    </MSBuild>
  </Target>

  <!--
  ================================================================================================
  CalculateUploadBlobs

    Calculates the UploadBlob items from the released files.

  ================================================================================================
  -->
  <Target Name="CalculateUploadBlobs"
          DependsOnTargets="GetReleasedFiles">
    <ItemGroup>
      <UploadBlob Include="@(ReleasedFiles)">
        <Container>%(Container)</Container>
        <BlobName>%(RelativeUploadFolder)%(Filename)%(Extension)</BlobName>
      </UploadBlob>
    </ItemGroup>
  </Target>

  <!--
  ================================================================================================
  UploadFiles

    Uploads the released files from a project's build.

  ================================================================================================
  -->
  <Target Name="UploadFiles"
          Condition=" $(SkipUploadFiles)=='' "
          DependsOnTargets="GetReleasedFiles;CalculateUploadBlobs">

    <Message Importance="Normal" Text="Uploading: @(UploadBlob) to: %(Container) at: %(BlobName)." />

    <UploadBlobs StorageAccount="$(StorageAccount)"
                 StorageAuthentication="$(StorageAuthentication)"
                 StorageEmulator="$(StorageUseEmulator)"
                 Container="%(Container)"
                 Public="true"
                 Blobs="@(UploadBlob)">
      <Output TaskParameter="Uploaded" ItemName="UploadedBlob" />
    </UploadBlobs>

    <Message Importance="Normal" Text="Upload complete." />
  </Target>

  <!--
  ================================================================================================
  PushHistoryToGithub

    Commits and pushes History.md changes to Github.

  ================================================================================================
  -->
  <Target Name="PushHistoryToGithub"
          Condition=" $(GithubUser)!='' "
          DependsOnTargets="GetVersionNumber">

    <Message Importance="Normal" Text="Pushing History.md as $(GithubUser)." />

    <Exec Command="git add History.md"
          WorkingDirectory="$(ProjectFolder)" />

    <Exec Command='git -c user.name="WixBuild" -c user.email="wix@wixtoolset.org" commit -m "WixBuild: Version $(Version)"'
          WorkingDirectory="$(ProjectFolder)" ContinueOnError="true" />

    <Exec Command='git push --repo https://$(GithubUser):$(GithubPassword)@github.com/wixtoolset/wix3.git origin HEAD:develop'
          WorkingDirectory="$(ProjectFolder)" />

    <Message Importance="Normal" Text="History push complete." />
  </Target>

  <!--
  ================================================================================================
  Release

    Releases a build to the WiX toolset website.

  ================================================================================================
  -->
  <PropertyGroup>
    <ReleaseDependsOn>
      UploadFiles;
      PushHistoryToGithub;
    </ReleaseDependsOn>
  </PropertyGroup>
  <Target Name="Release"
          DependsOnTargets="$(ReleaseDependsOn)" />
</Project>
