<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>net461;net6.0-windows</TargetFrameworks>
    <LangVersion>preview</LangVersion>
    <Nullable>enable</Nullable>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    <PublishTrimmed>true</PublishTrimmed>
    <TrimMode>Link</TrimMode>

    <AssemblyTitle>Windows Service Wrapper</AssemblyTitle>
    <Description>Allows arbitrary process to run as a Windows service by wrapping it.</Description>
    <Company>CloudBees, Inc.</Company>
    <Product>Windows Service Wrapper</Product>
    <Copyright>Copyright (c) 2008-2020 Kohsuke Kawaguchi, Sun Microsystems, Inc., CloudBees, Inc., Oleg Nenashev and other contributors</Copyright>
  </PropertyGroup>

  <PropertyGroup Condition="'$(TargetFramework)' == 'net6.0-windows' AND '$(RuntimeIdentifier)' != ''">
    <PublishSingleFile>true</PublishSingleFile>
  </PropertyGroup>

  <PropertyGroup Condition="'$(TargetFramework)' != 'net6.0-windows'">
    <ILMergeVersion>3.0.41</ILMergeVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20303.1" />
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)' != 'net6.0-windows'">
    <PackageReference Include="ilmerge" Version="$(ILMergeVersion)" />
    <Reference Include="System.ServiceProcess" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\WinSW.Core\WinSW.Core.csproj" />
    <ProjectReference Include="..\WinSW.Plugins\WinSW.Plugins.csproj" />
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)' != 'net6.0-windows'">
    <ProjectReference Include="..\WinSW.Tasks\WinSW.Tasks.csproj" ReferenceOutputAssembly="false" />
  </ItemGroup>

  <Target Name="PublishCoreExe" AfterTargets="Publish" Condition="'$(TargetFramework)' == 'net6.0-windows'">

    <MakeDir Directories="$(ArtifactsPublishDir)" />
    <Copy SourceFiles="$(PublishDir)$(TargetName).exe" DestinationFiles="$(ArtifactsPublishDir)WinSW-$(PlatformTarget).exe" />

  </Target>

  <!-- Merge plugins and other DLLs into the executable -->
  <Target Name="Merge" BeforeTargets="AfterBuild" Condition="'$(TargetFramework)' != 'net6.0-windows'">

    <PropertyGroup>
      <InputAssemblies>"$(OutDir)$(TargetFileName)"</InputAssemblies>
      <InputAssemblies>$(InputAssemblies) "$(OutDir)WinSW.Core.dll"</InputAssemblies>
      <InputAssemblies>$(InputAssemblies) "$(OutDir)WinSW.Plugins.dll"</InputAssemblies>
      <InputAssemblies>$(InputAssemblies) "$(OutDir)log4net.dll"</InputAssemblies>
      <InputAssemblies>$(InputAssemblies) "$(OutDir)System.CommandLine.dll"</InputAssemblies>
      <InputAssemblies>$(InputAssemblies) "$(OutDir)System.Buffers.dll"</InputAssemblies>
      <InputAssemblies>$(InputAssemblies) "$(OutDir)System.Memory.dll"</InputAssemblies>
      <InputAssemblies>$(InputAssemblies) "$(OutDir)System.Numerics.Vectors.dll"</InputAssemblies>
      <InputAssemblies>$(InputAssemblies) "$(OutDir)System.Runtime.CompilerServices.Unsafe.dll"</InputAssemblies>
      <InputAssemblies>$(InputAssemblies) "$(OutDir)System.ValueTuple.dll"</InputAssemblies>
      <OutputAssembly>"$(ArtifactsPublishDir)WinSW-$(TargetFramework).exe"</OutputAssembly>
    </PropertyGroup>

    <PropertyGroup>
      <ILMerge>$(NuGetPackageRoot)ilmerge\$(ILMergeVersion)\tools\net452\ILMerge.exe</ILMerge>
      <ILMergeArgs>/targetplatform:v4.5 /out:$(OutputAssembly) $(InputAssemblies)</ILMergeArgs>
      <ILMergeCommand>"$(ILMerge)" $(ILMergeArgs)</ILMergeCommand>
    </PropertyGroup>

    <MakeDir Directories="$(ArtifactsPublishDir)" />
    <Exec Command="$(ILMergeCommand)" />

  </Target>

  <UsingTask TaskName="WinSW.Tasks.Trim" AssemblyFile="$(ArtifactsBinDir)WinSW.Tasks\$(Configuration)\net461\WinSW.Tasks.dll" />
  <Target Name="Trim" AfterTargets="Merge" Condition="'$(TargetFramework)' != 'net6.0-windows'">
    <Trim Path="$(ArtifactsPublishDir)WinSW-$(TargetFramework).exe" />
  </Target>

</Project>
