IvanL
IvanL

Reputation: 2485

MSBuild Select partial RecursiveDir

I have a project that looks something like this:

src\ModuleA\code\somepath\test.js
src\ModuleA\code\ignorethis.txt
src\ModuleB\code\awesome.js
src\ModuleB\code\testing\file.js

And I would like to grab all these js files from the modules using an ItemGroup like this:

<ItemGroup>
  <_Scripts Include="..\..\..\src\**\code\**\*.js"/>
</ItemGroup>

But when I copy them into the output I would like to strip the Module and code paths. If I use a Copy like this

<Copy SourceFiles="@(_Scripts)" DestinationFiles="@(Scripts->'..\tmp\Test\%(RecursiveDir)%(Filename)%(Extension)')"/>

The result looks like this (because the RecursiveDir contains the whole path starting at the first **)

tmp\Test\ModuleA\code\somepath\test.js
tmp\Test\ModuleB\code\awesome.js
tmp\Test\ModuleB\code\testing\file.js

However I'm trying to get a result like this:

tmp\Test\somepath\test.js
tmp\Test\awesome.js
tmp\Test\testing\file.js

I have tried by adding Metadata with functions but I can't seem to find the proper escaping needed to make this work. My last attempt was like this (if I can strip at least 1 level of folder, I assumed stripping the second level would be a simple repetition in yet another Meta element...)

<Scripts Include="@(_Scripts)">
  <NewDir>$(&quot;%(_Scripts.RecursiveDir)&quot;.Substring(0, &quot;%(_Scripts.RecursiveDir)&quot;.IndexOf(&quot;\&quot;)))</NewDir>
</Scripts>

Which yields a Error MSB4184: The expression <cut for brevity> cannot be evaluated.

If there is a simpler method or anyone can tell me what the right escape sequence is to get this working?

Upvotes: 1

Views: 318

Answers (1)

IvanL
IvanL

Reputation: 2485

After struggling around with escaping and getting the values parsed and processed I managed to resolve it using the following:

<_ScriptsFiles Include="..\..\..\src\*\code\**\*.js"/>
<_Scripts Include="@(_ScriptsFiles)">
  <FirstSlash>$([System.String]::new('%(_ScriptsFiles.RecursiveDir)').IndexOf('\'))</FirstSlash>
  <DirMinusOne>$([System.String]::new('%(_ScriptsFiles.RecursiveDir)').Substring(%(_Scripts.FirstSlash)))</DirMinusOne>
  <SecondSlash>$([System.String]::new('%(_Scripts.DirMinusOne)').IndexOf('\', 1))</SecondSlash>
  <DirMinusTwo>$([System.String]::new('%(_Scripts.DirMinusOne)').Substring(%(_Scripts.SecondSlash)))</DirMinusTwo>
</_Scripts>
<Copy SourceFiles="@(_Scripts)" DestinationFiles="@(_Scripts->'..\..\..\tmp\%(DirMinusTwo)\%(Filename)%(Extension)')"/>

Upvotes: 1

Related Questions