<?xml version="1.0" encoding="utf-8"?>

<!--
  Copyright (C) 2011-2019 Xtensive LLC.
  All rights reserved.
  For conditions of distribution and use, see license.
-->

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

<PropertyGroup>
  <CompileDependsOn>$(CompileDependsOn);XtensiveOrmBuild</CompileDependsOn>
  <XtensiveOrmPath Condition="'$(XtensiveOrmPath)'==''">$(MSBuildThisFileDirectory)</XtensiveOrmPath>
  <XtensiveOrmPath Condition="!HasTrailingSlash('$(XtensiveOrmPath)')">$(XtensiveOrmPath)\</XtensiveOrmPath>
  <XtensiveOrmWeaver Condition="'$(XtensiveOrmWeaver)'==''">$(XtensiveOrmPath)tools\weaver\Xtensive.Orm.Weaver.dll</XtensiveOrmWeaver>
  <XtensiveOrmBuildDependsOn>$(XtensiveOrmBuildDependsOn)</XtensiveOrmBuildDependsOn>
</PropertyGroup>

<Target Name="XtensiveOrmBuild"
        AfterTargets="Compile"
        Inputs="@(IntermediateAssembly -> '%(FullPath)')"
        Outputs="@(IntermediateAssembly -> '%(FullPath).weaver-stamp')"
        DependsOnTargets="ResolveAssemblyReferences;_CopyFilesMarkedCopyLocal;$(XtensiveOrmBuildDependsOn)"
        Condition="'$(XtensiveOrmSkipProcessing)'!='true' AND '$(DesignTimeBuild)' != 'true'">
  <Error Condition="!Exists('$(XtensiveOrmWeaver)')"
         Text="Weaver is not found at '$(XtensiveOrmWeaver)'" />
  <Message Importance="low" Text="Using weaver at '$(XtensiveOrmWeaver)'" />
  <PropertyGroup>
    <XtensiveOrmWeaverResponseFile>@(IntermediateAssembly -> '%(FullPath).weaver-cmd')</XtensiveOrmWeaverResponseFile>
    <XtensiveOrmWeaverCommandLine>dotnet "$(XtensiveOrmWeaver)" @"$(XtensiveOrmWeaverResponseFile)"</XtensiveOrmWeaverCommandLine>
  </PropertyGroup>
  <ItemGroup>
    <XtensiveOrmWeaverParameters Include="/type:$(MSBuildProjectExtension)" />
    <XtensiveOrmWeaverParameters Include="/backup" />
    <XtensiveOrmWeaverParameters Include="/stamp" />
    <XtensiveOrmWeaverParameters Condition="'$(DebugSymbols)'=='true'" Include="/pdb" />
    <XtensiveOrmWeaverParameters Condition="'$(SignAssembly)'=='true'" Include="/snk:$(AssemblyOriginatorKeyFile)" />
    <XtensiveOrmWeaverParameters Include="@(IntermediateAssembly -> '/input:%(FullPath)')" />
    <XtensiveOrmWeaverParameters Include="@(ReferencePath -> '/r:%(FullPath)')" />
  </ItemGroup>
  <WriteLinesToFile
    Overwrite="true"
    File="$(XtensiveOrmWeaverResponseFile)"
    Lines="@(XtensiveOrmWeaverParameters)" />
  <Exec
    Command="$(XtensiveOrmWeaverCommandLine)"
    WorkingDirectory="$(MSBuildProjectDirectory)"
    IgnoreExitCode="true" >
    <Output TaskParameter="ExitCode" PropertyName="ErrorCode"/>
  </Exec>
  <Error Condition="$(ErrorCode)!=0" Text="Weaving of persistent types ended up with an error."/>
</Target>

</Project>
