<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup>
    <OverwriteOnPublish Condition="'$(OverwriteOnPublish)' == ''">false</OverwriteOnPublish>
  </PropertyGroup>

  <!-- gathers the items to be published -->
  <Target Name="GatherItemsForPattern">
    <Error Condition="'$(PublishPattern)' == ''" Text="Please specify a value for PublishPattern using standard msbuild 'include' syntax." />
    <ItemGroup>
      <ForPublishing Include="$(PublishPattern)" />
    </ItemGroup>
    <!-- add relative blob path metadata -->
    <ItemGroup>
      <ForPublishing>
        <RelativeBlobPath>$([System.String]::Copy('%(RecursiveDir)%(Filename)%(Extension)').Replace('\' ,'/'))</RelativeBlobPath>
        <ManifestArtifactData>ShipInstaller=dotnetcli</ManifestArtifactData>
      </ForPublishing>
    </ItemGroup>
    <Error Condition="'@(ForPublishing)' == ''" Text="No items were found matching pattern '$(PublishPattern)'." />
  </Target>

  <!-- publishes artifacts to blob storage in Azure -->
  <Target Name="UploadArtifactsToAzure" DependsOnTargets="GatherItemsForPattern">
    <Error Condition="'$(ArtifactCloudDropAccessToken)' == ''" Text="Missing property ArtifactCloudDropAccessToken." />
    <Error Condition="'$(ArtifactCloudDropURL)' == ''" Text="Missing property ArtifactCloudDropURL." />
                    
    <Message Text="Publish assets to blob storage started: $(ArtifactCloudDropURL)" />
    <PushToBlobFeed ExpectedFeedUrl="$(ArtifactCloudDropURL)"
                    AccountKey="$(ArtifactCloudDropAccessToken)"
                    ItemsToPush="@(ForPublishing)"
                    ManifestBranch="$(BranchName)"
                    ManifestBuildId="$(FullNugetVersion)"
                    ManifestCommit="$(CommitHash)"
                    ManifestName="$(BuildName)"
                    ManifestBuildData="ProductVersion=$(FullNugetVersion)"
                    Overwrite="$(OverwriteOnPublish)" 
                    PublishFlatContainer="true" 
                    SkipCreateManifest="$(IsNotOrchestratedPublish)" />
  </Target>

  <!-- publishes checksums for the artifacts to blob storage in Azure -->
  <Target Name="UploadChecksumsToAzure" DependsOnTargets="GenerateChecksums">
    <Error Condition="'$(ChecksumCloudDropAccessToken)' == ''" Text="Missing property ChecksumCloudDropAccessToken." />
    <Error Condition="'$(ChecksumCloudDropURL)' == ''" Text="Missing property ChecksumCloudDropURL." />
                    
    <Message Text="Publish checksums to blob storage started: $(ChecksumCloudDropURL)" />
    <PushToBlobFeed ExpectedFeedUrl="$(ChecksumCloudDropURL)"
                    AccountKey="$(ChecksumCloudDropAccessToken)"
                    ItemsToPush="@(ArtifactsForGeneratingChecksums->'%(DestinationPath)')"
                    ManifestBranch="$(BranchName)"
                    ManifestBuildId="$(FullNugetVersion)"
                    ManifestCommit="$(CommitHash)"
                    ManifestName="$(BuildName)"
                    ManifestBuildData="ProductVersion=$(FullNugetVersion)"
                    Overwrite="$(OverwriteOnPublish)" 
                    PublishFlatContainer="true" 
                    SkipCreateManifest="$(IsNotOrchestratedPublish)" />
  </Target>

</Project>
