<!-- Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the License.txt file in the project root for more information. -->
<Project>
  <PropertyGroup>
    <_SourceLinkGitLabAssemblyFile Condition="'$(MSBuildRuntimeType)' != 'Core'">$(MSBuildThisFileDirectory)..\tools\netframework\Microsoft.SourceLink.GitLab.dll</_SourceLinkGitLabAssemblyFile>
    <_SourceLinkGitLabAssemblyFile Condition="'$(MSBuildRuntimeType)' == 'Core'">$(MSBuildThisFileDirectory)..\tools\net\Microsoft.SourceLink.GitLab.dll</_SourceLinkGitLabAssemblyFile>
  </PropertyGroup>

  <UsingTask TaskName="Microsoft.SourceLink.GitLab.GetSourceLinkUrl" AssemblyFile="$(_SourceLinkGitLabAssemblyFile)"/>
  <UsingTask TaskName="Microsoft.SourceLink.GitLab.TranslateRepositoryUrls" AssemblyFile="$(_SourceLinkGitLabAssemblyFile)"/>

  <PropertyGroup>
    <SourceLinkUrlInitializerTargets>$(SourceLinkUrlInitializerTargets);_InitializeGitLabSourceLinkUrl</SourceLinkUrlInitializerTargets>
    <SourceControlManagerUrlTranslationTargets>$(SourceControlManagerUrlTranslationTargets);TranslateGitLabUrlsInSourceControlInformation</SourceControlManagerUrlTranslationTargets>
  </PropertyGroup>

  <Target Name="_InitializeGitLabSourceLinkUrl" Outputs="%(SourceRoot.Identity)">
    <!--
      The task calculates SourceLink URL for a given SourceRoot.

      If the SourceRoot is associated with a git repository with a recognized domain the <see cref="SourceLinkUrl"/>
      output property is set to the content URL corresponding to the domain, otherwise it is set to string "N/A".

      Recognized domains are specified via Hosts (initialized from SourceLinkGitLabHost item group).
      In addition, if SourceLinkHasSingleProvider is true an implicit host is parsed from RepositoryUrl.

      Example of SourceLinkGitLabHost items:

      <ItemGroup>
        <SourceLinkGitLabHost Include="mygitlab1.com" ContentUrl="http://mygitlab1.com"/>
        <SourceLinkGitLabHost Include="mygitlab2.com" />           ContentUrl defaults to https://mygitlab2.com
        <SourceLinkGitLabHost Include="mygitlab3.com:8080" />      ContentUrl defaults to https://mygitlab3.com:8080
      </ItemGroup>

      ContentUrl is optional. If not specified it defaults to "https://{domain}" or "http://{domain}", based on the scheme of SourceRoot.RepositoryUrl.
    -->
    <Microsoft.SourceLink.GitLab.GetSourceLinkUrl RepositoryUrl="$(PrivateRepositoryUrl)" SourceRoot="@(SourceRoot)" Hosts="@(SourceLinkGitLabHost)" IsSingleProvider="$(SourceLinkHasSingleProvider)">
      <Output TaskParameter="SourceLinkUrl" PropertyName="_SourceLinkUrlToUpdate"/>
    </Microsoft.SourceLink.GitLab.GetSourceLinkUrl>

    <ItemGroup>
      <!-- Only update the SourceLinkUrl metadata if the SourceRoot belongs to this source control -->
      <SourceRoot Update="%(Identity)" SourceLinkUrl="$(_SourceLinkUrlToUpdate)" Condition="'$(_SourceLinkUrlToUpdate)' != 'N/A'"/>
    </ItemGroup>
  </Target>

  <!-- 
    We need to translate ssh URLs to https.
  -->
  <Target Name="TranslateGitLabUrlsInSourceControlInformation">

    <ItemGroup>
      <_TranslatedSourceRoot Remove="@(_TranslatedSourceRoot)"/>
    </ItemGroup>

    <Microsoft.SourceLink.GitLab.TranslateRepositoryUrls RepositoryUrl="$(ScmRepositoryUrl)" SourceRoots="@(SourceRoot)" Hosts="@(SourceLinkGitLabHost)" IsSingleProvider="$(SourceLinkHasSingleProvider)">
      <Output TaskParameter="TranslatedRepositoryUrl" PropertyName="ScmRepositoryUrl"/>
      <Output TaskParameter="TranslatedSourceRoots" ItemName="_TranslatedSourceRoot"/>
    </Microsoft.SourceLink.GitLab.TranslateRepositoryUrls>

    <ItemGroup>
      <SourceRoot Remove="@(SourceRoot)"/>
      <SourceRoot Include="@(_TranslatedSourceRoot)"/>
    </ItemGroup>
  </Target>

</Project>
