I am creating a NuGet package, and have a .targets file that gets installed into the target .csproj file when the package is installed. This works perfectly as expected, with the .targets file having a Target element that executes before the Build and TransformOnBuild targets.
However, what I want to do now is to execute this Target after the package is installed. That is, I do not want the user to have to manually build their .csproj after they install my package. I want the package installation to run this defined Target after it installs the package into the target project.
The reason for this is that the Target autogenerates a few files in the target project (and are based on properties found within the target project), and I want these files to be generated with initial data upon installation and not wait until the user builds that project to create the data.
FWIW, the closest question I found that comes close to this is this one: Configure NuGet package to add a build event when installed, but this is for the build pipeline. I am wanting to initiate a build (my Target in the .targets file) after the NuGet project installation.
EDIT:
Thanks to #matt-ward I was able to land on this final answer. I am sharing the full answer here. It recursively goes through the project to find any .tt files, and sets their Custom Tool to blank, and then compiles the project to generate the needed artifacts. Here is the install.ps1 in full:
# Runs every time a package is installed in a project
param($installPath, $toolsPath, $package, $project)
# $installPath is the path to the folder where the package is installed.
# $toolsPath is the path to the tools directory in the folder where the package is installed.
# $package is a reference to the package object.
# $project is a reference to the project the package was installed to.
function AdjustCustomTool( $folder )
{
foreach ( $i in $folder.ProjectItems )
{
switch ( $i.Kind )
{
"{6BB5F8EF-4483-11D3-8BCF-00C04F8EC28C}" # Folder
{
AdjustCustomTool( $i );
break;
}
"{6BB5F8EE-4483-11D3-8BCF-00C04F8EC28C}" # File
{
switch ( [System.IO.Path]::GetExtension( $i.Name ) )
{
".tt"
{
$i.Properties.Item("CustomTool").Value = "";
break;
}
}
}
}
}
}
$templates = $project.ProjectItems.Item( "DragonSpark.Templates" );
AdjustCustomTool( $templates );
# Need to load MSBuild assembly if it's not loaded yet.
Add-Type -AssemblyName 'Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
# Grab the loaded MSBuild project for the project
$project = [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection.GetLoadedProjects($project.FullName) | Select-Object -First 1
$project.Build( "GenerateTemplateReferences" );
FWIW, here is my .targets file:
<Error Condition="'#(TargetReferenceFile -> Count())' == 0" Text="No DragonSpark.Templates\Generated\References.tt file found" />
<Error Condition="'#(IncludesFile -> Count())' == 0" Text="No DragonSpark.Templates\Generated\Includes.tt file found" />
<WriteLinesToFile File="#(TargetReferenceFile)" Lines="<# /* AUTOGENERATED FILE */ #>" Overwrite="true" Encoding="Unicode" />
<!-- T4Toolbox -->
<WriteLinesToFile File="#(TargetReferenceFile)" Lines="<## include file="T4Toolbox.tt" #>" Overwrite="false" Encoding="Unicode" />
<WriteLinesToFile File="#(TargetReferenceFile)" Lines="<## assembly name="" completion="T4Toolbox.dll" #>" Overwrite="false" Encoding="Unicode" />
<!-- Visual Studio IDE -->
<WriteLinesToFile File="#(TargetReferenceFile)" Lines="<## assembly name="EnvDTE, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" #>" Overwrite="false" Encoding="Unicode" />
<WriteLinesToFile File="#(TargetReferenceFile)" Lines="<## assembly name="EnvDTE80, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" #>" Overwrite="false" Encoding="Unicode" />
<WriteLinesToFile File="#(TargetReferenceFile)" Lines="<## assembly name="VSLangProj, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" #>" Overwrite="false" Encoding="Unicode" />
<WriteLinesToFile File="#(TargetReferenceFile)" Lines="<## assembly name="Microsoft.VisualStudio.Shell.Interop.8.0, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" #>" Overwrite="false" Encoding="Unicode" />
<WriteLinesToFile File="#(TargetReferenceFile)" Lines="<## assembly name="Microsoft.VisualStudio.TextTemplating.Interfaces.10.0, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" #>" Overwrite="false" Encoding="Unicode" />
<WriteLinesToFile File="#(TargetReferenceFile)" Lines="<## assembly name="Microsoft.VisualStudio.TextTemplating.12.0, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" #>" Overwrite="false" Encoding="Unicode" />
<!-- With HintPath -->
<WriteLinesToFile File="#(TargetReferenceFile)" Lines="<## assembly name="%24%28ProjectDir%29%(Reference.HintPath)" #>" Overwrite="false" Encoding="Unicode" Condition="'%(Reference.HintPath)' != '' And $([System.String]::new('%(Reference.HintPath)').Contains('T4Toolbox.dll')) == 'false'" />
<!-- Without HintPath -->
<WriteLinesToFile File="#(TargetReferenceFile)" Lines="<## assembly name="%(Reference.Identity)" #>" Overwrite="false" Encoding="Unicode" Condition="'%(Reference.HintPath)' == ''" />
<!-- ProjectReference -->
<WriteLinesToFile File="#(TargetReferenceFile)" Lines="<## assembly name="%24%28ProjectDir%29%(ProjectReference.RelativeDir)bin\$(Configuration)\%(ProjectReference.Name).dll" #>" Overwrite="false" Encoding="Unicode" Condition="'%(ProjectReference.Name)' != ''" />
<!-- Includes -->
<WriteLinesToFile File="#(IncludesFile)" Lines="<# /* AUTOGENERATED FILE */ #>" Overwrite="true" Encoding="Unicode" />
<WriteLinesToFile File="#(IncludesFile)" Lines="<## include file="..\Includes\%(Includes.Filename)%(Includes.Extension)" #>" Overwrite="false" Encoding="Unicode" />
<Message Text="Generating %(TargetReferenceFile.FullPath) &%(IncludesFile.FullPath)" Importance="high" />
</Target>
</Project>
You could use a PowerShell script (install.ps1) to run a build after the NuGet package is installed.
The PowerShell script could use the Visual Studio API to do the build. Something similar to the following would build the project:
param($installPath, $toolsPath, $package, $project)
$config = $dte.Solution.SolutionBuild.ActiveConfiguration.Name
$dte.Solution.SolutionBuild.BuildProject($config, $project.UniqueName, $false)
To build a specific MSBuild target is more tricky. You could call MSBuild.exe directly. This is something the PostSharp NuGet package does. Or possibly you could use the MSBuild API to build a particular target. Something like:
Add-Type -AssemblyName 'Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
# Grab the loaded MSBuild project for the project
$buildProject = [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection.GetLoadedProjects($project.FullName) | Select-Object -First 1
$buildProject.Build("YourTargetName")
Note that I have not tested these PowerShell scripts. They are meant to give you a pointer to the correct implementation.
Related
I'm Trying to Transform T4 Template using MSBUILD , but I'm getting the following error
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VisualStudio\v16.0\TextTemplating\Micro
soft.TextTemplating.targets(224,5): error MSB4175: The task factory "CodeTaskFactory" could not be loaded from the asse
mbly "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Build.Tasks.Cores.dll
". Could not load file or assembly 'file:///C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Curre
nt\Bin\Microsoft.Build.Tasks.Cores.dll' or one of its dependencies. The system cannot find the file specified. [E:\csha
rpprojects\Automateodessa\Automateodessa\Automateodessa.csproj]
My .tt file content
<## template debug="false" hostspecific="false" language="C#" #>
<## assembly name="System.Core" #>
<## import namespace="System.Linq" #>
<## import namespace="System.Text" #>
<## import namespace="System.Collections.Generic" #>
<## output extension=".txt" #>
<#
for(int i = 0; i < 10; i++)
WriteLine($"Hello World {i}");
#>
Hello$(classname)
My .csproj file content
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="16.0">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<builtdir>E:\BuiltApp</builtdir>
<FileUpgradeFlags>
</FileUpgradeFlags>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>2.0</OldToolsVersion>
</PropertyGroup>
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{58C4A28E-C37D-4D95-BBEE-6427A3F3129D}</ProjectGuid>
<OutputType>WinExe</OutputType>
<RootNamespace>Automateodessa</RootNamespace>
<AssemblyName>Automateodessa</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xaml">
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
</ItemGroup>
<ItemGroup>
<T4ParameterValues Include="classname">
<Value>asdsd</Value>
<Visible>False</Visible>
</T4ParameterValues>
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
</ItemGroup>
<ItemGroup>
<Content Include="TextTemplate1.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>TextTemplate1.txt</LastGenOutput>
</Content>
<Resource Include="TextTemplate1.txt">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>TextTemplate1.tt</DependentUpon>
</Resource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VisualStudio\v16.0\TextTemplating\Microsoft.TextTemplating.targets" />
<Target Name="AfterBuild">
<Message Text="The output file is hhahah" />
</Target>
</Project>
And my command for MSbuild is
MSBuild.exe E:\csharpprojects\Automateodessa\Automateodessa\Automateodessa.csproj /t:Transform /p:TransformFile="TextTemplate1.tt"
Can Someone tell me why am I getting this error?
P.S
My MSBUILD Variable got by /v:diag
MSBuildBinPath = C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin
MSBuildExtensionsPath = C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild
MSBuildExtensionsPath32 = C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild
MSBuildExtensionsPath64 = C:\Program Files\MSBuild
MSBuildFrameworkToolsPath = C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\
MSBuildFrameworkToolsPath32 = C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\
MSBuildFrameworkToolsPath64 = C:\WINDOWS\Microsoft.NET\Framework64\v4.0.30319\
MSBuildFrameworkToolsRoot = C:\WINDOWS\Microsoft.NET\Framework\
MSBuildLoadMicrosoftTargetsReadOnly = true
MSBuildNodeCount = 1
MSBuildProgramFiles32 = C:\Program Files (x86)
MSBuildProjectDefaultTargets = Build
MSBuildProjectDirectory = E:\csharpprojects\StudySnippets
MSBuildProjectDirectoryNoRoot = csharpprojects\StudySnippets
MSBuildProjectExtension = .csproj
MSBuildProjectExtensionsPath = E:\csharpprojects\StudySnippets\obj\
MSBuildProjectFile = Testing.csproj
MSBuildProjectFullPath = E:\csharpprojects\StudySnippets\Testing.csproj
MSBuildProjectName = Testing
MSBuildRuntimeType = Full
MSBuildRuntimeVersion = 4.0.30319
MSBuildSDKsPath = C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Sdks
MSBuildStartupDirectory = C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin
MSBuildToolsPath = C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin
MSBuildToolsPath32 = C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin
MSBuildToolsPath64 = C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\amd64
MSBuildToolsRoot = C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild
MSBuildToolsVersion = Current
MSBuildUserExtensionsPath = C:\Users\admin\AppData\Local\Microsoft\MSBuild
MSBuildVersion = 16.2.37902
MsTestToolsTargets = C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VisualStudio\v16.0
\TeamTest\Microsoft.TeamTest.targets
NetFrameworkPropsPath = C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.NET
Framework.CurrentVersion.props
NetFrameworkTargetsPath = C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.N
ETFramework.CurrentVersion.targets```
Not able to Transform T4 Template using MSBUILD toolset=“16.0”
I think you just use msbuild.exe from an old framework or just use an old version of MSBuild like VS2017. Since you have set MSBUILD toolset="16.0", it means that you should use MSBuild v16.0 to build your project.
1) Please use C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe rather than any other old version of MSBuild.exe
2) just use Developer Command Prompt for VS2019
Update 1
Besides, if you still face the issue after the solution, I think there are something wrong with your project or VS Environment.
Solution
1) I think you have create a new VS2019 project and then migrate the old project into it to test whether it is your old project's problem.
2) Please do a repair in VS Installer or update your VS to the latest version to test whether it is the issue of your VS Environment.
Hope this could help you.
I've modified a csproj file in VS2017 to create a Nuget package when my .NET 4.5 project is release-built. The next automation step is to add this package to my private feed on a network share. Here are the commands I'm using:
<Exec Command="nuget.exe pack -Properties "Configuration=Release" -Symbols $(ProjectName).csproj" />
<Exec Command="nuget.exe add $(ProjectName).$(ProductVersion).nupkg -source \\MYSERVER\Nuget packages" />
Line 1 works and produces a nupkg file of the form productname.nn.nn.nn.nn.
However line 2 is not returning a value for the ProductVersion token (which was a guess on my part).
I've struggled to find a reference for MSBUILD tokens (that in itself would be useful to know), but what I really need to know is the correct MSBUILD token/variable/property for the the version - and that is the same value as the generated Nuget package.
I explored $(PackageVersion) suggested by Martin Ullrich, but it's not going to work with older projects even with Nuget 4.10 installed. Also I couldn't get Troopers sample to work how I wanted and I ended up with a variation on one of the posts here. I adapted it to give me the assembly version as opposed to the file version.
<UsingTask TaskName="GetVersionParts" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<AssemblyPath ParameterType="System.String" Required="true" />
<MajorVersion ParameterType="System.Int32" Output="true" />
<MinorVersion ParameterType="System.Int32" Output="true" />
<BuildVersion ParameterType="System.Int32" Output="true" />
</ParameterGroup>
<Task>
<Using Namespace="System.Reflection" />
<Code Type="Fragment" Language="cs">
<![CDATA[
Version v = AssemblyName.GetAssemblyName(this.AssemblyPath).Version;
this.MajorVersion = v.Major;
this.MinorVersion = v.Minor;
this.BuildVersion = v.Build;
]]>
</Code>
</Task>
</UsingTask>
<Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release'">
<Message Text="**** After-build process starting ****" />
<Exec Command="nuget.exe pack -Properties "Configuration=Release" -Symbols $(ProjectName).csproj" />
<GetVersionParts AssemblyPath="$(OutputPath)$(AssemblyName).dll">
<Output TaskParameter="MajorVersion" PropertyName="MajorVersionNumber" />
<Output TaskParameter="MinorVersion" PropertyName="MinorVersionNumber" />
<Output TaskParameter="BuildVersion" PropertyName="BuildVersionNumber" />
</GetVersionParts>
<Exec Command="nuget.exe add $(MSBuildProjectName).$(MajorVersionNumber).$(MinorVersionNumber).$(BuildVersionNumber).nupkg -source "\\My feed server\Shared location"" />
<Exec Command="move *.symbols.nupkg "\\My feed server\Shared location\Symbols"" />
<Message Text="**** After-build process completed ****" />
</Target>
Although this works, I can't help feeling that it should be easier.
Further reading
https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-inline-tasks
https://learn.microsoft.com/en-us/visualstudio/msbuild/common-msbuild-project-properties
https://learn.microsoft.com/en-us/nuget/tools/nuget-exe-cli-reference#add
You could get the assembly version with this macro. I call it before the postbuild event for example
<!--Macro to get the version -->
<Target Name="PostBuildMacros">
<GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
<Output TaskParameter="Assemblies" ItemName="CurrentAssembly" />
</GetAssemblyIdentity>
<ItemGroup>
<VersionNumber Include="%(CurrentAssembly.Version)" />
</ItemGroup>
</Target>
<!-- override PostBuildEvent to call PostBuildMacros -->
<PropertyGroup>
<PostBuildEventDependsOn>
$(PostBuildEventDependsOn);
PostBuildMacros;
</PostBuildEventDependsOn>
<PostBuildEvent>
...
</PostBuildEvent>
</PropertyGroup>
We have an (old) build definition is using the UpgradeTemplate.xaml on TFS 2015 (and an underlying TFSBuild.proj which has a heap of custom actions). As such, the task of properly modernising the build is going to take time.
I'd like to hack the UpgradeTemplate to add in C#6/VB14 support without requiring a full re-write of the build definition, in order to keep the devs happy.
I attempted to edit the UpgradeTemplate.xaml to add a ToolPath property on the TfsBuild. However, now that I have done this, I get the following error on nearly all my projects:
ArgumentOutOfRangeException: Index and length must refer to a location within the string. Parameter name: length
On investigation, the lines of code in these projects all look like this:
<MSBuild.ExtensionPack.VisualStudio.TfsVersion TaskAction="GetVersion"
BuildName="$(BuildDefinition)" TfsBuildNumber="$(BuildNumber)"
VersionFormat="DateTime" DateFormat="MMdd" Major="$(MajorVersion)"
Minor="$(MinorVersion)">
The values of these variables as set printed out by Message tasks on the vbproj:
BuildDefinition: MyBuild-Testing
BuildNumber: 57902
MajorVersion: 43
MinorVersion: 2
The Build server has version 3.5.10 on the MSBuild ExtensionPack installed.
How do I resolve this issue? I'm testing this with a new build definition to allow devs to continue working while I get this set up, so I don't want to replace the ExtensionPack with the latest release (if possible) if it is likely to break the existing build.
Upgrade Template
<Activity mc:Ignorable="sad" x:Class="TfsBuild.Process" xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mtbc="clr-namespace:Microsoft.TeamFoundation.Build.Client;assembly=Microsoft.TeamFoundation.Build.Client" xmlns:mtbw="clr-namespace:Microsoft.TeamFoundation.Build.Workflow;assembly=Microsoft.TeamFoundation.Build.Workflow" xmlns:mtbwa="clr-namespace:Microsoft.TeamFoundation.Build.Workflow.Activities;assembly=Microsoft.TeamFoundation.Build.Workflow" xmlns:mtbwt="clr-namespace:Microsoft.TeamFoundation.Build.Workflow.Tracking;assembly=Microsoft.TeamFoundation.Build.Workflow" xmlns:mtvc="clr-namespace:Microsoft.TeamFoundation.VersionControl.Client;assembly=Microsoft.TeamFoundation.VersionControl.Client" xmlns:mva="clr-namespace:Microsoft.VisualBasic.Activities;assembly=System.Activities" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:sad="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation" xmlns:sad1="clr-namespace:System.Activities.Debugger;assembly=System.Activities" xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib" xmlns:this="clr-namespace:TfsBuild;" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<x:Members>
<x:Property Name="ConfigurationFolderPath" Type="InArgument(x:String)" />
<x:Property Name="AgentSettings" Type="InArgument(mtbwa:AgentSettings)" />
<x:Property Name="MSBuildArguments" Type="InArgument(x:String)" />
<x:Property Name="MSBuildPlatform" Type="InArgument(mtbwa:ToolPlatform)" />
<x:Property Name="DoNotDownloadBuildType" Type="InArgument(x:Boolean)" />
<x:Property Name="LogFilePerProject" Type="InArgument(x:Boolean)" />
<x:Property Name="SourcesSubdirectory" Type="InArgument(x:String)" />
<x:Property Name="BinariesSubdirectory" Type="InArgument(x:String)" />
<x:Property Name="TestResultsSubdirectory" Type="InArgument(x:String)" />
<x:Property Name="RecursionType" Type="InArgument(mtvc:RecursionType)" />
<x:Property Name="Verbosity" Type="InArgument(mtbw:BuildVerbosity)" />
<x:Property Name="Metadata" Type="mtbw:ProcessParameterMetadataCollection" />
<x:Property Name="SupportedReasons" Type="mtbc:BuildReason" />
</x:Members>
<this:Process.ConfigurationFolderPath>
<InArgument x:TypeArguments="x:String" />
</this:Process.ConfigurationFolderPath>
<this:Process.AgentSettings>[New Microsoft.TeamFoundation.Build.Workflow.Activities.AgentSettings() With {.MaxWaitTime = New System.TimeSpan(4, 0, 0), .MaxExecutionTime = New System.TimeSpan(0, 0, 0), .TagComparison = Microsoft.TeamFoundation.Build.Workflow.Activities.TagComparison.MatchExactly }]</this:Process.AgentSettings>
<this:Process.MSBuildArguments>
<InArgument x:TypeArguments="x:String" />
</this:Process.MSBuildArguments>
<this:Process.MSBuildPlatform>[Microsoft.TeamFoundation.Build.Workflow.Activities.ToolPlatform.Auto]</this:Process.MSBuildPlatform>
<this:Process.DoNotDownloadBuildType>[False]</this:Process.DoNotDownloadBuildType>
<this:Process.LogFilePerProject>[False]</this:Process.LogFilePerProject>
<this:Process.SourcesSubdirectory>
<InArgument x:TypeArguments="x:String" />
</this:Process.SourcesSubdirectory>
<this:Process.BinariesSubdirectory>
<InArgument x:TypeArguments="x:String" />
</this:Process.BinariesSubdirectory>
<this:Process.TestResultsSubdirectory>
<InArgument x:TypeArguments="x:String" />
</this:Process.TestResultsSubdirectory>
<this:Process.RecursionType>[Microsoft.TeamFoundation.VersionControl.Client.RecursionType.OneLevel]</this:Process.RecursionType>
<this:Process.Verbosity>[Microsoft.TeamFoundation.Build.Workflow.BuildVerbosity.Normal]</this:Process.Verbosity>
<this:Process.Metadata>
<mtbw:ProcessParameterMetadataCollection />
</this:Process.Metadata>
<this:Process.SupportedReasons>All</this:Process.SupportedReasons>
<mva:VisualBasic.Settings>Assembly references and imported namespaces serialized as XML namespaces</mva:VisualBasic.Settings>
<Sequence mtbwt:BuildTrackingParticipant.Importance="None">
<Sequence.Variables>
<Variable x:TypeArguments="mtbc:IBuildDetail" Name="BuildDetail" />
</Sequence.Variables>
<mtbwa:GetBuildDetail DisplayName="Get the Build" Result="[BuildDetail]" />
<mtbwa:InvokeForReason DisplayName="Update Build Number for Triggered Builds" Reason="Triggered">
<mtbwa:UpdateBuildNumber BuildNumberFormat="["$(BuildDefinitionName)_$(Date:yyyyMMdd)$(Rev:.r)"]" DisplayName="Update Build Number" />
</mtbwa:InvokeForReason>
<mtbwa:AgentScope DisplayName="Run On Agent" MaxExecutionTime="[AgentSettings.MaxExecutionTime]" MaxWaitTime="[AgentSettings.MaxWaitTime]" ReservationSpec="[AgentSettings.GetAgentReservationSpec()]">
<mtbwa:AgentScope.Variables>
<Variable x:TypeArguments="x:String" Name="buildDirectory" />
</mtbwa:AgentScope.Variables>
<mtbwa:GetBuildDirectory DisplayName="Get the Build Directory" Result="[buildDirectory]" />
<If Condition="[Not String.IsNullOrEmpty(ConfigurationFolderPath)]" DisplayName="If Not String.IsNullOrEmpty(ConfigurationFolderPath)">
<If.Then>
<mtbwa:TfsBuild BinariesSubdirectory="[BinariesSubdirectory]" BuildDirectory="[buildDirectory]" CommandLineArguments="[MSBuildArguments]" ConfigurationFolderPath="[ConfigurationFolderPath]" DisplayName="Run TfsBuild for Configuration Folder" DoNotDownloadBuildType="[DoNotDownloadBuildType]" LogFilePerProject="[LogFilePerProject]" RecursionType="[RecursionType]" SourcesSubdirectory="[SourcesSubdirectory]" TestResultsSubdirectory="[TestResultsSubdirectory]" ToolPath="C:\Program Files (x86)\MSBuild\12.0\Bin\" ToolPlatform="[MSBuildPlatform]" Verbosity="[Verbosity]" />
</If.Then>
</If>
<If Condition="[BuildDetail.CompilationStatus = Microsoft.TeamFoundation.Build.Client.BuildPhaseStatus.Unknown]" DisplayName="If CompilationStatus = Unknown">
<If.Then>
<mtbwa:SetBuildProperties CompilationStatus="[Microsoft.TeamFoundation.Build.Client.BuildPhaseStatus.Succeeded]" DisplayName="Set CompilationStatus to Succeeded" PropertiesToSet="CompilationStatus" />
</If.Then>
</If>
<If Condition="[BuildDetail.TestStatus = Microsoft.TeamFoundation.Build.Client.BuildPhaseStatus.Unknown]" DisplayName="If TestStatus = Unknown">
<If.Then>
<mtbwa:SetBuildProperties DisplayName="Set TestStatus to Succeeded" PropertiesToSet="TestStatus" TestStatus="[Microsoft.TeamFoundation.Build.Client.BuildPhaseStatus.Succeeded]" />
</If.Then>
</If>
</mtbwa:AgentScope>
<mtbwa:InvokeForReason Reason="CheckInShelveset">
<mtbwa:CheckInGatedChanges DisplayName="Check In Gated Changes" />
</mtbwa:InvokeForReason>
</Sequence>
</Activity>
In Particular, I added ToolPath="C:\Program Files (x86)\MSBuild\12.0\Bin\" to line 58.
TFSBuild.proj
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="DesktopBuild" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\TeamBuild\Microsoft.TeamFoundation.Build.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\SDC\Microsoft.Sdc.Common.tasks" />
<Import Project="$(MSBuildExtensionsPath)\ExtensionPack\MSBuild.ExtensionPack.tasks"/>
<ProjectExtensions>
<!-- Team Foundation Build Version - DO NOT CHANGE -->
<ProjectFileVersion>2</ProjectFileVersion>
</ProjectExtensions>
<PropertyGroup>
<RunCodeAnalysis>Never</RunCodeAnalysis>
<UpdateAssociatedWorkItems>false</UpdateAssociatedWorkItems>
<AdditionalVCOverrides></AdditionalVCOverrides>
<CustomPropertiesForClean></CustomPropertiesForClean>
<CustomPropertiesForBuild></CustomPropertiesForBuild>
<SkipGetChangesetsAndUpdateWorkItems>False</SkipGetChangesetsAndUpdateWorkItems>
<SkipWorkItemCreation>true</SkipWorkItemCreation>
<BuildConfigurationsInParallel>true</BuildConfigurationsInParallel>
<SkipDropBuild>false</SkipDropBuild>
</PropertyGroup>
<ItemGroup>
<SolutionToBuild Include="$(BuildProjectFolderPath)/SolutionsToBuild/Common.sln">
<Targets></Targets>
<Properties></Properties>
</SolutionToBuild>
</ItemGroup>
<ItemGroup>
<ConfigurationToBuild Include="Release|Any CPU">
<FlavorToBuild>Release</FlavorToBuild>
<PlatformToBuild>Any CPU</PlatformToBuild>
</ConfigurationToBuild>
</ItemGroup>
<PropertyGroup>
<SkipClean>false</SkipClean>
<SkipInitializeWorkspace>true</SkipInitializeWorkspace>
<ForceGet>true</ForceGet>
<IncrementalBuild>false</IncrementalBuild>
</PropertyGroup>
</Project>
Update
I think it might be something to do with running a private build (Latest + Shelveset). When I run a normal build, the BuildNumber variable is MyBuild-Testing_20170328.1. This appears to be working fine.
This issue is due to a difference between Private and Public builds. With Public Builds, the build is immediately numbered as BuildName_DateFormat.BuildNumber. Private builds however, are just numeric (e.g. 57902 above).
The Code in the extension does the following:
string buildstring = this.TfsBuildNumber.Replace(string.Concat(this.BuildName, "_"),
string.Empty);
char[] chrArray = new char[] { '.' };
string[] buildParts = buildstring.Split(chrArray, StringSplitOptions.RemoveEmptyEntries);
DateTime t = new DateTime(Convert.ToInt32(buildParts[0].Substring(0, 4),
CultureInfo.CurrentCulture),
Convert.ToInt32(buildParts[0].Substring(4, 2),
CultureInfo.CurrentCulture),
Convert.ToInt32(buildParts[0].Substring(6, 2),
CultureInfo.InvariantCulture));
Are you can see, its substringing on the build name, assuming it's been stripped down to the date component (20170328). Private builds aren't this long, and so fail.
This build works fine when running as a public build - basically it means that private builds on this definition are not available until an upgrade takes place.
How can I get the list of project files from a solution when using MSBUILD?
For example getting all the .csproj from a .sln.
I was previously using MSBuild Community Tasks's GetSolutionProjects for this but unfortunately it has a dependency on .NET 3.5.
To accomplish this using a CodeTask (available since .NET 4) do the following:
<UsingTask TaskName="GetProjectsFromSolutionCodeTask" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
<ParameterGroup>
<Solution ParameterType="System.String" Required="true"/>
<Output ParameterType="Microsoft.Build.Framework.ITaskItem[]" Output="true"/>
</ParameterGroup>
<Task>
<Reference Include="System.Xml"/>
<Reference Include="Microsoft.Build"/>
<Using Namespace="Microsoft.Build.Construction"/>
<Code Type="Fragment" Language="cs">
<![CDATA[
var _solutionFile = SolutionFile.Parse(Solution);
Output = _solutionFile.ProjectsInOrder
.Where(proj => proj.ProjectType == SolutionProjectType.KnownToBeMSBuildFormat)
.Select(proj => new TaskItem(proj.AbsolutePath))
.ToArray();
]]>
</Code>
</Task>
</UsingTask>
and invoke it like so:
<!-- Gets the projects composing the specified solution -->
<Target Name="GetProjectsFromSolution">
<GetProjectsFromSolutionCodeTask Solution="%(Solution.Fullpath)">
<Output ItemName="ProjectFiles" TaskParameter="Output"/>
</GetProjectsFromSolutionCodeTask >
</Target>
This will populate a ProjectFiles item collection with the absolute path of all the projects within the solution.
Please note: path to CodeTaskFactory varies by MSBuild version. Example here is for MSBuild 14.0.
I feel like I've fixed this before, but I can't remember how.
I have a tasks file that looks like this (CustomTasks.tasks):
<UsingTask AssemblyFile="CustomTasks.dll" TaskName="MyCustomTask"/>
it references an assembly (namely Ionic.Zip.dll). Ionic.Zip.dll is not in the GAC (and I don't want it to be). It sits right next to my CustomTasks.dll.
I have a directory called MSBuild one level up from my sln file which has CustomTasks.tasks, CustomTasks.dll and Ionic.Zip.dll in it.
I have a csproj that references the tasks file and calls the custom task:
<Import Project="$(ProjectDir)\..\MSBuild\CustomTasks.tasks" />
<MyCustomTask ..... />
at build time, this yields:
The "MyCustomTask" task could not be loaded from the assembly ....MyCustomTasks.dll. Could not load file or assembly 'Ionic.Zip,......' or one of its dependencies.
Got tired and frustrated and took a direct approach...I don't think this is the same way I solved the problem previously...but maybe this will help someone else. Other, more elegant solutions are more than welcome.
<Target Name="BeforeBeforeBuild" BeforeTargets="BeforeBuild">
<HandleAssemblyResolve SearchPath="$(ProjectDir)\..\MSBuild\" />
</Target>
<UsingTask TaskName="HandleAssemblyResolve" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<SearchPath ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Reflection" />
<Code Type="Fragment" Language="cs">
<![CDATA[
AppDomain.CurrentDomain.AssemblyResolve += (sender, e) =>
{
var assemblySearchPath = Path.Combine(SearchPath, e.Name.Split(',')[0]);
if (File.Exists(assemblySearchPath)) return Assembly.LoadFrom(assemblySearchPath);
return null;
};
]]>
</Code>
</Task>
</UsingTask>
This is actually easy to fix. Put your custom build tasks and dependencies in a different folder. Then dependencies are loaded correctly.
For example like so:
<UsingTask AssemblyFile="..\BuildTools\CustomTasks.dll" TaskName="MyCustomTask"/>