MSBuild: How do I create and use a Task to convert Content items at build time?

前端 未结 3 1795
再見小時候
再見小時候 2020-12-29 14:38

I have a Silverlight 3 project with something like this:


  
  
         


        
3条回答
  •  余生分开走
    2020-12-29 15:04

    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.

    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:

    
        
    
    

    Receive the path of the item in a task.

    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:

    
    

    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:

        
    

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

    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

    • Inputs says that we want to use the "Content" itemgroup
    • The Condition attribute ensures that we are only working with Content items that end in the .jpg extension
    • The Outputs attribute says that of the inputs that we are working with, we'll be generating files that have similar path and filename, but end with the .png extension

    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

    
    
    

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

    
            
        
    
        
    
        
        
    
    

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

提交回复
热议问题