Why does the MsbuildExtension Detokenise class reload the project? - msbuild

This is problematic because any Properties being passed in are lost.
Further Explanation: I pass in a property to the project file. This property is a path to a .props file. It contains tokens and replacement values for the detokenise class. The task apparently reloads the project and the path is not maintained. This doesn't seem to be the case for other task, for example the guid tasks.
In the example I am using a example proj entitled guids.proj
Invoked Using :
<MSBuild.ExtensionPack.FileSystem.Detokenise TaskAction="Detokenise" TargetFiles="#(FileCollectionToBeDetokenized )"/>
Some command line out put follows :
Task "MSBuild.ExtensionPack.FileSystem.Detokenise" (TaskId:11)
Detokenise Task Execution Started [13:04:35] (TaskId:11)
Loading Project: C:\Users\bstrausser\Desktop\guids.proj (TaskId:11)
Detokenising Collection: 1 files (TaskId:11)
C:\Users\*****\Desktop\guids.proj(37,9): error : Property not found: Asset
Directory
Full project file :
Project ToolsVersion="4.0" DefaultTargets="Default" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(ParentMSBuildProjectDirectory)\Bin\MSBuild\ExtensionPack\MSBuild.ExtensionPack.tasks" Condition="Exists('$(ParentMSBuildProjectDirectory)\Bin\MSBuild\ExtensionPack\MSBuild.ExtensionPack.tasks')"/>
<Import Project="C:\Program Files (x86)\MSBuild\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks" Condition="!Exists('$(ParentMSBuildProjectDirectory)\Bin\MSBuild\ExtensionPack\MSBuild.ExtensionPack.tasks') AND Exists('C:\Program Files (x86)\MSBuild\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks')"/>
<PropertyGroup>
<TPath>$(MSBuildProjectDirectory)\..\MSBuild.ExtensionPack.tasks</TPath>
<TPath Condition="Exists('$(MSBuildProjectDirectory)\..\..\Common\MSBuild.ExtensionPack.tasks')">$(MSBuildProjectDirectory)\..\..\Common\MSBuild.ExtensionPack.tasks</TPath>
<PROPS>$(DACP)</PROPS>
</PropertyGroup>
<Import Project="$(PROPS)" Condition="'$(DACP)' != ''" />
<Target Name="Default">
<Message text = "$(DACP)" />
<!-- Create a new Guid and get the formatted and unformatted values -->
<MSBuild.ExtensionPack.Framework.Guid TaskAction="Create">
<Output TaskParameter="FormattedGuidString" PropertyName="FormattedGuidString1" />
<Output TaskParameter="GuidString" PropertyName="GuidStringItem" />
</MSBuild.ExtensionPack.Framework.Guid>
<Message Text="GuidStringItem: $(GuidStringItem)"/>
<Message Text="FormattedGuidString: $(FormattedGuidString1)"/>
<!-- Create a new cryptographically strong Guid and get the formatted and unformatted values -->
<MSBuild.ExtensionPack.Framework.Guid TaskAction="CreateCrypto">
<Output TaskParameter="FormattedGuidString" PropertyName="FormattedGuidString1" />
<Output TaskParameter="GuidString" PropertyName="GuidStringItem" />
</MSBuild.ExtensionPack.Framework.Guid>
<Message Text="GuidStringItem Crypto: $(GuidStringItem)"/>
<Message Text="FormattedGuidString Crypto: $(FormattedGuidString1)"/>
<ItemGroup>
<FileCollectionToBeDetokenized Include="C:\Code\MSBuildGit\Configuration\TaskExecutorConfigTransforms\App.GREEN.SCRATCH.config"/>
</ItemGroup>
<Message text = "BaseUrl : $(BaseUrl)" />
<Message text = "DetokenizedTransformFile : #(FileCollectionToBeDetokenized)" />
<MSBuild.ExtensionPack.FileSystem.Detokenise TaskAction="Detokenise" TargetFiles="#(FileCollectionToBeDetokenized )"/>
</Target>

Related

Expand MSBuild property containing the name of other property

Let's say I have property $(Foo), that is defined as a result of some function, that returns the string value $(Bar). Is it possible to expand it somehow, so that $(Foo) will be expanded to the value of $(Bar)?
Given example project:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Bar>Bar Value</Bar>
<Foo>$([System.String]::Concat("$(","Bar",")"))</Foo>
<Baz>$(Foo)</Baz>
<Qux>$(Bar)</Qux>
</PropertyGroup>
<Target Name="Test">
<Message Text="Foo == $(Foo)" />
<Message Text="Baz == $(Baz)" />
<Message Text="Qux == $(Qux)" />
</Target>
</Project>
Here's what I have:
S:\>msbuild Test.proj /t:Test /nologo
Build started 18.09.2013 17:52:14.
Project "S:\Test.proj" on node 1 (Test target(s)).
Test:
Foo == $(Bar)
Baz == $(Bar)
Qux == Bar Value
Done Building Project "S:\Test.proj" (Test target(s)).
Build succeeded.
0 Warning(s)
0 Error(s)
So, $(Qux), which is defined to $(Bar) directly, is expanded correctly, but $(Foo) and $(Baz) are not. Is it possible to expand them too?
S:\>msbuild /version
Microsoft (R) Build Engine version 4.0.30319.17929
[Microsoft .NET Framework, version 4.0.30319.18052]
Copyright (C) Microsoft Corporation. All rights reserved.
4.0.30319.17929
You want to simulate something like $($(Foo)) that is invalid syntax for MsBuild. But you can simulate this behavior by using items and by dynamically creating items in targets. You can’t do it in the "global scope" because of Property and Item evaluation order.
So you have to do it in some targets.
Here is sample that sets property by means of InitialTargets.
<?xml version="1.0" encoding="utf-8"?>
<Project InitialTargets="MyPropertiesSetup" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Bar>Bar Value</Bar>
<!-- A value of Foo property specifies the name of the property it takes value from. -->
<Foo>Bar</Foo>
<Baz></Baz>
<Qux>$(Bar)</Qux>
</PropertyGroup>
<Target Name="Test">
<Message Text="Foo == $(Foo)" />
<Message Text="Baz == $(Baz)" />
<Message Text="Qux == $(Qux)" />
</Target>
<Target Name="MyPropertiesSetup">
<ItemGroup>
<_Foo Include="$(Foo)" />
<_Baz Include="%(_Foo.Identity)" />
</ItemGroup>
<PropertyGroup>
<Foo>$(%(_Foo.Identity))</Foo>
<Baz>$(%(_Baz.Identity))</Baz>
</PropertyGroup>
</Target>
</Project>
If you can use item for values instead properties, there is more elegant way do it (MSBuild Trickery #68 - #($(CanYouDoThis)):
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Foo>Bar</Foo>
<Foo2>Bar2</Foo2>
</PropertyGroup>
<ItemGroup>
<Bar Include="Bar Value" />
<Bar2 Include="Bar2 Value" />
</ItemGroup>
<Target Name="Test">
<Message Text="Foo == '#($(Foo))'" />
<Message Text="Foo2 == '#($(Foo2))'" />
</Target>
</Project>

MSBuild copy task and condition name from file with wildcards

I have a FilesToExclude2.txt file which contains all excludes like this :
*.settings
*#*
*.vbproj*
*.csproj*
*\Errors\*
*\Errors
*\_vti_*\*
*\_vti_*
*\CVS\*
In my tasks file, there is a copy task
<Target Name="CustomModuleCopy">
<ItemGroup>
<ModuleFiles Include="$(SolutionModuleName)\$(ProjectModuleName)\**\*.*" />
<FileToExclude Include="$(BasePath)\$(SolutionModuleName)\FilesToExclude2.txt" />
</ItemGroup>
<ReadLinesFromFile File="#(FileToExclude)">
<Output TaskParameter="Lines" ItemName="FileContents" />
</ReadLinesFromFile>
<Copy SourceFiles="#(ModuleFiles)" DestinationFiles="#(ModuleFiles->'$(DestFolder)\$(ProjectModuleName)\%(RecursiveDir)%(Filename)%(Extension)')" Condition="'%(ModuleFiles.Identity)' != #(FileContents)" ContinueOnError="false" />
<!--CallTarget Targets="RemoveCustomModuleConfigFiles" /-->
</Target>
Without the "condition" attribute on the copy task, everything's works fine, but I do not know how to implement the condition with wildcard and if it is possible
Any kind of help would be greatly appreciated :) this is freaking me out for few hours...
Would it not be easier to just use the exclude property instead?
as in
Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ModuleFiles Include="c:\code\**\*.*"
Exclude="#(FileContents)" />
</ItemGroup>
<!--<Copy SourceFiles="#(ModuleFiles)" DestinationFolder="Some|Destination" Condition="'%(ModuleFiles.Identity)' != #(FileContents)" ContinueOnError="false" />
CallTarget Targets="RemoveCustomModuleConfigFiles" /-->
the following code
<Target Name="CustomModuleCopy">
<ItemGroup>
<FileToExclude Include="$(BasePath)\$(SolutionModuleName)\FilesToExclude.txt" />
</ItemGroup>
<ReadLinesFromFile File="#(FileToExclude)">
<Output TaskParameter="Lines" ItemName="FileContents" />
</ReadLinesFromFile>
<ItemGroup>
<ModuleFiles Include="$(SolutionModuleName)\$(ProjectModuleName)\**\*.*" Exclude="#(FileContents)" />
</ItemGroup>
<Copy SourceFiles="#(ModuleFiles)" DestinationFiles="#(ModuleFiles->'$(DestFolder)\$(ProjectModuleName)\%(RecursiveDir)%(Filename)%(Extension)')" ContinueOnError="false" />
<Message Text="Exclude = #(FileContents)" />
<!--CallTarget Targets="RemoveCustomModuleConfigFiles" /-->
</Target>
produce this output in console :
copy /y "CPBMessaging\CPBMessagingWeb_NotificationMessage\Web.Debug.config" "C:\__CPB\CPBSite\CPBMessagingWeb_NotificationMessage\Web.Debug.config"
Copying file from "CPBMessaging\CPBMessagingWeb_NotificationMessage\Web.Release.config" to "C:\__CPB\CPBSite\CPBMessagingWeb_NotificationMessage\Web.Release.config"
copy /y "CPBMessaging\CPBMessagingWeb_NotificationMessage\Web.Release.config" "C:\__CPB\CPBSite\CPBMessagingWeb_NotificationMessage\Web.Release.config"
Exclude = *.cab;*.config;*.vb;*.cs;*.resx;*.xsx;*.bak;*.myapp;*.settings;*#*;
*.vbproj*;*.csproj*;*\Errors\*;*\Errors;*\_vti_*\*;*\_vti_*;*\CVS\*;*\CVS;

Copy Item's Metadata for batching

This is an extension to this question.
Suppose I'm creating items from other item's metadata:
<ItemGroup>
<A>
<files>f1;f2;f3;...</files>
<x>...</x>
<y>...</y>
<z>...</z>
...
</A>
<B Include="%(A.files)">
<x>%(A.x)</x>
<y>%(A.y)</y>
<z>%(A.z)</z>
...
</B>
</ItemGroup>
%(A.files) is a list of files separated by ;, such that for each A item I'm creating many B items (one for each file).
But frequently when I process B item I need the original's A item metadata. In this example I copied each metadata manually from A to B.
Is there a way to copy all of A's metadata to B without explicitly specifying each one of them?
Little late, but I like this solution better:
<B Include="#(A->Metadata('files'))" />
Full Example:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<A Include="A1">
<files>a1_file1.htm;a1_file2.htm</files>
<x>a1</x>
<y>b1</y>
<z>c1</z>
</A>
<A Include="A2">
<files>a2_file.proj</files>
<x>a2</x>
<y>b2</y>
<z>c2</z>
</A>
<B Include="#(A->Metadata('files'))" />
</ItemGroup>
<Target Name="Build">
<Message Text="A: #(A->'%(Identity) x:%(x) y:%(y) z:%(z) files:%(files)', '
')" />
<Message Text="B: #(B->'%(Identity) x:%(x) y:%(y) z:%(z) files:%(files)', '
')" />
</Target>
</Project>
Output:
Build:
A: A1 x:a1 y:b1 z:c1 files:a1_file1.htm;a1_file2.htm
A2 x:a2 y:b2 z:c2 files:a2_file.proj
B: a1_file1.htm x:a1 y:b1 z:c1 files:a1_file1.htm;a1_file2.htm
a1_file2.htm x:a1 y:b1 z:c1 files:a1_file1.htm;a1_file2.htm
a2_file.proj x:a2 y:b2 z:c2 files:a2_file.proj
As far as I can tell, MSBuild only copies metadata when there is a one-to-one mapping between the input and output item lists. In your case, you're starting with one item and expanding to many items. To get around this, I suggest using item batching:
<?xml version="1.0" encoding="iso-8859-1"?>
<Project
xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
ToolsVersion="4.0"
DefaultTargets="Print">
<ItemGroup>
<A Include="A1">
<files>test.htm;test_sol1.htm</files>
<x>a1</x>
<y>b1</y>
<z>c1</z>
</A>
<A Include="A2">
<files>test.proj</files>
<x>a2</x>
<y>b2</y>
<z>c2</z>
</A>
</ItemGroup>
<Target Name="ExpandA">
<ItemGroup>
<ExpandedA Include="%(A.files)">
<Original>%(Identity)</Original>
</ExpandedA>
</ItemGroup>
</Target>
<Target
Name="CopyMetadata"
Outputs="%(ExpandedA.Identity)"
DependsOnTargets="ExpandA">
<PropertyGroup>
<ExpandedAIdentity>%(ExpandedA.Identity)</ExpandedAIdentity>
<ExpandedAOriginal>%(ExpandedA.Original)</ExpandedAOriginal>
</PropertyGroup>
<ItemGroup>
<ExpandedAMetadata Include="#(A)" Condition=" '%(Identity)' == '$(ExpandedAOriginal)' ">
<Expanded>$(ExpandedAIdentity)</Expanded>
</ExpandedAMetadata>
</ItemGroup>
</Target>
<Target Name="Print" DependsOnTargets="CopyMetadata">
<ItemGroup>
<B Include="#(ExpandedAMetadata->'%(Expanded)')" />
</ItemGroup>
<!--Use commas to illustrate that "files" has been expanded-->
<Message Text="A: %(A.files)" />
<Message Text="ExpandedA: #(ExpandedA, ',')" />
<Message Text="ExpandedAMetadata: #(ExpandedAMetadata, ',')" />
<Message Text="B: #(B->'%(Identity) x:%(x) y:%(y) z:%(z)', ',')" />
</Target>
</Project>
and the output of the "Print" target:
Print:
A: test.htm;test_sol1.htm
A: test.proj
ExpandedA: test.htm,test_sol1.htm,test.proj
ExpandedAMetadata: A1,A1,A2
B: test.htm x:a1 y:b1 z:c1,test_sol1.htm x:a1 y:b1 z:c1,test.proj x:a2 y:b2 z:c2
ExpandedA is similar to B in your original question; it is the expanded version of A but without any metadata. Then I run the CopyMetadata target once for each item in ExpandedA (thanks to item batching). Each run, the original A item is copied to the ExpandedAMetadata item group along with all of its metadata. The Original metadata is used to ensure that the correct A item is associated with each file. Finally, in the Print target, B is constructed using an item transformation, so that all the metadata from ExpandedAMetadata is copied over as well.
I'm not sure if I get what you need, but you can copy all A metedata to B as in the answer for related question:
<B Include="#(A)">
It should copy all metedata from A to B.
Here is a direct answer to the original question:
<!-- Copy all metadata from an input item to an output item -->
<UsingTask
TaskName="CopyMetadataTask"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
<ParameterGroup>
<InputItem ParameterType="Microsoft.Build.Framework.ITaskItem" Required="true" />
<OutputItem ParameterType="Microsoft.Build.Framework.ITaskItem" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System"/>
<Code Type="Fragment" Language="cs">
<![CDATA[
InputItem.CopyMetadataTo(OutputItem);
]]>
</Code>
</Task>
</UsingTask>
You can use this task to copy metadata between items the way it was originally intended, and it can easily be adapted to multiple items on input/output, though doing so does induce issues whereby it is necessary to match the inputs and outputs 1/1, or make other compromises, though it can easily be adapted to the exact situation since you can modify the C# code however is necessary. E.g.:
<UsingTask
TaskName="CopyMetadataTask"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
<ParameterGroup>
<InputItem ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
<OutputItem ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System"/>
<Code Type="Fragment" Language="cs">
<![CDATA[
for (int i = 0; i < InputItem.Length; ++i)
{
InputItem[i].CopyMetadataTo(OutputItem[i]);
}
]]>
</Code>
</Task>
</UsingTask>
The task can then be called to copy metadata directly, for example (using the second version):
<ItemGroup>
<ItemListA Include="a;b;c" Metadata="MetadataValue" />
<ItemListB Include="d;e;f" OtherMetadata="OtherValue" />
</ItemGroup>
<CopyMetadataTask InputItem="#(ItemListA)" OutputItem="#(ItemListB)" />

Is there a way to easily output the current time in MSBuild (version 3.5)?

I'd like to output the current time (preferably before every task or at minimum when starting/completing every target) when invoking MSBuild 3.5.
I tried creating a target that I would continually call that looks like:
<Target Name="EchoTime">
<Time Format="yyyy-MM-dd HH:mm:ss.fff">
<Output TaskParameter="FormattedTime" PropertyName="currentTime" />
</Time>
<Message Text = "$(currentTime)" />
</Target>
...but it turns out that one target can only call another target once per execution.
So if I try...
<Target Name="TimeTest" >
<Message Text = "--------------------------------------------------" />
<CallTarget Targets="EchoTime" />
<Message Text = " " />
<Message Text = "Try calling EchoTime again" />
<Message Text = " " />
<CallTarget Targets="EchoTime" />
<Message Text = "--------------------------------------------------" />
</Target>
Then the output looks like...
Build started 10/12/2011 2:24:52 PM.
Project "C:\Temp\MSBuildSandbox\MSBuild_EchoTime.xml" on node 0 (TimeTest target(s)).
--------------------------------------------------
EchoTime:
2011-10-12 14:24:52.756
TimeTest:
Try calling EchoTime again
--------------------------------------------------
Done Building Project "C:\Temp\MSBuildSandbox\MSBuild_EchoTime.xml" (TimeTest target(s)).
Anyone know an easy way to achieve this?
CallTarget cannot execute the same target twice but there's a workaround using the MSBuild task:
<Target Name="TimeTest" >
<Message Text = "--------------------------------------------------" />
<MSBuild
Targets="EchoTime"
Projects="$(MSBuildProjectFile)"
Properties="prop1=val1"
/>
<Message Text = " " />
<Message Text = "Try calling EchoTime again" />
<Message Text = " " />
<MSBuild
Targets="EchoTime"
Projects="$(MSBuildProjectFile)"
Properties="prop1=val2"
/>
<Message Text = "--------------------------------------------------" />
</Target>
Note setting the value for prop1 to a different value for each subsequent call.

msbuild ITaskItem output

I'm trying to use MSBuild.ExtensionPack.Web.Iis7AppPool task with GetInfo task action.
This task's output parameter is SiteInfo property, which its type is ITaskItem. This is a propery, not a collection.
How can I access the SiteInfo metadata? I can access with $, but then all I can get is the web site name. I tried to use %(SiteInfo.) with no success.
If the output parameter is an ITaskItem and you want to access the metadata then you need to assign it to an ItemGroup instead of a Property.
<Target Name="Test">
<MSBuild.ExtensionPack.Web.Iis7AppPool
TaskAction="GetInfo"
Name="DefaultAppPool">
<Output TaskParameter="AppPoolInfo" ItemName="PoolInfo" />
</MSBuild.ExtensionPack.Web.Iis7AppPool>
<Message Text="AppPool info = [#(PoolInfo)]" />
<Message Text="AppPool MaxProcesses = [%(PoolInfo.MaxProcesses)]" />
<MSBuild.ExtensionPack.Web.Iis7WebSite
TaskAction="GetInfo"
Name="Default Web Site">
<Output TaskParameter="SiteId" PropertyName="WebId" />
<Output TaskParameter="SiteInfo" ItemName="WebInfo" />
</MSBuild.ExtensionPack.Web.Iis7WebSite>
<Message Text="WebSite id = [$(WebId)]" />
<Message Text="WebSite app pool = [%(WebInfo.ApplicationPoolName)]" />
</Target>