What is the right macro for platform? - visual-studio

I'm trying to write a pre-build event script that must be able to adapt the the build platform (Windows / Mac / Azure DevOps image) in my case.
So far I have found the $(Platform) macro but it returns me AnyCPU
What I need is Darwin or Win32/Win64, and I can't find it in the documentation.
Is there such a thing ?
Thanks in advance.
EDIT :
For Azure DevOps there is Agent.OS and Agent.OSArchitecture

I found a solution. It was as easy as moving from a build event to a MS Build copy file task.
Inside this task, you can have a condition on the OS.
For Windows:
<Target Name="CopyWindowsRTELogConfigFile" BeforeTargets="PreBuildEvent" Condition="'$(OS)' == 'Windows_NT'">
<ItemGroup>
<RTELogWindowsConfigurationJson Include="$(ProjectDir)Configuration\Windows_NT\$(ConfigurationName)\specific.rtelog.appsettings.json" />
</ItemGroup>
<Copy SourceFiles="#(RTELogWindowsConfigurationJson)" DestinationFolder="$(ProjectDir)" />
</Target>
And Mac:
<Target Name="CopyDarwinRTELogConfigFile" BeforeTargets="PreBuildEvent" Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))'">
<ItemGroup>
<RTELogWindowsConfigurationJson Include="$(ProjectDir)Configuration\Darwin\$(ConfigurationName)\specific.rtelog.appsettings.json" />
</ItemGroup>
<Copy SourceFiles="#(RTELogWindowsConfigurationJson)" DestinationFolder="$(ProjectDir)" />
</Target>

Related

How can I create a cycle of compiling my project with incrementing of some option?

I wrote a C# template for creating of the .Net extensions for AutoCAD. Before, for each AutoCAD version it is was necessary to point the individual referenses set, the output directory, the target .Net Framework Platform, etc. Exist many versions of AutoCAD: AutoCAD 2009, 2010, ..., 2015. Now my template do it instead of me. My csproj-file has the CAD_Year property:
<PropertyGroup>
<CAD_Year>2013</CAD_Year>
<Min_Year>2009</Min_Year>
<Max_Year>2015</Max_Year>
</PropertyGroup>
When I change CAD_Year value (manually edit this option in the csproj-file) - all settings of my project do change too according target AutoCAD version. It works fine.
But I need to compile my code for all versions of AutoCAD always... It is inconvenient to change the CAD_Year every time for this... :(((
How can I create the cycle of compiling my project for the versions Min_Year, ..., Max_Year when I press the Rebuild Solution menu item?
Thank you, #stijn. I will mark your answer as a solution. Here I create an "answer" for the code highlighting. My current code works:
<!-- Redefine the CoreClean target, otherwise MSBuild will remove all results
of building except for the last. -->
<Target Name="CoreClean">
<ItemGroup>
<AllFiles Include="$(OutputPath)\*.*" />
</ItemGroup>
<Copy SourceFiles="#(AllFiles)" DestinationFolder="$(OutputPath)\temp" />
</Target>
<Target Name="BatchRebuild">
<ItemGroup>
<CADYearsItem Include="$(BuildFor)" />
</ItemGroup>
<Msbuild Projects="$(MsBuildThisFile)" Targets="Rebuild" Properties="CAD_Year_Platform=%(CADYearsItem.Identity)" />
<ItemGroup>
<AllFilesBack Include="$(OutputPath)\temp\*.*" />
</ItemGroup>
<Move SourceFiles="#(AllFilesBack)" DestinationFolder="$(OutputPath)" />
<!-- Doesn't work for Debug. The $(OutputPath)\temp\ will not removed.
But it work for Release.-->
<RemoveDir Directories="$(OutputPath)\temp\" />
</Target>
I see, the RemoveDir task doesn't work for the Debug for me, but it is not a big problem. Now my template is complete, and I will do refactoring of this. Thank you very much!
If you add this to your project file:
<ItemGroup>
<CADYears Include="2013;2014;2015"/>
</ItemGroup>
<Target Name="BatchRebuild">
<Msbuild Projects="$(MsBuildThisFile)" Targets="Rebuild" Properties="CAD_Year=%(CADYears.Identity)"/>
</Target>
and call
msbuild <path_to_projectfile> /t:BatchRebuild
on the commandline, it will build path_to_projectfile 3 times each with a different CAD_Year property.
To get this invoked by VS is trickier since you need to override the Rebuild target, but this for instance works for VS2013 (Actualrebuild target was copied from the Rebuild target in C:\Program Files (x86)\MSBuild\12.0\Bin\Microsoft.Common.CurrentVersion.targets):
<ItemGroup>
<CADYears Include="2013;2014;2015"/>
</ItemGroup>
<Target Name="ActualRebuild"
Condition=" '$(_InvalidConfigurationWarning)' != 'true' "
DependsOnTargets="$(RebuildDependsOn)"
Returns="$(TargetPath)"/>
<Target Name="BatchRebuild">
<Msbuild Projects="$(MsBuildThisFile)" Targets="ActualRebuild" Properties="CAD_Year=%(CADYears.Identity)"/>
</Target>
<Target Name="Rebuild">
<Msbuild Projects="$(MsBuildThisFile)" Targets="BatchRebuild"/>
</Target>
Edit
Since the template system in VS tries to copies ItemGroups it finds in the project root (which seems like a bug to me, or at the least a very annoying feature) you can work around that by using a property and converting it into an item when needed:
<PropertyGroup>
<CADYears>2013;2014;2015<CADYears/>
</PropertyGroup>
<Target Name="BatchRebuild">
<ItemGroup>
<CADYearsItem Include="$(CADYears)"/>
</ItemGroup>
<Msbuild Projects="$(MsBuildThisFile)" Targets="Rebuild" Properties="CAD_Year=%(CADYearsItem .Identity)"/>
</Target>
Note: in the project you posted in the link you are invoking the Rebuild target in the Afterbuild target. I didn't try it, but that will almost certainly lead to infinite recursion. So you should stick to a solution like posted above with a seperate target.

MSBuild projects with different build-configs, without using sln

Related
I have two projects in my VS solution, BookApp.Web and BookApp.Domain.
BookApp.Web references BookApp.Domain.
BookApp.Web has the following build configurations: debug, staging, prod-eu, prod-us and prod-as. We have three datacenters for production and a staging environment.
BookApp.Domain so far only has two build configurations, debug.
When building the solution from within Visual Studio, I can use the build configurator to make sure that no matter what build config is selected for the Web project, the debug config is always used for the Domain project.
However, when building with MSBuild on my continuous integration server, things go wrong. I use this in my rollout.msbuild file:
<MSBuild Projects="BookApp.Web\BookApp.Web.csproj" Properties="Configuration=Prod-us" />
When I run this, MSBuild expects all dependent projects to have the same build configuration. Since that's not the case (and shouldn't be IMO), it fails with this error message:
The OutputPath property is not set for project 'BookApp.Domain.csproj'. Please check to make sure that you have specified a valid combination of Configuration and Platform for this project. Configuration='Prod-us' Platform='AnyCPU'.
An answer to a related question suggests creating separate .sln solutions for each build configuration and running that with MSBuild. To me that doesn't sound like a good idea.
Copying all the build configurations to the Domain project is also not ideal.
Is there a better way of telling MSBuild to use different build configs?
Have a look at this answer it explains how the configurations are passed from project to Project through the MSBuild Task and Using the MetaData of the configuration to pass the desired configuration for the target project
here
UPDATE
I created a Solution with a class library(Sample.Domain) and ConsoleApplication(SampleApp.Console). I added two more configurations to the SamplApp.Console: prod-us;prod-eu, Sample.Domain remained with debug;release.
I then Changed the csproj file of the ConsoleApplication, like so:
ProjectReferences
<!--<ItemGroup>
<ProjectReference Include="..\Sample.Domain\Sample.Domain.csproj">
<Project>{73e8a7fd-0a24-47c5-a527-7601550d4b92}</Project>
<Name>Sample.Domain</Name>
</ProjectReference>
</ItemGroup>-->
<ItemGroup>
<ProjectReference Include="..\Sample.Domain\Sample.Domain.csproj" >
<Targets>Build</Targets>
</ProjectReference>
</ItemGroup>
Added a switch case on the configuration passed to MSBuild, to configure some properties for Outputfiles and reference files:
<Choose>
<When Condition="'$(Configuration)' != 'Debug'">
<PropertyGroup>
<OutputProperty>$(OutputPath)\$(Configuration)</OutputProperty>
<FileCopy>$(OutputProperty)</FileCopy>
</PropertyGroup>
</When>
<Otherwise>
<PropertyGroup>
<OutputProperty>$(OutputPath)</OutputProperty>
<FileCopy>$(OutputProperty)</FileCopy>
</PropertyGroup>
</Otherwise>
</Choose>
Created a Target to switch the Configuration passed to MSBuild, so that Debug will pass Debug to Sample.Domain, everything else it will pass Release
<Target Name="MultiConfiguration" >
<CreateProperty Value="Debug">
<Output TaskParameter="Value" PropertyName="LibConfiguration" Condition="'$(Configuration)' == 'Debug'"/>
</CreateProperty>
<CreateProperty Value="Release">
<Output TaskParameter="Value" PropertyName="LibConfiguration" Condition="'$(Configuration)' != 'Debug' "/>
</CreateProperty>
</Target>
The Build Target uses the Properties we have added so the Output and Copy of references files will have the right values according to Configuration value
<!--Build Process-->
<Target Name="Build" DependsOnTargets="Clean;MultiConfiguration;ComputeProjectReference" >
<Csc Sources="#(Compile)" References="#(NewAssemblies)" TargetType="exe" OutputAssembly="$(OutputProperty)\$(AssemblyName).exe"/>
</Target>
<Target Name="ComputeProjectReference" Inputs="#(ProjectReference)" Outputs="%(ProjectReference.Identity)__Forced">
<MSBuild Projects="#(ProjectReference)" Targets="%(ProjectReference.Targets)" Properties="Configuration=$(LibConfiguration);Platform=AnyCPU;OutputPath=bin\$(LibConfiguration)">
<Output TaskParameter="TargetOutputs" ItemName="ResolvedProjectReferences"/>
</MSBuild>
</Target>
<Target Name="AfterProjectReference" AfterTargets="ComputeProjectReference">
<CreateItem Include="#(ResolvedProjectReferences)">
<Output TaskParameter="Include" ItemName="CopyFiles" />
</CreateItem>
<Copy SourceFiles="#(CopyFiles)" DestinationFolder="$(FileCopy)" SkipUnchangedFiles="false" />
<ItemGroup>
<NewAssemblies Include="$(OutputProperty)\%(CopyFiles.FileName)%(CopyFiles.Extension)" />
</ItemGroup>
</Target>
To call the Debug configuration is done like this
msbuild SampleApp.Console.csproj
To call (Release;prod-us;prod-eu;...) is done like this
msbuild SampleApp.Console.csproj /p:Configuration="prod-us" /p:OutputPath="bin"
I'm sure it can be optimized, and might be some way easier, but it works.

msbuild transform one config few times

I have next config in my Web.config file
<Target Name="UpdateWebConfigForProjectsBeforeRun">
<ItemGroup>
<FilesToTransofm Include="ProjectsDeployBin\Web.*.$(Configuration).config"/>
</ItemGroup>
<Message Text="Transform file: %(FilesToTransofm.Identity)" />
<TransformXml Source="web.config"
Transform="%(FilesToTransofm.Identity)"
Destination="web.config" />
</Target>
What i am trying to do its get all configs from ProjectsDeployBin directory and apply each file to main web.config.
After first transformation main web.config locked by msbuild.
So how can i fix this issue? Is there any other way to transform my web.config by collection of files?
Thanks.
As you've noticed, the TransformXml task shipped with Visual Studio 2010 has a bug that leaves the source file locked.
To work around that, you can make a temporary copy of your source file before each transformation.
Since you'll then be executing multiple tasks for each transform file (copy and transform), you'll need to switch to Target Batching instead of Task Batching.
Example:
<ItemGroup>
<FilesToTransofm Include="ProjectsDeployBin\Web.*.$(Configuration).config"/>
</ItemGroup>
<Target Name="UpdateWebConfigForProjectsBeforeRun"
Inputs="#(FilesToTransofm)"
Outputs="%(Identity).AlwaysRun">
<Message Text="Transform file: %(FilesToTransofm.Identity)" />
<Copy SourceFiles="web.config"
DestinationFiles="web.pre-%(FilesToTransofm.Filename).temp.config" />
<TransformXml Source="web.pre-%(FilesToTransofm.Filename).temp.config"
Transform="%(FilesToTransofm.Identity)"
Destination="web.config" />
</Target>
From a quick test, it looks like this bug is fixed in Visual Studio 2012, but I'm not able to find a reference / source that documents that, and the original Connect bug isn't viewable anymore.

Visual Studio 2010 custom output path for references

I have VS2010 project with several third-party references. Is there any way to automatically output these references to $(OutputPath)\Libraries instead of just $(OutputPath)?
Right now I have a custom AfterBuild target which looks like this,
<Target Name="AfterBuild">
<ItemGroup>
<LibFiles Include="$(SolutionDir)\lib\dotnetzip-1.9\Release\Ionic.Zip.dll" />
<LibFiles Include="$(SolutionDir)\lib\ninject-2.2.0.0\Ninject.dll" />
<LibFiles Include="$(SolutionDir)\lib\nlog-2.0.0.2000\NLog.dll" />
<LibFiles Include="$(SolutionDir)\lib\nlog-2.0.0.2000\NLog.Extended.dll" />
</ItemGroup>
<Copy SourceFiles="#(LibFiles)" DestinationFolder="$(OutputPath)\Libraries" />
</Target>
However this gets tiring since I have to manually add references to #(LibFiles) when adding a reference in VS.
Is there an easier way?
Try to do it this way:
<ItemGroup>
<LibFiles Include="$(SolutionDir)\lib\**\*.dll" />
</ItemGroup>
<Target Name="AfterBuild" Inputs="#(LibFiles)">
<Copy SourceFiles="#(LibFiles)" DestinationFolder="$(OutputPath)\Libraries" />
</Target>
Pros:
you don't have to modify AfterBuild target everytime you add new
reference into your projects
libraries are copied only once or if datetime of any of files in
#(LibFiles) is changed (after update)
Cons:
you will have more dlls in Libraries folder, I guess. But you can filter them using Exclude="$(SolutionDir)\lib\**\Debug\*.dll" for example

Visual Studio. Publish project from command line

Is there a way to publish a web project in MS Visual Studio 2010 using CLI? I use DevEnv.exe /Build to build a project and it works fine, but I could not find option to Publish a project.
One other thing I want to mention. I am trying to publish web project NOT to the IIS directly. I have a location where I publish several projects and then build them automatically into NSIS bundle to be deployed.
From ASP.NET Web Deployment using Visual Studio: Command Line Deployment, you can use
msbuild myproject.csproj /p:DeployOnBuild=true /p:PublishProfile=MyPublishProfile
where MyPublishProfile is the profile name that you've already set up somewhere
What works best is to add following target to the project file:
<Target Name="AfterBuild">
<Message Text="Copying to Deployment Dir:" />
<Copy SourceFiles="#(Content)" DestinationFolder="..\XXX\%(Content.RelativeDir)" />
<CreateItem Include="$(OutputPath)\*">
<Output TaskParameter="Include" ItemName="Binaries"/>
</CreateItem>
<Copy SourceFiles="#(Binaries)" DestinationFolder="..\XXX\bin" />
</Target>
This way, whenever project got build (from command line or from IDE) it automatically get deployed to specified folder. Thank you everybody for pointing me to right direction.
The /t:publish switch is for ClickOnce applications only, it's not applicable to web projects. Hence the error saying it's unpublishable. :)
#RobKent As https://stackoverflow.com/a/2775437/21233364
using
<Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release' ">
<Message Text="Copying to Deployment Dir:" />
<Copy SourceFiles="#(Content)" DestinationFolder="..\XXX\%(Content.RelativeDir)" />
<CreateItem Include="$(OutputPath)\*">
<Output TaskParameter="Include" ItemName="Binaries"/>
</CreateItem>
<Copy SourceFiles="#(Binaries)" DestinationFolder="..\XXX\bin" />
</Target>
you can have publish only on release compiling.

Resources