You asked specific questions in order to achieve your common goal, I suppose you want to learn about MSBuild, and not get a response to your common task (this is what you are going to get from many other people because of your generosity), so I I will answer your individual questions, and then try to turn all of them into a solution.
So, let's say you want to convert all .jpg files to .png.
Create a sub-list from the list of content items based on the extension:
<ItemGroup> <Sublist Include="@(Content)" Condition=" '%(Extension)' == '.jpg' " /> </ItemGroup>
Get the path to the item in the task.
Two ways - depends on the input that your task can take. This method is similar to the โforeachโ above each item in a Sublist, and I would like to use it with the Exec task:
<Exec Command="convert.exe /Input:%(Sublist.FullPath)" />
Specifying the output path also depends on the .exe or task that you are, and what the output path means for a specific task:
it is a directory or just a file name with a different extension. But I assume that you want to output files with the same name, but a different extension:
<Exec Command="convert.exe "%(Sublist.FullPath)" "%(Sublist.RootDir)%(Sublist.Directory)%(Sublist.Filename).png"" />
How to rebuild png if jpg changes (or clears).
Well, this uses the Inputs and Outputs attribute of the containing target element where our convert command is executed. The inputs indicate what the source files are, and the outputs indicate what the target will produce. Then MSBuild compares the date and time of the inputs with the output datetime and, if they are out of date, the output is restored
<Target Name="ConvertJpg" Inputs="@(Content)" Outputs="@(Content -> '%(RootDir)%(Directory)%(Filename).png' )" Condition=" '%(Extension)' == '.jpg' "
- Attachments say we want to use the Content Group of Items
- The Condition attribute ensures that we only work with content elements that end with the .jpg extension.
- The Outputs attribute says that from the inputs we work with, we will generate files with a similar path and file name, but end with the .png extension
Finally, you correctly noticed that you need to re-insert the generated .png files back into the @Content element group - well, it's simple, just include them in the Content element. Recall that Sublist contains .jpg files - we want these files, but with the completion of .png. We also DO NOT want .jpg files in the content element group after creating the png
<Content Remove="@(Sublist)" /> <Content Include="@(Sublist -> '%(RootDir)%(Directory)%(Filename).png' )" />
So, to summarize, your goal will look something like I think:
<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>
By the way, ImageMagik has a command line tool that converts jpg to png ...