问题描述
我要创建适用IL rwriting到我的输出组件的自定义MSBuild任务。
I want to create a custom msbuild task that applies IL rwriting to my output assembly.
目前我已经在使用PostSharp现在尽量延长重写能力。
At the moment i am already using PostSharp and now try to extend the rewriting capabilities.
对于一些特殊情况下,我使用Mono.Cecil能改写一些代理类型到装配。这现在工作得很好。
For some special cases i use Mono.Cecil to rewrite some proxy types into the assembly. This works fine right now.
但现在我想拦截的实际构建和PostSharp之间的转换生成过程中,以产生得到实现我的代理类型方面通过PostSharp在下一步
But now i want to intercept the build process between the actual build and the PostSharp transformation in order to generate aspects on my proxy types that get implemented by PostSharp in the next step.
我已经联系了PostSharp支持,并得到了一个方向:
I already contacted the PostSharp support and got a direction:
PostSharp注入itselft到构建过程中通过重写的MSBuild财产CompileDependsOn(更多信息MSDN上的)。
您可以执行自己的任务编译后,但通过重写CompileDependsOn属性PostSharp.targets在* .csproj的file.PostSharp注入itselft到构建过程中通过重写的MSBuild财产CompileDependsOn(MSDN上的)。
You can execute your own tasks after compilation but before PostSharp by overriding the CompileDependsOn property before the PostSharp.targets import statement in your *.csproj file.PostSharp injects itselft into a build process by overriding the MSBuild property CompileDependsOn (more info on MSDN https://msdn.microsoft.com/en-us/library/ms366724.aspx).
您可以在编译之后,但之前PostSharp通过重写CompileDependsOn属性PostSharp.targets在* .csproj的文件中导入语句之前执行自己的任务。
You can execute your own tasks after compilation but before PostSharp by overriding the CompileDependsOn property before the PostSharp.targets import statement in your *.csproj file.
我已经找到了PostSharp.targets文件,其中包含覆盖的位置:
I already found the location in the PostSharp.targets file that contains the overrides:
<PropertyGroup Condition="'$(InjectPostSharp30)' != 'False'">
<PostSharp30DependsOn>
$(PostSharp30DependsOn);
PostSharp30ExtractBinaries;
BeforePostSharpTransformation; // I added this one
</PostSharp30DependsOn>
<PostSharpInspectDependsOn>
$(PostSharpInspectDependsOn);
PostSharp30InspectConstants;
PostSharp30InspectReferences;
PostSharp30DisablePreviousVersions
</PostSharpInspectDependsOn>
<CoreCompileDependsOn>
PostSharpInspect;
PostSharp30DefineConstant;
$(CoreCompileDependsOn)
</CoreCompileDependsOn>
<CompileDependsOn>
PostSharp30TimestampBeforeCompile;
$(CompileDependsOn);
PostSharp30TimestampAfterCompile;
PostSharp30
</CompileDependsOn>
<BuildDependsOn>
$(BuildDependsOn);
PostSharp30Verify
</BuildDependsOn>
<CleanDependsOn>
$(CleanDependsOn);
PostSharp30Clean
</CleanDependsOn>
我也有我的MSBuild任务运行。这是越来越调用,也将被设置有用于装配在正确的道路,但是当构建真正被调用时被调用太早,无法找到,因为构建尚未完成组装。
I also got my msbuild task running. It is getting invoked and also gets provided with the right path for the assembly but when the build actually gets invoked it is invoked too early and can not find the assembly because the build is not finished yet.
如果我连接到后生成事件PostSharp并已经运行,但我需要它,以我的自定义转换后运行实现使用方面我的类型。
If i attach to the post build event PostSharp did already run but i need it to run after my custom transformations in order to implement my types using aspects.
我的测试任务的实现是这样的:
My test task is implemented like this:
public class RewritingTask : Task
{
[Required]
public string OutputAssembly { get; set; }
[Output]
public string PreTransformationAssembly { get; set; }
public override bool Execute()
{
string preTransformDir = Path.GetDirectoryName(OutputAssembly) + "\\PreTransform\\";
if (!Directory.Exists(preTransformDir))
{
Directory.CreateDirectory(preTransformDir);
}
if (!File.Exists(OutputAssembly))
{
return false;
}
File.Copy(OutputAssembly, preTransformDir + Path.GetFileName(OutputAssembly), true);
return true;
}
}
该错误是出现FileNotFoundException因为输出组件缺失
The error is a FileNotFoundException because the output assembly is missing.
基本上我需要知道我可以覆盖使用的MSBuild像描述的postsharp支持CompileDependsOn财产。我不是太熟悉的MSBuild的脚本,不好意思
Basically i need to know how i can overwrite the CompileDependsOn property using msbuild like the postsharp support described. I am not too familiar with msbuild-scripts, sorry
推荐答案
有关转换目标的合适位置是
The right place for the transformation target is
<CompileDependsOn>
PostSharp30TimestampBeforeCompile;
$(CompileDependsOn);
HERE;
PostSharp30TimestampAfterCompile;
PostSharp30
</CompileDependsOn>
我的解决方案完全从我们的切入点解耦PostSharp:
My solution completely decouples PostSharp from our entry point:
<!-- If PostSharp is imported, override with combined targets -->
<PropertyGroup Condition="'$(InjectPostSharp30)' == 'True'">
<CompileDependsOn>
PostSharp30TimestampBeforeCompile;
$(CompileDependsOn);
ApplyILRewriting;
PostSharp30TimestampAfterCompile;
PostSharp30
</CompileDependsOn>
<BuildDependsOn>
$(BuildDependsOn);
PostSharp30Verify;
AfterILRewritingPostBuild
</BuildDependsOn>
</PropertyGroup>
<!-- If PostSharp is not imported, override with necessary targets -->
<PropertyGroup Condition="'$(InjectPostSharp30)' != 'True'">
<CompileDependsOn>
$(CompileDependsOn);
HERE
</CompileDependsOn>
<BuildDependsOn>
$(BuildDependsOn);
AfterILRewritingPostBuild
</BuildDependsOn>
</PropertyGroup>
所以我的脚本作品PostSharp和非PostSharp项目。
So my script can works for PostSharp and non PostSharp projects.
这篇关于自IL重写插件的MSBuild的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!