<?xml version="1.0" encoding="utf-8" ?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <!-- give project-specific settings reasonable defaults -->
    <ILMergePackagesPath Condition=" $(ILMergePackagePath) == '' ">$(SolutionDir)packages</ILMergePackagesPath>
    <ILMergeOrderFile Condition=" $(ILMergeOrderFile) == '' ">$(MSBuildProjectDir)ILMergeOrder.txt</ILMergeOrderFile>
    <ILMergeKeyFile Condition=" $(ILMergeKeyFile) == '' ">$(KeyOriginatorFile)</ILMergeKeyFile>
    <ILMergeAssemblyVersion Condition=" $(ILMergeAssemblyVersion) == '' "></ILMergeAssemblyVersion>
    <ILMergeToolsPath>$(MSBuildThisFileDirectory)..\tools\</ILMergeToolsPath>
    <ILMergeInternalizeExcludeFile Condition=" $(ILMergeInternalizeExcludeFile) == '' ">$(MSBuildProjectDir)ILMergeInternalizeExcludeList.txt</ILMergeInternalizeExcludeFile>
  </PropertyGroup>

  <!-- decide what goes into output after compile-->
  <Target Name="SaveILMergeData" AfterTargets="CoreCompile">
    <Message Text="Transitive merge" Importance="high" Condition="$(ILMergeTransitive) == 'true'" />

    <!-- all copy local assemblies referenced from this project that go to the executable except the main one-->
    <CreateItem Include="@(ReferencePath)" Condition=" '%(CopyLocal)' == 'true' ">
      <Output TaskParameter="Include"  ItemName="MergedAssemblies"/>
    </CreateItem>

    <!-- all copy local dependency assemblies-->
    <CreateItem Include="@(ReferenceDependencyPaths)" Condition=" '%(CopyLocal)' == 'true' ">
      <Output TaskParameter="Include" ItemName="MergedDependencies"/>
    </CreateItem>
    
    <!-- all assemblies that doesn't so we use their directories as library path -->
    <CreateItem Include="@(ReferencePath)" Condition=" '%(CopyLocal)' == 'false' ">
      <Output TaskParameter="Include" ItemName="UnmergedAssemblies"/>
    </CreateItem>

    <!-- all content items marked as copy always or newest -->
    <CreateItem Include="@(Content)" Condition=" '%(Content.CopyToOutputDirectory)' == 'Always' OR '%(Content.CopyToOutputDirectory)' == 'PreserveNewest' ">
      <Output TaskParameter="Include"  ItemName="LocalContentFiles"/>
    </CreateItem>

    <PropertyGroup Condition=" $(ILMergeTransitive) == 'true' ">
      <!-- add the main assembly as the first one -->
      <MergedAssemblies>@(IntermediateAssembly->'%(FullPath)');@(MergedAssemblies->'%(FullPath)');@(MergedDependencies->'%(FullPath)')</MergedAssemblies>
    </PropertyGroup>
    <PropertyGroup Condition=" $(ILMergeTransitive) == 'false' OR $(ILMergeTransitive) == '' ">
      <MergedAssemblies>@(IntermediateAssembly->'%(FullPath)');@(MergedAssemblies->'%(FullPath)')</MergedAssemblies>
    </PropertyGroup>
    
    <PropertyGroup>
      <!-- Ideally we should copy all not-copy-local assemblies that are not in the ILMergeLibraryPath to -->
      <!-- a temp directory and add it to the start search path, but we keep it simple here -->
      <UnmergedAssemblies>@(UnmergedAssemblies->'%(FullPath)')</UnmergedAssemblies>
      <MergeOutputFile>$(TargetPath)</MergeOutputFile>
    </PropertyGroup>
  </Target>

  <!-- do not copy copy-local assemblies, they will be merged -->
  <Target Name="_CopyFilesMarkedCopyLocal" />

  <!-- override standard target our own merge-and-copy-content -->
  <Target Name="CopyFilesToOutputDirectory">
    <Message Text="Merged assemblies: $(MergedAssemblies)" Importance="high" />
    <Message Text="Not merged assemblies: $(UnmergedAssemblies)" Importance="normal" />
    <Message Text="Merged Output in: $(MergeOutputFile)" Importance="normal" />
    <Message Text="Key file: $(ILMergeKeyFile)" Importance="normal" />
    <Message Text="Libraries in: $(ILMergeLibraryPath)" Importance="normal" />
    <Message Text="Packages in: $(ILMergePackagesPath)" Importance="normal" />
    <Message Text="Merge order file: $(ILMergeOrderFile)" Importance="normal" />
    <Message Text="Local content: @(LocalContentFiles)" Importance="low" />
    <Message Text="Internalization enabled: $(ILMergeInternalize)" Importance="normal" />

    <!-- run ILMerge  -->
    <MSBuild.AutoILMerge.Task TargetPlatform="net40"
        KeyFile="$(KeyOriginatorFile)"
        OutputFile="$(MergeOutputFile)"
        LibraryPath="$(ILMergeLibraryPath)"
        InputAssemblies="$(MergedAssemblies)"
        LibraryAssemblies="$(UnmergedAssemblies)"
        PackagesDir="$(ILMergePackagesPath)"
        MergeOrderFile="$(ILMergeOrderFile)"
        DebugInfo="true" ShouldLog="true"
        Internalize="$(ILMergeInternalize)"
        AllowDuplicateType="$(AllowDuplicateType)"
        InternalizeExcludeFile="$(ILMergeInternalizeExcludeFile)" />
    <!-- copy content files marked as copy always or newest -->
    <Copy SourceFiles="@(LocalContentFiles)" DestinationFolder="$(OutputPath)" />
  </Target>
  <UsingTask AssemblyFile="$(ILMergeToolsPath)MSBuild.AutoILMerge.Task.dll" TaskName="MSBuild.AutoILMerge.Task" />
</Project>
