Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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. </p> <p>So let's say you want to convert all .jpg files to .png.</p> <p>Create a sub-list from the content item list based on extension:</p> <pre><code>&lt;ItemGroup&gt; &lt;Sublist Include="@(Content)" Condition=" '%(Extension)' == '.jpg' " /&gt; &lt;/ItemGroup&gt; </code></pre> <blockquote> <p>Receive the path of the item in a task. </p> </blockquote> <p>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:</p> <pre><code>&lt;Exec Command="convert.exe /Input:%(Sublist.FullPath)" /&gt; </code></pre> <blockquote> <p>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:</p> </blockquote> <p>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:</p> <pre><code>&lt;Exec Command="convert.exe &amp;quot;%(Sublist.FullPath)&amp;quot; &amp;quot;%(Sublist.RootDir)%(Sublist.Directory)%(Sublist.Filename).png&amp;quot;" /&gt; </code></pre> <blockquote> <p>How to rebuild the png if the jpg changes (or is cleaned). </p> </blockquote> <p>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</p> <pre><code>&lt;Target Name="ConvertJpg" Inputs="@(Content)" Outputs="@(Content -&gt; '%(RootDir)%(Directory)%(Filename).png' )" Condition=" '%(Extension)' == '.jpg' " </code></pre> <ul> <li>Inputs says that we want to use the "Content" itemgroup</li> <li>The Condition attribute ensures that we are only working with Content items that end in the .jpg extension</li> <li>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</li> </ul> <p>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</p> <pre><code>&lt;Content Remove="@(Sublist)" /&gt; &lt;Content Include="@(Sublist -&gt; '%(RootDir)%(Directory)%(Filename).png' )" /&gt; </code></pre> <p>So summing up, your target would looks something like this I believe:</p> <pre><code>&lt;Target Name="ConvertJpg" Inputs="@(Content)" Outputs="@(Content -&gt; '%(RootDir)%(Directory)%(Filename).png' )" Condition=" '%(Extension)' == '.jpg' " &lt;ItemGroup&gt; &lt;Sublist Include="@(Content)" Condition=" '%(Extension)' == '.jpg' " /&gt; &lt;/ItemGroup&gt; &lt;Exec Command="convert.exe /Input:%(Sublist.FullPath) Output=%(Sublist.RootDir)%(Sublist.Directory)%(Sublist.Filename).png" /&gt; &lt;Content Remove="@(Sublist)" /&gt; &lt;Content Include="@(Sublist -&gt; '%(RootDir)%(Directory)%(Filename).png' )" /&gt; &lt;/Target&gt; </code></pre> <p>By the way, ImageMagik has a command line tool that will convert jpg to png...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload