Is there an indexer available in MSBuild ItemGroups? - visual-studio

I'm using this stack overflow answer to return the built assembly's #(Version) as an ItemGroup for use in the post-build event.
However, I want to have Major and Minor separately, as I only want vMajor.Minor in the post build. To do this, I'd like to index inside the Include attribute.
The following works, but will break if the Major or Minor numbers go over 9 (single digits only):
<Target Name="PostBuildMacros">
<GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
<Output TaskParameter="Assemblies" ItemName="Targets" />
</GetAssemblyIdentity>
<ItemGroup>
<Version Include="#(Targets->'%(Version)')" />
<Major Include="#(Version->Substring(0,1))" />
<Minor Include="#(Version->Substring(2,1))" />
</ItemGroup>
</Target>
<PropertyGroup>
<PostBuildEventDependsOn>
$(PostBuildEventDependsOn);
PostBuildMacros;
</PostBuildEventDependsOn>
<PostBuildEvent>
if $(ConfigurationName) == Release copy /y $(TargetPath) $(SolutionDir)..\Builds\$(TargetName)_v#(Major).#(Minor)$(TargetExt)
</PostBuildEvent>
</PropertyGroup>
What I'm looking for is something more along the lines of:
<Major Include="#(Version->Split('.')[0])" />
<Minor Include="#(Version->Split('.')[1])" />
or, only if an indexer is really not possible,
<Major Include="#(Targets->Version->Major)" />
<Minor Include="#(Targets->Version->Minor)" />
<Major Include="#(Targets->'%(Version)'->'%(Major)')" />
<Minor Include="#(Targets->'%(Version)'->'%(Minor)')" />
<Major Include="#(Targets->'%(Version.Major)')" />
<Minor Include="#(Targets->'%(Version.Minor)')" />
What options, if any, do I have to index an array or otherwise pick apart a version number like 1.0.2838.24877 into 1 and 0?
Edit
This now works, by first using a <PropertyGroup> I can utilitize Split('.')[n] like in this post, but the $(Major) Properties ($()) are not available in the Post-Build event (why not? Am I missing something?). This would be more elegant if I could just directly use $(Major) in the post-build.
<Target Name="PostBuildMacros">
<GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
<Output TaskParameter="Assemblies" ItemName="Targets" />
</GetAssemblyIdentity>
<PropertyGroup>
<Version>#(Targets->'%(Version)')</Version>
<Major>$(Version.Split('.')[0])</Major>
<Minor>$(Version.Split('.')[1])</Minor>
<Build>$(Version.Split('.')[2])</Build>
<Revision>$(Version.Split('.')[3])</Revision>
</PropertyGroup>
<ItemGroup>
<Version Include="$(Version)" />
<Major Include="$(Major)" />
<Minor Include="$(Minor)" />
<Build Include="$(Build)" />
<Revision Include="$(Revision)" />
</ItemGroup>
<Message Text="Version: #(Version)" />
<Message Text="Major.Minor.Build.Revision: #(Major).#(Minor).#(Build).#(Revision)" />
</Target>
<PropertyGroup>
<PostBuildEventDependsOn>
$(PostBuildEventDependsOn);
PostBuildMacros;
</PostBuildEventDependsOn>
<PostBuildEvent>
if $(ConfigurationName) == Release copy /y $(TargetPath) $(SolutionDir)..\Builds\$(TargetName)_v#(Major).#(Minor)$(TargetExt)
</PostBuildEvent>
</PropertyGroup>
Why are ItemGroups #() available and PropertyGroups $() not?

This is the best I've come to, with little understanding of what else would be available or how to use indexers within ItemGroups.
<Target Name="PostBuildMacros">
<GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
<Output TaskParameter="Assemblies" ItemName="Targets" />
</GetAssemblyIdentity>
<PropertyGroup>
<Version>#(Targets->'%(Version)')</Version>
</PropertyGroup>
<ItemGroup>
<Version Include="$(Version)" />
<Major Include="$(Version.Split('.')[0])" />
<Minor Include="$(Version.Split('.')[1])" />
<Build Include="$(Version.Split('.')[2])" />
<Revision Include="$(Version.Split('.')[3])" />
</ItemGroup>
<Message Text="Version: #(Version)" />
<Message Text="Major.Minor.Build.Revision: #(Major).#(Minor).#(Build).#(Revision)" />
</Target>
<PropertyGroup>
<PostBuildEventDependsOn>
$(PostBuildEventDependsOn);
PostBuildMacros;
</PostBuildEventDependsOn>
<PostBuildEvent>
if $(ConfigurationName) == Release copy /y $(TargetPath) $(SolutionDir)..\Builds\$(TargetName)_v#(Major).#(Minor)$(TargetExt)
</PostBuildEvent>
</PropertyGroup>
I would like to be able to accomplish this without the <PropertyGroup> section, if possible.

Related

Building web project causes web config to be updated setting 'environmentVariable' to 'Development'

Time back I created a .net core with react SPA project from a template, ever since I have been unable to identify what is causing the web.config to be update each time I build.
Well, to be precise it is updated only adding back the environment variables tags, setting the environment to Development.
Can anyone please tell me how to prevent this?
<environmentVariables>
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>
Here is the csproj file content:
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
<TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
<IsPackable>false</IsPackable>
<SpaRoot>ClientApp\</SpaRoot>
<DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\**</DefaultItemExcludes>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OData" Version="8.0.12" />
<PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="6.0.13" />
<PackageReference Include="Minded" Version="0.1.3" />
[...]
<ItemGroup>
<!-- Don't publish the SPA source files, but do show them in the project files list -->
<Content Remove="$(SpaRoot)**" />
<None Remove="$(SpaRoot)**" />
<None Include="$(SpaRoot)**" Exclude="$(SpaRoot)node_modules\**" />
</ItemGroup>
<ItemGroup>
<Folder Include="ClientApp\src\components\" />
</ItemGroup>
<Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
<!-- Ensure Node.js is installed -->
<Exec Command="node --version" ContinueOnError="true">
<Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
</Exec>
<Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
<Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..." />
<Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
</Target>
<Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
<!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
<Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
<Exec WorkingDirectory="$(SpaRoot)" Command="npm run build" />
<!-- Include the newly-built files in the publish output -->
<ItemGroup>
<DistFiles Include="$(SpaRoot)build\**" />
<ResolvedFileToPublish Include="#(DistFiles->'%(FullPath)')" Exclude="#(ResolvedFileToPublish)">
<RelativePath>%(DistFiles.Identity)</RelativePath>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</ResolvedFileToPublish>
</ItemGroup>
</Target>

How to publish file from subfolder to main application folder?

I have a WinForms NET4.8 desktop app that I publish using ClickOnce. I have a dll file in a project subfolder that I need published to the same directory as the main app. I have the following in my csproj file:
<PropertyGroup>
<UnreferencedDlls>lib\twaindsm.dll</UnreferencedDlls>
</PropertyGroup>
<PropertyGroup>
<CopyAllFilesToSingleFolderForPackageDependsOn>
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForPackageDependsOn);
</CopyAllFilesToSingleFolderForPackageDependsOn>
</PropertyGroup>
<Target Name="AfterBuild">
<Message Text="Copying unreferenced DLLs to bin" Importance="High" />
<CreateItem Include="$(UnreferencedDlls)">
<Output TaskParameter="Include" ItemName="_UnReferencedDLLs" />
</CreateItem>
<Copy SourceFiles="#(_UnReferencedDLLs)" DestinationFolder="$(OutputPath)\%(RecursiveDir)" SkipUnchangedFiles="true" />
</Target>
<Target Name="CustomCollectFiles">
<Message Text="Publishing unreferenced DLLs" Importance="High" />
<ItemGroup>
<_CustomFiles Include="$(UnreferencedDlls)" />
<FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
<DestinationRelativePath>$(OutputPath)\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
</FilesForPackagingFromProject>
</ItemGroup>
</Target>
The file is published to the subfolder.
I change this line:
<DestinationRelativePath>$(OutputPath)\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
to:
<DestinationRelativePath>$(OutputPath)\%(Filename)%(Extension)</DestinationRelativePath>
But it still doesn't work.
How can I force the file to be published to the same folder as the app?
I don't know if it can help you: %(RecursiveDir)
If the Include attribute contains the wildcard **, this metadata
specifies the part of the path that replaces the wildcard.
Maybe you can try to change the path of the dll. I put it into ProjectDir\lib\Debug\ and here is my code:
<PropertyGroup>
<UnreferencedDlls>lib\**\ClassLibrary1.dll</UnreferencedDlls>
</PropertyGroup>
<PropertyGroup>
<CopyAllFilesToSingleFolderForPackageDependsOn>
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForPackageDependsOn);
</CopyAllFilesToSingleFolderForPackageDependsOn>
</PropertyGroup>
<Target Name="AfterBuild">
<Message Text="Copying unreferenced DLLs to bin" Importance="High" />
<CreateItem Include="$(UnreferencedDlls)">
<Output TaskParameter="Include" ItemName="_UnReferencedDLLs" />
</CreateItem>
<Copy SourceFiles="#(_UnReferencedDLLs)" DestinationFolder="bin\%(RecursiveDir)" SkipUnchangedFiles="true" />
</Target>
<Target Name="CustomCollectFiles">
<Message Text="Publishing unreferenced DLLs" Importance="High" />
<ItemGroup>
<_CustomFiles Include="$(UnreferencedDlls)" />
<FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
<DestinationRelativePath>bin\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
</FilesForPackagingFromProject>
</ItemGroup>
</Target>
You can also refer to this page hope it can help you.

Visual Studio Post build command line Deployment

In Visual Studio am creating a post-build event for Deploying using
md "$(SolutionDir)Deploy\bin"
which created the bin folder inside Deploy folder, inside my Solution.
How do I point this to the folder in some remote machine (where I have the web server)?
$(SolutionDir) to some other folder on a remote machine?
It may look simple to you. :) This is the first time am trying this stuff.
Thanks
The easiest way is to replace $(SolutionDir) with \\server\share
Just as an alternative, I like to keep my .sln and .csproj files "clean".
Then use a second (mini) .msbuild ( which is just a .xml file) to build the .sln, and then do these copy type events as a second action.
Here is a basic example:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="AllTargetsWrapper">
<PropertyGroup>
<WorkingCheckout>.</WorkingCheckout>
<WorkingOutputs>m:\working\outputs</WorkingOutputs>
</PropertyGroup>
<Target Name="AllTargetsWrapper">
<CallTarget Targets="Clean" />
<CallTarget Targets="Build" />
<CallTarget Targets="CopyItUp" />
</Target>
<Target Name="Clean">
<RemoveDir Directories="$(WorkingOutputs)" />
<MakeDir Directories="$(WorkingOutputs)" />
<Message Text="Cleaning done" />
</Target>
<Target Name="Build">
<MSBuild Projects="$(WorkingCheckout)\MySolution.sln" Targets="Build" Properties="Configuration=$(Configuration)">
<Output TaskParameter="TargetOutputs" ItemName="TargetOutputsItemName"/>
</MSBuild>
<Message Text="Build completed" />
</Target>
<!-- -->
<Target Name="CopyItUp" >
<ItemGroup>
<MyExcludeFiles Include="$(WorkingCheckout)\**\SuperSecretStuff.txt" />
<MyExcludeFiles Include="$(WorkingCheckout)\**\SuperSecretStuff.doc" />
</ItemGroup>
<ItemGroup>
<MyIncludeFiles Include="$(WorkingCheckout)\MyCsProject\bin\$(Configuration)\**\*.*" Exclude="#(MyExcludeFiles)"/>
</ItemGroup>
<Copy
SourceFiles="#(MyIncludeFiles)"
DestinationFiles="#(MyIncludeFiles->'$(WorkingOutputs)\%(RecursiveDir)%(Filename)%(Extension)')"
/>
</Target>
</Project>

Include MajorVersion etc in filename (OutputName) when building MSI file (wix project)

In my Defines.wxi I have:
<?define MajorVersion="1" ?>
<?define MinorVersion="08" ?>
<?define BuildVersion="11" ?>
In my MyProject.Setup.wixproj I have:
<OutputName>MyProject</OutputName>
<OutputType>Package</OutputType>
Is it possible to include the version variables in the filename somehow, so that my file can be named MyProject.1.08.11.msi?
This didn't work (no such variable is defined):
<OutputName>MyProject-$(MajorVersion)</OutputName>
<OutputType>Package</OutputType>
This didn't work (no such variable is defined):
<Target Name="AfterBuild" Condition="'$(Configuration)' == 'Release'">
<Copy SourceFiles="$(OutputPath)$(OutputName).msi" DestinationFiles="C:\$(OutputName)-$(MajorVersion).msi" />
</Target>
It seems very clear to me that $(MajorVersion) is not the correct way of fetching the definition from the Defines.wxi file. What is?
Update
I tried to put this in MyProject.Setup.wixproj:
<InstallerMajorVersion>7</InstallerMajorVersion>
<InstallerMinorVersion>7</InstallerMinorVersion>
<InstallerBuildNumber>7</InstallerBuildNumber>
...
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<OutputPath>bin\$(Configuration)\</OutputPath>
<IntermediateOutputPath>obj\$(Configuration)\</IntermediateOutputPath>
<DefineConstants>PrebuildPath=..\..\obj\prebuild\web\;InstallerMajorVersion=$(InstallerMajorVersion);InstallerMinorVersion=$(InstallerMinorVersion);InstallerBuildNumber=$(InstallerBuildNumber)</DefineConstants>
</PropertyGroup>
And this in Defines.wxi:
<?define MajorVersion="$(var.InstallerMajorVersion)" ?>
<?define MinorVersion="$(var.InstallerMinorVersion)" ?>
<?define BuildVersion="$(var.InstallerBuildNumber)" ?>
<?define Revision="0" ?>
<?define VersionNumber="$(var.InstallerMajorVersion).$(var.InstallerMinorVersion).$(var.InstallerBuildNumber)" ?>
Didn't work either. Got these error messages:
The Product/#Version attribute's value, '..', is not a valid version.
Legal version values should look like 'x.x.x.x' where x is an integer
from 0 to 65534.
The Product/#Version attribute was not found; it is
required.
This is what I ended up with, and it works!
Setup.Version.proj
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<InstallerMajorVersion>55</InstallerMajorVersion>
<InstallerMinorVersion>66</InstallerMinorVersion>
<InstallerBuildVersion>$(BuildNumber)</InstallerBuildVersion>
<InstallerBuildVersion Condition="$(InstallerBuildVersion) == ''">0</InstallerBuildVersion>
</PropertyGroup>
</Project>
MyProject.Setup.wixproj
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="Setup.Version.proj" />
<PropertyGroup>
<OutputName>MyProject_$(InstallerMajorVersion)_$(InstallerMinorVersion)_$(InstallerBuildVersion)</OutputName>
<OutputType>Package</OutputType>
<DefineConstants>InstallerMajorVersion=$(InstallerMajorVersion);InstallerMinorVersion=$(InstallerMinorVersion);InstallerBuildVersion=$(InstallerBuildVersion)</DefineConstants>
...
Defines.wxi
<?define MajorVersion="$(var.InstallerMajorVersion)" ?>
<?define MinorVersion="$(var.InstallerMinorVersion)" ?>
<?define BuildVersion="$(var.InstallerBuildVersion)" ?>
This common task should be simplified in future versions of WiX!
This solution combines #Wimmel's and this post. It draws the version from a target .exe, and otherwise does not store version numbers in files; it doesn't rename the output file in post-build. But, it is necessary to update the property ProjectDefineConstants, from which the candle arguments are derived (in wix.targets). Otherwise, updating only the TargetPath property does not change the inputs to candle.exe.
*.wixproj:
<Import Project="$(WixTargetsPath)" />
<Target Name="BeforeBuild">
<!-- Read the version from the to-be-installed .exe -->
<GetAssemblyIdentity AssemblyFiles="path.to.primary.exe">
<Output TaskParameter="Assemblies" ItemName="AsmInfo" />
</GetAssemblyIdentity>
<!-- Create the MSBuild property $(VersionNumber) -->
<CreateProperty Value="%(AsmInfo.Version)">
<Output TaskParameter="Value" PropertyName="VersionNumber" />
</CreateProperty>
<!-- Create the WiX preprocessor variable $(var.VersionNumber) -->
<CreateProperty Value="$(DefineConstants);VersionNumber=$(VersionNumber)">
<Output TaskParameter="Value" PropertyName="DefineConstants" />
</CreateProperty>
<!-- Update the MSBuild properties $(TargetName), etc. -->
<CreateProperty Value="$(SolutionName)-$(Platform)-$(VersionNumber)">
<Output TaskParameter="Value" PropertyName="TargetName" />
</CreateProperty>
<CreateProperty Value="$(TargetName)$(TargetExt)">
<Output TaskParameter="Value" PropertyName="TargetFileName" />
</CreateProperty>
<CreateProperty Value="$(TargetName)$(TargetPdbExt)">
<Output TaskParameter="Value" PropertyName="TargetPdbName" />
</CreateProperty>
<CreateProperty Value="$(TargetDir)$(TargetFileName)">
<Output TaskParameter="Value" PropertyName="TargetPath" />
</CreateProperty>
<CreateProperty Value="$(TargetPdbDir)$(TargetPdbName)">
<Output TaskParameter="Value" PropertyName="TargetPdbPath" />
</CreateProperty>
<!-- Update the MSBuild property from which candle.exe args are derived -->
<CreateProperty Value="
Configuration=$(ConfigurationName);
OutDir=$(OutDir);
Platform=$(PlatformName);
ProjectDir=$(ProjectDir);
ProjectExt=$(ProjectExt);
ProjectFileName=$(ProjectFileName);
ProjectName=$(ProjectName);
ProjectPath=$(ProjectPath);
TargetDir=$(TargetDir);
TargetExt=$(TargetExt);
TargetFileName=$(TargetFileName);
TargetName=$(TargetName);
TargetPath=$(TargetPath);
">
<Output TaskParameter="Value" PropertyName="ProjectDefineConstants" />
</CreateProperty>
</Target>
*.wxs
<Product Id="*" Version="$(var.VersionNumber)" ... >
...
</Product>
In your .wixproj file. Add the following section just before the </Project> tag.
<!-- rename the output msi with Version number -->
<Target Name="AfterBuild">
<GetAssemblyIdentity AssemblyFiles="[Path of the main assembly with the assembly version number you want to use]">
<Output TaskParameter="Assemblies" ItemName="AssemblyVersion"/>
</GetAssemblyIdentity>
<Copy SourceFiles=".\bin\$(Configuration)\$(OutputName).msi" DestinationFiles=".\bin\$(Configuration)\$(OutputName)_%(AssemblyVersion.Version).msi" />
<Delete Files=".\bin\$(Configuration)\$(OutputName).msi" />
</Target>
This works for me.
It is not possible to read the .wxi file from the .wixproj file. So you have to use another way to specify the version. I can give an example where I read the version from a assembly included in the installer, and use that version to rename the msi;
Open the .wixproj file in an editor and add a ReadVersion target:
<Target Name="ReadVersion">
<GetAssemblyIdentity AssemblyFiles="bin\program.exe">
<Output TaskParameter="Assemblies" ItemName="MyAssemblyIdentities" />
</GetAssemblyIdentity>
<Message Text="AssemblyVersion = %(MyAssemblyIdentities.Version)" />
<CreateProperty Value="$(TargetName) %(MyAssemblyIdentities.Version)">
<Output TaskParameter="Value" PropertyName="TargetName" />
</CreateProperty>
<CreateProperty Value="$(TargetName)$(TargetExt)">
<Output TaskParameter="Value" PropertyName="TargetFileName" />
</CreateProperty>
<CreateProperty Value="$(OutDir)$(TargetFileName)">
<Output TaskParameter="Value" PropertyName="TargetPath" />
</CreateProperty>
</Target>
This reads the version from bin\program.exe, displays it for debugging purposes, and changes the TargetName, TargetFileName and TargetPath.
After the line containing <Import Project="$(WixTargetsPath)" />, add the following to inject this target into the build process:
<PropertyGroup>
<BuildDependsOn>ReadVersion;$(BuildDependsOn)</BuildDependsOn>
</PropertyGroup>
One way would be to define the variables in your MSBuild script, and have it update Defines.wxi at build time, as in this example.
In your MSBuild script, you could define the version properties as follows:
<PropertyGroup>
<MajorVersion>1</MajorVersion>
<MinorVersion>08</MinorVersion>
<BuildVersion>11</BuildVersion>
<WixConfigPath>.\Defines.wxi</WixConfigPath>
<_VariableDefinitions>
<Root>
<VariableDefinition Name="MajorVersion" NewValue="$(MajorVersion)" />
<VariableDefinition Name="MinorVersion" NewValue="$(MinorVersion)" />
<VariableDefinition Name="BuildVersion" NewValue="$(BuildVersion)" />
</Root>
</_VariableDefinitions>
</PropertyGroup>
<Target Name="UpdateWixVars">
<WixVarSubstitution SourceFile="$(WixConfigPath)" VariableDefinitions="$(_VariableDefinitions)"/>
</Target>
Then running the UpdateWixVars target will update the version numbers in Defines.wxi with the version numbers specified in your MSBuild project.
Note that I could not find an actual compiled dll with this custom build task, so I had to create it by:
Download the source from here. Build it and name the file Tranxition.BuildTasks.dll.
Add the reference to the build task like so:
<UsingTask TaskName="WixVarSubstitution"
AssemblyFile="$(MSBuildExtensionsPath)\Tranxition\Tranxition.BuildTasks.dll"/>
You can accomplish this seamlessly by implementing these two answers:
First, update your project's AfterBuild task to rename the WIX-built MSI: https://stackoverflow.com/a/10716409/374198
Then, update your WIX project to automatically use the verison number of the assembly you're packaging: https://stackoverflow.com/a/641094/374198
The other answers are much too complex!
PS: If you want to drop the fourth digit, following semantic versioning, you can do it like this:
<Target Name="AfterBuild">
<GetAssemblyIdentity AssemblyFiles="..\Path\To\MyProject\bin\$(Platform)\$(Configuration)\MyProject.dll">
<Output TaskParameter="Assemblies" ItemName="AssemblyInfo" />
</GetAssemblyIdentity>
<PropertyGroup>
<In>%(AssemblyInfo.Version)</In>
<Pattern>^(\d+.\d+.\d+)</Pattern>
<AssemblyVersion>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern)))</AssemblyVersion>
</PropertyGroup>
<Move SourceFiles="bin\$(Platform)\$(Configuration)\MyProject.msi" DestinationFiles="bin\$(Platform)\$(Configuration)\CodeGenerator-$(AssemblyVersion).$(Platform).msi" />
</Target>
This will create an MSI named, for example, MyProject-1.2.3.x64.msi. See this answer for more.
This is a full example of a wixproj file where you can set a version number in the UI and use that to modify the output msi file name.
In Visual Studio (e.g. 2015):
define a version number in "Define preprocessor variables" in the
project properties window. I've entered VersionNumber=1.14 for this
example;
unload your project in the solution explorer;
right click your project in the solution explorer and select edit yourproject.wixproj file;
add code in the Target Name="BeforeBuild" element as shown below.
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" InitialTargets="EnsureWixToolsetInstalled" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>3.10</ProductVersion>
<ProjectGuid>PROJECT-GUID</ProjectGuid>
<SchemaVersion>2.0</SchemaVersion>
<OutputName>my project</OutputName>
<OutputType>Package</OutputType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<OutputPath>bin\$(Configuration)\</OutputPath>
<IntermediateOutputPath>obj\$(Configuration)\</IntermediateOutputPath>
<!-- These constants can be set in the UI -->
<DefineConstants>VersionNumber=1.14</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<OutputPath>bin\$(Configuration)\</OutputPath>
<IntermediateOutputPath>obj\$(Configuration)\</IntermediateOutputPath>
<DefineConstants>VersionNumber=1.14</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Compile Include="Product.wxs" />
</ItemGroup>
<ItemGroup>
<WixExtension Include="WixUIExtension">
<HintPath>$(WixExtDir)\WixUIExtension.dll</HintPath>
<Name>WixUIExtension</Name>
</WixExtension>
</ItemGroup>
<Import Project="$(WixTargetsPath)" Condition=" '$(WixTargetsPath)' != '' " />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\WiX\v3.x\Wix.targets" Condition=" '$(WixTargetsPath)' == '' AND Exists('$(MSBuildExtensionsPath32)\Microsoft\WiX\v3.x\Wix.targets') " />
<Target Name="BeforeBuild">
<!-- This extracts the version number from a setting in the UI -->
<!-- This method comes from: geekswithblogs.net/alexhildyard/archive/2013/03/09/passing-data-between-msbuild-and-wix.aspx -->
<ItemGroup>
<DefineConstantsKVPairs Include="$(DefineConstants)" />
</ItemGroup>
<!-- Evaluate each key/value pair with task batching, then make a conditional assignment -->
<PropertyGroup>
<VersionNumber Condition="$([System.String]::new('%(DefineConstantsKVPairs.Identity)').Contains('VersionNumber='))">$([System.String]::new('%(DefineConstantsKVPairs.Identity)').Split('=')[1])</VersionNumber>
</PropertyGroup>
<CreateProperty Value="$(OutputName)-$(VersionNumber)">
<Output TaskParameter="Value" PropertyName="TargetName" />
</CreateProperty>
<CreateProperty Value="$(TargetName)$(TargetExt)">
<Output TaskParameter="Value" PropertyName="TargetFileName" />
</CreateProperty>
<CreateProperty Value="$(TargetDir)$(TargetFileName)">
<Output TaskParameter="Value" PropertyName="TargetPath" />
</CreateProperty>
</Target>
</Project>

Setting Visual Studio to automatically minifying using Ajax Minifier

I use VS 2010 I would like at the pressing of Build Button have all CSS and .JS files minified.
Following this article all is working great for JS... adding some extra code for the CSS (pasted below) I'm not able to Minifying the CSS. What I'm doing wrong here?
Thanks for your time.
<Import Project="$(MSBuildExtensionsPath)\Microsoft\MicrosoftAjax\ajaxmin.tasks" />
<Target Name="AfterBuild">
<ItemGroup>
<JS Include="**\*.js" Exclude="**\*.min.js;Scripts\*.js" />
<JS Include="**\*.css" Exclude="**\*.min.css;Scripts\*.css" />
</ItemGroup>
<AjaxMin SourceFiles="#(JS)" SourceExtensionPattern="\.js$" TargetExtension=".min.js" />
<AjaxMin SourceFiles="#(CSS)" SourceExtensionPattern="\.css$" TargetExtension=".min.css" />
</Target>
I found out the solution to my problem, more resource here
Unable to run Ajax Minifier as post-build in Visual Studio
http://ajaxmin.codeplex.com/wikipage?title=AjaxMinTask
Also very interesting:
http://www.codeproject.com/Articles/81317/Automatically-compress-embedded-JavaScript-resourc.aspx?msg=3802401&display=Mobile
<Import Project="$(MSBuildExtensionsPath)\Microsoft\MicrosoftAjax\ajaxmin.tasks" />
<Target Name="AfterBuild">
<ItemGroup>
<JS Include="**\*.js" Exclude="**\*.min.js;Scripts\*.js" />
</ItemGroup>
<ItemGroup>
<CSS Include="**\*.css" Exclude="**\*.min.css" />
</ItemGroup>
<AjaxMin
JsSourceFiles="#(JS)" JsSourceExtensionPattern="\.js$" JsTargetExtension=".min.js"
CssSourceFiles="#(CSS)" CssSourceExtensionPattern="\.css$" CssTargetExtension=".min.css" />
</Target>
On my project file I used "UsingTask" instead of "Import" and it's working:
<UsingTask TaskName="AjaxMin" AssemblyFile="$(MSBuildProjectDirectory)\..\..\packages\AjaxMin.5.14.5506.26202\tools\net40\AjaxMinTask.dll" />
<Target Name="AfterBuild">
<ItemGroup>
<JS Include="**\*.js" Exclude="**\*.min.js;Scripts\*.js" />
</ItemGroup>
<ItemGroup>
<CSS Include="**\*.css" Exclude="**\*.min.css" />
</ItemGroup>
<AjaxMin
JsSourceFiles="#(JS)" JsSourceExtensionPattern="\.js$" JsTargetExtension=".min.js"
CssSourceFiles="#(CSS)" CssSourceExtensionPattern="\.css$" CssTargetExtension=".min.css" />
</Target>

Resources