<?xml version="1.0" encoding="utf-8" ?>
<!--
***********************************************************************************************
SlowCheetah.targets
WARNING:  DO NOT MODIFY this file, this file is added to your project automatically
          through the SlowCheetah NuGet package. If you modify this file it may
          get out of sync when you update the package at a later date.
This file defines the steps in order to transform XML files.
Copyright (C) Sayed Ibrahim Hashimi, Chuck England 2011-2013. All rights reserved.
***********************************************************************************************
-->
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup>
    <SlowCheetahTaskPath>$(MSBuildThisFileDirectory)..\tools\</SlowCheetahTaskPath>
  </PropertyGroup>
  
  <!--Main transformation task-->
  <UsingTask TaskName="TransformXml" AssemblyFile="$(SlowCheetahTaskPath)SlowCheetah.dll"/>

  <ItemDefinitionGroup>
    <!-- Default TransformOnBuild values for file types -->
    <None>
      <TransformOnBuild>false</TransformOnBuild>
      <Link></Link>
    </None>
    <Content>
      <TransformOnBuild>false</TransformOnBuild>
      <Link></Link>
    </Content>
    <Resource>
      <TransformOnBuild>false</TransformOnBuild>
      <Link></Link>
    </Resource>
    <EmbeddedResource>
      <TransformOnBuild>false</TransformOnBuild>
      <Link></Link>
    </EmbeddedResource>

    <!--Initializing list of files to be transformed-->
    <_FilesToTransform>
      <IsAppConfig>false</IsAppConfig>
    </_FilesToTransform>
  </ItemDefinitionGroup>

  <PropertyGroup>
    <!--Verifies the project type-->
    <WapProjectTypeGuid>349c5851-65df-11da-9384-00065b846f21</WapProjectTypeGuid>
    <_IsWap Condition=" '$(WapProjectTypeGuid)' != '' and '$(ProjectTypeGuids)' != '' ">$(ProjectTypeGuids.Contains($(WapProjectTypeGuid)))</_IsWap>
    <_IsWap Condition=" '$(_IsWap)' == '' ">false</_IsWap>
    <IsWap Condition=" '$(IsWap)' == ''">$(_IsWap)</IsWap>
    
    <!-- References .dll.config files in referenced projects -->
    <ScAllowCopyReferencedConfig Condition=" '$(ScAllowCopyReferencedConfig)'=='' ">true</ScAllowCopyReferencedConfig>
    <AllowedReferenceRelatedFileExtensions Condition=" '$(ScAllowCopyReferencedConfig)'=='true' ">
      $(AllowedReferenceRelatedFileExtensions);
      .dll.config
    </AllowedReferenceRelatedFileExtensions>

    <!-- If the project is not a Web Application, execute these two tasks before build -->
    <BuildDependsOn Condition=" '$(IsWap)'!='true' ">
      $(BuildDependsOn);
      TransformAllFiles;
      ScReplaceAppConfigItem;
    </BuildDependsOn>
  
    <!-- Before transforming the files, discover which should be transformed -->
    <TransformAllFilesDependsOn>
      <!--CopyAssembliesToLocalPath;-->
      DiscoverFilesToTransform;
    </TransformAllFilesDependsOn>
  </PropertyGroup>
  
  

  <!-- The path for the intermediate app.config file generated by SlowCheetah -->
  <PropertyGroup>
    <__SC_IntermediateAppConfig>$(IntermediateOutputPath)$(MSBuildProjectFile)-sc.App.config</__SC_IntermediateAppConfig>
  </PropertyGroup>
    
  <!-- 
    Discover all the files that should be transformed
    Separate them into app.config and not app.config
  -->
  <Target Name="DiscoverFilesToTransform">

    <!--
    This will look through items list: None & Content for those
    with Metadata <TransformOnBuild>True</TransformOnBuild>.
    -->
    <ItemGroup>
      <_FilesToTransform Include="@(None);@(Content);@(Resource);@(EmbeddedResource)"
                         Condition=" '%(TransformOnBuild)' == 'true' ">
        <Link>%(Link)</Link>
        <!-- Required to remove the item if necessary later -->
        <OriginalItemSpec>%(Identity)</OriginalItemSpec>
      </_FilesToTransform>
    </ItemGroup>

    <PropertyGroup>
      <_AppConfigFullPath>@(AppConfigWithTargetPath->'%(RootDir)%(Directory)%(Filename)%(Extension)')</_AppConfigFullPath>
    </PropertyGroup>

    <!-- Now look to see if any of these are the app.config file -->
    <ItemGroup>
      <_FilesToTransform Condition=" '%(Filename)%(Extension)'=='app.config' ">
        <IsAppConfig>true</IsAppConfig>
        <!-- Required to remove the item if necessary later -->
        <OriginalItemSpec>%(Identity)</OriginalItemSpec>
      </_FilesToTransform>
    </ItemGroup>

    <ItemGroup>
      <_FilesToTransformNotAppConfig Include="@(_FilesToTransform)"
                                     Condition=" '%(IsAppConfig)'!='true'">
        <!-- Required to remove the item if necessary later -->
        <OriginalItemSpec>%(Identity)</OriginalItemSpec>
        <Link>%(_FilesToTransform.Link)</Link>
      </_FilesToTransformNotAppConfig>

      <_AppConfigToTransform  Include="@(_FilesToTransform)"
                              Condition=" '%(IsAppConfig)'=='true'"/>
    </ItemGroup>
  </Target>
  
  <!--Main task for transforming all the files (if not associated with a Web Application)-->
  <Target Name="TransformAllFiles"
          DependsOnTargets="$(TransformAllFilesDependsOn)" BeforeTargets="_CopyAppConfigFile">
    
    <!--  -->
    <ItemGroup>
      <_AppConfigTarget Include="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')" />
    </ItemGroup>

    <!--  -->
    <PropertyGroup>
      <_AppConfigDest>@(_AppConfigTarget->'%(FullPath)')</_AppConfigDest>
    </PropertyGroup>

    <!--  -->
    <ItemGroup>
      <_TmpLinkFiles Remove="@(_TmpLinkFiles)" />
      <_TmpLinkFiles Include="@(_FilesToTransformNotAppConfig->'%(Link)')" />
    </ItemGroup>

    <!-- This will handle non Link files -->
    <MakeDir Directories="@(_FilesToTransformNotAppConfig->'$(OutDir)%(RelativeDir)')"
             Condition="Exists('%(RelativeDir)%(Filename).$(Configuration)%(Extension)')
             and '%(Link)'=='' " />

    <!-- This will handle Link files -->
    <MakeDir Directories="@(_TmpLinkFiles->'$(OutDir)%(RelativeDir)')"
                 Condition=" '%(Link)'!='' " />

     <!-- Determines wether the project requires an app.config transformation -->
    <PropertyGroup>
      <_Sc_HasAppConfigTransform>false</_Sc_HasAppConfigTransform>
      <_Sc_HasAppConfigConfigurationTransform Condition=" Exists( '@(_AppConfigToTransform->'%(RelativeDir)%(Filename).$(Configuration)%(Extension)')' ) ">true</_Sc_HasAppConfigConfigurationTransform>
      <_Sc_HasAppConfigPublishProfileTransform Condition=" Exists( '@(_AppConfigToTransform->'%(RelativeDir)%(Filename).$(PublishProfile)%(Extension)')' ) ">true</_Sc_HasAppConfigPublishProfileTransform>
      <_Sc_HasAppConfigPublishProfileTransform Condition=" '$(Configuration)'=='$(PublishProfile)' ">false</_Sc_HasAppConfigPublishProfileTransform>
      <_Sc_HasAppConfigTransform Condition=" '$(_Sc_HasAppConfigConfigurationTransform)'=='true' ">true</_Sc_HasAppConfigTransform>
      <_Sc_HasAppConfigTransform Condition=" '$(_Sc_HasAppConfigPublishProfileTransform)'=='true' ">true</_Sc_HasAppConfigTransform>
    </PropertyGroup>

    <Message Text="Tasks path: $(SlowCheetahTaskPath)" Importance="low"/>

    <!-- we may transform the file multiple times so copy it to dest and then use that as source for all transforms -->
    <Copy SourceFiles="$(AppConfig)" DestinationFiles="$(__SC_IntermediateAppConfig)"/>

    <SlowCheetah.TransformXml Source="$(__SC_IntermediateAppConfig)"
                  Transform="@(_AppConfigToTransform->'%(RelativeDir)%(Filename).$(Configuration)%(Extension)')"
                  Destination="$(__SC_IntermediateAppConfig)"
                  Condition=" '$(_Sc_HasAppConfigConfigurationTransform)'=='true' " />

    <SlowCheetah.TransformXml Source="$(__SC_IntermediateAppConfig)"
                  Transform="@(_AppConfigToTransform->'%(RelativeDir)%(Filename).$(PublishProfile)%(Extension)')"
                  Destination="$(__SC_IntermediateAppConfig)"
                  Condition=" '$(_Sc_HasAppConfigPublishProfileTransform)'=='true' " />

    <PropertyGroup Condition=" '$(_Sc_HasAppConfigTransform)'=='true' " >
      <AppConfig>$(__SC_IntermediateAppConfig)</AppConfig>
    </PropertyGroup>

    <ItemGroup Condition=" '$(_Sc_HasAppConfigTransform)'=='true' " >
      <AppConfigWithTargetPath Remove="@(AppConfigWithTargetPath)" />
      <AppConfigWithTargetPath Include="$(AppConfig)">
        <TargetPath>$(TargetFileName).config</TargetPath>
      </AppConfigWithTargetPath>
    </ItemGroup>

    <!-- Transform the non-Link files -->
    <SlowCheetah.TransformXml Source="@(_FilesToTransformNotAppConfig->'%(FullPath)')"
                  Transform="%(RelativeDir)%(Filename).$(Configuration)%(Extension)"
                  Destination="@(_FilesToTransformNotAppConfig->'$(OutDir)%(RelativeDir)%(Filename)%(Extension)')"
                  Condition=" Exists('%(RelativeDir)%(Filename).$(Configuration)%(Extension)')
                              and '%(Link)'=='' " />

    <!-- Transform the Link files into correct location -->
    <SlowCheetah.TransformXml Source="@(_FilesToTransformNotAppConfig->'%(FullPath)')"
                  Transform="%(RelativeDir)%(Filename).$(Configuration)%(Extension)"
                  Destination="@(_FilesToTransformNotAppConfig->'$(OutDir)%(Link)')"
                  Condition="Exists('%(RelativeDir)%(Filename).$(Configuration)%(Extension)')
                              and '%(Link)'!='' " />

  </Target>

  <!-- 
  ***********************************************************
                    WAP related items below
  ***********************************************************
  -->

  <ItemGroup>
    <!-- This will exclude packageRestore.proj from the web publish process -->
    <ExcludeFromPackageFiles Include="packageRestore.proj">
      <FromTarget>Project</FromTarget>
    </ExcludeFromPackageFiles>
  </ItemGroup>
  
  <PropertyGroup Condition=" '$(IsWap)' == 'true' ">
    
    <OnAfterPipelinePreDeployCopyAllFilesToOneFolder>
      $(OnAfterPipelinePreDeployCopyAllFilesToOneFolder);
      CopyTransformFilesWap;
    </OnAfterPipelinePreDeployCopyAllFilesToOneFolder>
    
    <CopyAllFilesToSingleFolderForMsdeploy>
      $(CopyAllFilesToSingleFolderForMsdeploy);
      CopyTransformFilesWap;
    </CopyAllFilesToSingleFolderForMsdeploy>

    <CopyTransformFilesWapDependsOn>
      CoreCopyTransformFilesWap;
      CopyTransformFileWapPublishProfile;
    </CopyTransformFilesWapDependsOn>

    <CoreCopyTransformFilesWapDependsOn>
      DiscoverFilesToTransform;
    </CoreCopyTransformFilesWapDependsOn>

    <!-- For VS2012 -->
    <PipelineCopyAllFilesToOneFolderForMsdeployDependsOn>
      $(PipelineCopyAllFilesToOneFolderForMsdeployDependsOn);
      CopyTransformFilesWap
    </PipelineCopyAllFilesToOneFolderForMsdeployDependsOn>

    <!-- Required for File System -->
    <PipelinePreDeployCopyAllFilesToOneFolderDependsOn>
      $(PipelinePreDeployCopyAllFilesToOneFolderDependsOn);
      CopyTransformFilesWap;
    </PipelinePreDeployCopyAllFilesToOneFolderDependsOn>
    
    <!-- required for FS support from the VS publish dialog -->
    <OnAfterCopyAllFilesToSingleFolderForPackage>
      $(OnAfterCopyAllFilesToSingleFolderForPackage);
      CopyTransformFilesWap;
    </OnAfterCopyAllFilesToSingleFolderForPackage>
  </PropertyGroup>

  <Target Name="CopyTransformFilesWap" DependsOnTargets="$(CopyTransformFilesWapDependsOn)"/>

  <Target Name="CoreCopyTransformFilesWap"
         DependsOnTargets="$(CoreCopyTransformFilesWapDependsOn)">
    
    <ItemGroup>
      <_TmpLinkFiles Remove="@(_TmpLinkFiles)" />
      <_TmpLinkFiles Include="@(_FilesToTransformNotAppConfig->'%(Link)')" />
    </ItemGroup>

    <!-- This will ignore link files and the second one will handle them -->
    <MakeDir Directories="@(_FilesToTransformNotAppConfig->'$(_PackageTempDir)\%(RelativeDir)')"
         Condition="Exists('%(RelativeDir)%(Filename).$(Configuration)%(Extension)')
                    and '%(Link)' == '' "/>

    <MakeDir Directories="@(_TmpLinkFiles->'$(OutDir)%(RelativeDir)')"
                 Condition=" '%(Link)'!='' " />

    <!--Make sure that we do not do this for web.config-->
    <!-- This usage will skip Link files, the task usage below will handle Link files -->
    <SlowCheetah.TransformXml Source="@(_FilesToTransformNotAppConfig->'%(FullPath)')"
              Transform="%(RelativeDir)%(Filename).$(Configuration)%(Extension)"
              Destination="@(_FilesToTransformNotAppConfig->'$(_PackageTempDir)\%(RelativeDir)%(Filename)%(Extension)')"
              Condition=" '%(Filename)%(Extension)' != 'web.config' and
                          Exists('%(RelativeDir)%(Filename).$(Configuration)%(Extension)')
                          and '%(Link)'=='' " />

    <!-- Transform the Link files, they always go into the root directory -->
    <SlowCheetah.TransformXml Source="@(_FilesToTransformNotAppConfig->'%(FullPath)')"
              Transform="%(RelativeDir)%(Filename).$(Configuration)%(Extension)"
              Destination="@(_FilesToTransformNotAppConfig->'$(_PackageTempDir)\%(Link)')"
              Condition=" '%(Filename)%(Extension)' != 'web.config' and
                          Exists('%(RelativeDir)%(Filename).$(Configuration)%(Extension)')
                          and '%(Link)'!='' " />
  </Target>

  <Target Name="CopyTransformFileWapPublishProfile" DependsOnTargets="CoreCopyTransformFilesWap">
    
    <ItemGroup>
      <_ScWapPubProfileFullPath Include="$(WebPublishProfileFile)"/>
    </ItemGroup>
    
    <PropertyGroup>
      <_WapPubProfile Condition=" '$(_WapPubProfile)'=='' and '@(_ScWapPubProfileFullPath)'!='' ">%(_ScWapPubProfileFullPath.Filename)</_WapPubProfile>
    </PropertyGroup>

    <!--Make sure that we do not do this for web.config-->
    <!-- This usage will skip Link files, the task usage below will handle Link files -->
    <SlowCheetah.TransformXml Source="@(_FilesToTransformNotAppConfig->'$(_PackageTempDir)\%(RelativeDir)%(Filename)%(Extension)')"
              Transform="%(RelativeDir)%(Filename).$(_WapPubProfile)%(Extension)"
              Destination="@(_FilesToTransformNotAppConfig->'$(_PackageTempDir)\%(RelativeDir)%(Filename)%(Extension)')"
              Condition=" '%(Filename)%(Extension)' != 'web.config' and
                          Exists('%(RelativeDir)%(Filename).$(_WapPubProfile)%(Extension)')
                          and '%(Link)'=='' and '$(_WapPubProfile)'!='' and '$(_WapPubProfile)'!='$(Configuration)' " />

    <!-- Transform the Link files, they always go into the root directory -->
    <SlowCheetah.TransformXml Source="@(_FilesToTransformNotAppConfig->'$(_PackageTempDir)\%(Link)')"
              Transform="%(RelativeDir)%(Filename).$(_WapPubProfile)%(Extension)"
              Destination="@(_FilesToTransformNotAppConfig->'$(_PackageTempDir)\%(Link)')"
              Condition=" '%(Filename)%(Extension)' != 'web.config' and
                          Exists('%(RelativeDir)%(Filename).$(_WapPubProfile)%(Extension)')
                          and '%(Link)'!=''  and '$(_WapPubProfile)'!='' and '$(_WapPubProfile)'!='$(Configuration)' " />
  </Target>

  <!-- 
  ***********************************************************
                ClickOnce related items below
  ***********************************************************
  -->

  <!-- Target was named: SlowCheetah_ClickOnceUpdate -->
  <Target Name="ScReplaceAppConfigItem"
          BeforeTargets="_DeploymentComputeClickOnceManifestInfo;BuiltProjectOutputGroup"
          DependsOnTargets="TransformAllFiles">
    <!--<Message Text="ScReplaceAppConfigItem" Importance="low"/>
    <PropertyGroup>
      <_SlowCheetahAppConfigTransformExists Condition="Exists(@(_AppConfigToTransform->'%(RelativeDir)%(Filename).$(Configuration)%(Extension)'))">true</_SlowCheetahAppConfigTransformExists>
    </PropertyGroup>
    <PropertyGroup>
      <_OldAppConfigWithTargetPath>%(AppConfigWithTargetPath.TargetPath)</_OldAppConfigWithTargetPath>
      <_SCNewAppConfigFile>@(AppConfigWithTargetPath->'$(IntermediateOutputPath)SlowCheetah\%(Filename)%(Extension)')</_SCNewAppConfigFile>
      <_TmpFileExist>false</_TmpFileExist>
      <_TmpFileExist Condition="Exists('@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')')">true</_TmpFileExist>
    </PropertyGroup>
    <Message Text="****_TmpFileExis: [$(_TmpFileExist)]" Importance="high"/>
    <Copy SourceFiles="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')" 
          DestinationFiles="$(_SCNewAppConfigFile)" 
          Condition="'$(_SlowCheetahAppConfigTransformExists)'=='true' and Exists('@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')')" />
    
    <ItemGroup Condition="'$(_SlowCheetahAppConfigTransformExists)'=='true'">
      <AppConfigWithTargetPath Remove="@(AppConfigWithTargetPath)" />
      <AppConfigWithTargetPath Include="$(_SCNewAppConfigFile)">
        <TargetPath>$(_OldAppConfigWithTargetPath)</TargetPath>
      </AppConfigWithTargetPath>
    </ItemGroup>

    <Message Text="Updated app.config to point to transformed file" />
    <Message Text="    AppConfigWithTargetPath: @(AppConfigWithTargetPath)"/>
    <Message Text="    AppConfigWithTargetPath.TargetPath: @(AppConfigWithTargetPath->'%(TargetPath)')"/>-->

  </Target>

  <Target Name="SlowCheetah_ClickOnceLooseFileUpdate" AfterTargets="_DeploymentComputeClickOnceManifestInfo" DependsOnTargets="DiscoverFilesToTransform">
    <!-- For non app.config files which are being transformed we need to remove the original item and replace it with the transformed one -->
    <ItemGroup>
      <_DeploymentManifestFiles Remove="%(_FilesToTransformNotAppConfig.OriginalItemSpec)" />
      <!-- Implementation for non-Link files -->
      <_DeploymentManifestFiles Include="@(_FilesToTransformNotAppConfig->'$(OutDir)%(RelativeDir)%(Filename)%(Extension)')"
                                Condition=" '%(_FilesToTransformNotAppConfig.Link)'=='' ">
        <TargetPath Condition=" '%(_FilesToTransformNotAppConfig.Link)'=='' ">%(RelativeDir)%(Filename)%(Extension)</TargetPath>
      </_DeploymentManifestFiles>

      <!-- Implementation for Linked files -->
      <_DeploymentManifestFiles Include="@(_FilesToTransformNotAppConfig->'$(OutDir)%(Link)')"
                                Condition=" '%(_FilesToTransformNotAppConfig.Link)'!='' ">
        <TargetPath>%(_FilesToTransformNotAppConfig.Link)</TargetPath>
      </_DeploymentManifestFiles>
    </ItemGroup>
  </Target>

  <!-- 
  ***********************************************************
              Setup project related items below
  ***********************************************************
  -->

  <PropertyGroup>
    <SlowCheetahEnableSetupProjects Condition=" '$(SlowCheetahEnableSetupProjects)'=='' ">true</SlowCheetahEnableSetupProjects>
  </PropertyGroup>

  <PropertyGroup Condition=" '$(SlowCheetahEnableSetupProjects)'=='true'">
    
    <AddAppConfigToBuildOutputs>false</AddAppConfigToBuildOutputs>

    <BuiltProjectOutputGroupDependsOn>
      $(BuiltProjectOutputGroupDependsOn);
      AfterBuiltProjectOutputGroup
    </BuiltProjectOutputGroupDependsOn>
  </PropertyGroup>

  <Target Name="AfterBuiltProjectOutputGroup" DependsOnTargets="TransformAllFiles">
    
    <ItemGroup>
      <_TmpAppConfig Include="@(AppConfigWithTargetPath->'$(OutDir)%(TargetPath)')" />
    </ItemGroup>

    <!-- 
      We need to get the full path to the files included in the Identiy metadata.
      This is required for Worker Roles. The target CopyWorkerRoleFiles attempts to copy but
      does not use the FullPath so it will not locate the files otherwise.
      See https://github.com/sayedihashimi/slow-cheetah/issues/44.
    -->
    <ItemGroup>
      <_FilesToTransformNotAppConfig>
        <FullPathToItem Condition=" '%(Link)'=='' ">$([System.IO.Path]::GetFullPath( $(OutDir)%(RelativeDir)%(Filename)%(Extension) ))</FullPathToItem>
        <FullPathToItem Condition=" '%(Link)'!='' ">$([System.IO.Path]::GetFullPath( $(OutDir)%(Link) ))</FullPathToItem>
      </_FilesToTransformNotAppConfig>
    </ItemGroup>

    <ItemGroup>
      
      <BuiltProjectOutputGroupOutput Include="@(_TmpAppConfig->'%(FullPath)')">
        <!-- For compatibility with 2.0 -->
        <OriginalItemSpec>$(AppConfig)</OriginalItemSpec>
      </BuiltProjectOutputGroupOutput>

      <BuiltProjectOutputGroupOutput Include="@(_FilesToTransformNotAppConfig->'%(FullPathToItem)')"
                                     Condition=" '%(_FilesToTransformNotAppConfig.Link)'==''">
        <OriginalItemSpec>@(_FilesToTransformNotAppConfig->'$(OutDir)%(RelativeDir)%(Filename)%(Extension)')</OriginalItemSpec>
        <TargetPath>@(_FilesToTransformNotAppConfig->'%(RelativeDir)%(Filename)%(Extension)')</TargetPath>
      </BuiltProjectOutputGroupOutput>

      <BuiltProjectOutputGroupOutput Include="@(_FilesToTransformNotAppConfig->'%(FullPathToItem)')"
                                     Condition=" '%(_FilesToTransformNotAppConfig.Link)'!=''">
        <OriginalItemSpec>@(_FilesToTransformNotAppConfig->'$(OutDir)%(Link)')</OriginalItemSpec>
        <TargetPath>@(_FilesToTransformNotAppConfig->'%(Link)')</TargetPath>
      </BuiltProjectOutputGroupOutput>
      
    </ItemGroup>

    <!--
    <Message Text="OutDir: $(Outdir)" Importance="high"/>
    <Message Text="BuiltProjectOutputGroupOutput: [%(BuiltProjectOutputGroupOutput.Identity),%(BuiltProjectOutputGroupOutput.TargetPath)]" Importance="high"/>
    -->
    
  </Target>
  
</Project>
