<Project>
    <PropertyGroup>
        <PrepareDependsOn>DisableStrongNameVerification;$(PrepareDependsOn)</PrepareDependsOn>
    </PropertyGroup>
    <Target Name="DisableStrongNameVerification" Condition="'$(CI)' == 'true'">
        <Message Text="Disabling Strong Name Verification for *,31bf3856ad364e35" />
        <Exec Command="&quot;$(MSBuildProgramFiles32)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\x64\sn.exe&quot; -Vr *,31bf3856ad364e35" />
        <Exec Command="&quot;$(MSBuildProgramFiles32)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\sn.exe&quot; -Vr *,31bf3856ad364e35" />
    </Target>

    <Target Name="ComputeLocalizedPackages" Condition="'$(Localize)' == 'true'">
        <ItemGroup>
            <!-- Specify the locale here so that an item is generated for each locale -->
            <LocalizedPackage Include="Microsoft.AspNet.SignalR.Client" Locale="%(TranslationLanguage.PackageLocaleName)" />
            <LocalizedPackage Include="Microsoft.AspNet.SignalR" Locale="%(TranslationLanguage.PackageLocaleName)" />
            <LocalizedPackage Include="Microsoft.AspNet.SignalR.Core" Locale="%(TranslationLanguage.PackageLocaleName)" />
            <LocalizedPackage Include="Microsoft.AspNet.SignalR.Redis" Locale="%(TranslationLanguage.PackageLocaleName)" />
            <LocalizedPackage Include="Microsoft.AspNet.SignalR.ServiceBus" Locale="%(TranslationLanguage.PackageLocaleName)" />
            <LocalizedPackage Include="Microsoft.AspNet.SignalR.ServiceBus3" Locale="%(TranslationLanguage.PackageLocaleName)" />
            <LocalizedPackage Include="Microsoft.AspNet.SignalR.SqlServer" Locale="%(TranslationLanguage.PackageLocaleName)" />
            <LocalizedPackage Include="Microsoft.AspNet.SignalR.SystemWeb" Locale="%(TranslationLanguage.PackageLocaleName)" />
        </ItemGroup>

        <ItemGroup>
            <!-- Copy items across and fill in common details -->
            <SatellitePackage Include="%(LocalizedPackage.Identity).%(LocalizedPackage.Locale)">
                <PackageId>%(LocalizedPackage.Identity)</PackageId>
                <ProjectRoot>$(RepositoryRoot)src\%(LocalizedPackage.Identity)</ProjectRoot>
                <Locale>%(LocalizedPackage.Locale)</Locale>
            </SatellitePackage>
        </ItemGroup>
    </Target>

    <PropertyGroup>
        <PackageDependsOn>$(PackageDependsOn);PackSatellitePackages;PrepareSymbols</PackageDependsOn>
    </PropertyGroup>
    <Target Name="PrepareSymbols">
        <Exec Command="powershell -NoProfile -ExecutionPolicy Bypass -File $(MSBuildThisFileDirectory)prepare-symbols.ps1 -SourceDirectory $(ArtifactsBinDir) -OutputDirectory $(SymbolsOutputPath) -ToolsDirectory $(ArtifactsDir)tools"
            WorkingDirectory="$(RepositoryRoot)"
            Condition=" '$(OS)' == 'Windows_NT'" />
    </Target>

    <Target Name="PackSatellitePackages" Condition="'$(Localize)' == 'true'" DependsOnTargets="ComputeLocalizedPackages">
        <PropertyGroup>
            <_ConstantProperties>Configuration=$(Configuration)</_ConstantProperties>
            <_ConstantProperties>$(ConstantProperties);PackageVersion=$(PackageVersion)</_ConstantProperties>
        </PropertyGroup>

        <!-- We have to restore first -->
        <MSBuild
            Projects="$(RepositoryRoot)build\loc\SatellitePackage.csproj"
            Targets="Restore" />

        <MSBuild
            Projects="$(RepositoryRoot)build\loc\SatellitePackage.csproj"
            Targets="Pack"
            Properties="PackageId=%(SatellitePackage.PackageId);Locale=%(SatellitePackage.Locale);$(_ConstantProperties);PackageVersion=$(PackageVersion)" />
    </Target>

    <Target Name="CodeSign" AfterTargets="Package" DependsOnTargets="GetToolsets" Condition=" '$(OS)' == 'Windows_NT' ">
        <MSBuild Projects="$(MSBuildThisFileDirectory)sign.proj"
                Targets="Restore;CodeSign"
                Properties="VisualStudioMSBuildx86Path=$(VisualStudioMSBuildx86Path);Configuration=$(Configuration);ArtifactsDir=$(ArtifactsDir);Localize=$(Localize)" />
    </Target>


    <PropertyGroup>
        <RestoreDependsOn>$(RestoreDependsOn);RestoreNpm;LocateMicroBuild</RestoreDependsOn>
    </PropertyGroup>
    <Target Name="RestoreNpm" Condition="'$(PreflightRestore)' != 'True'">
        <Message Text="Restoring NPM modules" Importance="high" />
        <Exec Command="npm ci --include=optional" WorkingDirectory="$(RepositoryRoot)test/Microsoft.AspNet.SignalR.Client.JS.Tests" />
    </Target>
    <Target Name="LocateMicroBuild">
        <!-- Manually find MicroBuild plugins because MicroBuild.Core doesn't work in dotnet.exe -->
        <PropertyGroup>
            <MicroBuildSentinelFile>packages\MicroBuild.Core.Sentinel.1.0.0\sentinel.txt</MicroBuildSentinelFile>
            <MicroBuildPluginDirectory>$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), $(MicroBuildSentinelFile)))</MicroBuildPluginDirectory>
            <MicroBuildPluginDirectory Condition="'$(MicroBuildPluginDirectory)' != ''">$(MicroBuildPluginDirectory)\packages</MicroBuildPluginDirectory>
            <MicroBuildPluginDirectory Condition="'$(MicroBuildOverridePluginDirectory)' != ''">$(MicroBuildOverridePluginDirectory)</MicroBuildPluginDirectory>
            <SolutionProperties Condition="'$(MicroBuildPluginDirectory)' != ''">$(SolutionProperties);MicroBuildPluginDirectory=$(MicroBuildPluginDirectory)</SolutionProperties>
        </PropertyGroup>
        <Message Text="MicroBuild not present. Skipping Localization and Signing." Importance="high" Condition="'$(MicroBuildPluginDirectory)' == ''" />
        <Message Text="MicroBuild found at '$(MicroBuildPluginDirectory)'." Importance="high" Condition="'$(MicroBuildPluginDirectory)' != ''" />
    </Target>

    <PropertyGroup>
        <TestDependsOn>$(TestDependsOn);RunBrowserTests</TestDependsOn>
    </PropertyGroup>

    <Target Name="RunBrowserTests">
        <Message Text="Running JavaScript client Browser tests" Importance="high" />
        <Exec Command="npm test -- --server-path &quot;$(ArtifactsBinDir)\Microsoft.AspNet.SignalR.Client.JS.Tests\$(DefaultTestFrameworks)\Microsoft.AspNet.SignalR.Client.JS.Tests.exe&quot;" WorkingDirectory="$(RepositoryRoot)test/Microsoft.AspNet.SignalR.Client.JS.Tests" IgnoreStandardErrorWarningFormat="true" />
    </Target>
</Project>
