2011-10-09 61 views
2

我已经做了几个自定义任务MSBuild,但我在这里面临新的情况。MSBuild自定义任务与变量no输入参数

总之,我想这样做:

<Target Name="MyTarget"> 
    <CustomTask Files=""> 
    <Input Name="SomeName" Action="SomeActionName /> 
    <Input Name="SomeName" Action="SomeActionName /> 
    <Input Name="SomeName" Action="SomeActionName /> 
    </CustomTask> 
</Target> 

我想这样做,因为我觉得它比使用Itemgroups/propertygroups更具可读性。 有一个属性,如输出这几乎是我所需要的。它应该只是而不是输入(因此名称)。

到目前为止,我已经尝试使用两个任务解决此问题:CustomTask和InputTask。

请注意,输入不一定是任务。这只是一个测试和获得可变大小的输入集合的手段。

public class CustomTask : Task 
{ 
    [Required] 
    public TaskItem[] Files { get; set; } 

    public InputTask[] Subs { get; set; } 

    public override bool Execute() 
    { 
     if(Subs != null) 
     { 
      Subs.ToList().ForEach(sub => sub.Execute()); 
     } 
     else 
     { 
      Log.LogMessage("No Subs"); 
     } 
     return true; 
    } 
} 

public class InputTask: Task 
{ 
    [Required] 
    public TaskItem Name{ get; set; } 

    [Required] 
    public TaskItem Action{ get; set; } 

    public override bool Execute() 
    { 
     Log.LogMessage("" + Name + " should " + Action); 
     return true; 
    } 
} 

的想法是,MBSuild可以“检测”子任务,然后会递给我他们的集合,但我只是得到一个MSB4067错误。

我已经浏览了很多在线操作系统任务和官方文档,但是我还没有找到任何这样的例子。

这甚至有可能这样做吗?

如果不是,你会如何推荐我解决这个问题(PropertyGroup/ItemGroup/Other)?

回答

3

你试图做的是不可能的。您可以用项目元数据来近似它。

<Target Name="MyTarget"> 

    <ItemGroup> 
     <Input Identity="SomeName"><Action>SomeActionName</Action></Input> 
     <Input Identity="SomeName"><Action>SomeActionName</Action></Input> 
     <Input Identity="SomeName"><Action>SomeActionName</Action></Input> 
    </ItemGroup> 

    <CustomTask Files="" Input="@(Input)"> 

</Target> 
+0

是的,这绝对是我已经在帖子中提到的所有已经考虑的方式。我可能最终会这样做,但希望有更多的读者友好的方式来做到这一点。如果我找到一个好的选择,我会回来。 –