ms build dynamic resource naming - c#

I want to add some files to embedded resourses at compile time with special naming. For this I wrote in .csproj
<Target Name="BeforeBuild" >
<ItemGroup>
<EmbeddedResource Include="..\Bin\$(Configuration)\*.*">
</EmbeddedResource>
</ItemGroup>
</Target>
But it always set <LogicalName> to default.
How can I do this?

I found solution. This renaming works:
<Target Name="BeforeBuild">
<ItemGroup>
<Frameworks Include="..\Bin\$(Configuration)\Frameworks\*.*" />
<Steps Include="..\Bin\$(Configuration)\Steps\*.*" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="#(Frameworks)">
<LogicalName>Frameworks.%(Filename)%(Extension)</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="#(Steps)">
<LogicalName>Steps.%(Filename)%(Extension)</LogicalName>
</EmbeddedResource>
</ItemGroup>
<Message Text="EmbeddedResource - #(EmbeddedResource)" />
</Target>

Related

Smarter File nesting inside csproj wildcard

I need better file nesting.
I have Index.razor, Index.razor.cs, Index.Model.cs, Index.Interface.cs inside the Pages folder.
In csproj
I have
<ItemGroup>
<Compile Update="Pages\Index.razor.cs" DependentUpon="Pages\Index.razor" />
<Compile Update="Pages\Index.Model.cs" DependentUpon="Pages\Index.razor" />
<Compile Update="Pages\Index.Interface.cs" DependentUpon="Pages\Index.razor" />
</ItemGroup>
I created this functionality into Wildcard
<ItemGroup>
<Compile Update="**\*.Interface.cs" DependentUpon="$([System.String]::Copy('%(FileName)').Replace('.Interface', '.razor'))" />
<Compile Update="**\*.Model.cs" DependentUpon="$([System.String]::Copy('%(FileName)').Replace('.Model', '.razor'))" />
</ItemGroup>
What I want is better version of this
<ItemGroup>
<Compile Update="**\*.Interface.cs" Condition="Exists('Check Razor')" >
<DependentUpon>"$([System.String]::Copy('%(FileName)').Replace('.Interface', '.razor'))"</DependentUpon>
</Compile>
<Compile Update="**\*.Model.cs" Condition="Exists('Check Razor')" >
<DependentUpon>"$([System.String]::Copy('%(FileName)').Replace('.Model', '.razor'))"</DependentUpon>
</Compile>
<Compile Update="**\*.Interface.cs" Condition="Exists('Check cshtml')" >
<DependentUpon>"$([System.String]::Copy('%(FileName)').Replace('.Interface', '.cshtml'))"</DependentUpon>
</Compile>
<Compile Update="**\*.Model.cs" Condition="Exists('Check cshtml')" >
<DependentUpon>"$([System.String]::Copy('%(FileName)').Replace('.Model', '.cshtml'))"</DependentUpon>
</Compile>
Need to check here also
<Compile Update="**\*.Interface.cs" DependentUpon="$([System.String]::Copy('%(FileName)').Replace('.Interface', '.cs'))" />
<Compile Update="**\*.Model.cs" DependentUpon="$([System.String]::Copy('%(FileName)').Replace('.Model', '.cs'))" />
</ItemGroup>
So that I can use Isolation in cshtml and razor project without hasle.
For example to work in xaml, you can use MSBuild15's static update syntax.
<Compile Update="**\*.xaml.cs" DependentUpon="%(Filename)" />
MSbuild splits paths into ( => ) and ( => ), so it can be used to reference xaml files.
Filenamefoo.xaml.csfoo.xamlExtensionfoo.xaml.cs.cs%(Filename)

MSBuild target before build

I have project A that need to insert a TextId.cs before build. And, there is project B. TextId.cs would generated after project B is compiled and executed.
Now I'd like to integrate the compile and execute in Directory.Build.targets in project A. It is not worked as I expect. TextId.cs will generate but the build would still failed as no TextId.cs if I set BeforeTargets="BeforeBuild" as below.
Anyone knows that which target is OK? or, any other solution?
<Project>
<ItemGroup>
<ProjectReferences Include="c:\code\textidfilegenerator\*.*proj" />
</ItemGroup>
<Target Name="BuildOtherProjects">
<Message Importance="High" Text="-----------------------" />
<MSBuild
Projects="#(ProjectReferences)"
Targets="Build">
</MSBuild>
</Target>
<Target Name="CopyText" DependsOnTargets="BuildOtherProjects" BeforeTargets="BeforeBuild">
<Message Importance="High" Text="**********************" />
<Exec Command="C:\Code\TextIdFileGenerator\bin\Debug\net6.0\TextIdFileGenerator.exe C:\Code\Sys1500TestDriver\TextProvider\TextIds.cs" IgnoreExitCode="true"/>
</Target>
</Project>
Before you will dive into the comments below, go through these docs:
MSBuild reserved and well-known properties
Common MSBuild project properties
<Project>
<ItemGroup>
<ProjectReferences Include="c:\code\textidfilegenerator\*.*proj" /> <!--you should use relative path like <ProjectReference Include="../**/*.csproj or absolute with MSBuild well-known properties" />-->
</ItemGroup>
<Target Name="BuildOtherProjects"> <!--you don't need this target if you have project reference-->
<Message Importance="High" Text="-----------------------" />
<MSBuild
Projects="#(ProjectReferences)"
Targets="Build">
</MSBuild>
</Target>
<Target Name="CopyText" DependsOnTargets="BuildOtherProjects" BeforeTargets="BeforeBuild"><!-- 'BeforeBuild' will not work because TextIdFileGenerator.exe needs to be created before you will do anything with it so you should use 'AfterTargets="Build"'-->
<Message Importance="High" Text="**********************" />
<Exec Command="C:\Code\TextIdFileGenerator\bin\Debug\net6.0\TextIdFileGenerator.exe C:\Code\Sys1500TestDriver\TextProvider\TextIds.cs" IgnoreExitCode="true"/><!-- again, you should use relative paths or absolute ones with combination of MSBuildProjectDirectory and OutDir -->
</Target>
</Project>

Disabling a specific C# 9 source generator

Is there any way to disable a specific C# 9 source generator? Or alternatively disable them all?
the package in question is https://github.com/Husqvik/GraphQlClientGenerator#c-9-source-generator which is mean to be able to be used as both a lib and a source generator. but those are mutually exclusive, ie the majority of use cases it make no sense to gen code both by executing code and by code gen
seems this will disable all
<Target Name="DisableAnalyzers"
BeforeTargets="CoreCompile">
<ItemGroup>
<Analyzer Remove="#(Analyzer)" />
</ItemGroup>
</Target>
removing a named one uses the file path
<Target Name="DisableAnalyzers"
BeforeTargets="CoreCompile">
<ItemGroup>
<Analyzer Remove="D:\nugets\nugetx\0.9.2\analyzers\dotnet\cs\NugetXAnalizer.dll" />
</ItemGroup>
</Target>
ok and finally u can remove based on filename
<Target Name="DisableAnalyzers"
BeforeTargets="CoreCompile">
<ItemGroup>
<Analyzer Remove="#(Analyzer)"
Condition="'%(Filename)' == 'NugetXAnalizer'"/>
</ItemGroup>
</Target>

MSBuild. Create EmbeddedResource before build

I want to embed local references in the assembly before compiling the main unit. But the written target does not work.
<Target Name="EmbedLocal" BeforeTargets="CoreCompile">
<Message Text="Run EmbedLocal for $(MSBuildProjectFullPath)..." Importance="high"/>
<ItemGroup>
<EmbeddedResource Include="#( ReferencePath->WithMetadataValue( 'CopyLocal', 'true' )->Metadata( 'FullPath' ) )"/>
</ItemGroup>
<Message Text="Embed local references complete for $(OutputPath)$(TargetFileName)." Importance="high" />
</Target>
#(EmbeddedResource) at this moment contains valid list of paths.
Update:
Now my import file contains:
<Project ToolsVersion="$(MSBuildToolsVersion)" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<EmbedLocalReferences Condition=" '$(EmbedLocalReferences)' == '' ">True</EmbedLocalReferences>
</PropertyGroup>
<Target Name="EmbedLocal" BeforeTargets="ResolveReferences" Condition=" '$(EmbedLocalReferences)' == 'True' ">
<Message Text="Run EmbedLocal for $(MSBuildProjectFullPath)..." Importance="high"/>
<ItemGroup>
<EmbeddedResource Include="#(ReferenceCopyLocalPaths->WithMetadataValue( 'Extension', '.dll' )->Metadata( 'FullPath' ))">
<LogicalName>%(ReferenceCopyLocalPaths.Filename)%(ReferenceCopyLocalPaths.Extension)</LogicalName>
</EmbeddedResource>
</ItemGroup>
<Message Text="Embed local references complete for $(OutputPath)$(TargetFileName)." Importance="high" />
</Target>
</Project>
It works fine. Output assembly contains all .dll references as EmbeddedResource.
MSBuild. Create EmbeddedResource before build
You can try to use BeforeBuild action to the csproj file to include the embedded resources:
<Target Name="BeforeBuild">
...
<ItemGroup>
<EmbeddedResource Include="..."/>
</ItemGroup>
...
</Target>
Now MSBuild will add this file as embedded resource into your assembly.
Update:
Thanks #Martin Ullrich. He pointed out the correct direction, we could use <Target Name="EmbedLocal" BeforeTargets="PrepareForBuild"> in the Directory.Build.props to resolve this issue. You can check if it works for you.
<Target Name="EmbedLocal" BeforeTargets="PrepareForBuild">
...
<ItemGroup>
<EmbeddedResource Include="..."/>
</ItemGroup>
...
</Target>

How to add a linked file to a csproj file with MSBuild. (3.5 Framework)

I'm trying to us MSBuild to add a linked file to my .csproj file.
This is .Net Framework 3.5 (and not 4.0). I mention that because I'm seen some 4.0 specific stuff trying to manipulate the XML.
Here is what I'm starting with:
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="MySuperCoolClass.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
This is what I'm trying to get:
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="MySuperCoolClass.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="..\..\SomeFunFolder\MyLinkFile.ext">
<Link>MyLinkFile.ext</Link>
</Content>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
I have:
MSBuild.Community.Tasks.dll
and
MSBuild.ExtensionPack.dll
available.
Any concrete help?
One liner comments like use 'MSBuild.ExtensionPack.Xml.XmlFile' won't be helpful.
But I appreciate any pointers or coded examples immensely.
Well, I opened up the code for "MSBuild.ExtensionPack.Xml.XmlFile(.cs)" and looked around.
Thank goodness for open source.
I figured out..you gotta "build it up".
And I had to add a little voodoo trick (with the "MyUniqueKeyHelper123" seen below).
I'll post here.
If you're having trouble with "MSBuild.ExtensionPack.Xml.XmlFile(.cs)", get the source code and look at it. You can figure out how to set the properties by looking at the method.
It was a little tricky at first, but figure-out-able.
<PropertyGroup>
<MSBuildExtensionPackPath Condition="'$(MSBuildExtensionPackPath)' == ''">.\ExtensionPackFiles</MSBuildExtensionPackPath>
<MSBuildExtensionPackLib>$(MSBuildExtensionPackPath)\MSBuild.ExtensionPack.dll</MSBuildExtensionPackLib>
</PropertyGroup>
<UsingTask AssemblyFile="$(MSBuildExtensionPackLib)" TaskName="MSBuild.ExtensionPack.Xml.XmlFile" />
<Target Name="XmlTest01Target">
<Message Text="MSBuildExtensionPackLib = $(MSBuildExtensionPackLib)" />
<!--
The goal is:
<ItemGroup>
<Content Include="..\..\SomeFunFolder\MyLinkFile.ext">
<Link>MyLinkFile.ext</Link>
</Content>
</ItemGroup>
-->
<!-- Define a custom namespace. I used "peanut" just to show it is any name you give it -->
<ItemGroup>
<Namespaces Include="Mynamespace">
<Prefix>peanut</Prefix>
<Uri>http://schemas.microsoft.com/developer/msbuild/2003</Uri>
</Namespaces>
</ItemGroup>
<!--
Add the <ItemGroup> (new) Element. HOWEVER, since there will probably be multiple <ItemGroup> nodes, tag it with some unique identifier. Will Clean up later.
-->
<XmlFile
TaskAction="AddElement"
Namespaces="#(Namespaces)"
File=".\MyCSharpProjectFile.csproj"
Element="ItemGroup"
Key="MyUniqueKeyHelper123"
Value ="MyUniqueValueHelper123"
XPath="//peanut:Project"
/>
<!--
Add the <Content> (new) Element. With Attribute Value.
-->
<XmlFile
TaskAction="AddElement"
File=".\MyCSharpProjectFile.csproj"
Element="Content"
Key="Include"
Value ="..\..\SomeFunFolder\MyLinkFile.ext"
Namespaces="#(Namespaces)"
XPath="//peanut:Project/peanut:ItemGroup[#MyUniqueKeyHelper123='MyUniqueValueHelper123']"
/>
<!--
Add the <Content> (new) Element. With Element Value Value.
-->
<XmlFile
TaskAction="AddElement"
File=".\MyCSharpProjectFile.csproj"
Element="Link"
InnerText ="MyLinkFile.ext"
Namespaces="#(Namespaces)"
XPath="//peanut:Project/peanut:ItemGroup[#MyUniqueKeyHelper123='MyUniqueValueHelper123']"
/>
<!--
Clean up the "unique" attribute to leave clean xml.
-->
<XmlFile
TaskAction="RemoveAttribute"
File=".\MyCSharpProjectFile.csproj"
Element="Link"
Key="MyUniqueKeyHelper123"
Namespaces="#(Namespaces)"
XPath="//peanut:Project/peanut:ItemGroup[#MyUniqueKeyHelper123='MyUniqueValueHelper123']"
/>
</Target>
Is it feasible for you to use the following?
using System;
using System.Text;
using Microsoft.Build.BuildEngine;
namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
var fullPathName = #"PathToProjectFile\Project.csproj";
Project project = new Project();
project.Load(fullPathName);
var itemGroup = project.AddNewItemGroup();
var buildItem = itemGroup.AddNewItem("Content", #"..\..\SomeFunFolder\MyLinkFile.ext");
buildItem.SetMetadata("Link", "MyLinkFile.ext");
project.Save(fullPathName, Encoding.UTF8);
}
}
}

Categories