MSBuild:如何在构建时创建和使用任务转换内容项? [英] MSBuild: How do I create and use a Task to convert Content items at build time?

查看:77
本文介绍了MSBuild:如何在构建时创建和使用任务转换内容项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Silverlight 3项目,内容如下:

I have a Silverlight 3 project with something like this:

<ItemGroup>
  <Content Include="Content\image1.png">
  </Content>
</ItemGroup>

基本上,我已经向项目中添加了PNG文件,并将其构建操作设置为内容".效果很好.

Basically I've added a PNG file to my project and set its build action to "Content". This works nicely.

现在我想做的是能够将不同格式的图像添加到我的项目中,并在构建时将它们转换为PNG -因此最终结果就好像我首先在项目中(作为内容)添加了PNG图像一样.

Now what I'd like to do is be able to add images in a different format to my project, and have them converted to PNG at build time - so that the end result is as if I had added a PNG image to the project (as Content) in the first place.

换句话说-我希望图像以PNG格式显示在我的XAP包中.

In other words - I want the image to appear, in PNG format, in my XAP package.

理想情况下,我希望这种方式可以与Visual Web Developer 2008 Express一起使用(这样,我可以通过将图像文件拖到IDE中并可能更改其生成操作来将图像文件添加到我的项目中)进行系统范围内的任何更改.

Ideally I would like to do this in such a way that it will work with Visual Web Developer 2008 Express (so I can add image files to my project by dragging them into the IDE and maybe changing their build action), and without making any system-wide changes.

我要转换的特定格式是XCF-我已经有.NET代码来转换为PNG.我假设我必须创建MSBuild任务.

The specific format I want to convert is XCF - I already have the .NET code to do the conversion to PNG. I am assuming that I must create a MSBuild Task.

我实际上没有太多的MSBuild经验,我想知道如何将这样的东西放在一起.

I don't really have much MSBuild experience, and I'd like to know how to put such a thing together.

基于对MSBuild工作原理的粗略了解,我认为我需要知道:

Based on my rough understanding of how MSBuild works, I think that I need to know:

  • 如何根据文件扩展名从(c)>(或其他)集合中移走(或移出)它们来创建一个项目集合?
    • OR:创建可在Visual Web Developer 2008 Express中使用的自定义生成操作
    • How to create a collection of items by (re)moving them from the @(Content) (or some other) collection, based on their file extension?
      • OR: Create a custom Build Action I can use in Visual Web Developer 2008 Express
      • OR:将它们加入XAP的其他方法吗?

      如果这看起来像是做事的合理方式,还是我错过了任何事情?

      And if this seems like a reasonable way to do things or if I've missed anything?

      推荐答案

      您已经提出了一些具体的子问题,以实现总体目标,我想您想了解MSBuild,而不是死记硬背总体任务(由于您的赏金,您将从其他人那里得到的东西),所以我将回答您的个人问题,然后尝试将所有问题汇总成一个解决方案.

      You have asked specific sub-questions with a view to achieving your overall goal, I presume you want to learn about MSBuild, rather than get a rote answer to your overall task (which is what you are gonna get from many other people due to your bounty), so I will answer your individual questions and then attempt to roll them all up into a solution.

      因此,假设您要将所有.jpg文件转换为.png.

      So let's say you want to convert all .jpg files to .png.

      根据扩展名从内容项列表中创建一个子列表:

      Create a sub-list from the content item list based on extension:

      <ItemGroup>
          <Sublist Include="@(Content)" Condition=" '%(Extension)' == '.jpg' " />
      </ItemGroup>
      

      接收任务中项目的路径.

      Receive the path of the item in a task.

      两种方法-取决于任务可以接受的输入.这种方式就像对子列表中每个项目的"foreach"一样,我倾向于将其与Exec任务一起使用:

      Two ways - depends on the input that your task can accept. This way is like a "foreach" over each item in Sublist, and I would tend to use it with an Exec task:

      <Exec Command="convert.exe /Input:%(Sublist.FullPath)" />
      

      指定输出路径还取决于.exe或您所在的任务,以及输出路径对特定任务的含义:

      Specifying an output path also depends on the .exe or the task that you are and what an output path means to a particular task:

      是目录还是只是具有不同扩展名的文件名.但我假设您要输出名称相同但扩展名不同的文件:

      is it a directory, or just a filename with a different extension. But I'll assume you want to output files with the same name, but a different extension:

      <Exec Command="convert.exe &quot;%(Sublist.FullPath)&quot;  &quot;%(Sublist.RootDir)%(Sublist.Directory)%(Sublist.Filename).png&quot;" />    
      

      如果jpg更改(或清除),如何重建png.

      How to rebuild the png if the jpg changes (or is cleaned).

      好吧,这是使用包含我们转换命令的目标元素的Inputs和Outputs属性.输入指定源文件是什么,输出指定目标将产生什么.然后,MSBuild将输入的日期时间与输出的日期时间进行比较,如果输入的日期时间已过时,则将重新构建输出

      Well, this is using the Inputs and Outputs attribute of the containing target element where our convert command is executed. Inputs specifies what the source files are, and outputs specifies what the target will produce. MSBuild then compares the datetime of the Inputs with the datetime of the output and if they are out of date, then the outputs get rebuilt

      <Target Name="ConvertJpg"  
              Inputs="@(Content)"
              Outputs="@(Content -> '%(RootDir)%(Directory)%(Filename).png' )"
              Condition=" '%(Extension)' == '.jpg' "
      

      • 输入项表示我们要使用内容"项组
      • Condition属性确保我们仅使用以.jpg扩展名结尾的内容项
      • Outputs属性表示我们正在处理的输入中,我们将生成具有相似路径和文件名,但以.png扩展名结尾的文件
      • 最后,您正确地发现需要将生成的.png文件重新注入到@Content项组中-很好,这很简单,只需将它们包含在Content项中即可.回想一下,子列表包含.jpg文件-我们需要这些文件,但文件末尾带有.png.一旦生成png,我们也不希望Content项目组中的.jpg文件

        Lastly, you correctly have spotted that you need to re-inject the generated .png files back into the @Content item group - well, that's easy, you just Include them into the Content item. Recall that Sublist contains .jpg files - we want those files but with a .png ending. We also do NOT want the .jpg files in the Content item group once a png has been generated

        <Content Remove="@(Sublist)" />
        <Content Include="@(Sublist -> '%(RootDir)%(Directory)%(Filename).png' )" />
        

        总而言之,我相信您的目标看起来像这样:

        So summing up, your target would looks something like this I believe:

        <Target Name="ConvertJpg"  
                Inputs="@(Content)"
                Outputs="@(Content -> '%(RootDir)%(Directory)%(Filename).png' )"
                Condition=" '%(Extension)' == '.jpg' "
            <ItemGroup>
                <Sublist Include="@(Content)" Condition=" '%(Extension)' == '.jpg' " />
            </ItemGroup>
        
            <Exec Command="convert.exe /Input:%(Sublist.FullPath) Output=%(Sublist.RootDir)%(Sublist.Directory)%(Sublist.Filename).png" />
        
            <Content Remove="@(Sublist)" />
            <Content Include="@(Sublist -> '%(RootDir)%(Directory)%(Filename).png' )" />
        </Target>
        

        顺便说一下,ImageMagik有一个命令行工具,可以将jpg转换为png ...

        By the way, ImageMagik has a command line tool that will convert jpg to png...

        这篇关于MSBuild:如何在构建时创建和使用任务转换内容项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆