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

    <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>disable</Nullable>
    </PropertyGroup>

    <ItemGroup>
      <Folder Include="Preferences\" />
    </ItemGroup>

    <ItemGroup>
      <PackageReference Include="protobuf-net" Version="3.2.30" />
    </ItemGroup>
  
  <PropertyGroup>
    <ProtogenVersion>3.2.42</ProtogenVersion>
  </PropertyGroup>
  
  <Target Name="ProtogenCheck" BeforeTargets="GenerateProtoContracts">
    <PropertyGroup>
      <ProtogenExists>false</ProtogenExists>
    </PropertyGroup>
    <Exec Command="dotnet tool run protogen --version" IgnoreExitCode="true">
      <Output TaskParameter="ExitCode" PropertyName="ProtogenExitCode"/>
    </Exec>
    <PropertyGroup>
      <ProtogenExists Condition="'$(ProtogenExitCode)' == '0'">true</ProtogenExists>
    </PropertyGroup>
  </Target>
  
  <Target Name="InstallProtogen" BeforeTargets="GenerateProtoContracts"
          Condition="'$(ProtogenExists)' != 'true'">
    <Message Text="Downloading protogen v$(ProtogenVersion)..." Importance="high"/>
    <Exec Command="dotnet tool install --local protobuf-net.Protogen --version $(ProtogenVersion)"/>
    <PropertyGroup>
      <ProtogenExists>true</ProtogenExists>
    </PropertyGroup>
    
    <Message Text="protogen installed successfully." Importance="high"/>
  </Target>
    
  
  <Target Name="GenerateProtoContracts" BeforeTargets="BeforeCompile"
          Inputs="$(MSBuildProjectDirectory)\DataContracts\*.proto"
          Outputs="$(MSBuildProjectDirectory)\ProtoAutogen\*.cs">
    <Exec Command="dotnet tool run protogen --csharp_out=ProtoAutogen --proto_path=DataContracts +listset=yes *.proto"/>

    <ItemGroup>
      <Compile Include="ProtoAutogen\*.cs" KeepDuplicates="false"/>
    </ItemGroup>

  </Target>

    <ItemGroup>
      <ProjectReference Include="..\PixiEditor.Extensions.CommonApi.Diagnostics\PixiEditor.Extensions.CommonApi.Diagnostics.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
    </ItemGroup>
  
    <ItemGroup>
      <PackageReference Include="protobuf-net" Version="3.2.30" />
    </ItemGroup>
  
  <PropertyGroup>
    <ProtogenVersion>3.2.42</ProtogenVersion>
  </PropertyGroup>
  
  <Target Name="ProtogenCheck" BeforeTargets="GenerateProtoContracts">
    <PropertyGroup>
      <ProtogenExists>false</ProtogenExists>
    </PropertyGroup>
    <Exec Command="dotnet tool run protogen --version" IgnoreExitCode="true">
      <Output TaskParameter="ExitCode" PropertyName="ProtogenExitCode"/>
    </Exec>
    <PropertyGroup>
      <ProtogenExists Condition="'$(ProtogenExitCode)' == '0'">true</ProtogenExists>
    </PropertyGroup>
  </Target>
  
  <Target Name="InstallProtogen" BeforeTargets="GenerateProtoContracts"
          Condition="'$(ProtogenExists)' != 'true'">
    <Message Text="Downloading protogen v$(ProtogenVersion)..." Importance="high"/>
    <Exec Command="dotnet tool install --local protobuf-net.Protogen --version $(ProtogenVersion)"/>
    <PropertyGroup>
      <ProtogenExists>true</ProtogenExists>
    </PropertyGroup>
    
    <Message Text="protogen installed successfully." Importance="high"/>
  </Target>
    
  
  <Target Name="GenerateProtoContracts" BeforeTargets="BeforeCompile"
          Inputs="$(MSBuildProjectDirectory)\DataContracts\*.proto"
          Outputs="$(MSBuildProjectDirectory)\ProtoAutogen\*.cs">
    <Exec Command="dotnet tool run protogen --csharp_out=ProtoAutogen --proto_path=DataContracts +listset=yes *.proto"/>

    <ItemGroup>
      <Compile Include="ProtoAutogen\*.cs" KeepDuplicates="false"/>
    </ItemGroup>

  </Target>
</Project>
