0

I have a small custom MSBuild target:

<ItemGroup>
   <Foo Include="file1.foo"/>
   <Foo Include="file2.foo"/>
   <Foo Include="file3.foo"/>
   ...
</ItemGroup>
<Target Name="Foo2Bar" Inputs="foo.config;@(Foo)" Outputs="%(Foo.Filename).bar">
   <Exec Command="path\to\script @(Foo) -o %(Foo.Filename).bar"/>
</Target>

Now I want to be able to to select one of the *.foo-files in Visual Studio and trigger processing just this one (e.g. by pressing CtrlF7 or right clicking in the Solution Explorer -> Compile). It seems that this sets a property $(SelectedFiles), but I don't find any usable tutorial or example how to make this work...

Tobi
  • 2,591
  • 15
  • 34

1 Answers1

0

Limit custom MSBuild target to the file(s) selected in Visual Studio

If I understand you correctly, you want to select one of *.foo file to the target, if yes, you can pass the list around as a property, so we need to convert item into a property:

<Target Name="BuildMigrationZip">
   <PropertyGroup>
      <FooProperty>@(Foo)</FooProperty>
   </PropertyGroup>

  <MSBuild Projects="$(MSBuildThisFile)" Targets="Foo2Bar"
           Properties="FilesToFoo=$(FooProperty)" />
</Target>

Then when we build the this file with MSBuild command line, we could pass the property FooProperty:

msbuild.exe "YourCustomTargetFile" /p:FooProperty=file1.foo

You can check this thread for some more details info.

If I understand you incorrect, please let me know for free, I will keep follow ASAP.

Hope this helps.

Community
  • 1
  • 1
Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • This only lets me select files on the command line, right?. I want to select the files in the Visual Studio IDE. It already works that with F7 all out-of-date files are processed. I now want to trigger processing selected files with Ctrl-F7 (no matter if out-of-date or not, just like with the built-in `ClCompile` target) – Tobi Nov 02 '18 at 08:11
  • @Tobi, Yes, this only let you select file on the command line. If you want to select the file in the Visual Studio IDE, we have to over write the Ctrl-F7 with your custom target. Or you can develop a item extension like Ctrl-F8 to invoke your custom target. These two methods are not simple. You can check this thread for the details: https://stackoverflow.com/questions/45835043/how-to-override-compile-command-of-visual-studio-2017-community – Leo Liu Nov 02 '18 at 08:40
  • Thanks for your quick replies @Leo! Somehow this seems way too complicated for such a simple thing. For comparison: In Xcode, my desired behavior is the default. Cmd-B executes all build rules Ctrl-Cmd-B just the one for the selected file, not matter if it's a built-in or custom processing rule. And I still believe it's somehow possible in MSBuild, as it works for the built-in targets: If a `*.cpp` file is selected, only `ClCompile` is executed (and only on this file), if an `*.idl` is selected, only the `Midl` target (also only on the selected file)... – Tobi Nov 02 '18 at 09:15