MSBuild clean directory string - msbuild

I have a property in MSBuild to represent the directory above the MSBuildProjectDirectory:
<PropertyGroup>
<BuildDir>$(MSBuildProjectDirectory)\..</PRSBuildDir>
</PropertyGroup>
I need to then use this property, but I need the directory string cleaned so that it doesn't include the ... In other words I need the .. evaluated, so that if the current project file is in C:\Test\Tom\MyDir, then I need a property containing the string C:\Test\Tom.
The reason I'm asking is because I'm trying to run a command like this:
msiexec /passive /i "D:\Build\2.3.84.40394\Deployment\..\Vendor\LogParser.msi"
But it's complaining about the path to the msi: This installation package could not be opened. Verify that the package exists and that you can access it, or contact the application vendor to verify that this is a valid Windows Installer package.

There's a ConvertToAbsolutePath task, that any use?

The best method I've got right now is below, but I was wondering if there might be a better way..
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<BuildDir>$(MSBuildProjectDirectory)\..</BuildDir>
</PropertyGroup>
<Target Name="Test">
<ItemGroup>
<CleanBuildDir Include="$(BuildDir)" />
</ItemGroup>
<PropertyGroup>
<BuildDir>%(CleanBuildDir.FullPath)</BuildDir>
</PropertyGroup>
<Message Text="$(BuildDir)" />
</Target>
</Project>

If you want to have wildcard evaluated, you should use Item instead of Property.
<ItemGroup>
<BuildDir Include="$(MSBuildProjectDirectory)\.."/>
</ItemGroup>
<Target Name="ExecMSIExec">
<Exec Command="msiexec /passive /i %(BuildDir.FullPath)\Vendor\LogParser.msi"/>
</Target>

(deleted my answer as I didn't see that Tom had answered in exactly the same way!)
By the way, why don't you set the "WorkingDirectory" attribute of the Exec task where you actually call msiexec to be the location of your MSI - that way you won't run into an path length issues

Related

Wix bootstrapper - Set version number in Bundle

I've got a Wix installer that uses a bootstrapper to launch my msi file. I've done this by calling a batch file as a post build event in my wix project. This then calls candle and light manually and passes various variables into the Bundle.wxs file. This all works and generates the exe which calls my msi file..
However, I now want to pass the msi BuildVersion into the bundle file. In the wxs file that creates the msi I am using the BuildVersion that I have setup in the BeforeBuild section, using the BuildVersion=%(AssemblyVersion.Version).
I cannot access this variable no matter what I try, in order to pass it to my build_bootstrapper.bat file. I can however pass in hardcoded values. I am currently setting up my own AssemblyVersionNumber enviornment variable as you can see below in the AfterBuild section:
<AssemblyVersionNumber Condition="'$(AssemblyVersionNumber)' == ''">$(BuildVersion)</AssemblyVersionNumber>
but it is empty by the time it gets to my script file (even though it's populated if hardcoded). I've tried everything.
Does anybody have any ideas of how I can get the %(AssemblyVersion.Version); to my command file from the post build step?
Thanks in advance
<Target Name="BeforeBuild">
<GetAssemblyIdentity AssemblyFiles="..\..\App\AppThing\bin\Release\AppThing.exe">
<Output TaskParameter="Assemblies" ItemName="AssemblyVersion" />
</GetAssemblyIdentity>
<PropertyGroup>
<DefineConstants>BuildVersion=%(AssemblyVersion.Version);</DefineConstants>
</PropertyGroup>
</Target>
<Target Name="AfterBuild">
<PropertyGroup>
<DefineConstants>BuildVersion=%(AssemblyVersion.Version);</DefineConstants>
<AssemblyVersionNumber Condition="'$(AssemblyVersionNumber)' == ''">$(BuildVersion)</AssemblyVersionNumber>
</PropertyGroup>
</Target>
<PropertyGroup>
<PreBuildEvent>$(ProjectDir)scripts\copy_services.bat $(SolutionDir) $(ProjectDir)</PreBuildEvent>
</PropertyGroup>
<Target Name="AfterClean">
<Message Text="Cleaning wix files, TargetDir is: $(TargetDir)" Importance="High" ContinueOnError="true" />
<CreateItem Include="$(TargetDir)\**\*.*">
<Output TaskParameter="Include" ItemName="BinFilesDir" />
</CreateItem>
<Delete Files="#(BinFilesDir)" />
</Target>
<PropertyGroup>
<PostBuildEvent>$(ProjectDir)scripts\build_bootstrapper.bat $(ProjectDir) $(ConfigurationName) $(AssemblyVersionNumber)</PostBuildEvent>
</PropertyGroup>
$(BuildVersion) isn't set to anything.
You're setting define constants to "BuildVersion=%(AssemblyVersion.Version)" but never actually defining a MSBuild property called "BuildVersion" so the value of $(BuildVersion) is "".
Use %(AssemblyVersion.Version).
<AssemblyVersionNumber Condition="'$(AssemblyVersionNumber)' == ''">%(AssemblyVersion.Version)</AssemblyVersionNumber>

How to find if file exist with msbuild command(or before)

I need to find if certain file exist prior to the run of a power-shell script
The file (if exist) will be in a specific folder.
Can i check for it's existence through the proj file or something like that?
Note the second MyCheck looks at the (conditional) value of (the first) MyCheck
<PropertyGroup>
<MyCheck Condition="Exists($(MyFileOrFolderName))">true</MyCheck>
<MyCheck Condition="'$(MyCheck)'==''">false</MyCheck>
</PropertyGroup>
<Message Text="My-File-Or-Folder-Name already exists? : $(MyCheck)" />
OR
<PropertyGroup>
<MyCheck>false</MyCheck>
<MyCheck Condition="Exists($(MyFileOrFolderName))">true</MyCheck>
</PropertyGroup>
<Message Text="MyFileOrFolderNameexists? : $(MyCheck)" />
In order to execute PS script, depending on the existance of a file, you can create a Target element in your *.*proj file with condition depending on the file existance:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="14.0">
<PropertyGroup>
<ScriptLocation>.\Do-Something.ps1</ScriptLocation>
<PowerShellExe Condition=" '$(PowerShellExe)'=='' ">%WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe
</PowerShellExe>
</PropertyGroup>
<Target Name="RunPSScript" Condition="Exists($(ScriptLocation))">
<Exec Command="$(PowerShellExe) -NonInteractive -executionpolicy Unrestricted -command "$(ScriptLocation)""/>
</Target>
</Project>
See more details here about executing PS scripts from msbuild projects. You can use AfterTargets, BeforeTargers, or any other methods to control order of the execution this target.

Msbuild - build with output in diffrent folders

I have right now this in a script and want to have it in a msbuild instead.
msbuild /t:Build;PipelinePreDeployCopyAllFilesToOneFolder XXXXX\XXXX.XXX.xxx\XXXXX.XXXX.XXXXX1.csproj /p:Configuration="Release";_PackageTempDir=....\Deploy\XXXX1
msbuild /t:Build;PipelinePreDeployCopyAllFilesToOneFolder XXXXX\XXXX.XXX.xxx\XXXXX.XXXX.XXXXX2.csproj /p:Configuration="Release";_PackageTempDir=....\Deploy\XXXX2
How would this be written in a msbuild script? I only have managed to do it with one build but to create diffrent folders in the Deploy folder I havent been able to do.
Could someone teach me?
You can specify OutDir (new projects) or OutputPath (some old project types) as Properties when you call msbuild task to build your project. Or whatever property you want, like your "_PackageTempDir"
Something like this:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="BatchMyProjects" ToolsVersion="4.0">
<ItemGroup>
<BuildMyProjects Include="XXXX-Project-1" />
<BuildMyProjects Include="XXXX-Project-2" />
</ItemGroup>
<Target Name="BatchMyProjects" >
<ItemGroup>
<ProjectsToBuild Condition="Exists('SomeSourcePath\%(BuildMyProjects.Identity)/%(BuildMyProjects.Identity).csproj')">
<ProjectName>SomeSourcePath/%(BuildMyProjects.Identity)/%(BuildMyProjects.Identity).csproj</ProjectName>
<PublishSubFolder>%(BuildMyProjects.Identity)</PublishSubFolder>
</ProjectsToBuild>
</ItemGroup>
<MSBuild Projects="%(ProjectsToBuild.ProjectName)" Targets="Build;PipelinePreDeployCopyAllFilesToOneFolder"
Properties="Configuration=Release;
OutDir=SomePathToDeploy/Deploy/%(ProjectsToBuild.PublishSubFolder)/;
OutputPath= SomePathToDeploy/Deploy/%(ProjectsToBuild.PublishSubFolder)/;
_PackageTempDir=SomePathToDeploy/Deploy/%(ProjectsToBuild.PublishSubFolder)/
" />
</Target>
</Project>
Also I created gist for this same example
This do work but that seems abit strange. I rateher have it to work as you mentioned
<Target Name="testar" >
<MSBuild Projects="..\xxxxxx\xxxxxx1.csproj" Targets="Build;PipelinePreDeployCopyAllFilesToOneFolder" Properties="Configuration=Release;_PackageTempDir=../../Deploy/xxx1/"/>
<MSBuild Projects="..\xxxx\xxxxxx2.csproj" Targets="Build;PipelinePreDeployCopyAllFilesToOneFolder" Properties="Configuration=Release;_PackageTempDir=../../Deploy/xxx2/"/>
</Target>

Property scope using msbuild extension pack detokenise

Im trying to use the msbuild extensions pack to fix up the configuration of our app on deploy,
i want to be able to pass a property (ENV) which will load my environment specific config file to use with the detokeniser, and fix up my application configs.
Like this:
<UsingTask TaskName="MSBuild.ExtensionPack.FileSystem.Detokenise"
AssemblyFile=".\Tools\MSBuild Extension Pack 4.0.3.0\MSBuild.ExtensionPack.dll"/>
<Import Project=".\Environments\$(Env).properties"/>
<Target Name="Build" >
<ItemGroup>
<SourceTemplates Include=".\Templates\**\*.*"/>
</ItemGroup>
<RemoveDir Directories=".\Temp"/>
<MakeDir Directories=".\Temp"/>
<Message Text="#(SourceTemplates)"/>
<Copy SourceFiles="#(SourceTemplates)"
DestinationFolder=".\Temp\%(RecursiveDir)" />
<ItemGroup>
<TargetTemplates Include=".\Temp\**\*.*"/>
</ItemGroup>
<MSBuild.ExtensionPack.FileSystem.Detokenise
TaskAction="Detokenise"
TargetFiles="#(TargetTemplates)"/>
</Target>
So i call this using
msbuild Detokenise.msbuild /p:Env=Prod
Msbuild knows about my file and i have access to its properties, but when the detokeniser runs i get the error:
Detokenise Task Execution Completed [15:07:50]
C:\Source\1.2\Build\Detokenise.msbuild(27,3):
error : InvalidProjectFileException: The imported project "C:\Source\1.2\Build\Environments\.properties" was not found.
Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.
C:\Source\1.2\Build\Detokenise.msbuild\r
C:\Source\1.2\Build\Detokenise.msbuild(27,3): error :
All works fine if i hard code it-
Any ideas how to solve this. I thought of doing some text replacement on the msbuild before i execute...
You could try to assign this parameter to a local property:
<PropertyGroup Condition="'$(Env)'=='Prod'">
<TargetEnv>Prod</TargetEnv>
</PropertyGroup>
<!-- add other environments as needed -->
<PropertyGroup Condition="'$(Env)'=='Test'">
<TargetEnv>Test</TargetEnv>
</PropertyGroup>
<Import Project=".\Environments\$(TargetEnv).properties"/>
You could also try to enclose your parameter value in quotes:
msbuild Detokenise.msbuild /p:"Env=Prod"
As is your problem can't be reproduced, so it may be a side effect of other parameters not shown in your sample code.
I've seen a number of other questions where a similar problems was happening:
Visual Studio Ignoring MSBuild file (csproj) Customizations

msbuild exec task call msbuild

I need to call exec and build a wix setup project.
Currently I have the following in my TFSbuild.proj
<PropertyGroup>
<WebRoot>$(DropLocation)\Latest\x86\Release\_PublishedWebsites\Web</WebRoot>
<DBRoot>$(DropLocation)\Latest\x86\Release\Database</DBRoot>
</PropertyGroup>
<PropertyGroup>
<Msbuildexe>"msbuild"</Msbuildexe>
<Configuration>"/p:Configuration:"Release""</Configuration>
<DefineConstants>" /p:DefineConstants:"WebRoot=$(WebRoot);DBRoot=$(DBRoot)""</DefineConstants>
<WixSolution>"$(MSBuildProjectDirectory)\Setup\Setup.sln"</WixSolution>
</PropertyGroup>
<Message Text="Bulding setup solution" />
<Message Text="$(Msbuildexe) $(Configuration) $(DefineConstants) $(WixSolution)" />
<Exec Command="$(Msbuildexe) $(Configuration) $(DefineConstants) $(WixSolution)" />
I've tried to simply as much as possible so I don't get confused where the " are meant to be. When I run this the debug message (2nd last command) outputs
"msbuild"
"/p:Configuration:"Release"" "
/p:DefineConstants:"WebRoot=\server\drops\app\Installer Build\Latest\x86\Release_PublishedWebsites\Web;DBRoot=\server\drops\app\Installer Build\Latest\x86\Release\Database""
"f:\builds\app\Installer Build\BuildType\Setup\Setup.sln"
And I get the following error in the log
'"msbuild"' is not recognized as an
internal or external command,
operable program or batch file.
f:\builds\app\Installer
Build\BuildType\TFSBuild.proj(538,5):
error MSB3073: The command ""msbuild"
"/p:Configuration:"Release"" "
/p:DefineConstants:"WebRoot=\server\drops\app\Installer Build\Latest\x86\Release_PublishedWebsites\Web;DBRoot=\server\drops\app\Installer Build\Latest\x86\Release\Database""
"f:\builds\app\Installer
Build\BuildType\Setup\Setup.sln""
exited with code 9009.
I'm not sure if this is being caused by not being able to call the msbuild command from the command line or a " issue. If it is because I can't call msbuild from the command line like this how would I go about referencing it, is there a property that points to it?
To start with, you don't need most of the quotes, especially if the paths you are using don't contain spaces, but I'd trim it down to this, allowing for spaces in the paths for $(WebRoot), $(DbRoot) and $(MSBuildProjectDirectory):
<PropertyGroup>
<WebRoot>$(DropLocation)\Latest\x86\Release\_PublishedWebsites\Web</WebRoot>
<DBRoot>$(DropLocation)\Latest\x86\Release\Database</DBRoot>
</PropertyGroup>
<PropertyGroup>
<MsbuildExe>{still-needs-a-path-to}\msbuild</MsbuildExe>
<Configuration>/p:Configuration:Release</Configuration>
<DefineConstants>/p:DefineConstants:"WebRoot=$(WebRoot);DBRoot=$(DBRoot)"</DefineConstants>
<WixSolution>"$(MSBuildProjectDirectory)\Setup\Setup.sln"</WixSolution>
</PropertyGroup>
<Message
Text="Bulding setup solution"
/>
<Message
Text="$(MsbuildExe) $(Configuration) $(DefineConstants) $(WixSolution)"
/>
<Exec
Command="$(MsbuildExe) $(Configuration) $(DefineConstants) $(WixSolution)"
/>
However, you still won't be able to execute MSBuild with this, since the path to MSBuild isn't specified. It is typically found in the $(WINDIR)\Framework\Microsoft.Net\v4.0.30319 folder. There are a few ways to get this, either encode it directly, rely on an environment variable (that has to be set up somehow), use the predefined $(MSBuildBinPath), or extract it from the registry using the MSBuild registry syntax, which would look like this:
$(Registry:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\4.0\MSBuildToolsPath)
However, it isn't clear why you are running MSBuild using Exec rather than just using the MSBuild task. Change the line with Exec to this:
<MSBuild
Project="$(WixSolution)"
Properties="$(DefineConstants)"
/>
removing your declaration for <Configuration> and changing <DefineConstants> to this:
<DefineConstants>Configuration=$(Configuration);WebRoot=$(WebRoot);DBRoot=$(DBRoot)</DefineConstants>
Following up on my comment I'd suggest you try using the MSBuild Task instead of Exec:
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="BuildWiXSolution">
<!-- Include the custom build targets installed with WiX -->
<Import Project="$(MSBuildExtensionsPath)\Wix\Wix.targets"/>
<PropertyGroup>
<WebRoot>$(DropLocation)\Latest\x86\Release\_PublishedWebsites\Web</WebRoot>
<DBRoot>$(DropLocation)\Latest\x86\Release\Database</DBRoot>
</PropertyGroup>
<ItemGroup>
<WiXSolution Include="$(MSBuildProjectDirectory)\Setup\Setup.sln">
<Properties>Configuration=Release</Properties>
<AdditionalProperties>WebRoot=$(WebRoot);DBRoot=$(DBRoot)</AdditionalProperties>
</WiXSolution>
</ItemGroup>
<Target Name="BuildWiXSolution">
<MSBuild Projects="#(WiXSolution)" />
</Target>
</Project>
It allows you to keep configuration properties and additional properties together with your Wix solution.