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

  <UsingTask TaskName="CreateAzureContainer" AssemblyFile="$(ToolsDir)net45/Microsoft.DotNet.Build.CloudTestTasks.dll"/>
  <UsingTask TaskName="UploadToAzure" AssemblyFile="$(ToolsDir)net45/Microsoft.DotNet.Build.CloudTestTasks.dll"/>

  <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>
    <Error Condition="'@(ForPublishing)' == ''" Text="No items were found matching pattern '$(PublishPattern)'." />
  </Target>

  <!-- publishes items to blob storage in Azure -->
  <Target Name="UploadToAzure" DependsOnTargets="GatherItemsForPattern">
    <Error Condition="'$(ContainerName)' == ''" Text="Missing property ContainerName." />
    <Error Condition="'$(CloudDropAccountName)' == ''" Text="Missing property CloudDropAccountName." />
    <Error Condition="'$(CloudDropAccessToken)' == ''" Text="Missing property CloudDropAccessToken." />
    <!-- create the container if it doesn't exist -->
    <CreateAzureContainer
      AccountKey="$(CloudDropAccessToken)"
      AccountName="$(CloudDropAccountName)"
      ContainerName="$(ContainerName)" />
    <!-- add relative blob path metadata -->
    <ItemGroup>
      <ForPublishing>
        <RelativeBlobPath>%(RecursiveDir)%(Filename)%(Extension)</RelativeBlobPath>
      </ForPublishing>
    </ItemGroup>
    <!-- now upload the items -->
    <UploadToAzure
      AccountKey="$(CloudDropAccessToken)"
      AccountName="$(CloudDropAccountName)"
      ContainerName="$(ContainerName)"
      Items="@(ForPublishing)"
      Overwrite="$(OverwriteOnPublish)" />
  </Target>

</Project>
