Skip to content

Commit

Permalink
msbuild custom tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
jackliusr committed Jul 3, 2024
1 parent 330e557 commit 1f51b4f
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions content/posts/2024/06/msbuild-custom-task.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
title: msbuild custom task and runtime reflection
date: 2024-06-30T20:10:00+08:00
categories:
- tech
tags:
- dotnet
- msbuild
- mono.cecil
---

Most information about custom task of msbuild are quite basic, it will get complicated when it is involved reflection.
I tried several ways according searching results and answers from chatgpt: reflection, assemblyLoadContext and mono cecil. The most promising one is **mono.cecil**.

I tried directly loading assemblies and via AssemblyLoadContext. They all failed to load some microsoft assemblies. It looks awkward even I can change my build scripts to copy those assemblies.
Thought of several discussion threads and several open source libraries mentioned in the thread, They all adopt other ways to do that. I only found mono.cecil has a business friendly license term (MIT).

During testing my custom task, the following two commands are helpful to get the logs.

[source, batch]
----
dotnet build -v:n
dotnet build -v:d
----

Message task is helpful to show properties in logs.

[source, xml]
----
<Target Name="Test" Outputs="$(RootFolder)\$(OutputFolder)\metadata\*.json" AfterTargets="Build">
<Message Importance="High" Text="+++ Justin Dearing +++" />
<!--Calling our custom task-->
<ExportConformanceResource
InputAssembly="$(TargetPath)"
OutputPath="$(RootFolder)\$(OutputFolder)" />
</Target>
----

Another way to develop and debug fastly:

. Add System.Diagnostics.Debugger.Launch() in code.
. Build debug version, pack, add to local feed, and use that package
. Copy any changes (ddl, props, targets) to %userprofile%/packages/[your package]/[versionid]

0 comments on commit 1f51b4f

Please sign in to comment.