2
votes

Strange way to put a question, anyway ... Basically this is my first real attempt with MSBuild.

I managed to create a Target (RenameJs) which produces a new Output Items List named JsMinifyOutput (a renamed version of another input list).

Then, in a dependent Target (ClosureCompile) I'm trying to actually use the previously generated Output list. I have a Message Task as the following, where I can see the Output List values printed on the console:

    <Message Text="Outputs: %(JsMinifyOutput.FullPath)" />

But then, in the same dependent Target, when I try to use the Output List in the actual Exec Task, the list seems to be empty. The following task:

<Exec Command="$(ClosureCompilerCmd) --js %(JsMinify.FullPath) --js_output_file %(JsMinifyOutput.FullPath)" />

appears on the console as just:

java -jar Some\Path\To\SomeInputFile.debug.js --js_output_file

while I would expect something like:

java -jar Some\Path\To\SomeInputFile.js --js_output_file Some\Path\To\SomeFileRenamed.js

If I try with this simple Exec Task, it properly shows all file names:

<Exec Command="echo %(JsMinifyOutput.FullPath)" />

What I am trying to achieve here is the following: starting from an input ItemGroup list, create another (output) ItemGroup list by means of some transformation over the former (e.g. renaming, replacing text, ...). Then, having this two ItemGroup lists, loop over them in parallel so to say, and perform an Exec task over them. In pseudo-language, something like:

 for(i from 0 to inList.length)
 {
   Exec Command="Path\To\Some\Command --in inList[i] --out outList[i]
 } 

Am I doing something completely silly here? PS: This is with Build Engine Version 4.0, .NET Framework Version 4.0

2
might depend on what's in the 'omitted' part: is it references another list with a % sign it might screw up thingsstijn
yes, it references another list with '%', coming from an ItemGroup. Does that cause problems? BTW, I edited the question to remove the omitted part.superjos

2 Answers

1
votes

Uhm, it seems that Exec Task cannot iterate over two parallel ItemGroups at the same time. ... At least that is what a MSBuild guru answers here:

The problem is that you are batching on 2 items at a time.

I'm not totally sure if what I'm trying to do here is "batching on 2 items at a time".

EDIT

Having cleared what's the actual reason for the task not working, here's my approach to (what I think is) a clean solution:

  1. Add a new metadata to the existing input ItemGroup. That metadata is the calculated output path and takes the place of the former output ItemGroup. Inside the metadata you find the filepath transformation (rename, replace, ...) achieved somehow (through Inline Task, Property Function, Transform, you name it).
  2. Reference the new metadata where the former output ItemGroup was used, e.g. within Exec Command attribute, or within the Target Outputs attribute.

By way of an example:

<Target Name="JsRename"> <!-- This adds the metadata -->
  <ItemGroup>
    <JsMinify>
      <OutputPath>[do something over %(FullPath)</OutputPath>
    </JsMinify>
  </ItemGroup>
</Target>
<Target Name="ClosureCompile" DependsOnTargets="JsRename"> <!-- Here is the actual task -->
  <Exec Command="$(ClosureCompilerCmd) --js %(JsMinify.FullPath) --js_output_file %(JsMinify.OutputPath)" />
</Target>
0
votes

From my (limited) experience with msbuild, it seems that you cannot use two lists like that. Just put the same in the Message task and you'll see what's it doing.

You can use one list with metadata though, which just requires you to generate the cross-product manually from the two other lists:

<ItemGroup>
  <JsMinify Include="a"/>
  <JsMinify Include="b"/>
  <JsMinifyOutput Include="d"/>
  <JsMinifyOutput Include="e"/>
</ItemGroup>

<Target Name="CrossProduct">
  <ItemGroup>
    <InPath Include="@(JsMinify->'%(Identity)')">
      <OutPath>%(JsMinifyOutput.Identity)</OutPath>
    </InPath>
  </ItemGroup>
</Target>

<Target Name="Show" DependsOnTargets="CrossProduct">
  <Message Text="%(InPath.Identity) and %(InPath.OutPath)"/>
</Target>

running this will output

  a and d
  b and d
  a and e
  b and e

Update There are multiple way batching can be done; here's a slightly different in which you also have to add metadata but in another way; in fact, following your loop pseudo-code, the JsGroup I used below corresponds to the loop counter. While this sample seems to do what you are after, I unfortunately do not know an automatic way to generate the numbers so it's not really a solution but I'll post it anyway as it's interesting enough.

<ItemGroup>
  <JsMinify Include="a">
    <JsGroup>1</JsGroup>
  </JsMinify>
  <JsMinify Include="b">
    <JsGroup>2</JsGroup>
  </JsMinify>

  <JsMinifyOutput Include="d">
    <JsGroup>1</JsGroup>
  </JsMinifyOutput>
  <JsMinifyOutput Include="e">
    <JsGroup>2</JsGroup>
  </JsMinifyOutput>
</ItemGroup>

<Message Text="%(JsGroup): @(JsMinify) and @(JsMinifyOutput)"/>

Outputs

a and d
b and e

I looked further and since you are using msbuild 4, a whole new world of possibilities opens. Using the same ItemGroup and above, here's an inline task that calls your loop in real code:

<UsingTask TaskName="LoopBoth" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
  <ParameterGroup>
    <JsMinify ParameterType="System.String[]" Required="true" />
    <JsMinifyOutput ParameterType="System.String[]" Required="true" />
  </ParameterGroup>
  <Task>
    <Code Type="Fragment" Language="cs">
      <![CDATA[
          for( int i = 0 ; i < JsMinify.Length ; ++i )
          {
            Log.LogMessage( JsMinify[ i ] + " and " + JsMinifyOutput[ i ] );
            System.Diagnostics.Process.Start( "java",
              "-jar " + JsMinify[ i ] + " --js_output_file " + JsMinifyOutput[ i ] );
          }
      ]]>
    </Code>
  </Task>
</UsingTask>

<Target Name="DoIt">
  <LoopBoth JsMinify="@(JsMinify)" JsMinifyOutput="@(JsMinifyOutput)"/>
</Target>

You could off course extend this and pass just JsMinify and do the transformation within the task.