1

I have below MSBuild Target file setup. This works fine with one JSON file.

<Target Name="dev" 
            AfterTargets="PrepareForBuild"
            BeforeTargets="BeforeBuild"
            Condition="$(Configuration) != 'Debug'">  

        <Message Text="Building Application (dev.json)" />

        <ItemGroup>
            <ScriptFile Include="$(MSBuildThisFileDirectory)myscript.ps1" />
    </ItemGroup>
    <ItemGroup>
            <ScriptArgs Include="-Source &quot;$(MSBuildProjectDirectory)&quot;" />
            <ScriptArgs Include="-PathToManifest &quot;$(MSBuildProjectDirectory)\test\dev.json&quot;" />
        </ItemGroup>

        <Exec  Command="powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File &quot;@(ScriptFile)&quot; @(ScriptArgs,' ')" 
               Condition="Exists('@(ScriptFile)')" />
        <Error Condition="!Exists('@(ScriptFile)')"
               Text="Script file: &quot;@(ScriptFile)&quot; not found. Existing dev.json incomplete!" />

    </Target>

I have verified a few questions here but didn't help so posting here. How do I add this in a loop so I can run the same script with different JSON files?

I have test\dev.json configured, and I have test.json, prod.json.

Everything stays the same but the input file needs to looping through.

I have tried a suggestion as below but still didn't work.

 <ItemGroup>
        <MyJsonFile Include="dev.json" />
        <MyJsonFile Include="test.json" />
        <MyJsonFile Include="prod.json" />
 </ItemGroup>
 <ItemGroup>
        <ScriptFile Include="$(MSBuildThisFileDirectory)myscript.ps1" />
 </ItemGroup>
 <ItemGroup>
        <ScriptArgs Include="-Source &quot;$(MSBuildProjectDirectory)&quot;" />
        <ScriptArgs Include="-PathToManifest &quot;$(MSBuildProjectDirectory)\test\%(MyJsonFile.Identity)&quot;" />
    </ItemGroup>
<Target Name="dev" 
            AfterTargets="PrepareForBuild"
            BeforeTargets="BeforeBuild"
            Condition="$(Configuration) != 'Debug'">  
<Exec  Command="powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File &quot;@(ScriptFile)&quot; @(ScriptArgs,' ')" 
               Condition="Exists('@(ScriptFile)')" />
        <Error Condition="!Exists('@(ScriptFile)')"
               Text="Script file: &quot;@(ScriptFile)&quot; not found. Existing dev.json incomplete!" />

    </Target>

I get

Cannot validate argument on parameter 1> 'PathToManifest'.

I tried moving my ScriptArgs' ItemGroup inside Target but that gives another error

Cannot bind parameter because parameter 1> 'PathToManifest' is specified more than once. To provide multiple values to parameters that can accept multiple 1> values, use the array syntax.

daffyduck
  • 13
  • 3
  • probably should not put %(MyJsonFile.Identity) into another item group, but leave it specified right in the parameter you send to exec. – C.J. Nov 01 '18 at 15:49
  • yes, that's right. Removed the second ItemGroup and used Identity directly in EXEC. – daffyduck Nov 02 '18 at 16:06

1 Answers1

2

How do I loop in MSBuild Target file with different files?

You can add those different JSON files in to the ItemGroup, then use the MSBuild item metadata Identity to loop those input files, like:

  <ItemGroup>
    <MyJsonFile Include="test.json" />
    <MyJsonFile Include="dev.json" />
    <MyJsonFile Include="prod.json" />
  </ItemGroup> 

Then use %(MyJsonFile.Identity) to loop those input files:

<ItemGroup>
        ...
        <ScriptArgs Include="-PathToManifest &quot;$(MSBuildProjectDirectory)\test\%(MyJsonFile.Identity)&quot;" />

</ItemGroup>

Check this thread for some more details.

Hope this helps.

Community
  • 1
  • 1
Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • Should I move my JSON Files `` and ScriptArgs' `` outside (above) of `` so my ``? Did I understand correct? – daffyduck Nov 01 '18 at 15:05
  • I have tried the logic on that link but it's not picking up the name in ItemGroup. I get below error on build `PathToManifest "BuildManifest\%(MyJsonFile.Identity)"" exited with code 1` – daffyduck Nov 01 '18 at 15:30
  • 1
    You don't have to move your if you don't want to. It just has to be defined BEFORE the exec command. – C.J. Nov 01 '18 at 15:46
  • I solved it by removing my second `ItemGroup` and directly using my scripts inside `exec`. Thanks – daffyduck Nov 01 '18 at 15:49